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 312cfbb5e82Sdan /* 313cfbb5e82Sdan ** If the expression passed as the only argument is of type TK_VECTOR 314cfbb5e82Sdan ** return the number of expressions in the vector. Or, if the expression 315cfbb5e82Sdan ** is a sub-select, return the number of columns in the sub-select. For 316cfbb5e82Sdan ** any other type of expression, return 1. 317cfbb5e82Sdan */ 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 326ba00e30aSdan /* 327ba00e30aSdan ** If the expression passed as the first argument is a TK_VECTOR, return 328ba00e30aSdan ** a pointer to the i'th field of the vector. Or, if the first argument 329ba00e30aSdan ** points to a sub-select, return a pointer to the i'th returned column 330ba00e30aSdan ** value. Otherwise, return a copy of the first argument. 331ba00e30aSdan */ 33271c57db0Sdan static Expr *exprVectorField(Expr *pVector, int i){ 333cfbb5e82Sdan if( (pVector->flags & EP_Vector)==0 ){ 334cfbb5e82Sdan assert( i==0 ); 335cfbb5e82Sdan return pVector; 336cfbb5e82Sdan }else if( pVector->flags & EP_xIsSelect ){ 33771c57db0Sdan return pVector->x.pSelect->pEList->a[i].pExpr; 33871c57db0Sdan } 33971c57db0Sdan return pVector->x.pList->a[i].pExpr; 34071c57db0Sdan } 34171c57db0Sdan 3428da209b1Sdan static int exprVectorSubselect(Parse *pParse, Expr *pExpr){ 3438da209b1Sdan int reg = 0; 3448da209b1Sdan if( pExpr->flags & EP_xIsSelect ){ 3458da209b1Sdan assert( pExpr->op==TK_REGISTER || pExpr->op==TK_SELECT ); 3468da209b1Sdan if( pExpr->op==TK_REGISTER ){ 3478da209b1Sdan reg = pExpr->iTable; 3488da209b1Sdan }else{ 3498da209b1Sdan reg = sqlite3CodeSubselect(pParse, pExpr, 0, 0); 3508da209b1Sdan } 3518da209b1Sdan } 3528da209b1Sdan return reg; 3538da209b1Sdan } 3548da209b1Sdan 35571c57db0Sdan static void codeVectorCompare(Parse *pParse, Expr *pExpr, int dest){ 35671c57db0Sdan Vdbe *v = pParse->pVdbe; 35771c57db0Sdan Expr *pLeft = pExpr->pLeft; 35871c57db0Sdan Expr *pRight = pExpr->pRight; 35971c57db0Sdan int nLeft = sqlite3ExprVectorSize(pLeft); 36071c57db0Sdan int nRight = sqlite3ExprVectorSize(pRight); 36171c57db0Sdan int addr = sqlite3VdbeMakeLabel(v); 36271c57db0Sdan 36371c57db0Sdan /* Check that both sides of the comparison are vectors, and that 36471c57db0Sdan ** both are the same length. */ 36571c57db0Sdan if( nLeft!=nRight ){ 36671c57db0Sdan sqlite3ErrorMsg(pParse, "invalid use of row value"); 36771c57db0Sdan }else{ 36871c57db0Sdan int p5 = (pExpr->op==TK_IS || pExpr->op==TK_ISNOT) ? SQLITE_NULLEQ : 0; 36971c57db0Sdan int opCmp; 37071c57db0Sdan int opTest; 37171c57db0Sdan int i; 37271c57db0Sdan int p3 = 1; 37371c57db0Sdan int regLeft = 0; 37471c57db0Sdan int regRight = 0; 37571c57db0Sdan 37671c57db0Sdan assert( pExpr->op==TK_EQ || pExpr->op==TK_NE 37771c57db0Sdan || pExpr->op==TK_IS || pExpr->op==TK_ISNOT 37871c57db0Sdan || pExpr->op==TK_LT || pExpr->op==TK_GT 37971c57db0Sdan || pExpr->op==TK_LE || pExpr->op==TK_GE 38071c57db0Sdan ); 38171c57db0Sdan 38271c57db0Sdan switch( pExpr->op ){ 38371c57db0Sdan case TK_EQ: 38471c57db0Sdan case TK_IS: 38571c57db0Sdan opTest = OP_IfNot; 38671c57db0Sdan opCmp = OP_Eq; 38771c57db0Sdan break; 38871c57db0Sdan 38971c57db0Sdan case TK_NE: 39071c57db0Sdan case TK_ISNOT: 39171c57db0Sdan opTest = OP_If; 39271c57db0Sdan opCmp = OP_Ne; 39371c57db0Sdan break; 39471c57db0Sdan 39571c57db0Sdan case TK_LT: 39671c57db0Sdan case TK_LE: 39771c57db0Sdan case TK_GT: 39871c57db0Sdan case TK_GE: 39971c57db0Sdan opCmp = OP_Cmp; 40071c57db0Sdan opTest = OP_CmpTest; 40171c57db0Sdan p3 = pExpr->op; 40271c57db0Sdan break; 40371c57db0Sdan } 40471c57db0Sdan 4058da209b1Sdan regLeft = exprVectorSubselect(pParse, pLeft); 4068da209b1Sdan regRight = exprVectorSubselect(pParse, pRight); 40771c57db0Sdan if( pParse->nErr ) return; 40871c57db0Sdan 40971c57db0Sdan for(i=0; i<nLeft; i++){ 41071c57db0Sdan int regFree1 = 0, regFree2 = 0; 41171c57db0Sdan Expr *pL, *pR; 41271c57db0Sdan int r1, r2; 41371c57db0Sdan 414*19ff12ddSdan if( i ) sqlite3ExprCachePush(pParse); 41571c57db0Sdan if( regLeft ){ 41671c57db0Sdan pL = pLeft->x.pSelect->pEList->a[i].pExpr; 41771c57db0Sdan r1 = regLeft+i; 41871c57db0Sdan }else{ 41971c57db0Sdan pL = pLeft->x.pList->a[i].pExpr; 42071c57db0Sdan r1 = sqlite3ExprCodeTemp(pParse, pL, ®Free1); 42171c57db0Sdan } 42271c57db0Sdan 42371c57db0Sdan if( regRight ){ 42471c57db0Sdan pR = pRight->x.pSelect->pEList->a[i].pExpr; 42571c57db0Sdan r2 = regRight+i; 42671c57db0Sdan }else{ 42771c57db0Sdan pR = pRight->x.pList->a[i].pExpr; 42871c57db0Sdan r2 = sqlite3ExprCodeTemp(pParse, pR, ®Free1); 42971c57db0Sdan } 43071c57db0Sdan 43171c57db0Sdan codeCompare(pParse, pL, pR, opCmp, r1, r2, dest, SQLITE_STOREP2 | p5); 43271c57db0Sdan sqlite3VdbeAddOp3(v, opTest, dest, addr, p3); 43371c57db0Sdan sqlite3ReleaseTempReg(pParse, regFree1); 43471c57db0Sdan sqlite3ReleaseTempReg(pParse, regFree2); 435*19ff12ddSdan if( i ) sqlite3ExprCachePop(pParse); 43671c57db0Sdan } 43771c57db0Sdan } 43871c57db0Sdan 43971c57db0Sdan sqlite3VdbeResolveLabel(v, addr); 44071c57db0Sdan } 44171c57db0Sdan 4424b5255acSdanielk1977 #if SQLITE_MAX_EXPR_DEPTH>0 4434b5255acSdanielk1977 /* 4444b5255acSdanielk1977 ** Check that argument nHeight is less than or equal to the maximum 4454b5255acSdanielk1977 ** expression depth allowed. If it is not, leave an error message in 4464b5255acSdanielk1977 ** pParse. 4474b5255acSdanielk1977 */ 4487d10d5a6Sdrh int sqlite3ExprCheckHeight(Parse *pParse, int nHeight){ 4494b5255acSdanielk1977 int rc = SQLITE_OK; 4504b5255acSdanielk1977 int mxHeight = pParse->db->aLimit[SQLITE_LIMIT_EXPR_DEPTH]; 4514b5255acSdanielk1977 if( nHeight>mxHeight ){ 4524b5255acSdanielk1977 sqlite3ErrorMsg(pParse, 4534b5255acSdanielk1977 "Expression tree is too large (maximum depth %d)", mxHeight 4544b5255acSdanielk1977 ); 4554b5255acSdanielk1977 rc = SQLITE_ERROR; 4564b5255acSdanielk1977 } 4574b5255acSdanielk1977 return rc; 4584b5255acSdanielk1977 } 4594b5255acSdanielk1977 4604b5255acSdanielk1977 /* The following three functions, heightOfExpr(), heightOfExprList() 4614b5255acSdanielk1977 ** and heightOfSelect(), are used to determine the maximum height 4624b5255acSdanielk1977 ** of any expression tree referenced by the structure passed as the 4634b5255acSdanielk1977 ** first argument. 4644b5255acSdanielk1977 ** 4654b5255acSdanielk1977 ** If this maximum height is greater than the current value pointed 4664b5255acSdanielk1977 ** to by pnHeight, the second parameter, then set *pnHeight to that 4674b5255acSdanielk1977 ** value. 4684b5255acSdanielk1977 */ 4694b5255acSdanielk1977 static void heightOfExpr(Expr *p, int *pnHeight){ 4704b5255acSdanielk1977 if( p ){ 4714b5255acSdanielk1977 if( p->nHeight>*pnHeight ){ 4724b5255acSdanielk1977 *pnHeight = p->nHeight; 4734b5255acSdanielk1977 } 4744b5255acSdanielk1977 } 4754b5255acSdanielk1977 } 4764b5255acSdanielk1977 static void heightOfExprList(ExprList *p, int *pnHeight){ 4774b5255acSdanielk1977 if( p ){ 4784b5255acSdanielk1977 int i; 4794b5255acSdanielk1977 for(i=0; i<p->nExpr; i++){ 4804b5255acSdanielk1977 heightOfExpr(p->a[i].pExpr, pnHeight); 4814b5255acSdanielk1977 } 4824b5255acSdanielk1977 } 4834b5255acSdanielk1977 } 4844b5255acSdanielk1977 static void heightOfSelect(Select *p, int *pnHeight){ 4854b5255acSdanielk1977 if( p ){ 4864b5255acSdanielk1977 heightOfExpr(p->pWhere, pnHeight); 4874b5255acSdanielk1977 heightOfExpr(p->pHaving, pnHeight); 4884b5255acSdanielk1977 heightOfExpr(p->pLimit, pnHeight); 4894b5255acSdanielk1977 heightOfExpr(p->pOffset, pnHeight); 4904b5255acSdanielk1977 heightOfExprList(p->pEList, pnHeight); 4914b5255acSdanielk1977 heightOfExprList(p->pGroupBy, pnHeight); 4924b5255acSdanielk1977 heightOfExprList(p->pOrderBy, pnHeight); 4934b5255acSdanielk1977 heightOfSelect(p->pPrior, pnHeight); 4944b5255acSdanielk1977 } 4954b5255acSdanielk1977 } 4964b5255acSdanielk1977 4974b5255acSdanielk1977 /* 4984b5255acSdanielk1977 ** Set the Expr.nHeight variable in the structure passed as an 4994b5255acSdanielk1977 ** argument. An expression with no children, Expr.pList or 5004b5255acSdanielk1977 ** Expr.pSelect member has a height of 1. Any other expression 5014b5255acSdanielk1977 ** has a height equal to the maximum height of any other 5024b5255acSdanielk1977 ** referenced Expr plus one. 5032308ed38Sdrh ** 5042308ed38Sdrh ** Also propagate EP_Propagate flags up from Expr.x.pList to Expr.flags, 5052308ed38Sdrh ** if appropriate. 5064b5255acSdanielk1977 */ 5074b5255acSdanielk1977 static void exprSetHeight(Expr *p){ 5084b5255acSdanielk1977 int nHeight = 0; 5094b5255acSdanielk1977 heightOfExpr(p->pLeft, &nHeight); 5104b5255acSdanielk1977 heightOfExpr(p->pRight, &nHeight); 5116ab3a2ecSdanielk1977 if( ExprHasProperty(p, EP_xIsSelect) ){ 5126ab3a2ecSdanielk1977 heightOfSelect(p->x.pSelect, &nHeight); 5132308ed38Sdrh }else if( p->x.pList ){ 5146ab3a2ecSdanielk1977 heightOfExprList(p->x.pList, &nHeight); 5152308ed38Sdrh p->flags |= EP_Propagate & sqlite3ExprListFlags(p->x.pList); 5166ab3a2ecSdanielk1977 } 5174b5255acSdanielk1977 p->nHeight = nHeight + 1; 5184b5255acSdanielk1977 } 5194b5255acSdanielk1977 5204b5255acSdanielk1977 /* 5214b5255acSdanielk1977 ** Set the Expr.nHeight variable using the exprSetHeight() function. If 5224b5255acSdanielk1977 ** the height is greater than the maximum allowed expression depth, 5234b5255acSdanielk1977 ** leave an error in pParse. 5242308ed38Sdrh ** 5252308ed38Sdrh ** Also propagate all EP_Propagate flags from the Expr.x.pList into 5262308ed38Sdrh ** Expr.flags. 5274b5255acSdanielk1977 */ 5282308ed38Sdrh void sqlite3ExprSetHeightAndFlags(Parse *pParse, Expr *p){ 52974893a4cSdrh if( pParse->nErr ) return; 5304b5255acSdanielk1977 exprSetHeight(p); 5317d10d5a6Sdrh sqlite3ExprCheckHeight(pParse, p->nHeight); 5324b5255acSdanielk1977 } 5334b5255acSdanielk1977 5344b5255acSdanielk1977 /* 5354b5255acSdanielk1977 ** Return the maximum height of any expression tree referenced 5364b5255acSdanielk1977 ** by the select statement passed as an argument. 5374b5255acSdanielk1977 */ 5384b5255acSdanielk1977 int sqlite3SelectExprHeight(Select *p){ 5394b5255acSdanielk1977 int nHeight = 0; 5404b5255acSdanielk1977 heightOfSelect(p, &nHeight); 5414b5255acSdanielk1977 return nHeight; 5424b5255acSdanielk1977 } 5432308ed38Sdrh #else /* ABOVE: Height enforcement enabled. BELOW: Height enforcement off */ 5442308ed38Sdrh /* 5452308ed38Sdrh ** Propagate all EP_Propagate flags from the Expr.x.pList into 5462308ed38Sdrh ** Expr.flags. 5472308ed38Sdrh */ 5482308ed38Sdrh void sqlite3ExprSetHeightAndFlags(Parse *pParse, Expr *p){ 5492308ed38Sdrh if( p && p->x.pList && !ExprHasProperty(p, EP_xIsSelect) ){ 5502308ed38Sdrh p->flags |= EP_Propagate & sqlite3ExprListFlags(p->x.pList); 5512308ed38Sdrh } 5522308ed38Sdrh } 5534b5255acSdanielk1977 #define exprSetHeight(y) 5544b5255acSdanielk1977 #endif /* SQLITE_MAX_EXPR_DEPTH>0 */ 5554b5255acSdanielk1977 556be5c89acSdrh /* 557b7916a78Sdrh ** This routine is the core allocator for Expr nodes. 558b7916a78Sdrh ** 559a76b5dfcSdrh ** Construct a new expression node and return a pointer to it. Memory 560b7916a78Sdrh ** for this node and for the pToken argument is a single allocation 561b7916a78Sdrh ** obtained from sqlite3DbMalloc(). The calling function 562a76b5dfcSdrh ** is responsible for making sure the node eventually gets freed. 563b7916a78Sdrh ** 564b7916a78Sdrh ** If dequote is true, then the token (if it exists) is dequoted. 565e792b5b4Sdrh ** If dequote is false, no dequoting is performed. The deQuote 566b7916a78Sdrh ** parameter is ignored if pToken is NULL or if the token does not 567b7916a78Sdrh ** appear to be quoted. If the quotes were of the form "..." (double-quotes) 568b7916a78Sdrh ** then the EP_DblQuoted flag is set on the expression node. 56933e619fcSdrh ** 57033e619fcSdrh ** Special case: If op==TK_INTEGER and pToken points to a string that 57133e619fcSdrh ** can be translated into a 32-bit integer, then the token is not 57233e619fcSdrh ** stored in u.zToken. Instead, the integer values is written 57333e619fcSdrh ** into u.iValue and the EP_IntValue flag is set. No extra storage 57433e619fcSdrh ** is allocated to hold the integer text and the dequote flag is ignored. 575a76b5dfcSdrh */ 576b7916a78Sdrh Expr *sqlite3ExprAlloc( 577a1644fd8Sdanielk1977 sqlite3 *db, /* Handle for sqlite3DbMallocZero() (may be null) */ 57817435752Sdrh int op, /* Expression opcode */ 579b7916a78Sdrh const Token *pToken, /* Token argument. Might be NULL */ 580b7916a78Sdrh int dequote /* True to dequote */ 58117435752Sdrh ){ 582a76b5dfcSdrh Expr *pNew; 58333e619fcSdrh int nExtra = 0; 584cf697396Sshane int iValue = 0; 585b7916a78Sdrh 586575fad65Sdrh assert( db!=0 ); 587b7916a78Sdrh if( pToken ){ 58833e619fcSdrh if( op!=TK_INTEGER || pToken->z==0 58933e619fcSdrh || sqlite3GetInt32(pToken->z, &iValue)==0 ){ 590b7916a78Sdrh nExtra = pToken->n+1; 591d50ffc41Sdrh assert( iValue>=0 ); 59233e619fcSdrh } 593a76b5dfcSdrh } 594575fad65Sdrh pNew = sqlite3DbMallocRawNN(db, sizeof(Expr)+nExtra); 595b7916a78Sdrh if( pNew ){ 596ca3862dcSdrh memset(pNew, 0, sizeof(Expr)); 5971bd10f8aSdrh pNew->op = (u8)op; 598a58fdfb1Sdanielk1977 pNew->iAgg = -1; 599a76b5dfcSdrh if( pToken ){ 60033e619fcSdrh if( nExtra==0 ){ 60133e619fcSdrh pNew->flags |= EP_IntValue; 60233e619fcSdrh pNew->u.iValue = iValue; 60333e619fcSdrh }else{ 60433e619fcSdrh pNew->u.zToken = (char*)&pNew[1]; 605b07028f7Sdrh assert( pToken->z!=0 || pToken->n==0 ); 606b07028f7Sdrh if( pToken->n ) memcpy(pNew->u.zToken, pToken->z, pToken->n); 60733e619fcSdrh pNew->u.zToken[pToken->n] = 0; 608244b9d6eSdrh if( dequote && sqlite3Isquote(pNew->u.zToken[0]) ){ 609244b9d6eSdrh if( pNew->u.zToken[0]=='"' ) pNew->flags |= EP_DblQuoted; 61033e619fcSdrh sqlite3Dequote(pNew->u.zToken); 611a34001c9Sdrh } 612a34001c9Sdrh } 61333e619fcSdrh } 614b7916a78Sdrh #if SQLITE_MAX_EXPR_DEPTH>0 615b7916a78Sdrh pNew->nHeight = 1; 616b7916a78Sdrh #endif 617a34001c9Sdrh } 618a76b5dfcSdrh return pNew; 619a76b5dfcSdrh } 620a76b5dfcSdrh 621a76b5dfcSdrh /* 622b7916a78Sdrh ** Allocate a new expression node from a zero-terminated token that has 623b7916a78Sdrh ** already been dequoted. 624b7916a78Sdrh */ 625b7916a78Sdrh Expr *sqlite3Expr( 626b7916a78Sdrh sqlite3 *db, /* Handle for sqlite3DbMallocZero() (may be null) */ 627b7916a78Sdrh int op, /* Expression opcode */ 628b7916a78Sdrh const char *zToken /* Token argument. Might be NULL */ 629b7916a78Sdrh ){ 630b7916a78Sdrh Token x; 631b7916a78Sdrh x.z = zToken; 632b7916a78Sdrh x.n = zToken ? sqlite3Strlen30(zToken) : 0; 633b7916a78Sdrh return sqlite3ExprAlloc(db, op, &x, 0); 634b7916a78Sdrh } 635b7916a78Sdrh 636b7916a78Sdrh /* 637b7916a78Sdrh ** Attach subtrees pLeft and pRight to the Expr node pRoot. 638b7916a78Sdrh ** 639b7916a78Sdrh ** If pRoot==NULL that means that a memory allocation error has occurred. 640b7916a78Sdrh ** In that case, delete the subtrees pLeft and pRight. 641b7916a78Sdrh */ 642b7916a78Sdrh void sqlite3ExprAttachSubtrees( 643b7916a78Sdrh sqlite3 *db, 644b7916a78Sdrh Expr *pRoot, 645b7916a78Sdrh Expr *pLeft, 646b7916a78Sdrh Expr *pRight 647b7916a78Sdrh ){ 648b7916a78Sdrh if( pRoot==0 ){ 649b7916a78Sdrh assert( db->mallocFailed ); 650b7916a78Sdrh sqlite3ExprDelete(db, pLeft); 651b7916a78Sdrh sqlite3ExprDelete(db, pRight); 652b7916a78Sdrh }else{ 653b7916a78Sdrh if( pRight ){ 654b7916a78Sdrh pRoot->pRight = pRight; 655885a5b03Sdrh pRoot->flags |= EP_Propagate & pRight->flags; 656b7916a78Sdrh } 657b7916a78Sdrh if( pLeft ){ 658b7916a78Sdrh pRoot->pLeft = pLeft; 659885a5b03Sdrh pRoot->flags |= EP_Propagate & pLeft->flags; 660b7916a78Sdrh } 661b7916a78Sdrh exprSetHeight(pRoot); 662b7916a78Sdrh } 663b7916a78Sdrh } 664b7916a78Sdrh 665b7916a78Sdrh /* 66660ec914cSpeter.d.reid ** Allocate an Expr node which joins as many as two subtrees. 667b7916a78Sdrh ** 668bf664469Sdrh ** One or both of the subtrees can be NULL. Return a pointer to the new 669bf664469Sdrh ** Expr node. Or, if an OOM error occurs, set pParse->db->mallocFailed, 670bf664469Sdrh ** free the subtrees and return NULL. 671206f3d96Sdrh */ 67217435752Sdrh Expr *sqlite3PExpr( 67317435752Sdrh Parse *pParse, /* Parsing context */ 67417435752Sdrh int op, /* Expression opcode */ 67517435752Sdrh Expr *pLeft, /* Left operand */ 67617435752Sdrh Expr *pRight, /* Right operand */ 67717435752Sdrh const Token *pToken /* Argument token */ 67817435752Sdrh ){ 6795fb52caaSdrh Expr *p; 6801167d327Sdrh if( op==TK_AND && pParse->nErr==0 ){ 6815fb52caaSdrh /* Take advantage of short-circuit false optimization for AND */ 6825fb52caaSdrh p = sqlite3ExprAnd(pParse->db, pLeft, pRight); 6835fb52caaSdrh }else{ 6841167d327Sdrh p = sqlite3ExprAlloc(pParse->db, op & TKFLG_MASK, pToken, 1); 685b7916a78Sdrh sqlite3ExprAttachSubtrees(pParse->db, p, pLeft, pRight); 6865fb52caaSdrh } 6872b359bdbSdan if( p ) { 6882b359bdbSdan sqlite3ExprCheckHeight(pParse, p->nHeight); 6892b359bdbSdan } 6904e0cff60Sdrh return p; 6914e0cff60Sdrh } 6924e0cff60Sdrh 6934e0cff60Sdrh /* 69408de4f79Sdrh ** Add pSelect to the Expr.x.pSelect field. Or, if pExpr is NULL (due 69508de4f79Sdrh ** do a memory allocation failure) then delete the pSelect object. 69608de4f79Sdrh */ 69708de4f79Sdrh void sqlite3PExprAddSelect(Parse *pParse, Expr *pExpr, Select *pSelect){ 69808de4f79Sdrh if( pExpr ){ 69908de4f79Sdrh pExpr->x.pSelect = pSelect; 70008de4f79Sdrh ExprSetProperty(pExpr, EP_xIsSelect|EP_Subquery); 70108de4f79Sdrh sqlite3ExprSetHeightAndFlags(pParse, pExpr); 70208de4f79Sdrh }else{ 70308de4f79Sdrh assert( pParse->db->mallocFailed ); 70408de4f79Sdrh sqlite3SelectDelete(pParse->db, pSelect); 70508de4f79Sdrh } 70608de4f79Sdrh } 70708de4f79Sdrh 70808de4f79Sdrh 70908de4f79Sdrh /* 710991a1985Sdrh ** If the expression is always either TRUE or FALSE (respectively), 711991a1985Sdrh ** then return 1. If one cannot determine the truth value of the 712991a1985Sdrh ** expression at compile-time return 0. 713991a1985Sdrh ** 714991a1985Sdrh ** This is an optimization. If is OK to return 0 here even if 715991a1985Sdrh ** the expression really is always false or false (a false negative). 716991a1985Sdrh ** But it is a bug to return 1 if the expression might have different 717991a1985Sdrh ** boolean values in different circumstances (a false positive.) 7185fb52caaSdrh ** 7195fb52caaSdrh ** Note that if the expression is part of conditional for a 7205fb52caaSdrh ** LEFT JOIN, then we cannot determine at compile-time whether or not 7215fb52caaSdrh ** is it true or false, so always return 0. 7225fb52caaSdrh */ 723991a1985Sdrh static int exprAlwaysTrue(Expr *p){ 724991a1985Sdrh int v = 0; 725991a1985Sdrh if( ExprHasProperty(p, EP_FromJoin) ) return 0; 726991a1985Sdrh if( !sqlite3ExprIsInteger(p, &v) ) return 0; 727991a1985Sdrh return v!=0; 728991a1985Sdrh } 7295fb52caaSdrh static int exprAlwaysFalse(Expr *p){ 7305fb52caaSdrh int v = 0; 7315fb52caaSdrh if( ExprHasProperty(p, EP_FromJoin) ) return 0; 7325fb52caaSdrh if( !sqlite3ExprIsInteger(p, &v) ) return 0; 7335fb52caaSdrh return v==0; 7345fb52caaSdrh } 7355fb52caaSdrh 7365fb52caaSdrh /* 73791bb0eedSdrh ** Join two expressions using an AND operator. If either expression is 73891bb0eedSdrh ** NULL, then just return the other expression. 7395fb52caaSdrh ** 7405fb52caaSdrh ** If one side or the other of the AND is known to be false, then instead 7415fb52caaSdrh ** of returning an AND expression, just return a constant expression with 7425fb52caaSdrh ** a value of false. 74391bb0eedSdrh */ 7441e536953Sdanielk1977 Expr *sqlite3ExprAnd(sqlite3 *db, Expr *pLeft, Expr *pRight){ 74591bb0eedSdrh if( pLeft==0 ){ 74691bb0eedSdrh return pRight; 74791bb0eedSdrh }else if( pRight==0 ){ 74891bb0eedSdrh return pLeft; 7495fb52caaSdrh }else if( exprAlwaysFalse(pLeft) || exprAlwaysFalse(pRight) ){ 7505fb52caaSdrh sqlite3ExprDelete(db, pLeft); 7515fb52caaSdrh sqlite3ExprDelete(db, pRight); 7525fb52caaSdrh return sqlite3ExprAlloc(db, TK_INTEGER, &sqlite3IntTokens[0], 0); 75391bb0eedSdrh }else{ 754b7916a78Sdrh Expr *pNew = sqlite3ExprAlloc(db, TK_AND, 0, 0); 755b7916a78Sdrh sqlite3ExprAttachSubtrees(db, pNew, pLeft, pRight); 756b7916a78Sdrh return pNew; 757a76b5dfcSdrh } 758a76b5dfcSdrh } 759a76b5dfcSdrh 760a76b5dfcSdrh /* 761a76b5dfcSdrh ** Construct a new expression node for a function with multiple 762a76b5dfcSdrh ** arguments. 763a76b5dfcSdrh */ 76417435752Sdrh Expr *sqlite3ExprFunction(Parse *pParse, ExprList *pList, Token *pToken){ 765a76b5dfcSdrh Expr *pNew; 766633e6d57Sdrh sqlite3 *db = pParse->db; 7674b202ae2Sdanielk1977 assert( pToken ); 768b7916a78Sdrh pNew = sqlite3ExprAlloc(db, TK_FUNCTION, pToken, 1); 769a76b5dfcSdrh if( pNew==0 ){ 770d9da78a2Sdrh sqlite3ExprListDelete(db, pList); /* Avoid memory leak when malloc fails */ 771a76b5dfcSdrh return 0; 772a76b5dfcSdrh } 7736ab3a2ecSdanielk1977 pNew->x.pList = pList; 7746ab3a2ecSdanielk1977 assert( !ExprHasProperty(pNew, EP_xIsSelect) ); 7752308ed38Sdrh sqlite3ExprSetHeightAndFlags(pParse, pNew); 776a76b5dfcSdrh return pNew; 777a76b5dfcSdrh } 778a76b5dfcSdrh 779a76b5dfcSdrh /* 780fa6bc000Sdrh ** Assign a variable number to an expression that encodes a wildcard 781fa6bc000Sdrh ** in the original SQL statement. 782fa6bc000Sdrh ** 783fa6bc000Sdrh ** Wildcards consisting of a single "?" are assigned the next sequential 784fa6bc000Sdrh ** variable number. 785fa6bc000Sdrh ** 786fa6bc000Sdrh ** Wildcards of the form "?nnn" are assigned the number "nnn". We make 787fa6bc000Sdrh ** sure "nnn" is not too be to avoid a denial of service attack when 788fa6bc000Sdrh ** the SQL statement comes from an external source. 789fa6bc000Sdrh ** 79051f49f17Sdrh ** Wildcards of the form ":aaa", "@aaa", or "$aaa" are assigned the same number 791fa6bc000Sdrh ** as the previous instance of the same wildcard. Or if this is the first 79260ec914cSpeter.d.reid ** instance of the wildcard, the next sequential variable number is 793fa6bc000Sdrh ** assigned. 794fa6bc000Sdrh */ 795fa6bc000Sdrh void sqlite3ExprAssignVarNumber(Parse *pParse, Expr *pExpr){ 79617435752Sdrh sqlite3 *db = pParse->db; 797b7916a78Sdrh const char *z; 79817435752Sdrh 799fa6bc000Sdrh if( pExpr==0 ) return; 800c5cd1249Sdrh assert( !ExprHasProperty(pExpr, EP_IntValue|EP_Reduced|EP_TokenOnly) ); 80133e619fcSdrh z = pExpr->u.zToken; 802b7916a78Sdrh assert( z!=0 ); 803b7916a78Sdrh assert( z[0]!=0 ); 804b7916a78Sdrh if( z[1]==0 ){ 805fa6bc000Sdrh /* Wildcard of the form "?". Assign the next variable number */ 806b7916a78Sdrh assert( z[0]=='?' ); 8078677d308Sdrh pExpr->iColumn = (ynVar)(++pParse->nVar); 808124c0b49Sdrh }else{ 809124c0b49Sdrh ynVar x = 0; 810124c0b49Sdrh u32 n = sqlite3Strlen30(z); 811124c0b49Sdrh if( z[0]=='?' ){ 812fa6bc000Sdrh /* Wildcard of the form "?nnn". Convert "nnn" to an integer and 813fa6bc000Sdrh ** use it as the variable number */ 814c8d735aeSdan i64 i; 815124c0b49Sdrh int bOk = 0==sqlite3Atoi64(&z[1], &i, n-1, SQLITE_UTF8); 816124c0b49Sdrh pExpr->iColumn = x = (ynVar)i; 817c5499befSdrh testcase( i==0 ); 818c5499befSdrh testcase( i==1 ); 819c5499befSdrh testcase( i==db->aLimit[SQLITE_LIMIT_VARIABLE_NUMBER]-1 ); 820c5499befSdrh testcase( i==db->aLimit[SQLITE_LIMIT_VARIABLE_NUMBER] ); 821c8d735aeSdan if( bOk==0 || i<1 || i>db->aLimit[SQLITE_LIMIT_VARIABLE_NUMBER] ){ 822fa6bc000Sdrh sqlite3ErrorMsg(pParse, "variable number must be between ?1 and ?%d", 823bb4957f8Sdrh db->aLimit[SQLITE_LIMIT_VARIABLE_NUMBER]); 824124c0b49Sdrh x = 0; 825fa6bc000Sdrh } 826fa6bc000Sdrh if( i>pParse->nVar ){ 8271df2db7fSshaneh pParse->nVar = (int)i; 828fa6bc000Sdrh } 829fa6bc000Sdrh }else{ 83051f49f17Sdrh /* Wildcards like ":aaa", "$aaa" or "@aaa". Reuse the same variable 831fa6bc000Sdrh ** number as the prior appearance of the same name, or if the name 832fa6bc000Sdrh ** has never appeared before, reuse the same variable number 833fa6bc000Sdrh */ 834124c0b49Sdrh ynVar i; 835124c0b49Sdrh for(i=0; i<pParse->nzVar; i++){ 836503a686eSdrh if( pParse->azVar[i] && strcmp(pParse->azVar[i],z)==0 ){ 837124c0b49Sdrh pExpr->iColumn = x = (ynVar)i+1; 838fa6bc000Sdrh break; 839fa6bc000Sdrh } 840fa6bc000Sdrh } 841124c0b49Sdrh if( x==0 ) x = pExpr->iColumn = (ynVar)(++pParse->nVar); 842fa6bc000Sdrh } 843124c0b49Sdrh if( x>0 ){ 844124c0b49Sdrh if( x>pParse->nzVar ){ 845124c0b49Sdrh char **a; 846124c0b49Sdrh a = sqlite3DbRealloc(db, pParse->azVar, x*sizeof(a[0])); 8474a642b60Sdrh if( a==0 ){ 8484a642b60Sdrh assert( db->mallocFailed ); /* Error reported through mallocFailed */ 8494a642b60Sdrh return; 8504a642b60Sdrh } 851124c0b49Sdrh pParse->azVar = a; 852124c0b49Sdrh memset(&a[pParse->nzVar], 0, (x-pParse->nzVar)*sizeof(a[0])); 853124c0b49Sdrh pParse->nzVar = x; 854124c0b49Sdrh } 855124c0b49Sdrh if( z[0]!='?' || pParse->azVar[x-1]==0 ){ 856124c0b49Sdrh sqlite3DbFree(db, pParse->azVar[x-1]); 857124c0b49Sdrh pParse->azVar[x-1] = sqlite3DbStrNDup(db, z, n); 858fa6bc000Sdrh } 859fa6bc000Sdrh } 860fa6bc000Sdrh } 861bb4957f8Sdrh if( !pParse->nErr && pParse->nVar>db->aLimit[SQLITE_LIMIT_VARIABLE_NUMBER] ){ 862832b2664Sdanielk1977 sqlite3ErrorMsg(pParse, "too many SQL variables"); 863832b2664Sdanielk1977 } 864fa6bc000Sdrh } 865fa6bc000Sdrh 866fa6bc000Sdrh /* 867f6963f99Sdan ** Recursively delete an expression tree. 868a2e00042Sdrh */ 8694f0010b1Sdrh static SQLITE_NOINLINE void sqlite3ExprDeleteNN(sqlite3 *db, Expr *p){ 8704f0010b1Sdrh assert( p!=0 ); 871d50ffc41Sdrh /* Sanity check: Assert that the IntValue is non-negative if it exists */ 872d50ffc41Sdrh assert( !ExprHasProperty(p, EP_IntValue) || p->u.iValue>=0 ); 873c5cd1249Sdrh if( !ExprHasProperty(p, EP_TokenOnly) ){ 874c5cd1249Sdrh /* The Expr.x union is never used at the same time as Expr.pRight */ 875c5cd1249Sdrh assert( p->x.pList==0 || p->pRight==0 ); 87671c57db0Sdan if( p->op!=TK_SELECT_COLUMN ) sqlite3ExprDelete(db, p->pLeft); 877633e6d57Sdrh sqlite3ExprDelete(db, p->pRight); 878c5cd1249Sdrh if( ExprHasProperty(p, EP_MemToken) ) sqlite3DbFree(db, p->u.zToken); 8796ab3a2ecSdanielk1977 if( ExprHasProperty(p, EP_xIsSelect) ){ 8806ab3a2ecSdanielk1977 sqlite3SelectDelete(db, p->x.pSelect); 8816ab3a2ecSdanielk1977 }else{ 8826ab3a2ecSdanielk1977 sqlite3ExprListDelete(db, p->x.pList); 8836ab3a2ecSdanielk1977 } 8846ab3a2ecSdanielk1977 } 88533e619fcSdrh if( !ExprHasProperty(p, EP_Static) ){ 886633e6d57Sdrh sqlite3DbFree(db, p); 887a2e00042Sdrh } 88833e619fcSdrh } 8894f0010b1Sdrh void sqlite3ExprDelete(sqlite3 *db, Expr *p){ 8904f0010b1Sdrh if( p ) sqlite3ExprDeleteNN(db, p); 8914f0010b1Sdrh } 892a2e00042Sdrh 893d2687b77Sdrh /* 8946ab3a2ecSdanielk1977 ** Return the number of bytes allocated for the expression structure 8956ab3a2ecSdanielk1977 ** passed as the first argument. This is always one of EXPR_FULLSIZE, 8966ab3a2ecSdanielk1977 ** EXPR_REDUCEDSIZE or EXPR_TOKENONLYSIZE. 8976ab3a2ecSdanielk1977 */ 8986ab3a2ecSdanielk1977 static int exprStructSize(Expr *p){ 8996ab3a2ecSdanielk1977 if( ExprHasProperty(p, EP_TokenOnly) ) return EXPR_TOKENONLYSIZE; 9006ab3a2ecSdanielk1977 if( ExprHasProperty(p, EP_Reduced) ) return EXPR_REDUCEDSIZE; 9016ab3a2ecSdanielk1977 return EXPR_FULLSIZE; 9026ab3a2ecSdanielk1977 } 9036ab3a2ecSdanielk1977 9046ab3a2ecSdanielk1977 /* 90533e619fcSdrh ** The dupedExpr*Size() routines each return the number of bytes required 90633e619fcSdrh ** to store a copy of an expression or expression tree. They differ in 90733e619fcSdrh ** how much of the tree is measured. 90833e619fcSdrh ** 90933e619fcSdrh ** dupedExprStructSize() Size of only the Expr structure 91033e619fcSdrh ** dupedExprNodeSize() Size of Expr + space for token 91133e619fcSdrh ** dupedExprSize() Expr + token + subtree components 91233e619fcSdrh ** 91333e619fcSdrh *************************************************************************** 91433e619fcSdrh ** 91533e619fcSdrh ** The dupedExprStructSize() function returns two values OR-ed together: 91633e619fcSdrh ** (1) the space required for a copy of the Expr structure only and 91733e619fcSdrh ** (2) the EP_xxx flags that indicate what the structure size should be. 91833e619fcSdrh ** The return values is always one of: 91933e619fcSdrh ** 92033e619fcSdrh ** EXPR_FULLSIZE 92133e619fcSdrh ** EXPR_REDUCEDSIZE | EP_Reduced 92233e619fcSdrh ** EXPR_TOKENONLYSIZE | EP_TokenOnly 92333e619fcSdrh ** 92433e619fcSdrh ** The size of the structure can be found by masking the return value 92533e619fcSdrh ** of this routine with 0xfff. The flags can be found by masking the 92633e619fcSdrh ** return value with EP_Reduced|EP_TokenOnly. 92733e619fcSdrh ** 92833e619fcSdrh ** Note that with flags==EXPRDUP_REDUCE, this routines works on full-size 92933e619fcSdrh ** (unreduced) Expr objects as they or originally constructed by the parser. 93033e619fcSdrh ** During expression analysis, extra information is computed and moved into 93133e619fcSdrh ** later parts of teh Expr object and that extra information might get chopped 93233e619fcSdrh ** off if the expression is reduced. Note also that it does not work to 93360ec914cSpeter.d.reid ** make an EXPRDUP_REDUCE copy of a reduced expression. It is only legal 93433e619fcSdrh ** to reduce a pristine expression tree from the parser. The implementation 93533e619fcSdrh ** of dupedExprStructSize() contain multiple assert() statements that attempt 93633e619fcSdrh ** to enforce this constraint. 9376ab3a2ecSdanielk1977 */ 9386ab3a2ecSdanielk1977 static int dupedExprStructSize(Expr *p, int flags){ 9396ab3a2ecSdanielk1977 int nSize; 94033e619fcSdrh assert( flags==EXPRDUP_REDUCE || flags==0 ); /* Only one flag value allowed */ 941aecd8021Sdrh assert( EXPR_FULLSIZE<=0xfff ); 942aecd8021Sdrh assert( (0xfff & (EP_Reduced|EP_TokenOnly))==0 ); 9433c19469cSdrh if( 0==flags ){ 9446ab3a2ecSdanielk1977 nSize = EXPR_FULLSIZE; 9456ab3a2ecSdanielk1977 }else{ 946c5cd1249Sdrh assert( !ExprHasProperty(p, EP_TokenOnly|EP_Reduced) ); 94733e619fcSdrh assert( !ExprHasProperty(p, EP_FromJoin) ); 948c5cd1249Sdrh assert( !ExprHasProperty(p, EP_MemToken) ); 949ebb6a65dSdrh assert( !ExprHasProperty(p, EP_NoReduce) ); 950aecd8021Sdrh if( p->pLeft || p->x.pList ){ 95133e619fcSdrh nSize = EXPR_REDUCEDSIZE | EP_Reduced; 95233e619fcSdrh }else{ 953aecd8021Sdrh assert( p->pRight==0 ); 95433e619fcSdrh nSize = EXPR_TOKENONLYSIZE | EP_TokenOnly; 95533e619fcSdrh } 9566ab3a2ecSdanielk1977 } 9576ab3a2ecSdanielk1977 return nSize; 9586ab3a2ecSdanielk1977 } 9596ab3a2ecSdanielk1977 9606ab3a2ecSdanielk1977 /* 96133e619fcSdrh ** This function returns the space in bytes required to store the copy 96233e619fcSdrh ** of the Expr structure and a copy of the Expr.u.zToken string (if that 96333e619fcSdrh ** string is defined.) 9646ab3a2ecSdanielk1977 */ 9656ab3a2ecSdanielk1977 static int dupedExprNodeSize(Expr *p, int flags){ 96633e619fcSdrh int nByte = dupedExprStructSize(p, flags) & 0xfff; 96733e619fcSdrh if( !ExprHasProperty(p, EP_IntValue) && p->u.zToken ){ 96833e619fcSdrh nByte += sqlite3Strlen30(p->u.zToken)+1; 9696ab3a2ecSdanielk1977 } 970bc73971dSdanielk1977 return ROUND8(nByte); 9716ab3a2ecSdanielk1977 } 9726ab3a2ecSdanielk1977 9736ab3a2ecSdanielk1977 /* 9746ab3a2ecSdanielk1977 ** Return the number of bytes required to create a duplicate of the 9756ab3a2ecSdanielk1977 ** expression passed as the first argument. The second argument is a 9766ab3a2ecSdanielk1977 ** mask containing EXPRDUP_XXX flags. 9776ab3a2ecSdanielk1977 ** 9786ab3a2ecSdanielk1977 ** The value returned includes space to create a copy of the Expr struct 97933e619fcSdrh ** itself and the buffer referred to by Expr.u.zToken, if any. 9806ab3a2ecSdanielk1977 ** 9816ab3a2ecSdanielk1977 ** If the EXPRDUP_REDUCE flag is set, then the return value includes 9826ab3a2ecSdanielk1977 ** space to duplicate all Expr nodes in the tree formed by Expr.pLeft 9836ab3a2ecSdanielk1977 ** and Expr.pRight variables (but not for any structures pointed to or 9846ab3a2ecSdanielk1977 ** descended from the Expr.x.pList or Expr.x.pSelect variables). 9856ab3a2ecSdanielk1977 */ 9866ab3a2ecSdanielk1977 static int dupedExprSize(Expr *p, int flags){ 9876ab3a2ecSdanielk1977 int nByte = 0; 9886ab3a2ecSdanielk1977 if( p ){ 9896ab3a2ecSdanielk1977 nByte = dupedExprNodeSize(p, flags); 9906ab3a2ecSdanielk1977 if( flags&EXPRDUP_REDUCE ){ 991b7916a78Sdrh nByte += dupedExprSize(p->pLeft, flags) + dupedExprSize(p->pRight, flags); 9926ab3a2ecSdanielk1977 } 9936ab3a2ecSdanielk1977 } 9946ab3a2ecSdanielk1977 return nByte; 9956ab3a2ecSdanielk1977 } 9966ab3a2ecSdanielk1977 9976ab3a2ecSdanielk1977 /* 9986ab3a2ecSdanielk1977 ** This function is similar to sqlite3ExprDup(), except that if pzBuffer 9996ab3a2ecSdanielk1977 ** is not NULL then *pzBuffer is assumed to point to a buffer large enough 100033e619fcSdrh ** to store the copy of expression p, the copies of p->u.zToken 10016ab3a2ecSdanielk1977 ** (if applicable), and the copies of the p->pLeft and p->pRight expressions, 100260ec914cSpeter.d.reid ** if any. Before returning, *pzBuffer is set to the first byte past the 10036ab3a2ecSdanielk1977 ** portion of the buffer copied into by this function. 10046ab3a2ecSdanielk1977 */ 10053c19469cSdrh static Expr *exprDup(sqlite3 *db, Expr *p, int dupFlags, u8 **pzBuffer){ 10063c19469cSdrh Expr *pNew; /* Value to return */ 10073c19469cSdrh u8 *zAlloc; /* Memory space from which to build Expr object */ 10083c19469cSdrh u32 staticFlag; /* EP_Static if space not obtained from malloc */ 10096ab3a2ecSdanielk1977 10103c19469cSdrh assert( db!=0 ); 10113c19469cSdrh assert( p ); 10123c19469cSdrh assert( dupFlags==0 || dupFlags==EXPRDUP_REDUCE ); 10133c19469cSdrh assert( pzBuffer==0 || dupFlags==EXPRDUP_REDUCE ); 10146ab3a2ecSdanielk1977 10156ab3a2ecSdanielk1977 /* Figure out where to write the new Expr structure. */ 10166ab3a2ecSdanielk1977 if( pzBuffer ){ 10176ab3a2ecSdanielk1977 zAlloc = *pzBuffer; 101833e619fcSdrh staticFlag = EP_Static; 10196ab3a2ecSdanielk1977 }else{ 10203c19469cSdrh zAlloc = sqlite3DbMallocRawNN(db, dupedExprSize(p, dupFlags)); 10213c19469cSdrh staticFlag = 0; 10226ab3a2ecSdanielk1977 } 10236ab3a2ecSdanielk1977 pNew = (Expr *)zAlloc; 10246ab3a2ecSdanielk1977 10256ab3a2ecSdanielk1977 if( pNew ){ 10266ab3a2ecSdanielk1977 /* Set nNewSize to the size allocated for the structure pointed to 10276ab3a2ecSdanielk1977 ** by pNew. This is either EXPR_FULLSIZE, EXPR_REDUCEDSIZE or 10286ab3a2ecSdanielk1977 ** EXPR_TOKENONLYSIZE. nToken is set to the number of bytes consumed 102933e619fcSdrh ** by the copy of the p->u.zToken string (if any). 10306ab3a2ecSdanielk1977 */ 10313c19469cSdrh const unsigned nStructSize = dupedExprStructSize(p, dupFlags); 103233e619fcSdrh const int nNewSize = nStructSize & 0xfff; 103333e619fcSdrh int nToken; 103433e619fcSdrh if( !ExprHasProperty(p, EP_IntValue) && p->u.zToken ){ 103533e619fcSdrh nToken = sqlite3Strlen30(p->u.zToken) + 1; 103633e619fcSdrh }else{ 103733e619fcSdrh nToken = 0; 103833e619fcSdrh } 10393c19469cSdrh if( dupFlags ){ 10406ab3a2ecSdanielk1977 assert( ExprHasProperty(p, EP_Reduced)==0 ); 10416ab3a2ecSdanielk1977 memcpy(zAlloc, p, nNewSize); 10426ab3a2ecSdanielk1977 }else{ 10433e6a1411Sdan u32 nSize = (u32)exprStructSize(p); 10446ab3a2ecSdanielk1977 memcpy(zAlloc, p, nSize); 104572ea29d7Sdrh if( nSize<EXPR_FULLSIZE ){ 10466ab3a2ecSdanielk1977 memset(&zAlloc[nSize], 0, EXPR_FULLSIZE-nSize); 10476ab3a2ecSdanielk1977 } 104872ea29d7Sdrh } 10496ab3a2ecSdanielk1977 105033e619fcSdrh /* Set the EP_Reduced, EP_TokenOnly, and EP_Static flags appropriately. */ 1051c5cd1249Sdrh pNew->flags &= ~(EP_Reduced|EP_TokenOnly|EP_Static|EP_MemToken); 105233e619fcSdrh pNew->flags |= nStructSize & (EP_Reduced|EP_TokenOnly); 105333e619fcSdrh pNew->flags |= staticFlag; 10546ab3a2ecSdanielk1977 105533e619fcSdrh /* Copy the p->u.zToken string, if any. */ 10566ab3a2ecSdanielk1977 if( nToken ){ 105733e619fcSdrh char *zToken = pNew->u.zToken = (char*)&zAlloc[nNewSize]; 105833e619fcSdrh memcpy(zToken, p->u.zToken, nToken); 10596ab3a2ecSdanielk1977 } 10606ab3a2ecSdanielk1977 10616ab3a2ecSdanielk1977 if( 0==((p->flags|pNew->flags) & EP_TokenOnly) ){ 10626ab3a2ecSdanielk1977 /* Fill in the pNew->x.pSelect or pNew->x.pList member. */ 10636ab3a2ecSdanielk1977 if( ExprHasProperty(p, EP_xIsSelect) ){ 10643c19469cSdrh pNew->x.pSelect = sqlite3SelectDup(db, p->x.pSelect, dupFlags); 10656ab3a2ecSdanielk1977 }else{ 10663c19469cSdrh pNew->x.pList = sqlite3ExprListDup(db, p->x.pList, dupFlags); 10676ab3a2ecSdanielk1977 } 10686ab3a2ecSdanielk1977 } 10696ab3a2ecSdanielk1977 10706ab3a2ecSdanielk1977 /* Fill in pNew->pLeft and pNew->pRight. */ 1071c5cd1249Sdrh if( ExprHasProperty(pNew, EP_Reduced|EP_TokenOnly) ){ 10723c19469cSdrh zAlloc += dupedExprNodeSize(p, dupFlags); 10736ab3a2ecSdanielk1977 if( ExprHasProperty(pNew, EP_Reduced) ){ 10743c19469cSdrh pNew->pLeft = p->pLeft ? 10753c19469cSdrh exprDup(db, p->pLeft, EXPRDUP_REDUCE, &zAlloc) : 0; 10763c19469cSdrh pNew->pRight = p->pRight ? 10773c19469cSdrh exprDup(db, p->pRight, EXPRDUP_REDUCE, &zAlloc) : 0; 10786ab3a2ecSdanielk1977 } 10796ab3a2ecSdanielk1977 if( pzBuffer ){ 10806ab3a2ecSdanielk1977 *pzBuffer = zAlloc; 10816ab3a2ecSdanielk1977 } 1082b7916a78Sdrh }else{ 1083c5cd1249Sdrh if( !ExprHasProperty(p, EP_TokenOnly) ){ 10846ab3a2ecSdanielk1977 pNew->pLeft = sqlite3ExprDup(db, p->pLeft, 0); 10856ab3a2ecSdanielk1977 pNew->pRight = sqlite3ExprDup(db, p->pRight, 0); 10866ab3a2ecSdanielk1977 } 10876ab3a2ecSdanielk1977 } 10886ab3a2ecSdanielk1977 } 10896ab3a2ecSdanielk1977 return pNew; 10906ab3a2ecSdanielk1977 } 10916ab3a2ecSdanielk1977 10926ab3a2ecSdanielk1977 /* 1093bfe31e7fSdan ** Create and return a deep copy of the object passed as the second 1094bfe31e7fSdan ** argument. If an OOM condition is encountered, NULL is returned 1095bfe31e7fSdan ** and the db->mallocFailed flag set. 1096bfe31e7fSdan */ 1097eede6a53Sdan #ifndef SQLITE_OMIT_CTE 1098bfe31e7fSdan static With *withDup(sqlite3 *db, With *p){ 10994e9119d9Sdan With *pRet = 0; 11004e9119d9Sdan if( p ){ 11014e9119d9Sdan int nByte = sizeof(*p) + sizeof(p->a[0]) * (p->nCte-1); 11024e9119d9Sdan pRet = sqlite3DbMallocZero(db, nByte); 11034e9119d9Sdan if( pRet ){ 11044e9119d9Sdan int i; 11054e9119d9Sdan pRet->nCte = p->nCte; 11064e9119d9Sdan for(i=0; i<p->nCte; i++){ 11074e9119d9Sdan pRet->a[i].pSelect = sqlite3SelectDup(db, p->a[i].pSelect, 0); 11084e9119d9Sdan pRet->a[i].pCols = sqlite3ExprListDup(db, p->a[i].pCols, 0); 11094e9119d9Sdan pRet->a[i].zName = sqlite3DbStrDup(db, p->a[i].zName); 11104e9119d9Sdan } 11114e9119d9Sdan } 11124e9119d9Sdan } 11134e9119d9Sdan return pRet; 11144e9119d9Sdan } 1115eede6a53Sdan #else 1116eede6a53Sdan # define withDup(x,y) 0 1117eede6a53Sdan #endif 11184e9119d9Sdan 1119a76b5dfcSdrh /* 1120ff78bd2fSdrh ** The following group of routines make deep copies of expressions, 1121ff78bd2fSdrh ** expression lists, ID lists, and select statements. The copies can 1122ff78bd2fSdrh ** be deleted (by being passed to their respective ...Delete() routines) 1123ff78bd2fSdrh ** without effecting the originals. 1124ff78bd2fSdrh ** 11254adee20fSdanielk1977 ** The expression list, ID, and source lists return by sqlite3ExprListDup(), 11264adee20fSdanielk1977 ** sqlite3IdListDup(), and sqlite3SrcListDup() can not be further expanded 1127ad3cab52Sdrh ** by subsequent calls to sqlite*ListAppend() routines. 1128ff78bd2fSdrh ** 1129ad3cab52Sdrh ** Any tables that the SrcList might point to are not duplicated. 11306ab3a2ecSdanielk1977 ** 1131b7916a78Sdrh ** The flags parameter contains a combination of the EXPRDUP_XXX flags. 11326ab3a2ecSdanielk1977 ** If the EXPRDUP_REDUCE flag is set, then the structure returned is a 11336ab3a2ecSdanielk1977 ** truncated version of the usual Expr structure that will be stored as 11346ab3a2ecSdanielk1977 ** part of the in-memory representation of the database schema. 1135ff78bd2fSdrh */ 11366ab3a2ecSdanielk1977 Expr *sqlite3ExprDup(sqlite3 *db, Expr *p, int flags){ 113772ea29d7Sdrh assert( flags==0 || flags==EXPRDUP_REDUCE ); 11383c19469cSdrh return p ? exprDup(db, p, flags, 0) : 0; 1139ff78bd2fSdrh } 11406ab3a2ecSdanielk1977 ExprList *sqlite3ExprListDup(sqlite3 *db, ExprList *p, int flags){ 1141ff78bd2fSdrh ExprList *pNew; 1142145716b3Sdrh struct ExprList_item *pItem, *pOldItem; 1143ff78bd2fSdrh int i; 1144575fad65Sdrh assert( db!=0 ); 1145ff78bd2fSdrh if( p==0 ) return 0; 1146575fad65Sdrh pNew = sqlite3DbMallocRawNN(db, sizeof(*pNew) ); 1147ff78bd2fSdrh if( pNew==0 ) return 0; 1148d872bb18Sdrh pNew->nExpr = i = p->nExpr; 1149d872bb18Sdrh if( (flags & EXPRDUP_REDUCE)==0 ) for(i=1; i<p->nExpr; i+=i){} 1150575fad65Sdrh pNew->a = pItem = sqlite3DbMallocRawNN(db, i*sizeof(p->a[0]) ); 1151e0048400Sdanielk1977 if( pItem==0 ){ 1152633e6d57Sdrh sqlite3DbFree(db, pNew); 1153e0048400Sdanielk1977 return 0; 1154e0048400Sdanielk1977 } 1155145716b3Sdrh pOldItem = p->a; 1156145716b3Sdrh for(i=0; i<p->nExpr; i++, pItem++, pOldItem++){ 11576ab3a2ecSdanielk1977 Expr *pOldExpr = pOldItem->pExpr; 1158b5526ea6Sdrh pItem->pExpr = sqlite3ExprDup(db, pOldExpr, flags); 115917435752Sdrh pItem->zName = sqlite3DbStrDup(db, pOldItem->zName); 1160b7916a78Sdrh pItem->zSpan = sqlite3DbStrDup(db, pOldItem->zSpan); 1161145716b3Sdrh pItem->sortOrder = pOldItem->sortOrder; 11623e7bc9caSdrh pItem->done = 0; 11632c036cffSdrh pItem->bSpanIsTab = pOldItem->bSpanIsTab; 1164c2acc4e4Sdrh pItem->u = pOldItem->u; 1165ff78bd2fSdrh } 1166ff78bd2fSdrh return pNew; 1167ff78bd2fSdrh } 116893758c8dSdanielk1977 116993758c8dSdanielk1977 /* 117093758c8dSdanielk1977 ** If cursors, triggers, views and subqueries are all omitted from 117193758c8dSdanielk1977 ** the build, then none of the following routines, except for 117293758c8dSdanielk1977 ** sqlite3SelectDup(), can be called. sqlite3SelectDup() is sometimes 117393758c8dSdanielk1977 ** called with a NULL argument. 117493758c8dSdanielk1977 */ 11756a67fe8eSdanielk1977 #if !defined(SQLITE_OMIT_VIEW) || !defined(SQLITE_OMIT_TRIGGER) \ 11766a67fe8eSdanielk1977 || !defined(SQLITE_OMIT_SUBQUERY) 11776ab3a2ecSdanielk1977 SrcList *sqlite3SrcListDup(sqlite3 *db, SrcList *p, int flags){ 1178ad3cab52Sdrh SrcList *pNew; 1179ad3cab52Sdrh int i; 1180113088ecSdrh int nByte; 1181575fad65Sdrh assert( db!=0 ); 1182ad3cab52Sdrh if( p==0 ) return 0; 1183113088ecSdrh nByte = sizeof(*p) + (p->nSrc>0 ? sizeof(p->a[0]) * (p->nSrc-1) : 0); 1184575fad65Sdrh pNew = sqlite3DbMallocRawNN(db, nByte ); 1185ad3cab52Sdrh if( pNew==0 ) return 0; 11864305d103Sdrh pNew->nSrc = pNew->nAlloc = p->nSrc; 1187ad3cab52Sdrh for(i=0; i<p->nSrc; i++){ 11884efc4754Sdrh struct SrcList_item *pNewItem = &pNew->a[i]; 11894efc4754Sdrh struct SrcList_item *pOldItem = &p->a[i]; 1190ed8a3bb1Sdrh Table *pTab; 119141fb5cd1Sdan pNewItem->pSchema = pOldItem->pSchema; 119217435752Sdrh pNewItem->zDatabase = sqlite3DbStrDup(db, pOldItem->zDatabase); 119317435752Sdrh pNewItem->zName = sqlite3DbStrDup(db, pOldItem->zName); 119417435752Sdrh pNewItem->zAlias = sqlite3DbStrDup(db, pOldItem->zAlias); 11958a48b9c0Sdrh pNewItem->fg = pOldItem->fg; 11964efc4754Sdrh pNewItem->iCursor = pOldItem->iCursor; 11975b6a9ed4Sdrh pNewItem->addrFillSub = pOldItem->addrFillSub; 11985b6a9ed4Sdrh pNewItem->regReturn = pOldItem->regReturn; 11998a48b9c0Sdrh if( pNewItem->fg.isIndexedBy ){ 12008a48b9c0Sdrh pNewItem->u1.zIndexedBy = sqlite3DbStrDup(db, pOldItem->u1.zIndexedBy); 12018a48b9c0Sdrh } 12028a48b9c0Sdrh pNewItem->pIBIndex = pOldItem->pIBIndex; 12038a48b9c0Sdrh if( pNewItem->fg.isTabFunc ){ 12048a48b9c0Sdrh pNewItem->u1.pFuncArg = 12058a48b9c0Sdrh sqlite3ExprListDup(db, pOldItem->u1.pFuncArg, flags); 12068a48b9c0Sdrh } 1207ed8a3bb1Sdrh pTab = pNewItem->pTab = pOldItem->pTab; 1208ed8a3bb1Sdrh if( pTab ){ 1209ed8a3bb1Sdrh pTab->nRef++; 1210a1cb183dSdanielk1977 } 12116ab3a2ecSdanielk1977 pNewItem->pSelect = sqlite3SelectDup(db, pOldItem->pSelect, flags); 12126ab3a2ecSdanielk1977 pNewItem->pOn = sqlite3ExprDup(db, pOldItem->pOn, flags); 121317435752Sdrh pNewItem->pUsing = sqlite3IdListDup(db, pOldItem->pUsing); 12146c18b6e0Sdanielk1977 pNewItem->colUsed = pOldItem->colUsed; 1215ad3cab52Sdrh } 1216ad3cab52Sdrh return pNew; 1217ad3cab52Sdrh } 121817435752Sdrh IdList *sqlite3IdListDup(sqlite3 *db, IdList *p){ 1219ff78bd2fSdrh IdList *pNew; 1220ff78bd2fSdrh int i; 1221575fad65Sdrh assert( db!=0 ); 1222ff78bd2fSdrh if( p==0 ) return 0; 1223575fad65Sdrh pNew = sqlite3DbMallocRawNN(db, sizeof(*pNew) ); 1224ff78bd2fSdrh if( pNew==0 ) return 0; 12256c535158Sdrh pNew->nId = p->nId; 1226575fad65Sdrh pNew->a = sqlite3DbMallocRawNN(db, p->nId*sizeof(p->a[0]) ); 1227d5d56523Sdanielk1977 if( pNew->a==0 ){ 1228633e6d57Sdrh sqlite3DbFree(db, pNew); 1229d5d56523Sdanielk1977 return 0; 1230d5d56523Sdanielk1977 } 12316c535158Sdrh /* Note that because the size of the allocation for p->a[] is not 12326c535158Sdrh ** necessarily a power of two, sqlite3IdListAppend() may not be called 12336c535158Sdrh ** on the duplicate created by this function. */ 1234ff78bd2fSdrh for(i=0; i<p->nId; i++){ 12354efc4754Sdrh struct IdList_item *pNewItem = &pNew->a[i]; 12364efc4754Sdrh struct IdList_item *pOldItem = &p->a[i]; 123717435752Sdrh pNewItem->zName = sqlite3DbStrDup(db, pOldItem->zName); 12384efc4754Sdrh pNewItem->idx = pOldItem->idx; 1239ff78bd2fSdrh } 1240ff78bd2fSdrh return pNew; 1241ff78bd2fSdrh } 12426ab3a2ecSdanielk1977 Select *sqlite3SelectDup(sqlite3 *db, Select *p, int flags){ 124323b1b372Sdrh Select *pNew, *pPrior; 1244575fad65Sdrh assert( db!=0 ); 1245ff78bd2fSdrh if( p==0 ) return 0; 1246575fad65Sdrh pNew = sqlite3DbMallocRawNN(db, sizeof(*p) ); 1247ff78bd2fSdrh if( pNew==0 ) return 0; 1248b7916a78Sdrh pNew->pEList = sqlite3ExprListDup(db, p->pEList, flags); 12496ab3a2ecSdanielk1977 pNew->pSrc = sqlite3SrcListDup(db, p->pSrc, flags); 12506ab3a2ecSdanielk1977 pNew->pWhere = sqlite3ExprDup(db, p->pWhere, flags); 12516ab3a2ecSdanielk1977 pNew->pGroupBy = sqlite3ExprListDup(db, p->pGroupBy, flags); 12526ab3a2ecSdanielk1977 pNew->pHaving = sqlite3ExprDup(db, p->pHaving, flags); 12536ab3a2ecSdanielk1977 pNew->pOrderBy = sqlite3ExprListDup(db, p->pOrderBy, flags); 1254ff78bd2fSdrh pNew->op = p->op; 125523b1b372Sdrh pNew->pPrior = pPrior = sqlite3SelectDup(db, p->pPrior, flags); 125623b1b372Sdrh if( pPrior ) pPrior->pNext = pNew; 125723b1b372Sdrh pNew->pNext = 0; 12586ab3a2ecSdanielk1977 pNew->pLimit = sqlite3ExprDup(db, p->pLimit, flags); 12596ab3a2ecSdanielk1977 pNew->pOffset = sqlite3ExprDup(db, p->pOffset, flags); 126092b01d53Sdrh pNew->iLimit = 0; 126192b01d53Sdrh pNew->iOffset = 0; 12627d10d5a6Sdrh pNew->selFlags = p->selFlags & ~SF_UsesEphemeral; 1263b9bb7c18Sdrh pNew->addrOpenEphm[0] = -1; 1264b9bb7c18Sdrh pNew->addrOpenEphm[1] = -1; 1265ec2da854Sdrh pNew->nSelectRow = p->nSelectRow; 12664e9119d9Sdan pNew->pWith = withDup(db, p->pWith); 1267eb9b884cSdrh sqlite3SelectSetName(pNew, p->zSelName); 1268ff78bd2fSdrh return pNew; 1269ff78bd2fSdrh } 127093758c8dSdanielk1977 #else 12716ab3a2ecSdanielk1977 Select *sqlite3SelectDup(sqlite3 *db, Select *p, int flags){ 127293758c8dSdanielk1977 assert( p==0 ); 127393758c8dSdanielk1977 return 0; 127493758c8dSdanielk1977 } 127593758c8dSdanielk1977 #endif 1276ff78bd2fSdrh 1277ff78bd2fSdrh 1278ff78bd2fSdrh /* 1279a76b5dfcSdrh ** Add a new element to the end of an expression list. If pList is 1280a76b5dfcSdrh ** initially NULL, then create a new expression list. 1281b7916a78Sdrh ** 1282b7916a78Sdrh ** If a memory allocation error occurs, the entire list is freed and 1283b7916a78Sdrh ** NULL is returned. If non-NULL is returned, then it is guaranteed 1284b7916a78Sdrh ** that the new entry was successfully appended. 1285a76b5dfcSdrh */ 128617435752Sdrh ExprList *sqlite3ExprListAppend( 128717435752Sdrh Parse *pParse, /* Parsing context */ 128817435752Sdrh ExprList *pList, /* List to which to append. Might be NULL */ 1289b7916a78Sdrh Expr *pExpr /* Expression to be appended. Might be NULL */ 129017435752Sdrh ){ 129117435752Sdrh sqlite3 *db = pParse->db; 1292575fad65Sdrh assert( db!=0 ); 1293a76b5dfcSdrh if( pList==0 ){ 1294575fad65Sdrh pList = sqlite3DbMallocRawNN(db, sizeof(ExprList) ); 1295a76b5dfcSdrh if( pList==0 ){ 1296d5d56523Sdanielk1977 goto no_mem; 1297a76b5dfcSdrh } 1298c263f7c4Sdrh pList->nExpr = 0; 1299575fad65Sdrh pList->a = sqlite3DbMallocRawNN(db, sizeof(pList->a[0])); 1300d872bb18Sdrh if( pList->a==0 ) goto no_mem; 1301d872bb18Sdrh }else if( (pList->nExpr & (pList->nExpr-1))==0 ){ 1302d5d56523Sdanielk1977 struct ExprList_item *a; 1303d872bb18Sdrh assert( pList->nExpr>0 ); 1304d872bb18Sdrh a = sqlite3DbRealloc(db, pList->a, pList->nExpr*2*sizeof(pList->a[0])); 1305d5d56523Sdanielk1977 if( a==0 ){ 1306d5d56523Sdanielk1977 goto no_mem; 1307a76b5dfcSdrh } 1308d5d56523Sdanielk1977 pList->a = a; 1309a76b5dfcSdrh } 13104efc4754Sdrh assert( pList->a!=0 ); 1311b7916a78Sdrh if( 1 ){ 13124efc4754Sdrh struct ExprList_item *pItem = &pList->a[pList->nExpr++]; 13134efc4754Sdrh memset(pItem, 0, sizeof(*pItem)); 1314e94ddc9eSdanielk1977 pItem->pExpr = pExpr; 1315a76b5dfcSdrh } 1316a76b5dfcSdrh return pList; 1317d5d56523Sdanielk1977 1318d5d56523Sdanielk1977 no_mem: 1319d5d56523Sdanielk1977 /* Avoid leaking memory if malloc has failed. */ 1320633e6d57Sdrh sqlite3ExprDelete(db, pExpr); 1321633e6d57Sdrh sqlite3ExprListDelete(db, pList); 1322d5d56523Sdanielk1977 return 0; 1323a76b5dfcSdrh } 1324a76b5dfcSdrh 1325a76b5dfcSdrh /* 1326bc622bc0Sdrh ** Set the sort order for the last element on the given ExprList. 1327bc622bc0Sdrh */ 1328bc622bc0Sdrh void sqlite3ExprListSetSortOrder(ExprList *p, int iSortOrder){ 1329bc622bc0Sdrh if( p==0 ) return; 1330bc622bc0Sdrh assert( SQLITE_SO_UNDEFINED<0 && SQLITE_SO_ASC>=0 && SQLITE_SO_DESC>0 ); 1331bc622bc0Sdrh assert( p->nExpr>0 ); 1332bc622bc0Sdrh if( iSortOrder<0 ){ 1333bc622bc0Sdrh assert( p->a[p->nExpr-1].sortOrder==SQLITE_SO_ASC ); 1334bc622bc0Sdrh return; 1335bc622bc0Sdrh } 1336bc622bc0Sdrh p->a[p->nExpr-1].sortOrder = (u8)iSortOrder; 1337bc622bc0Sdrh } 1338bc622bc0Sdrh 1339bc622bc0Sdrh /* 1340b7916a78Sdrh ** Set the ExprList.a[].zName element of the most recently added item 1341b7916a78Sdrh ** on the expression list. 1342b7916a78Sdrh ** 1343b7916a78Sdrh ** pList might be NULL following an OOM error. But pName should never be 1344b7916a78Sdrh ** NULL. If a memory allocation fails, the pParse->db->mallocFailed flag 1345b7916a78Sdrh ** is set. 1346b7916a78Sdrh */ 1347b7916a78Sdrh void sqlite3ExprListSetName( 1348b7916a78Sdrh Parse *pParse, /* Parsing context */ 1349b7916a78Sdrh ExprList *pList, /* List to which to add the span. */ 1350b7916a78Sdrh Token *pName, /* Name to be added */ 1351b7916a78Sdrh int dequote /* True to cause the name to be dequoted */ 1352b7916a78Sdrh ){ 1353b7916a78Sdrh assert( pList!=0 || pParse->db->mallocFailed!=0 ); 1354b7916a78Sdrh if( pList ){ 1355b7916a78Sdrh struct ExprList_item *pItem; 1356b7916a78Sdrh assert( pList->nExpr>0 ); 1357b7916a78Sdrh pItem = &pList->a[pList->nExpr-1]; 1358b7916a78Sdrh assert( pItem->zName==0 ); 1359b7916a78Sdrh pItem->zName = sqlite3DbStrNDup(pParse->db, pName->z, pName->n); 1360244b9d6eSdrh if( dequote ) sqlite3Dequote(pItem->zName); 1361b7916a78Sdrh } 1362b7916a78Sdrh } 1363b7916a78Sdrh 1364b7916a78Sdrh /* 1365b7916a78Sdrh ** Set the ExprList.a[].zSpan element of the most recently added item 1366b7916a78Sdrh ** on the expression list. 1367b7916a78Sdrh ** 1368b7916a78Sdrh ** pList might be NULL following an OOM error. But pSpan should never be 1369b7916a78Sdrh ** NULL. If a memory allocation fails, the pParse->db->mallocFailed flag 1370b7916a78Sdrh ** is set. 1371b7916a78Sdrh */ 1372b7916a78Sdrh void sqlite3ExprListSetSpan( 1373b7916a78Sdrh Parse *pParse, /* Parsing context */ 1374b7916a78Sdrh ExprList *pList, /* List to which to add the span. */ 1375b7916a78Sdrh ExprSpan *pSpan /* The span to be added */ 1376b7916a78Sdrh ){ 1377b7916a78Sdrh sqlite3 *db = pParse->db; 1378b7916a78Sdrh assert( pList!=0 || db->mallocFailed!=0 ); 1379b7916a78Sdrh if( pList ){ 1380b7916a78Sdrh struct ExprList_item *pItem = &pList->a[pList->nExpr-1]; 1381b7916a78Sdrh assert( pList->nExpr>0 ); 1382b7916a78Sdrh assert( db->mallocFailed || pItem->pExpr==pSpan->pExpr ); 1383b7916a78Sdrh sqlite3DbFree(db, pItem->zSpan); 1384b7916a78Sdrh pItem->zSpan = sqlite3DbStrNDup(db, (char*)pSpan->zStart, 1385cf697396Sshane (int)(pSpan->zEnd - pSpan->zStart)); 1386b7916a78Sdrh } 1387b7916a78Sdrh } 1388b7916a78Sdrh 1389b7916a78Sdrh /* 13907a15a4beSdanielk1977 ** If the expression list pEList contains more than iLimit elements, 13917a15a4beSdanielk1977 ** leave an error message in pParse. 13927a15a4beSdanielk1977 */ 13937a15a4beSdanielk1977 void sqlite3ExprListCheckLength( 13947a15a4beSdanielk1977 Parse *pParse, 13957a15a4beSdanielk1977 ExprList *pEList, 13967a15a4beSdanielk1977 const char *zObject 13977a15a4beSdanielk1977 ){ 1398b1a6c3c1Sdrh int mx = pParse->db->aLimit[SQLITE_LIMIT_COLUMN]; 1399c5499befSdrh testcase( pEList && pEList->nExpr==mx ); 1400c5499befSdrh testcase( pEList && pEList->nExpr==mx+1 ); 1401b1a6c3c1Sdrh if( pEList && pEList->nExpr>mx ){ 14027a15a4beSdanielk1977 sqlite3ErrorMsg(pParse, "too many columns in %s", zObject); 14037a15a4beSdanielk1977 } 14047a15a4beSdanielk1977 } 14057a15a4beSdanielk1977 14067a15a4beSdanielk1977 /* 1407a76b5dfcSdrh ** Delete an entire expression list. 1408a76b5dfcSdrh */ 1409affa855cSdrh static SQLITE_NOINLINE void exprListDeleteNN(sqlite3 *db, ExprList *pList){ 1410a76b5dfcSdrh int i; 1411be5c89acSdrh struct ExprList_item *pItem; 1412d872bb18Sdrh assert( pList->a!=0 || pList->nExpr==0 ); 1413be5c89acSdrh for(pItem=pList->a, i=0; i<pList->nExpr; i++, pItem++){ 1414633e6d57Sdrh sqlite3ExprDelete(db, pItem->pExpr); 1415633e6d57Sdrh sqlite3DbFree(db, pItem->zName); 1416b7916a78Sdrh sqlite3DbFree(db, pItem->zSpan); 1417a76b5dfcSdrh } 1418633e6d57Sdrh sqlite3DbFree(db, pList->a); 1419633e6d57Sdrh sqlite3DbFree(db, pList); 1420a76b5dfcSdrh } 1421affa855cSdrh void sqlite3ExprListDelete(sqlite3 *db, ExprList *pList){ 1422affa855cSdrh if( pList ) exprListDeleteNN(db, pList); 1423affa855cSdrh } 1424a76b5dfcSdrh 1425a76b5dfcSdrh /* 14262308ed38Sdrh ** Return the bitwise-OR of all Expr.flags fields in the given 14272308ed38Sdrh ** ExprList. 1428885a5b03Sdrh */ 14292308ed38Sdrh u32 sqlite3ExprListFlags(const ExprList *pList){ 1430885a5b03Sdrh int i; 14312308ed38Sdrh u32 m = 0; 14322308ed38Sdrh if( pList ){ 1433885a5b03Sdrh for(i=0; i<pList->nExpr; i++){ 1434d0c73053Sdrh Expr *pExpr = pList->a[i].pExpr; 1435de845c2fSdrh assert( pExpr!=0 ); 1436de845c2fSdrh m |= pExpr->flags; 1437885a5b03Sdrh } 14382308ed38Sdrh } 14392308ed38Sdrh return m; 1440885a5b03Sdrh } 1441885a5b03Sdrh 1442885a5b03Sdrh /* 1443059b2d50Sdrh ** These routines are Walker callbacks used to check expressions to 1444059b2d50Sdrh ** see if they are "constant" for some definition of constant. The 1445059b2d50Sdrh ** Walker.eCode value determines the type of "constant" we are looking 1446059b2d50Sdrh ** for. 144773b211abSdrh ** 14487d10d5a6Sdrh ** These callback routines are used to implement the following: 1449626a879aSdrh ** 1450059b2d50Sdrh ** sqlite3ExprIsConstant() pWalker->eCode==1 1451059b2d50Sdrh ** sqlite3ExprIsConstantNotJoin() pWalker->eCode==2 1452fcb9f4f3Sdrh ** sqlite3ExprIsTableConstant() pWalker->eCode==3 1453059b2d50Sdrh ** sqlite3ExprIsConstantOrFunction() pWalker->eCode==4 or 5 145487abf5c0Sdrh ** 1455059b2d50Sdrh ** In all cases, the callbacks set Walker.eCode=0 and abort if the expression 1456059b2d50Sdrh ** is found to not be a constant. 145787abf5c0Sdrh ** 1458feada2dfSdrh ** The sqlite3ExprIsConstantOrFunction() is used for evaluating expressions 1459059b2d50Sdrh ** in a CREATE TABLE statement. The Walker.eCode value is 5 when parsing 1460059b2d50Sdrh ** an existing schema and 4 when processing a new statement. A bound 1461feada2dfSdrh ** parameter raises an error for new statements, but is silently converted 1462feada2dfSdrh ** to NULL for existing schemas. This allows sqlite_master tables that 1463feada2dfSdrh ** contain a bound parameter because they were generated by older versions 1464feada2dfSdrh ** of SQLite to be parsed by newer versions of SQLite without raising a 1465feada2dfSdrh ** malformed schema error. 1466626a879aSdrh */ 14677d10d5a6Sdrh static int exprNodeIsConstant(Walker *pWalker, Expr *pExpr){ 1468626a879aSdrh 1469059b2d50Sdrh /* If pWalker->eCode is 2 then any term of the expression that comes from 1470059b2d50Sdrh ** the ON or USING clauses of a left join disqualifies the expression 14710a168377Sdrh ** from being considered constant. */ 1472059b2d50Sdrh if( pWalker->eCode==2 && ExprHasProperty(pExpr, EP_FromJoin) ){ 1473059b2d50Sdrh pWalker->eCode = 0; 14747d10d5a6Sdrh return WRC_Abort; 14750a168377Sdrh } 14760a168377Sdrh 1477626a879aSdrh switch( pExpr->op ){ 1478eb55bd2fSdrh /* Consider functions to be constant if all their arguments are constant 1479059b2d50Sdrh ** and either pWalker->eCode==4 or 5 or the function has the 1480059b2d50Sdrh ** SQLITE_FUNC_CONST flag. */ 1481eb55bd2fSdrh case TK_FUNCTION: 148263f84573Sdrh if( pWalker->eCode>=4 || ExprHasProperty(pExpr,EP_ConstFunc) ){ 1483b1fba286Sdrh return WRC_Continue; 1484059b2d50Sdrh }else{ 1485059b2d50Sdrh pWalker->eCode = 0; 1486059b2d50Sdrh return WRC_Abort; 1487b1fba286Sdrh } 1488626a879aSdrh case TK_ID: 1489626a879aSdrh case TK_COLUMN: 1490626a879aSdrh case TK_AGG_FUNCTION: 149113449892Sdrh case TK_AGG_COLUMN: 1492c5499befSdrh testcase( pExpr->op==TK_ID ); 1493c5499befSdrh testcase( pExpr->op==TK_COLUMN ); 1494c5499befSdrh testcase( pExpr->op==TK_AGG_FUNCTION ); 1495c5499befSdrh testcase( pExpr->op==TK_AGG_COLUMN ); 1496059b2d50Sdrh if( pWalker->eCode==3 && pExpr->iTable==pWalker->u.iCur ){ 1497059b2d50Sdrh return WRC_Continue; 1498059b2d50Sdrh }else{ 1499059b2d50Sdrh pWalker->eCode = 0; 15007d10d5a6Sdrh return WRC_Abort; 1501059b2d50Sdrh } 1502feada2dfSdrh case TK_VARIABLE: 1503059b2d50Sdrh if( pWalker->eCode==5 ){ 1504feada2dfSdrh /* Silently convert bound parameters that appear inside of CREATE 1505feada2dfSdrh ** statements into a NULL when parsing the CREATE statement text out 1506feada2dfSdrh ** of the sqlite_master table */ 1507feada2dfSdrh pExpr->op = TK_NULL; 1508059b2d50Sdrh }else if( pWalker->eCode==4 ){ 1509feada2dfSdrh /* A bound parameter in a CREATE statement that originates from 1510feada2dfSdrh ** sqlite3_prepare() causes an error */ 1511059b2d50Sdrh pWalker->eCode = 0; 1512feada2dfSdrh return WRC_Abort; 1513feada2dfSdrh } 1514feada2dfSdrh /* Fall through */ 1515626a879aSdrh default: 1516b74b1017Sdrh testcase( pExpr->op==TK_SELECT ); /* selectNodeIsConstant will disallow */ 1517b74b1017Sdrh testcase( pExpr->op==TK_EXISTS ); /* selectNodeIsConstant will disallow */ 15187d10d5a6Sdrh return WRC_Continue; 1519626a879aSdrh } 1520626a879aSdrh } 152162c14b34Sdanielk1977 static int selectNodeIsConstant(Walker *pWalker, Select *NotUsed){ 152262c14b34Sdanielk1977 UNUSED_PARAMETER(NotUsed); 1523059b2d50Sdrh pWalker->eCode = 0; 15247d10d5a6Sdrh return WRC_Abort; 15257d10d5a6Sdrh } 1526059b2d50Sdrh static int exprIsConst(Expr *p, int initFlag, int iCur){ 15277d10d5a6Sdrh Walker w; 1528aa87f9a6Sdrh memset(&w, 0, sizeof(w)); 1529059b2d50Sdrh w.eCode = initFlag; 15307d10d5a6Sdrh w.xExprCallback = exprNodeIsConstant; 15317d10d5a6Sdrh w.xSelectCallback = selectNodeIsConstant; 1532059b2d50Sdrh w.u.iCur = iCur; 15337d10d5a6Sdrh sqlite3WalkExpr(&w, p); 1534059b2d50Sdrh return w.eCode; 15357d10d5a6Sdrh } 1536626a879aSdrh 1537626a879aSdrh /* 1538059b2d50Sdrh ** Walk an expression tree. Return non-zero if the expression is constant 1539eb55bd2fSdrh ** and 0 if it involves variables or function calls. 15402398937bSdrh ** 15412398937bSdrh ** For the purposes of this function, a double-quoted string (ex: "abc") 15422398937bSdrh ** is considered a variable but a single-quoted string (ex: 'abc') is 15432398937bSdrh ** a constant. 1544fef5208cSdrh */ 15454adee20fSdanielk1977 int sqlite3ExprIsConstant(Expr *p){ 1546059b2d50Sdrh return exprIsConst(p, 1, 0); 1547fef5208cSdrh } 1548fef5208cSdrh 1549fef5208cSdrh /* 1550059b2d50Sdrh ** Walk an expression tree. Return non-zero if the expression is constant 15510a168377Sdrh ** that does no originate from the ON or USING clauses of a join. 15520a168377Sdrh ** Return 0 if it involves variables or function calls or terms from 15530a168377Sdrh ** an ON or USING clause. 15540a168377Sdrh */ 15550a168377Sdrh int sqlite3ExprIsConstantNotJoin(Expr *p){ 1556059b2d50Sdrh return exprIsConst(p, 2, 0); 15570a168377Sdrh } 15580a168377Sdrh 15590a168377Sdrh /* 1560fcb9f4f3Sdrh ** Walk an expression tree. Return non-zero if the expression is constant 1561059b2d50Sdrh ** for any single row of the table with cursor iCur. In other words, the 1562059b2d50Sdrh ** expression must not refer to any non-deterministic function nor any 1563059b2d50Sdrh ** table other than iCur. 1564059b2d50Sdrh */ 1565059b2d50Sdrh int sqlite3ExprIsTableConstant(Expr *p, int iCur){ 1566059b2d50Sdrh return exprIsConst(p, 3, iCur); 1567059b2d50Sdrh } 1568059b2d50Sdrh 1569059b2d50Sdrh /* 1570059b2d50Sdrh ** Walk an expression tree. Return non-zero if the expression is constant 1571eb55bd2fSdrh ** or a function call with constant arguments. Return and 0 if there 1572eb55bd2fSdrh ** are any variables. 1573eb55bd2fSdrh ** 1574eb55bd2fSdrh ** For the purposes of this function, a double-quoted string (ex: "abc") 1575eb55bd2fSdrh ** is considered a variable but a single-quoted string (ex: 'abc') is 1576eb55bd2fSdrh ** a constant. 1577eb55bd2fSdrh */ 1578feada2dfSdrh int sqlite3ExprIsConstantOrFunction(Expr *p, u8 isInit){ 1579feada2dfSdrh assert( isInit==0 || isInit==1 ); 1580059b2d50Sdrh return exprIsConst(p, 4+isInit, 0); 1581eb55bd2fSdrh } 1582eb55bd2fSdrh 15835b88bc4bSdrh #ifdef SQLITE_ENABLE_CURSOR_HINTS 15845b88bc4bSdrh /* 15855b88bc4bSdrh ** Walk an expression tree. Return 1 if the expression contains a 15865b88bc4bSdrh ** subquery of some kind. Return 0 if there are no subqueries. 15875b88bc4bSdrh */ 15885b88bc4bSdrh int sqlite3ExprContainsSubquery(Expr *p){ 15895b88bc4bSdrh Walker w; 15905b88bc4bSdrh memset(&w, 0, sizeof(w)); 1591bec2476aSdrh w.eCode = 1; 15925b88bc4bSdrh w.xExprCallback = sqlite3ExprWalkNoop; 15935b88bc4bSdrh w.xSelectCallback = selectNodeIsConstant; 15945b88bc4bSdrh sqlite3WalkExpr(&w, p); 159507194bffSdrh return w.eCode==0; 15965b88bc4bSdrh } 15975b88bc4bSdrh #endif 15985b88bc4bSdrh 1599eb55bd2fSdrh /* 160073b211abSdrh ** If the expression p codes a constant integer that is small enough 1601202b2df7Sdrh ** to fit in a 32-bit integer, return 1 and put the value of the integer 1602202b2df7Sdrh ** in *pValue. If the expression is not an integer or if it is too big 1603202b2df7Sdrh ** to fit in a signed 32-bit integer, return 0 and leave *pValue unchanged. 1604e4de1febSdrh */ 16054adee20fSdanielk1977 int sqlite3ExprIsInteger(Expr *p, int *pValue){ 160692b01d53Sdrh int rc = 0; 1607cd92e84dSdrh 1608cd92e84dSdrh /* If an expression is an integer literal that fits in a signed 32-bit 1609cd92e84dSdrh ** integer, then the EP_IntValue flag will have already been set */ 1610cd92e84dSdrh assert( p->op!=TK_INTEGER || (p->flags & EP_IntValue)!=0 1611cd92e84dSdrh || sqlite3GetInt32(p->u.zToken, &rc)==0 ); 1612cd92e84dSdrh 161392b01d53Sdrh if( p->flags & EP_IntValue ){ 161433e619fcSdrh *pValue = p->u.iValue; 1615e4de1febSdrh return 1; 1616e4de1febSdrh } 161792b01d53Sdrh switch( p->op ){ 16184b59ab5eSdrh case TK_UPLUS: { 161992b01d53Sdrh rc = sqlite3ExprIsInteger(p->pLeft, pValue); 1620f6e369a1Sdrh break; 16214b59ab5eSdrh } 1622e4de1febSdrh case TK_UMINUS: { 1623e4de1febSdrh int v; 16244adee20fSdanielk1977 if( sqlite3ExprIsInteger(p->pLeft, &v) ){ 1625f6418891Smistachkin assert( v!=(-2147483647-1) ); 1626e4de1febSdrh *pValue = -v; 162792b01d53Sdrh rc = 1; 1628e4de1febSdrh } 1629e4de1febSdrh break; 1630e4de1febSdrh } 1631e4de1febSdrh default: break; 1632e4de1febSdrh } 163392b01d53Sdrh return rc; 1634e4de1febSdrh } 1635e4de1febSdrh 1636e4de1febSdrh /* 1637039fc32eSdrh ** Return FALSE if there is no chance that the expression can be NULL. 1638039fc32eSdrh ** 1639039fc32eSdrh ** If the expression might be NULL or if the expression is too complex 1640039fc32eSdrh ** to tell return TRUE. 1641039fc32eSdrh ** 1642039fc32eSdrh ** This routine is used as an optimization, to skip OP_IsNull opcodes 1643039fc32eSdrh ** when we know that a value cannot be NULL. Hence, a false positive 1644039fc32eSdrh ** (returning TRUE when in fact the expression can never be NULL) might 1645039fc32eSdrh ** be a small performance hit but is otherwise harmless. On the other 1646039fc32eSdrh ** hand, a false negative (returning FALSE when the result could be NULL) 1647039fc32eSdrh ** will likely result in an incorrect answer. So when in doubt, return 1648039fc32eSdrh ** TRUE. 1649039fc32eSdrh */ 1650039fc32eSdrh int sqlite3ExprCanBeNull(const Expr *p){ 1651039fc32eSdrh u8 op; 1652cd7f457eSdrh while( p->op==TK_UPLUS || p->op==TK_UMINUS ){ p = p->pLeft; } 1653039fc32eSdrh op = p->op; 1654039fc32eSdrh if( op==TK_REGISTER ) op = p->op2; 1655039fc32eSdrh switch( op ){ 1656039fc32eSdrh case TK_INTEGER: 1657039fc32eSdrh case TK_STRING: 1658039fc32eSdrh case TK_FLOAT: 1659039fc32eSdrh case TK_BLOB: 1660039fc32eSdrh return 0; 16617248a8b2Sdrh case TK_COLUMN: 16627248a8b2Sdrh assert( p->pTab!=0 ); 166372673a24Sdrh return ExprHasProperty(p, EP_CanBeNull) || 166472673a24Sdrh (p->iColumn>=0 && p->pTab->aCol[p->iColumn].notNull==0); 1665039fc32eSdrh default: 1666039fc32eSdrh return 1; 1667039fc32eSdrh } 1668039fc32eSdrh } 1669039fc32eSdrh 1670039fc32eSdrh /* 1671039fc32eSdrh ** Return TRUE if the given expression is a constant which would be 1672039fc32eSdrh ** unchanged by OP_Affinity with the affinity given in the second 1673039fc32eSdrh ** argument. 1674039fc32eSdrh ** 1675039fc32eSdrh ** This routine is used to determine if the OP_Affinity operation 1676039fc32eSdrh ** can be omitted. When in doubt return FALSE. A false negative 1677039fc32eSdrh ** is harmless. A false positive, however, can result in the wrong 1678039fc32eSdrh ** answer. 1679039fc32eSdrh */ 1680039fc32eSdrh int sqlite3ExprNeedsNoAffinityChange(const Expr *p, char aff){ 1681039fc32eSdrh u8 op; 168205883a34Sdrh if( aff==SQLITE_AFF_BLOB ) return 1; 1683cd7f457eSdrh while( p->op==TK_UPLUS || p->op==TK_UMINUS ){ p = p->pLeft; } 1684039fc32eSdrh op = p->op; 1685039fc32eSdrh if( op==TK_REGISTER ) op = p->op2; 1686039fc32eSdrh switch( op ){ 1687039fc32eSdrh case TK_INTEGER: { 1688039fc32eSdrh return aff==SQLITE_AFF_INTEGER || aff==SQLITE_AFF_NUMERIC; 1689039fc32eSdrh } 1690039fc32eSdrh case TK_FLOAT: { 1691039fc32eSdrh return aff==SQLITE_AFF_REAL || aff==SQLITE_AFF_NUMERIC; 1692039fc32eSdrh } 1693039fc32eSdrh case TK_STRING: { 1694039fc32eSdrh return aff==SQLITE_AFF_TEXT; 1695039fc32eSdrh } 1696039fc32eSdrh case TK_BLOB: { 1697039fc32eSdrh return 1; 1698039fc32eSdrh } 16992f2855b6Sdrh case TK_COLUMN: { 170088376ca7Sdrh assert( p->iTable>=0 ); /* p cannot be part of a CHECK constraint */ 170188376ca7Sdrh return p->iColumn<0 17022f2855b6Sdrh && (aff==SQLITE_AFF_INTEGER || aff==SQLITE_AFF_NUMERIC); 17032f2855b6Sdrh } 1704039fc32eSdrh default: { 1705039fc32eSdrh return 0; 1706039fc32eSdrh } 1707039fc32eSdrh } 1708039fc32eSdrh } 1709039fc32eSdrh 1710039fc32eSdrh /* 1711c4a3c779Sdrh ** Return TRUE if the given string is a row-id column name. 1712c4a3c779Sdrh */ 17134adee20fSdanielk1977 int sqlite3IsRowid(const char *z){ 17144adee20fSdanielk1977 if( sqlite3StrICmp(z, "_ROWID_")==0 ) return 1; 17154adee20fSdanielk1977 if( sqlite3StrICmp(z, "ROWID")==0 ) return 1; 17164adee20fSdanielk1977 if( sqlite3StrICmp(z, "OID")==0 ) return 1; 1717c4a3c779Sdrh return 0; 1718c4a3c779Sdrh } 1719c4a3c779Sdrh 17209a96b668Sdanielk1977 /* 172169c355bdSdrh ** pX is the RHS of an IN operator. If pX is a SELECT statement 172269c355bdSdrh ** that can be simplified to a direct table access, then return 172369c355bdSdrh ** a pointer to the SELECT statement. If pX is not a SELECT statement, 172469c355bdSdrh ** or if the SELECT statement needs to be manifested into a transient 172569c355bdSdrh ** table, then return NULL. 1726ba00e30aSdan ** 1727ba00e30aSdan ** If parameter bNullSensitive is 0, then this operation will be 1728ba00e30aSdan ** used in a context in which there is no difference between a result 1729ba00e30aSdan ** of 0 and one of NULL. For example: 1730ba00e30aSdan ** 1731ba00e30aSdan ** ... WHERE (?,?) IN (SELECT ...) 1732ba00e30aSdan ** 1733b287f4b6Sdrh */ 1734b287f4b6Sdrh #ifndef SQLITE_OMIT_SUBQUERY 17357b35a77bSdan static Select *isCandidateForInOpt(Expr *pX){ 173669c355bdSdrh Select *p; 1737b287f4b6Sdrh SrcList *pSrc; 1738b287f4b6Sdrh ExprList *pEList; 1739b287f4b6Sdrh Table *pTab; 1740cfbb5e82Sdan int i; 174169c355bdSdrh if( !ExprHasProperty(pX, EP_xIsSelect) ) return 0; /* Not a subquery */ 174269c355bdSdrh if( ExprHasProperty(pX, EP_VarSelect) ) return 0; /* Correlated subq */ 174369c355bdSdrh p = pX->x.pSelect; 1744b287f4b6Sdrh if( p->pPrior ) return 0; /* Not a compound SELECT */ 17457d10d5a6Sdrh if( p->selFlags & (SF_Distinct|SF_Aggregate) ){ 1746b74b1017Sdrh testcase( (p->selFlags & (SF_Distinct|SF_Aggregate))==SF_Distinct ); 1747b74b1017Sdrh testcase( (p->selFlags & (SF_Distinct|SF_Aggregate))==SF_Aggregate ); 17487d10d5a6Sdrh return 0; /* No DISTINCT keyword and no aggregate functions */ 17497d10d5a6Sdrh } 1750b74b1017Sdrh assert( p->pGroupBy==0 ); /* Has no GROUP BY clause */ 1751b287f4b6Sdrh if( p->pLimit ) return 0; /* Has no LIMIT clause */ 1752b74b1017Sdrh assert( p->pOffset==0 ); /* No LIMIT means no OFFSET */ 1753b287f4b6Sdrh if( p->pWhere ) return 0; /* Has no WHERE clause */ 1754b287f4b6Sdrh pSrc = p->pSrc; 1755d1fa7bcaSdrh assert( pSrc!=0 ); 1756d1fa7bcaSdrh if( pSrc->nSrc!=1 ) return 0; /* Single term in FROM clause */ 1757b74b1017Sdrh if( pSrc->a[0].pSelect ) return 0; /* FROM is not a subquery or view */ 1758b287f4b6Sdrh pTab = pSrc->a[0].pTab; 175969c355bdSdrh assert( pTab!=0 ); 1760b74b1017Sdrh assert( pTab->pSelect==0 ); /* FROM clause is not a view */ 1761b287f4b6Sdrh if( IsVirtual(pTab) ) return 0; /* FROM clause not a virtual table */ 1762b287f4b6Sdrh pEList = p->pEList; 1763cfbb5e82Sdan 17647b35a77bSdan /* All SELECT results must be columns. */ 1765cfbb5e82Sdan for(i=0; i<pEList->nExpr; i++){ 1766cfbb5e82Sdan Expr *pRes = pEList->a[i].pExpr; 1767cfbb5e82Sdan if( pRes->op!=TK_COLUMN ) return 0; 176869c355bdSdrh assert( pRes->iTable==pSrc->a[0].iCursor ); /* Not a correlated subquery */ 1769cfbb5e82Sdan } 177069c355bdSdrh return p; 1771b287f4b6Sdrh } 1772b287f4b6Sdrh #endif /* SQLITE_OMIT_SUBQUERY */ 1773b287f4b6Sdrh 1774b287f4b6Sdrh /* 17751d8cb21fSdan ** Code an OP_Once instruction and allocate space for its flag. Return the 17761d8cb21fSdan ** address of the new instruction. 17771d8cb21fSdan */ 17781d8cb21fSdan int sqlite3CodeOnce(Parse *pParse){ 17791d8cb21fSdan Vdbe *v = sqlite3GetVdbe(pParse); /* Virtual machine being coded */ 17801d8cb21fSdan return sqlite3VdbeAddOp1(v, OP_Once, pParse->nOnce++); 17811d8cb21fSdan } 17821d8cb21fSdan 17831d8cb21fSdan /* 17844c259e9fSdrh ** Generate code that checks the left-most column of index table iCur to see if 17854c259e9fSdrh ** it contains any NULL entries. Cause the register at regHasNull to be set 17866be515ebSdrh ** to a non-NULL value if iCur contains no NULLs. Cause register regHasNull 17876be515ebSdrh ** to be set to NULL if iCur contains one or more NULL values. 17886be515ebSdrh */ 17896be515ebSdrh static void sqlite3SetHasNullFlag(Vdbe *v, int iCur, int regHasNull){ 1790728e0f91Sdrh int addr1; 17916be515ebSdrh sqlite3VdbeAddOp2(v, OP_Integer, 0, regHasNull); 1792728e0f91Sdrh addr1 = sqlite3VdbeAddOp1(v, OP_Rewind, iCur); VdbeCoverage(v); 17936be515ebSdrh sqlite3VdbeAddOp3(v, OP_Column, iCur, 0, regHasNull); 17946be515ebSdrh sqlite3VdbeChangeP5(v, OPFLAG_TYPEOFARG); 17954c259e9fSdrh VdbeComment((v, "first_entry_in(%d)", iCur)); 1796728e0f91Sdrh sqlite3VdbeJumpHere(v, addr1); 17976be515ebSdrh } 17986be515ebSdrh 1799bb53ecb1Sdrh 1800bb53ecb1Sdrh #ifndef SQLITE_OMIT_SUBQUERY 1801bb53ecb1Sdrh /* 1802bb53ecb1Sdrh ** The argument is an IN operator with a list (not a subquery) on the 1803bb53ecb1Sdrh ** right-hand side. Return TRUE if that list is constant. 1804bb53ecb1Sdrh */ 1805bb53ecb1Sdrh static int sqlite3InRhsIsConstant(Expr *pIn){ 1806bb53ecb1Sdrh Expr *pLHS; 1807bb53ecb1Sdrh int res; 1808bb53ecb1Sdrh assert( !ExprHasProperty(pIn, EP_xIsSelect) ); 1809bb53ecb1Sdrh pLHS = pIn->pLeft; 1810bb53ecb1Sdrh pIn->pLeft = 0; 1811bb53ecb1Sdrh res = sqlite3ExprIsConstant(pIn); 1812bb53ecb1Sdrh pIn->pLeft = pLHS; 1813bb53ecb1Sdrh return res; 1814bb53ecb1Sdrh } 1815bb53ecb1Sdrh #endif 1816bb53ecb1Sdrh 18176be515ebSdrh /* 18189a96b668Sdanielk1977 ** This function is used by the implementation of the IN (...) operator. 1819d4305ca6Sdrh ** The pX parameter is the expression on the RHS of the IN operator, which 1820d4305ca6Sdrh ** might be either a list of expressions or a subquery. 18219a96b668Sdanielk1977 ** 1822d4305ca6Sdrh ** The job of this routine is to find or create a b-tree object that can 1823d4305ca6Sdrh ** be used either to test for membership in the RHS set or to iterate through 1824d4305ca6Sdrh ** all members of the RHS set, skipping duplicates. 1825d4305ca6Sdrh ** 18263a85625dSdrh ** A cursor is opened on the b-tree object that is the RHS of the IN operator 1827d4305ca6Sdrh ** and pX->iTable is set to the index of that cursor. 1828d4305ca6Sdrh ** 1829b74b1017Sdrh ** The returned value of this function indicates the b-tree type, as follows: 18309a96b668Sdanielk1977 ** 18319a96b668Sdanielk1977 ** IN_INDEX_ROWID - The cursor was opened on a database table. 18321ccce449Sdrh ** IN_INDEX_INDEX_ASC - The cursor was opened on an ascending index. 18331ccce449Sdrh ** IN_INDEX_INDEX_DESC - The cursor was opened on a descending index. 18349a96b668Sdanielk1977 ** IN_INDEX_EPH - The cursor was opened on a specially created and 18359a96b668Sdanielk1977 ** populated epheremal table. 1836bb53ecb1Sdrh ** IN_INDEX_NOOP - No cursor was allocated. The IN operator must be 1837bb53ecb1Sdrh ** implemented as a sequence of comparisons. 18389a96b668Sdanielk1977 ** 1839d4305ca6Sdrh ** An existing b-tree might be used if the RHS expression pX is a simple 1840d4305ca6Sdrh ** subquery such as: 18419a96b668Sdanielk1977 ** 18429a96b668Sdanielk1977 ** SELECT <column> FROM <table> 18439a96b668Sdanielk1977 ** 1844d4305ca6Sdrh ** If the RHS of the IN operator is a list or a more complex subquery, then 1845d4305ca6Sdrh ** an ephemeral table might need to be generated from the RHS and then 184660ec914cSpeter.d.reid ** pX->iTable made to point to the ephemeral table instead of an 1847d4305ca6Sdrh ** existing table. 1848d4305ca6Sdrh ** 18493a85625dSdrh ** The inFlags parameter must contain exactly one of the bits 18503a85625dSdrh ** IN_INDEX_MEMBERSHIP or IN_INDEX_LOOP. If inFlags contains 18513a85625dSdrh ** IN_INDEX_MEMBERSHIP, then the generated table will be used for a 18523a85625dSdrh ** fast membership test. When the IN_INDEX_LOOP bit is set, the 18533a85625dSdrh ** IN index will be used to loop over all values of the RHS of the 18543a85625dSdrh ** IN operator. 18553a85625dSdrh ** 18563a85625dSdrh ** When IN_INDEX_LOOP is used (and the b-tree will be used to iterate 18573a85625dSdrh ** through the set members) then the b-tree must not contain duplicates. 18583a85625dSdrh ** An epheremal table must be used unless the selected <column> is guaranteed 18599a96b668Sdanielk1977 ** to be unique - either because it is an INTEGER PRIMARY KEY or it 1860b74b1017Sdrh ** has a UNIQUE constraint or UNIQUE index. 18610cdc022eSdanielk1977 ** 18623a85625dSdrh ** When IN_INDEX_MEMBERSHIP is used (and the b-tree will be used 18633a85625dSdrh ** for fast set membership tests) then an epheremal table must 18640cdc022eSdanielk1977 ** be used unless <column> is an INTEGER PRIMARY KEY or an index can 18650cdc022eSdanielk1977 ** be found with <column> as its left-most column. 18660cdc022eSdanielk1977 ** 1867bb53ecb1Sdrh ** If the IN_INDEX_NOOP_OK and IN_INDEX_MEMBERSHIP are both set and 1868bb53ecb1Sdrh ** if the RHS of the IN operator is a list (not a subquery) then this 1869bb53ecb1Sdrh ** routine might decide that creating an ephemeral b-tree for membership 1870bb53ecb1Sdrh ** testing is too expensive and return IN_INDEX_NOOP. In that case, the 1871bb53ecb1Sdrh ** calling routine should implement the IN operator using a sequence 1872bb53ecb1Sdrh ** of Eq or Ne comparison operations. 1873bb53ecb1Sdrh ** 1874b74b1017Sdrh ** When the b-tree is being used for membership tests, the calling function 18753a85625dSdrh ** might need to know whether or not the RHS side of the IN operator 1876e21a6e1dSdrh ** contains a NULL. If prRhsHasNull is not a NULL pointer and 18773a85625dSdrh ** if there is any chance that the (...) might contain a NULL value at 18780cdc022eSdanielk1977 ** runtime, then a register is allocated and the register number written 1879e21a6e1dSdrh ** to *prRhsHasNull. If there is no chance that the (...) contains a 1880e21a6e1dSdrh ** NULL value, then *prRhsHasNull is left unchanged. 18810cdc022eSdanielk1977 ** 1882e21a6e1dSdrh ** If a register is allocated and its location stored in *prRhsHasNull, then 18836be515ebSdrh ** the value in that register will be NULL if the b-tree contains one or more 18846be515ebSdrh ** NULL values, and it will be some non-NULL value if the b-tree contains no 18856be515ebSdrh ** NULL values. 18869a96b668Sdanielk1977 */ 1887284f4acaSdanielk1977 #ifndef SQLITE_OMIT_SUBQUERY 1888ba00e30aSdan int sqlite3FindInIndex( 1889ba00e30aSdan Parse *pParse, 1890ba00e30aSdan Expr *pX, 1891ba00e30aSdan u32 inFlags, 1892ba00e30aSdan int *prRhsHasNull, 1893ba00e30aSdan int *aiMap 1894ba00e30aSdan ){ 1895b74b1017Sdrh Select *p; /* SELECT to the right of IN operator */ 1896b74b1017Sdrh int eType = 0; /* Type of RHS table. IN_INDEX_* */ 1897b74b1017Sdrh int iTab = pParse->nTab++; /* Cursor of the RHS table */ 18983a85625dSdrh int mustBeUnique; /* True if RHS must be unique */ 1899b8475df8Sdrh Vdbe *v = sqlite3GetVdbe(pParse); /* Virtual machine being coded */ 19009a96b668Sdanielk1977 19011450bc6eSdrh assert( pX->op==TK_IN ); 19023a85625dSdrh mustBeUnique = (inFlags & IN_INDEX_LOOP)!=0; 19031450bc6eSdrh 19047b35a77bSdan /* If the RHS of this IN(...) operator is a SELECT, and if it matters 19057b35a77bSdan ** whether or not the SELECT result contains NULL values, check whether 19067b35a77bSdan ** or not NULL is actuall possible (it may not be, for example, due 19077b35a77bSdan ** to NOT NULL constraints in the schema). If no NULL values are possible, 19087b35a77bSdan ** set prRhsHasNull to 0 before continuing. 19097b35a77bSdan */ 19107b35a77bSdan if( prRhsHasNull && (pX->flags & EP_xIsSelect) ){ 19117b35a77bSdan int i; 19127b35a77bSdan ExprList *pEList = pX->x.pSelect->pEList; 19137b35a77bSdan for(i=0; i<pEList->nExpr; i++){ 19147b35a77bSdan if( sqlite3ExprCanBeNull(pEList->a[i].pExpr) ) break; 19157b35a77bSdan } 19167b35a77bSdan if( i==pEList->nExpr ){ 19177b35a77bSdan prRhsHasNull = 0; 19187b35a77bSdan } 19197b35a77bSdan } 19207b35a77bSdan 1921b74b1017Sdrh /* Check to see if an existing table or index can be used to 1922b74b1017Sdrh ** satisfy the query. This is preferable to generating a new 19237b35a77bSdan ** ephemeral table. */ 19247b35a77bSdan if( pParse->nErr==0 && (p = isCandidateForInOpt(pX))!=0 ){ 1925e1fb65a0Sdanielk1977 sqlite3 *db = pParse->db; /* Database connection */ 1926b07028f7Sdrh Table *pTab; /* Table <table>. */ 1927ba00e30aSdan i16 iDb; /* Database idx for pTab */ 1928cfbb5e82Sdan ExprList *pEList = p->pEList; 1929cfbb5e82Sdan int nExpr = pEList->nExpr; 1930e1fb65a0Sdanielk1977 1931b07028f7Sdrh assert( p->pEList!=0 ); /* Because of isCandidateForInOpt(p) */ 1932b07028f7Sdrh assert( p->pEList->a[0].pExpr!=0 ); /* Because of isCandidateForInOpt(p) */ 1933b07028f7Sdrh assert( p->pSrc!=0 ); /* Because of isCandidateForInOpt(p) */ 1934b07028f7Sdrh pTab = p->pSrc->a[0].pTab; 1935b07028f7Sdrh 1936b22f7c83Sdrh /* Code an OP_Transaction and OP_TableLock for <table>. */ 1937e1fb65a0Sdanielk1977 iDb = sqlite3SchemaToIndex(db, pTab->pSchema); 1938e1fb65a0Sdanielk1977 sqlite3CodeVerifySchema(pParse, iDb); 1939e1fb65a0Sdanielk1977 sqlite3TableLock(pParse, iDb, pTab->tnum, 0, pTab->zName); 19409a96b668Sdanielk1977 19419a96b668Sdanielk1977 /* This function is only called from two places. In both cases the vdbe 19429a96b668Sdanielk1977 ** has already been allocated. So assume sqlite3GetVdbe() is always 19439a96b668Sdanielk1977 ** successful here. 19449a96b668Sdanielk1977 */ 19459a96b668Sdanielk1977 assert(v); 1946cfbb5e82Sdan if( nExpr==1 && pEList->a[0].pExpr->iColumn<0 ){ 19477d176105Sdrh int iAddr = sqlite3CodeOnce(pParse); 19487d176105Sdrh VdbeCoverage(v); 19499a96b668Sdanielk1977 19509a96b668Sdanielk1977 sqlite3OpenTable(pParse, iTab, iDb, pTab, OP_OpenRead); 19519a96b668Sdanielk1977 eType = IN_INDEX_ROWID; 19529a96b668Sdanielk1977 19539a96b668Sdanielk1977 sqlite3VdbeJumpHere(v, iAddr); 19549a96b668Sdanielk1977 }else{ 1955e1fb65a0Sdanielk1977 Index *pIdx; /* Iterator variable */ 1956cfbb5e82Sdan int affinity_ok = 1; 1957cfbb5e82Sdan int i; 1958cfbb5e82Sdan 1959cfbb5e82Sdan /* Check that the affinity that will be used to perform each 1960cfbb5e82Sdan ** comparison is the same as the affinity of each column. If 1961cfbb5e82Sdan ** it not, it is not possible to use any index. */ 1962cfbb5e82Sdan for(i=0; i<nExpr && affinity_ok; i++){ 1963cfbb5e82Sdan Expr *pLhs = exprVectorField(pX->pLeft, i); 1964cfbb5e82Sdan int iCol = pEList->a[i].pExpr->iColumn; 1965cfbb5e82Sdan char idxaff = pTab->aCol[iCol].affinity; 1966cfbb5e82Sdan char cmpaff = sqlite3CompareAffinity(pLhs, idxaff); 1967cfbb5e82Sdan switch( cmpaff ){ 1968cfbb5e82Sdan case SQLITE_AFF_BLOB: 1969cfbb5e82Sdan break; 1970cfbb5e82Sdan case SQLITE_AFF_TEXT: 1971cfbb5e82Sdan affinity_ok = (idxaff==SQLITE_AFF_TEXT); 1972cfbb5e82Sdan break; 1973cfbb5e82Sdan default: 1974cfbb5e82Sdan affinity_ok = sqlite3IsNumericAffinity(idxaff); 1975cfbb5e82Sdan } 1976cfbb5e82Sdan } 1977e1fb65a0Sdanielk1977 19789a96b668Sdanielk1977 /* The collation sequence used by the comparison. If an index is to 19799a96b668Sdanielk1977 ** be used in place of a temp-table, it must be ordered according 1980e1fb65a0Sdanielk1977 ** to this collation sequence. */ 19819a96b668Sdanielk1977 19829a96b668Sdanielk1977 for(pIdx=pTab->pIndex; pIdx && eType==0 && affinity_ok; pIdx=pIdx->pNext){ 1983cfbb5e82Sdan if( pIdx->nKeyCol<nExpr ) continue; 1984cfbb5e82Sdan if( mustBeUnique && (pIdx->nKeyCol!=nExpr || !IsUniqueIndex(pIdx)) ){ 1985cfbb5e82Sdan continue; 1986cfbb5e82Sdan } 1987cfbb5e82Sdan 1988cfbb5e82Sdan for(i=0; i<nExpr; i++){ 1989cfbb5e82Sdan Expr *pLhs = exprVectorField(pX->pLeft, i); 1990cfbb5e82Sdan Expr *pRhs = pEList->a[i].pExpr; 1991cfbb5e82Sdan CollSeq *pReq = sqlite3BinaryCompareCollSeq(pParse, pLhs, pRhs); 1992cfbb5e82Sdan int j; 1993cfbb5e82Sdan 1994cfbb5e82Sdan for(j=0; j<nExpr; j++){ 1995cfbb5e82Sdan if( pIdx->aiColumn[j]!=pRhs->iColumn ) continue; 1996cfbb5e82Sdan assert( pIdx->azColl[j] ); 1997cfbb5e82Sdan if( sqlite3StrICmp(pReq->zName, pIdx->azColl[j])!=0 ) continue; 1998cfbb5e82Sdan break; 1999cfbb5e82Sdan } 2000cfbb5e82Sdan if( j==nExpr ) break; 2001ba00e30aSdan if( aiMap ) aiMap[i] = j; 2002cfbb5e82Sdan } 2003cfbb5e82Sdan 2004cfbb5e82Sdan if( i==nExpr ){ 20057d176105Sdrh int iAddr = sqlite3CodeOnce(pParse); VdbeCoverage(v); 20062ec2fb22Sdrh sqlite3VdbeAddOp3(v, OP_OpenRead, iTab, pIdx->tnum, iDb); 20072ec2fb22Sdrh sqlite3VdbeSetP4KeyInfo(pParse, pIdx); 2008207872a4Sdanielk1977 VdbeComment((v, "%s", pIdx->zName)); 20091ccce449Sdrh assert( IN_INDEX_INDEX_DESC == IN_INDEX_INDEX_ASC+1 ); 20101ccce449Sdrh eType = IN_INDEX_INDEX_ASC + pIdx->aSortOrder[0]; 20119a96b668Sdanielk1977 20127b35a77bSdan if( prRhsHasNull ){ 20137b35a77bSdan *prRhsHasNull = ++pParse->nMem; 20143480bfdaSdan #ifdef SQLITE_ENABLE_COLUMN_USED_MASK 2015cfbb5e82Sdan i64 mask = (1<<nExpr)-1; 20163480bfdaSdan sqlite3VdbeAddOp4Dup8(v, OP_ColumnsUsed, 2017cfbb5e82Sdan iTab, 0, 0, (u8*)&mask, P4_INT64); 20183480bfdaSdan #endif 20197b35a77bSdan if( nExpr==1 ){ 20206be515ebSdrh sqlite3SetHasNullFlag(v, iTab, *prRhsHasNull); 20210cdc022eSdanielk1977 } 20227b35a77bSdan } 2023552fd454Sdrh sqlite3VdbeJumpHere(v, iAddr); 20249a96b668Sdanielk1977 } 20259a96b668Sdanielk1977 } 20269a96b668Sdanielk1977 } 20279a96b668Sdanielk1977 } 20289a96b668Sdanielk1977 2029bb53ecb1Sdrh /* If no preexisting index is available for the IN clause 2030bb53ecb1Sdrh ** and IN_INDEX_NOOP is an allowed reply 2031bb53ecb1Sdrh ** and the RHS of the IN operator is a list, not a subquery 203271c57db0Sdan ** and the RHS is not constant or has two or fewer terms, 203360ec914cSpeter.d.reid ** then it is not worth creating an ephemeral table to evaluate 2034bb53ecb1Sdrh ** the IN operator so return IN_INDEX_NOOP. 2035bb53ecb1Sdrh */ 2036bb53ecb1Sdrh if( eType==0 2037bb53ecb1Sdrh && (inFlags & IN_INDEX_NOOP_OK) 2038bb53ecb1Sdrh && !ExprHasProperty(pX, EP_xIsSelect) 2039bb53ecb1Sdrh && (!sqlite3InRhsIsConstant(pX) || pX->x.pList->nExpr<=2) 2040bb53ecb1Sdrh ){ 2041bb53ecb1Sdrh eType = IN_INDEX_NOOP; 2042bb53ecb1Sdrh } 2043bb53ecb1Sdrh 20449a96b668Sdanielk1977 if( eType==0 ){ 20454387006cSdrh /* Could not find an existing table or index to use as the RHS b-tree. 2046b74b1017Sdrh ** We will have to generate an ephemeral table to do the job. 2047b74b1017Sdrh */ 20488e23daf3Sdrh u32 savedNQueryLoop = pParse->nQueryLoop; 20490cdc022eSdanielk1977 int rMayHaveNull = 0; 205041a05b7bSdanielk1977 eType = IN_INDEX_EPH; 20513a85625dSdrh if( inFlags & IN_INDEX_LOOP ){ 20524a5acf8eSdrh pParse->nQueryLoop = 0; 2053c5cd1249Sdrh if( pX->pLeft->iColumn<0 && !ExprHasProperty(pX, EP_xIsSelect) ){ 205441a05b7bSdanielk1977 eType = IN_INDEX_ROWID; 20550cdc022eSdanielk1977 } 2056e21a6e1dSdrh }else if( prRhsHasNull ){ 2057e21a6e1dSdrh *prRhsHasNull = rMayHaveNull = ++pParse->nMem; 2058cf4d38aaSdrh } 205941a05b7bSdanielk1977 sqlite3CodeSubselect(pParse, pX, rMayHaveNull, eType==IN_INDEX_ROWID); 2060cf4d38aaSdrh pParse->nQueryLoop = savedNQueryLoop; 20619a96b668Sdanielk1977 }else{ 20629a96b668Sdanielk1977 pX->iTable = iTab; 20639a96b668Sdanielk1977 } 2064ba00e30aSdan 2065ba00e30aSdan if( aiMap && eType!=IN_INDEX_INDEX_ASC && eType!=IN_INDEX_INDEX_DESC ){ 2066ba00e30aSdan int i, n; 2067ba00e30aSdan n = sqlite3ExprVectorSize(pX->pLeft); 2068ba00e30aSdan for(i=0; i<n; i++) aiMap[i] = i; 2069ba00e30aSdan } 20709a96b668Sdanielk1977 return eType; 20719a96b668Sdanielk1977 } 2072284f4acaSdanielk1977 #endif 2073626a879aSdrh 207471c57db0Sdan static char *exprINAffinity(Parse *pParse, Expr *pExpr){ 207571c57db0Sdan Expr *pLeft = pExpr->pLeft; 207671c57db0Sdan int nVal = sqlite3ExprVectorSize(pLeft); 207771c57db0Sdan char *zRet; 207871c57db0Sdan 207971c57db0Sdan zRet = sqlite3DbMallocZero(pParse->db, nVal+1); 208071c57db0Sdan if( zRet ){ 208171c57db0Sdan int i; 208271c57db0Sdan for(i=0; i<nVal; i++){ 208371c57db0Sdan Expr *pA; 208471c57db0Sdan char a; 20858da209b1Sdan if( nVal==1 && 0 ){ 208671c57db0Sdan pA = pLeft; 208771c57db0Sdan }else{ 208871c57db0Sdan pA = exprVectorField(pLeft, i); 208971c57db0Sdan } 209071c57db0Sdan a = sqlite3ExprAffinity(pA); 209171c57db0Sdan zRet[i] = sqlite3CompareAffinity(pExpr->x.pSelect->pEList->a[i].pExpr, a); 209271c57db0Sdan } 209371c57db0Sdan zRet[nVal] = '\0'; 209471c57db0Sdan } 209571c57db0Sdan return zRet; 209671c57db0Sdan } 209771c57db0Sdan 20988da209b1Sdan #ifndef SQLITE_OMIT_SUBQUERY 20998da209b1Sdan /* 21008da209b1Sdan ** Load the Parse object passed as the first argument with an error 21018da209b1Sdan ** message of the form: 21028da209b1Sdan ** 21038da209b1Sdan ** "sub-select returns N columns - expected M" 21048da209b1Sdan */ 21058da209b1Sdan void sqlite3SubselectError(Parse *pParse, int nActual, int nExpect){ 21068da209b1Sdan const char *zFmt = "sub-select returns %d columns - expected %d"; 21078da209b1Sdan sqlite3ErrorMsg(pParse, zFmt, nActual, nExpect); 21088da209b1Sdan } 21098da209b1Sdan #endif 21108da209b1Sdan 2111626a879aSdrh /* 2112d4187c71Sdrh ** Generate code for scalar subqueries used as a subquery expression, EXISTS, 2113d4187c71Sdrh ** or IN operators. Examples: 2114626a879aSdrh ** 21159cbe6352Sdrh ** (SELECT a FROM b) -- subquery 21169cbe6352Sdrh ** EXISTS (SELECT a FROM b) -- EXISTS subquery 21179cbe6352Sdrh ** x IN (4,5,11) -- IN operator with list on right-hand side 21189cbe6352Sdrh ** x IN (SELECT a FROM b) -- IN operator with subquery on the right 2119fef5208cSdrh ** 21209cbe6352Sdrh ** The pExpr parameter describes the expression that contains the IN 21219cbe6352Sdrh ** operator or subquery. 212241a05b7bSdanielk1977 ** 212341a05b7bSdanielk1977 ** If parameter isRowid is non-zero, then expression pExpr is guaranteed 212441a05b7bSdanielk1977 ** to be of the form "<rowid> IN (?, ?, ?)", where <rowid> is a reference 212541a05b7bSdanielk1977 ** to some integer key column of a table B-Tree. In this case, use an 212641a05b7bSdanielk1977 ** intkey B-Tree to store the set of IN(...) values instead of the usual 212741a05b7bSdanielk1977 ** (slower) variable length keys B-Tree. 2128fd773cf9Sdrh ** 2129fd773cf9Sdrh ** If rMayHaveNull is non-zero, that means that the operation is an IN 2130fd773cf9Sdrh ** (not a SELECT or EXISTS) and that the RHS might contains NULLs. 21313a85625dSdrh ** All this routine does is initialize the register given by rMayHaveNull 21323a85625dSdrh ** to NULL. Calling routines will take care of changing this register 21333a85625dSdrh ** value to non-NULL if the RHS is NULL-free. 21341450bc6eSdrh ** 21351450bc6eSdrh ** For a SELECT or EXISTS operator, return the register that holds the 21361450bc6eSdrh ** result. For IN operators or if an error occurs, the return value is 0. 2137cce7d176Sdrh */ 213851522cd3Sdrh #ifndef SQLITE_OMIT_SUBQUERY 21391450bc6eSdrh int sqlite3CodeSubselect( 2140fd773cf9Sdrh Parse *pParse, /* Parsing context */ 2141fd773cf9Sdrh Expr *pExpr, /* The IN, SELECT, or EXISTS operator */ 21426be515ebSdrh int rHasNullFlag, /* Register that records whether NULLs exist in RHS */ 2143fd773cf9Sdrh int isRowid /* If true, LHS of IN operator is a rowid */ 214441a05b7bSdanielk1977 ){ 21456be515ebSdrh int jmpIfDynamic = -1; /* One-time test address */ 21461450bc6eSdrh int rReg = 0; /* Register storing resulting */ 2147b3bce662Sdanielk1977 Vdbe *v = sqlite3GetVdbe(pParse); 21481450bc6eSdrh if( NEVER(v==0) ) return 0; 2149ceea3321Sdrh sqlite3ExprCachePush(pParse); 2150fc976065Sdanielk1977 215157dbd7b3Sdrh /* This code must be run in its entirety every time it is encountered 215257dbd7b3Sdrh ** if any of the following is true: 215357dbd7b3Sdrh ** 215457dbd7b3Sdrh ** * The right-hand side is a correlated subquery 215557dbd7b3Sdrh ** * The right-hand side is an expression list containing variables 215657dbd7b3Sdrh ** * We are inside a trigger 215757dbd7b3Sdrh ** 215857dbd7b3Sdrh ** If all of the above are false, then we can run this code just once 215957dbd7b3Sdrh ** save the results, and reuse the same result on subsequent invocations. 2160b3bce662Sdanielk1977 */ 2161c5cd1249Sdrh if( !ExprHasProperty(pExpr, EP_VarSelect) ){ 21626be515ebSdrh jmpIfDynamic = sqlite3CodeOnce(pParse); VdbeCoverage(v); 2163b3bce662Sdanielk1977 } 2164b3bce662Sdanielk1977 21654a07e3dbSdan #ifndef SQLITE_OMIT_EXPLAIN 21664a07e3dbSdan if( pParse->explain==2 ){ 216762aaa6caSdrh char *zMsg = sqlite3MPrintf(pParse->db, "EXECUTE %s%s SUBQUERY %d", 216862aaa6caSdrh jmpIfDynamic>=0?"":"CORRELATED ", 216962aaa6caSdrh pExpr->op==TK_IN?"LIST":"SCALAR", 217062aaa6caSdrh pParse->iNextSelectId 21714a07e3dbSdan ); 21724a07e3dbSdan sqlite3VdbeAddOp4(v, OP_Explain, pParse->iSelectId, 0, 0, zMsg, P4_DYNAMIC); 21734a07e3dbSdan } 21744a07e3dbSdan #endif 21754a07e3dbSdan 2176cce7d176Sdrh switch( pExpr->op ){ 2177fef5208cSdrh case TK_IN: { 2178b9bb7c18Sdrh int addr; /* Address of OP_OpenEphemeral instruction */ 2179d4187c71Sdrh Expr *pLeft = pExpr->pLeft; /* the LHS of the IN operator */ 2180323df790Sdrh KeyInfo *pKeyInfo = 0; /* Key information */ 218171c57db0Sdan int nVal; /* Size of vector pLeft */ 2182d3d39e93Sdrh 218371c57db0Sdan nVal = sqlite3ExprVectorSize(pLeft); 2184e014a838Sdanielk1977 2185e014a838Sdanielk1977 /* Whether this is an 'x IN(SELECT...)' or an 'x IN(<exprlist>)' 21868cff69dfSdrh ** expression it is handled the same way. An ephemeral table is 2187e014a838Sdanielk1977 ** filled with single-field index keys representing the results 2188e014a838Sdanielk1977 ** from the SELECT or the <exprlist>. 2189fef5208cSdrh ** 2190e014a838Sdanielk1977 ** If the 'x' expression is a column value, or the SELECT... 2191e014a838Sdanielk1977 ** statement returns a column value, then the affinity of that 2192e014a838Sdanielk1977 ** column is used to build the index keys. If both 'x' and the 2193e014a838Sdanielk1977 ** SELECT... statement are columns, then numeric affinity is used 2194e014a838Sdanielk1977 ** if either column has NUMERIC or INTEGER affinity. If neither 2195e014a838Sdanielk1977 ** 'x' nor the SELECT... statement are columns, then numeric affinity 2196e014a838Sdanielk1977 ** is used. 2197fef5208cSdrh */ 2198832508b7Sdrh pExpr->iTable = pParse->nTab++; 219971c57db0Sdan addr = sqlite3VdbeAddOp2(v, OP_OpenEphemeral, 220071c57db0Sdan pExpr->iTable, (isRowid?0:nVal)); 220171c57db0Sdan pKeyInfo = isRowid ? 0 : sqlite3KeyInfoAlloc(pParse->db, nVal, 1); 2202e014a838Sdanielk1977 22036ab3a2ecSdanielk1977 if( ExprHasProperty(pExpr, EP_xIsSelect) ){ 2204e014a838Sdanielk1977 /* Case 1: expr IN (SELECT ...) 2205e014a838Sdanielk1977 ** 2206e014a838Sdanielk1977 ** Generate code to write the results of the select into the temporary 2207e014a838Sdanielk1977 ** table allocated and opened above. 2208e014a838Sdanielk1977 */ 22094387006cSdrh Select *pSelect = pExpr->x.pSelect; 221071c57db0Sdan ExprList *pEList = pSelect->pEList; 22111013c932Sdrh 221241a05b7bSdanielk1977 assert( !isRowid ); 221371c57db0Sdan if( pEList->nExpr!=nVal ){ 22148da209b1Sdan sqlite3SubselectError(pParse, pEList->nExpr, nVal); 221571c57db0Sdan }else{ 221671c57db0Sdan SelectDest dest; 221771c57db0Sdan int i; 22181013c932Sdrh sqlite3SelectDestInit(&dest, SRT_Set, pExpr->iTable); 221971c57db0Sdan dest.zAffSdst = exprINAffinity(pParse, pExpr); 2220e014a838Sdanielk1977 assert( (pExpr->iTable&0x0000FFFF)==pExpr->iTable ); 22214387006cSdrh pSelect->iLimit = 0; 22224387006cSdrh testcase( pSelect->selFlags & SF_Distinct ); 2223812ea833Sdrh testcase( pKeyInfo==0 ); /* Caused by OOM in sqlite3KeyInfoAlloc() */ 22244387006cSdrh if( sqlite3Select(pParse, pSelect, &dest) ){ 222571c57db0Sdan sqlite3DbFree(pParse->db, dest.zAffSdst); 22262ec2fb22Sdrh sqlite3KeyInfoUnref(pKeyInfo); 22271450bc6eSdrh return 0; 222894ccde58Sdrh } 222971c57db0Sdan sqlite3DbFree(pParse->db, dest.zAffSdst); 2230812ea833Sdrh assert( pKeyInfo!=0 ); /* OOM will cause exit after sqlite3Select() */ 22313535ec3eSdrh assert( pEList!=0 ); 22323535ec3eSdrh assert( pEList->nExpr>0 ); 22332ec2fb22Sdrh assert( sqlite3KeyInfoIsWriteable(pKeyInfo) ); 223471c57db0Sdan for(i=0; i<nVal; i++){ 223571c57db0Sdan Expr *p = (nVal>1) ? exprVectorField(pLeft, i) : pLeft; 223671c57db0Sdan pKeyInfo->aColl[i] = sqlite3BinaryCompareCollSeq( 223771c57db0Sdan pParse, p, pEList->a[i].pExpr 223871c57db0Sdan ); 223971c57db0Sdan } 224071c57db0Sdan } 2241a7d2db17Sdrh }else if( ALWAYS(pExpr->x.pList!=0) ){ 2242fef5208cSdrh /* Case 2: expr IN (exprlist) 2243fef5208cSdrh ** 2244e014a838Sdanielk1977 ** For each expression, build an index key from the evaluation and 2245e014a838Sdanielk1977 ** store it in the temporary table. If <expr> is a column, then use 2246e014a838Sdanielk1977 ** that columns affinity when building index keys. If <expr> is not 2247e014a838Sdanielk1977 ** a column, use numeric affinity. 2248fef5208cSdrh */ 224971c57db0Sdan char affinity; /* Affinity of the LHS of the IN */ 2250e014a838Sdanielk1977 int i; 22516ab3a2ecSdanielk1977 ExprList *pList = pExpr->x.pList; 225257dbd7b3Sdrh struct ExprList_item *pItem; 2253ecc31805Sdrh int r1, r2, r3; 225457dbd7b3Sdrh 225571c57db0Sdan affinity = sqlite3ExprAffinity(pLeft); 2256e014a838Sdanielk1977 if( !affinity ){ 225705883a34Sdrh affinity = SQLITE_AFF_BLOB; 2258e014a838Sdanielk1977 } 2259323df790Sdrh if( pKeyInfo ){ 22602ec2fb22Sdrh assert( sqlite3KeyInfoIsWriteable(pKeyInfo) ); 2261323df790Sdrh pKeyInfo->aColl[0] = sqlite3ExprCollSeq(pParse, pExpr->pLeft); 2262323df790Sdrh } 2263e014a838Sdanielk1977 2264e014a838Sdanielk1977 /* Loop through each expression in <exprlist>. */ 22652d401ab8Sdrh r1 = sqlite3GetTempReg(pParse); 22662d401ab8Sdrh r2 = sqlite3GetTempReg(pParse); 226737e08081Sdrh if( isRowid ) sqlite3VdbeAddOp2(v, OP_Null, 0, r2); 226857dbd7b3Sdrh for(i=pList->nExpr, pItem=pList->a; i>0; i--, pItem++){ 226957dbd7b3Sdrh Expr *pE2 = pItem->pExpr; 2270e05c929bSdrh int iValToIns; 2271e014a838Sdanielk1977 227257dbd7b3Sdrh /* If the expression is not constant then we will need to 227357dbd7b3Sdrh ** disable the test that was generated above that makes sure 227457dbd7b3Sdrh ** this code only executes once. Because for a non-constant 227557dbd7b3Sdrh ** expression we need to rerun this code each time. 227657dbd7b3Sdrh */ 22776be515ebSdrh if( jmpIfDynamic>=0 && !sqlite3ExprIsConstant(pE2) ){ 22786be515ebSdrh sqlite3VdbeChangeToNoop(v, jmpIfDynamic); 22796be515ebSdrh jmpIfDynamic = -1; 22804794b980Sdrh } 2281e014a838Sdanielk1977 2282e014a838Sdanielk1977 /* Evaluate the expression and insert it into the temp table */ 2283e05c929bSdrh if( isRowid && sqlite3ExprIsInteger(pE2, &iValToIns) ){ 2284e05c929bSdrh sqlite3VdbeAddOp3(v, OP_InsertInt, pExpr->iTable, r2, iValToIns); 2285e05c929bSdrh }else{ 2286ecc31805Sdrh r3 = sqlite3ExprCodeTarget(pParse, pE2, r1); 228741a05b7bSdanielk1977 if( isRowid ){ 2288e05c929bSdrh sqlite3VdbeAddOp2(v, OP_MustBeInt, r3, 2289e05c929bSdrh sqlite3VdbeCurrentAddr(v)+2); 2290688852abSdrh VdbeCoverage(v); 229141a05b7bSdanielk1977 sqlite3VdbeAddOp3(v, OP_Insert, pExpr->iTable, r2, r3); 229241a05b7bSdanielk1977 }else{ 2293ecc31805Sdrh sqlite3VdbeAddOp4(v, OP_MakeRecord, r3, 1, r2, &affinity, 1); 22943c31fc23Sdrh sqlite3ExprCacheAffinityChange(pParse, r3, 1); 22952d401ab8Sdrh sqlite3VdbeAddOp2(v, OP_IdxInsert, pExpr->iTable, r2); 2296fef5208cSdrh } 229741a05b7bSdanielk1977 } 2298e05c929bSdrh } 22992d401ab8Sdrh sqlite3ReleaseTempReg(pParse, r1); 23002d401ab8Sdrh sqlite3ReleaseTempReg(pParse, r2); 2301fef5208cSdrh } 2302323df790Sdrh if( pKeyInfo ){ 23032ec2fb22Sdrh sqlite3VdbeChangeP4(v, addr, (void *)pKeyInfo, P4_KEYINFO); 230441a05b7bSdanielk1977 } 2305b3bce662Sdanielk1977 break; 2306fef5208cSdrh } 2307fef5208cSdrh 230851522cd3Sdrh case TK_EXISTS: 2309fd773cf9Sdrh case TK_SELECT: 2310fd773cf9Sdrh default: { 2311fd773cf9Sdrh /* If this has to be a scalar SELECT. Generate code to put the 2312fef5208cSdrh ** value of this select in a memory cell and record the number 2313fd773cf9Sdrh ** of the memory cell in iColumn. If this is an EXISTS, write 2314fd773cf9Sdrh ** an integer 0 (not exists) or 1 (exists) into a memory cell 2315fd773cf9Sdrh ** and record that memory cell in iColumn. 2316fef5208cSdrh */ 2317fd773cf9Sdrh Select *pSel; /* SELECT statement to encode */ 2318fd773cf9Sdrh SelectDest dest; /* How to deal with SELECt result */ 231971c57db0Sdan int nReg; /* Registers to allocate */ 23201398ad36Sdrh 2321cf697396Sshane testcase( pExpr->op==TK_EXISTS ); 2322cf697396Sshane testcase( pExpr->op==TK_SELECT ); 2323cf697396Sshane assert( pExpr->op==TK_EXISTS || pExpr->op==TK_SELECT ); 23246ab3a2ecSdanielk1977 assert( ExprHasProperty(pExpr, EP_xIsSelect) ); 232571c57db0Sdan 23266ab3a2ecSdanielk1977 pSel = pExpr->x.pSelect; 232771c57db0Sdan nReg = pExpr->op==TK_SELECT ? pSel->pEList->nExpr : 1; 232871c57db0Sdan sqlite3SelectDestInit(&dest, 0, pParse->nMem+1); 232971c57db0Sdan pParse->nMem += nReg; 233051522cd3Sdrh if( pExpr->op==TK_SELECT ){ 23316c8c8ce0Sdanielk1977 dest.eDest = SRT_Mem; 233253932ce8Sdrh dest.iSdst = dest.iSDParm; 233371c57db0Sdan dest.nSdst = nReg; 233471c57db0Sdan sqlite3VdbeAddOp3(v, OP_Null, 0, dest.iSDParm, dest.iSDParm+nReg-1); 2335d4e70ebdSdrh VdbeComment((v, "Init subquery result")); 233651522cd3Sdrh }else{ 23376c8c8ce0Sdanielk1977 dest.eDest = SRT_Exists; 23382b596da8Sdrh sqlite3VdbeAddOp2(v, OP_Integer, 0, dest.iSDParm); 2339d4e70ebdSdrh VdbeComment((v, "Init EXISTS result")); 234051522cd3Sdrh } 2341633e6d57Sdrh sqlite3ExprDelete(pParse->db, pSel->pLimit); 2342094430ebSdrh pSel->pLimit = sqlite3PExpr(pParse, TK_INTEGER, 0, 0, 2343094430ebSdrh &sqlite3IntTokens[1]); 234448b5b041Sdrh pSel->iLimit = 0; 2345772460fdSdrh pSel->selFlags &= ~SF_MultiValue; 23467d10d5a6Sdrh if( sqlite3Select(pParse, pSel, &dest) ){ 23471450bc6eSdrh return 0; 234894ccde58Sdrh } 23492b596da8Sdrh rReg = dest.iSDParm; 2350ebb6a65dSdrh ExprSetVVAProperty(pExpr, EP_NoReduce); 2351b3bce662Sdanielk1977 break; 235219a775c2Sdrh } 2353cce7d176Sdrh } 2354b3bce662Sdanielk1977 23556be515ebSdrh if( rHasNullFlag ){ 23566be515ebSdrh sqlite3SetHasNullFlag(v, pExpr->iTable, rHasNullFlag); 2357b3bce662Sdanielk1977 } 23586be515ebSdrh 23596be515ebSdrh if( jmpIfDynamic>=0 ){ 23606be515ebSdrh sqlite3VdbeJumpHere(v, jmpIfDynamic); 2361b3bce662Sdanielk1977 } 2362d2490904Sdrh sqlite3ExprCachePop(pParse); 2363fc976065Sdanielk1977 23641450bc6eSdrh return rReg; 2365cce7d176Sdrh } 236651522cd3Sdrh #endif /* SQLITE_OMIT_SUBQUERY */ 2367cce7d176Sdrh 2368e3365e6cSdrh #ifndef SQLITE_OMIT_SUBQUERY 2369e3365e6cSdrh /* 23707b35a77bSdan ** Expr pIn is an IN(...) expression. This function checks that the 23717b35a77bSdan ** sub-select on the RHS of the IN() operator has the same number of 23727b35a77bSdan ** columns as the vector on the LHS. Or, if the RHS of the IN() is not 23737b35a77bSdan ** a sub-query, that the LHS is a vector of size 1. 23747b35a77bSdan */ 23757b35a77bSdan int sqlite3ExprCheckIN(Parse *pParse, Expr *pIn){ 23767b35a77bSdan int nVector = sqlite3ExprVectorSize(pIn->pLeft); 23777b35a77bSdan if( (pIn->flags & EP_xIsSelect) ){ 23787b35a77bSdan if( nVector!=pIn->x.pSelect->pEList->nExpr ){ 23797b35a77bSdan sqlite3SubselectError(pParse, pIn->x.pSelect->pEList->nExpr, nVector); 23807b35a77bSdan return 1; 23817b35a77bSdan } 23827b35a77bSdan }else if( nVector!=1 ){ 23837b35a77bSdan if( (pIn->pLeft->flags & EP_xIsSelect) ){ 23847b35a77bSdan sqlite3SubselectError(pParse, nVector, 1); 23857b35a77bSdan }else{ 23867b35a77bSdan sqlite3ErrorMsg(pParse, "invalid use of row value"); 23877b35a77bSdan } 23887b35a77bSdan return 1; 23897b35a77bSdan } 23907b35a77bSdan return 0; 23917b35a77bSdan } 23927b35a77bSdan #endif 23937b35a77bSdan 23947b35a77bSdan #ifndef SQLITE_OMIT_SUBQUERY 23957b35a77bSdan /* 2396e3365e6cSdrh ** Generate code for an IN expression. 2397e3365e6cSdrh ** 2398e3365e6cSdrh ** x IN (SELECT ...) 2399e3365e6cSdrh ** x IN (value, value, ...) 2400e3365e6cSdrh ** 2401e3365e6cSdrh ** The left-hand side (LHS) is a scalar expression. The right-hand side (RHS) 2402e3365e6cSdrh ** is an array of zero or more values. The expression is true if the LHS is 2403e3365e6cSdrh ** contained within the RHS. The value of the expression is unknown (NULL) 2404e3365e6cSdrh ** if the LHS is NULL or if the LHS is not contained within the RHS and the 2405e3365e6cSdrh ** RHS contains one or more NULL values. 2406e3365e6cSdrh ** 24076be515ebSdrh ** This routine generates code that jumps to destIfFalse if the LHS is not 2408e3365e6cSdrh ** contained within the RHS. If due to NULLs we cannot determine if the LHS 2409e3365e6cSdrh ** is contained in the RHS then jump to destIfNull. If the LHS is contained 2410e3365e6cSdrh ** within the RHS then fall through. 2411e3365e6cSdrh */ 2412e3365e6cSdrh static void sqlite3ExprCodeIN( 2413e3365e6cSdrh Parse *pParse, /* Parsing and code generating context */ 2414e3365e6cSdrh Expr *pExpr, /* The IN expression */ 2415e3365e6cSdrh int destIfFalse, /* Jump here if LHS is not contained in the RHS */ 2416e3365e6cSdrh int destIfNull /* Jump here if the results are unknown due to NULLs */ 2417e3365e6cSdrh ){ 2418e3365e6cSdrh int rRhsHasNull = 0; /* Register that is true if RHS contains NULL values */ 2419e3365e6cSdrh int eType; /* Type of the RHS */ 2420e3365e6cSdrh int r1; /* Temporary use register */ 2421e3365e6cSdrh Vdbe *v; /* Statement under construction */ 2422ba00e30aSdan int *aiMap = 0; /* Map from vector field to index column */ 2423ba00e30aSdan char *zAff = 0; /* Affinity string for comparisons */ 2424ba00e30aSdan int nVector; /* Size of vectors for this IN(...) op */ 2425ba00e30aSdan int regSelect = 0; 2426ba00e30aSdan Expr *pLeft = pExpr->pLeft; 2427ba00e30aSdan int i; 2428e3365e6cSdrh 24297b35a77bSdan if( sqlite3ExprCheckIN(pParse, pExpr) ) return; 2430ba00e30aSdan nVector = sqlite3ExprVectorSize(pExpr->pLeft); 2431ba00e30aSdan aiMap = (int*)sqlite3DbMallocZero( 2432ba00e30aSdan pParse->db, nVector*(sizeof(int) + sizeof(char)) + 1 2433ba00e30aSdan ); 2434ba00e30aSdan if( !aiMap ) return; 2435ba00e30aSdan zAff = (char*)&aiMap[nVector]; 243671c57db0Sdan 24377b35a77bSdan 2438ba00e30aSdan /* Attempt to compute the RHS. After this step, if anything other than 2439ba00e30aSdan ** IN_INDEX_NOOP is returned, the table opened ith cursor pExpr->iTable 2440ba00e30aSdan ** contains the values that make up the RHS. If IN_INDEX_NOOP is returned, 2441ba00e30aSdan ** the RHS has not yet been coded. */ 2442e3365e6cSdrh v = pParse->pVdbe; 2443e3365e6cSdrh assert( v!=0 ); /* OOM detected prior to this routine */ 2444e3365e6cSdrh VdbeNoopComment((v, "begin IN expr")); 2445bb53ecb1Sdrh eType = sqlite3FindInIndex(pParse, pExpr, 2446bb53ecb1Sdrh IN_INDEX_MEMBERSHIP | IN_INDEX_NOOP_OK, 2447ba00e30aSdan destIfFalse==destIfNull ? 0 : &rRhsHasNull, aiMap); 2448e3365e6cSdrh 2449ba00e30aSdan assert( pParse->nErr || nVector==1 || eType==IN_INDEX_EPH 2450ba00e30aSdan || eType==IN_INDEX_INDEX_ASC || eType==IN_INDEX_INDEX_DESC 2451ba00e30aSdan ); 2452e3365e6cSdrh 2453ba00e30aSdan /* Code the LHS, the <expr> from "<expr> IN (...)". If the LHS is a 2454ba00e30aSdan ** vector, then it is stored in an array of nVector registers starting 2455ba00e30aSdan ** at r1. 2456e3365e6cSdrh */ 2457ba00e30aSdan r1 = sqlite3GetTempRange(pParse, nVector); 2458e3365e6cSdrh sqlite3ExprCachePush(pParse); 2459ba00e30aSdan if( nVector>1 && (pLeft->flags & EP_xIsSelect) ){ 2460ba00e30aSdan regSelect = sqlite3CodeSubselect(pParse, pLeft, 0, 0); 2461ba00e30aSdan } 2462ba00e30aSdan for(i=0; i<nVector; i++){ 2463ba00e30aSdan int iCol = aiMap[i]; 2464ba00e30aSdan Expr *pLhs = exprVectorField(pLeft, i); 2465ba00e30aSdan 2466ba00e30aSdan if( regSelect ){ 2467ba00e30aSdan sqlite3VdbeAddOp3(v, OP_Copy, regSelect+i, r1+iCol, 0); 2468ba00e30aSdan }else{ 2469ba00e30aSdan sqlite3ExprCode(pParse, pLhs, r1+iCol); 2470ba00e30aSdan } 2471ba00e30aSdan 2472ba00e30aSdan zAff[iCol] = sqlite3ExprAffinity(pLhs); 2473ba00e30aSdan if( pExpr->flags & EP_xIsSelect ){ 2474ba00e30aSdan zAff[iCol] = sqlite3CompareAffinity( 2475ba00e30aSdan pExpr->x.pSelect->pEList->a[iCol].pExpr, zAff[iCol] 2476ba00e30aSdan ); 2477ba00e30aSdan } 2478ba00e30aSdan } 2479e3365e6cSdrh 2480bb53ecb1Sdrh /* If sqlite3FindInIndex() did not find or create an index that is 2481bb53ecb1Sdrh ** suitable for evaluating the IN operator, then evaluate using a 2482bb53ecb1Sdrh ** sequence of comparisons. 2483bb53ecb1Sdrh */ 2484bb53ecb1Sdrh if( eType==IN_INDEX_NOOP ){ 2485bb53ecb1Sdrh ExprList *pList = pExpr->x.pList; 2486bb53ecb1Sdrh CollSeq *pColl = sqlite3ExprCollSeq(pParse, pExpr->pLeft); 2487bb53ecb1Sdrh int labelOk = sqlite3VdbeMakeLabel(v); 2488bb53ecb1Sdrh int r2, regToFree; 2489bb53ecb1Sdrh int regCkNull = 0; 2490bb53ecb1Sdrh int ii; 2491bb53ecb1Sdrh assert( !ExprHasProperty(pExpr, EP_xIsSelect) ); 2492bb53ecb1Sdrh if( destIfNull!=destIfFalse ){ 2493bb53ecb1Sdrh regCkNull = sqlite3GetTempReg(pParse); 2494a976979bSdrh sqlite3VdbeAddOp3(v, OP_BitAnd, r1, r1, regCkNull); 2495bb53ecb1Sdrh } 2496bb53ecb1Sdrh for(ii=0; ii<pList->nExpr; ii++){ 2497bb53ecb1Sdrh r2 = sqlite3ExprCodeTemp(pParse, pList->a[ii].pExpr, ®ToFree); 2498a976979bSdrh if( regCkNull && sqlite3ExprCanBeNull(pList->a[ii].pExpr) ){ 2499bb53ecb1Sdrh sqlite3VdbeAddOp3(v, OP_BitAnd, regCkNull, r2, regCkNull); 2500bb53ecb1Sdrh } 2501bb53ecb1Sdrh if( ii<pList->nExpr-1 || destIfNull!=destIfFalse ){ 2502bb53ecb1Sdrh sqlite3VdbeAddOp4(v, OP_Eq, r1, labelOk, r2, 25034336b0e6Sdrh (void*)pColl, P4_COLLSEQ); 25044336b0e6Sdrh VdbeCoverageIf(v, ii<pList->nExpr-1); 25054336b0e6Sdrh VdbeCoverageIf(v, ii==pList->nExpr-1); 2506ba00e30aSdan sqlite3VdbeChangeP5(v, zAff[0]); 2507bb53ecb1Sdrh }else{ 2508bb53ecb1Sdrh assert( destIfNull==destIfFalse ); 2509bb53ecb1Sdrh sqlite3VdbeAddOp4(v, OP_Ne, r1, destIfFalse, r2, 2510bb53ecb1Sdrh (void*)pColl, P4_COLLSEQ); VdbeCoverage(v); 2511ba00e30aSdan sqlite3VdbeChangeP5(v, zAff[0] | SQLITE_JUMPIFNULL); 2512bb53ecb1Sdrh } 2513bb53ecb1Sdrh sqlite3ReleaseTempReg(pParse, regToFree); 2514bb53ecb1Sdrh } 2515bb53ecb1Sdrh if( regCkNull ){ 2516bb53ecb1Sdrh sqlite3VdbeAddOp2(v, OP_IsNull, regCkNull, destIfNull); VdbeCoverage(v); 2517076e85f5Sdrh sqlite3VdbeGoto(v, destIfFalse); 2518bb53ecb1Sdrh } 2519bb53ecb1Sdrh sqlite3VdbeResolveLabel(v, labelOk); 2520bb53ecb1Sdrh sqlite3ReleaseTempReg(pParse, regCkNull); 2521bb53ecb1Sdrh }else{ 2522bb53ecb1Sdrh 25237b35a77bSdan /* If any value on the LHS is NULL, the result of the IN(...) operator 25247b35a77bSdan ** must be either false or NULL. If these two are handled identically, 25257b35a77bSdan ** test the LHS for NULLs and jump directly to destIfNull if any are 25267b35a77bSdan ** found. 25277b35a77bSdan ** 25287b35a77bSdan ** Otherwise, if NULL and false are handled differently, and the 25297b35a77bSdan ** IN(...) operation is not a vector operation, and the LHS of the 25307b35a77bSdan ** operator is NULL, then the result is false if the index is 25317b35a77bSdan ** completely empty, or NULL otherwise. */ 2532094430ebSdrh if( destIfNull==destIfFalse ){ 2533d49fd4e8Sdan for(i=0; i<nVector; i++){ 2534d49fd4e8Sdan Expr *p = exprVectorField(pExpr->pLeft, i); 2535d49fd4e8Sdan if( sqlite3ExprCanBeNull(p) ){ 2536d49fd4e8Sdan sqlite3VdbeAddOp2(v, OP_IsNull, r1+aiMap[i], destIfNull); 2537d49fd4e8Sdan } 2538d49fd4e8Sdan } 2539d49fd4e8Sdan }else if( nVector==1 && sqlite3ExprCanBeNull(pExpr->pLeft) ){ 2540688852abSdrh int addr1 = sqlite3VdbeAddOp1(v, OP_NotNull, r1); VdbeCoverage(v); 2541094430ebSdrh sqlite3VdbeAddOp2(v, OP_Rewind, pExpr->iTable, destIfFalse); 2542688852abSdrh VdbeCoverage(v); 2543076e85f5Sdrh sqlite3VdbeGoto(v, destIfNull); 2544094430ebSdrh sqlite3VdbeJumpHere(v, addr1); 2545094430ebSdrh } 2546e3365e6cSdrh 2547e3365e6cSdrh if( eType==IN_INDEX_ROWID ){ 25487b35a77bSdan /* In this case, the RHS is the ROWID of table b-tree */ 2549eeb9565aSdrh sqlite3VdbeAddOp3(v, OP_SeekRowid, pExpr->iTable, destIfFalse, r1); 2550688852abSdrh VdbeCoverage(v); 25517b35a77bSdan }else{ 25527b35a77bSdan /* In this case, the RHS is an index b-tree. Apply the comparison 25537b35a77bSdan ** affinities to each value on the LHS of the operator. */ 25547b35a77bSdan sqlite3VdbeAddOp4(v, OP_Affinity, r1, nVector, 0, zAff, nVector); 2555ba00e30aSdan 25567b35a77bSdan if( nVector>1 && destIfNull!=destIfFalse ){ 25577b35a77bSdan int iIdx = pExpr->iTable; 25587b35a77bSdan int addr; 25597b35a77bSdan int addrNext; 25607b35a77bSdan 25617b35a77bSdan /* Search the index for the key. */ 25627b35a77bSdan addr = sqlite3VdbeAddOp4Int(v, OP_Found, iIdx, 0, r1, nVector); 25637b35a77bSdan 25647b35a77bSdan /* At this point the specified key is not present in the index, 25657b35a77bSdan ** so the result of the IN(..) operator must be either NULL or 25667b35a77bSdan ** 0. The vdbe code generated below figures out which. */ 25677b35a77bSdan addrNext = 1+sqlite3VdbeAddOp2(v, OP_Rewind, iIdx, destIfFalse); 25687b35a77bSdan 2569ba00e30aSdan for(i=0; i<nVector; i++){ 2570ba00e30aSdan Expr *p; 2571ba00e30aSdan CollSeq *pColl; 25727b35a77bSdan int r2 = sqlite3GetTempReg(pParse); 2573ba00e30aSdan p = exprVectorField(pLeft, i); 2574ba00e30aSdan pColl = sqlite3ExprCollSeq(pParse, p); 2575ba00e30aSdan 25767b35a77bSdan sqlite3VdbeAddOp3(v, OP_Column, iIdx, i, r2); 25777b35a77bSdan sqlite3VdbeAddOp4(v, OP_Eq, r1+i, 0, r2, (void*)pColl,P4_COLLSEQ); 25787b35a77bSdan sqlite3VdbeChangeP5(v, SQLITE_JUMPIFNULL); 25797b35a77bSdan sqlite3VdbeAddOp2(v, OP_Next, iIdx, addrNext); 2580ba00e30aSdan sqlite3VdbeAddOp2(v, OP_Goto, 0, destIfFalse); 25817b35a77bSdan sqlite3VdbeJumpHere(v, sqlite3VdbeCurrentAddr(v)-3); 2582ba00e30aSdan sqlite3ReleaseTempReg(pParse, r2); 25837b35a77bSdan } 25847b35a77bSdan sqlite3VdbeAddOp2(v, OP_Goto, 0, destIfNull); 2585e3365e6cSdrh 25867b35a77bSdan /* The key was found in the index. If it contains any NULL values, 25877b35a77bSdan ** then the result of the IN(...) operator is NULL. Otherwise, the 25887b35a77bSdan ** result is 1. */ 25897b35a77bSdan sqlite3VdbeJumpHere(v, addr); 25907b35a77bSdan for(i=0; i<nVector; i++){ 25917b35a77bSdan Expr *p = exprVectorField(pExpr->pLeft, i); 25927b35a77bSdan if( sqlite3ExprCanBeNull(p) ){ 25937b35a77bSdan sqlite3VdbeAddOp2(v, OP_IsNull, r1+aiMap[i], destIfNull); 25947b35a77bSdan } 25957b35a77bSdan } 25967b35a77bSdan 25977b35a77bSdan }else if( rRhsHasNull==0 ){ 2598e3365e6cSdrh /* This branch runs if it is known at compile time that the RHS 25997b35a77bSdan ** cannot contain NULL values. This happens as a result 26007b35a77bSdan ** of "NOT NULL" constraints in the database schema. 2601e3365e6cSdrh ** 2602e3365e6cSdrh ** Also run this branch if NULL is equivalent to FALSE 26037b35a77bSdan ** for this particular IN operator. */ 2604ba00e30aSdan sqlite3VdbeAddOp4Int( 2605ba00e30aSdan v, OP_NotFound, pExpr->iTable, destIfFalse, r1, nVector 2606ba00e30aSdan ); 2607688852abSdrh VdbeCoverage(v); 2608e3365e6cSdrh }else{ 2609e3365e6cSdrh /* In this branch, the RHS of the IN might contain a NULL and 2610e3365e6cSdrh ** the presence of a NULL on the RHS makes a difference in the 2611e3365e6cSdrh ** outcome. 2612e3365e6cSdrh */ 2613728e0f91Sdrh int addr1; 2614e3365e6cSdrh 2615e3365e6cSdrh /* First check to see if the LHS is contained in the RHS. If so, 26166be515ebSdrh ** then the answer is TRUE the presence of NULLs in the RHS does 26176be515ebSdrh ** not matter. If the LHS is not contained in the RHS, then the 26186be515ebSdrh ** answer is NULL if the RHS contains NULLs and the answer is 26196be515ebSdrh ** FALSE if the RHS is NULL-free. 2620e3365e6cSdrh */ 2621728e0f91Sdrh addr1 = sqlite3VdbeAddOp4Int(v, OP_Found, pExpr->iTable, 0, r1, 1); 2622688852abSdrh VdbeCoverage(v); 26236be515ebSdrh sqlite3VdbeAddOp2(v, OP_IsNull, rRhsHasNull, destIfNull); 2624552fd454Sdrh VdbeCoverage(v); 2625076e85f5Sdrh sqlite3VdbeGoto(v, destIfFalse); 2626728e0f91Sdrh sqlite3VdbeJumpHere(v, addr1); 2627e3365e6cSdrh } 2628e3365e6cSdrh } 2629bb53ecb1Sdrh } 2630e3365e6cSdrh sqlite3ReleaseTempReg(pParse, r1); 2631d2490904Sdrh sqlite3ExprCachePop(pParse); 2632ba00e30aSdan sqlite3DbFree(pParse->db, aiMap); 2633e3365e6cSdrh VdbeComment((v, "end IN expr")); 2634e3365e6cSdrh } 2635e3365e6cSdrh #endif /* SQLITE_OMIT_SUBQUERY */ 2636e3365e6cSdrh 263713573c71Sdrh #ifndef SQLITE_OMIT_FLOATING_POINT 2638598f1340Sdrh /* 2639598f1340Sdrh ** Generate an instruction that will put the floating point 26409cbf3425Sdrh ** value described by z[0..n-1] into register iMem. 26410cf19ed8Sdrh ** 26420cf19ed8Sdrh ** The z[] string will probably not be zero-terminated. But the 26430cf19ed8Sdrh ** z[n] character is guaranteed to be something that does not look 26440cf19ed8Sdrh ** like the continuation of the number. 2645598f1340Sdrh */ 2646b7916a78Sdrh static void codeReal(Vdbe *v, const char *z, int negateFlag, int iMem){ 2647fd773cf9Sdrh if( ALWAYS(z!=0) ){ 2648598f1340Sdrh double value; 26499339da1fSdrh sqlite3AtoF(z, &value, sqlite3Strlen30(z), SQLITE_UTF8); 2650d0015161Sdrh assert( !sqlite3IsNaN(value) ); /* The new AtoF never returns NaN */ 2651598f1340Sdrh if( negateFlag ) value = -value; 265297bae794Sdrh sqlite3VdbeAddOp4Dup8(v, OP_Real, 0, iMem, 0, (u8*)&value, P4_REAL); 2653598f1340Sdrh } 2654598f1340Sdrh } 265513573c71Sdrh #endif 2656598f1340Sdrh 2657598f1340Sdrh 2658598f1340Sdrh /* 2659fec19aadSdrh ** Generate an instruction that will put the integer describe by 26609cbf3425Sdrh ** text z[0..n-1] into register iMem. 26610cf19ed8Sdrh ** 26625f1d6b61Sshaneh ** Expr.u.zToken is always UTF8 and zero-terminated. 2663fec19aadSdrh */ 266413573c71Sdrh static void codeInteger(Parse *pParse, Expr *pExpr, int negFlag, int iMem){ 266513573c71Sdrh Vdbe *v = pParse->pVdbe; 266692b01d53Sdrh if( pExpr->flags & EP_IntValue ){ 266733e619fcSdrh int i = pExpr->u.iValue; 2668d50ffc41Sdrh assert( i>=0 ); 266992b01d53Sdrh if( negFlag ) i = -i; 267092b01d53Sdrh sqlite3VdbeAddOp2(v, OP_Integer, i, iMem); 2671fd773cf9Sdrh }else{ 26725f1d6b61Sshaneh int c; 26735f1d6b61Sshaneh i64 value; 2674fd773cf9Sdrh const char *z = pExpr->u.zToken; 2675fd773cf9Sdrh assert( z!=0 ); 26769296c18aSdrh c = sqlite3DecOrHexToI64(z, &value); 26775f1d6b61Sshaneh if( c==0 || (c==2 && negFlag) ){ 2678158b9cb9Sdrh if( negFlag ){ value = c==2 ? SMALLEST_INT64 : -value; } 267997bae794Sdrh sqlite3VdbeAddOp4Dup8(v, OP_Int64, 0, iMem, 0, (u8*)&value, P4_INT64); 2680fec19aadSdrh }else{ 268113573c71Sdrh #ifdef SQLITE_OMIT_FLOATING_POINT 268213573c71Sdrh sqlite3ErrorMsg(pParse, "oversized integer: %s%s", negFlag ? "-" : "", z); 268313573c71Sdrh #else 26841b7ddc59Sdrh #ifndef SQLITE_OMIT_HEX_INTEGER 26859296c18aSdrh if( sqlite3_strnicmp(z,"0x",2)==0 ){ 26869296c18aSdrh sqlite3ErrorMsg(pParse, "hex literal too big: %s", z); 26871b7ddc59Sdrh }else 26881b7ddc59Sdrh #endif 26891b7ddc59Sdrh { 2690b7916a78Sdrh codeReal(v, z, negFlag, iMem); 26919296c18aSdrh } 269213573c71Sdrh #endif 2693fec19aadSdrh } 2694fec19aadSdrh } 2695c9cf901dSdanielk1977 } 2696fec19aadSdrh 2697bea119cdSdrh #if defined(SQLITE_DEBUG) 2698bea119cdSdrh /* 2699bea119cdSdrh ** Verify the consistency of the column cache 2700bea119cdSdrh */ 2701bea119cdSdrh static int cacheIsValid(Parse *pParse){ 2702bea119cdSdrh int i, n; 2703bea119cdSdrh for(i=n=0; i<SQLITE_N_COLCACHE; i++){ 2704bea119cdSdrh if( pParse->aColCache[i].iReg>0 ) n++; 2705bea119cdSdrh } 2706bea119cdSdrh return n==pParse->nColCache; 2707bea119cdSdrh } 2708bea119cdSdrh #endif 2709bea119cdSdrh 2710ceea3321Sdrh /* 2711ceea3321Sdrh ** Clear a cache entry. 2712ceea3321Sdrh */ 2713ceea3321Sdrh static void cacheEntryClear(Parse *pParse, struct yColCache *p){ 2714ceea3321Sdrh if( p->tempReg ){ 2715ceea3321Sdrh if( pParse->nTempReg<ArraySize(pParse->aTempReg) ){ 2716ceea3321Sdrh pParse->aTempReg[pParse->nTempReg++] = p->iReg; 2717ceea3321Sdrh } 2718ceea3321Sdrh p->tempReg = 0; 2719ceea3321Sdrh } 2720bea119cdSdrh p->iReg = 0; 2721bea119cdSdrh pParse->nColCache--; 2722ee65eea4Sdan assert( pParse->db->mallocFailed || cacheIsValid(pParse) ); 2723ceea3321Sdrh } 2724ceea3321Sdrh 2725ceea3321Sdrh 2726ceea3321Sdrh /* 2727ceea3321Sdrh ** Record in the column cache that a particular column from a 2728ceea3321Sdrh ** particular table is stored in a particular register. 2729ceea3321Sdrh */ 2730ceea3321Sdrh void sqlite3ExprCacheStore(Parse *pParse, int iTab, int iCol, int iReg){ 2731ceea3321Sdrh int i; 2732ceea3321Sdrh int minLru; 2733ceea3321Sdrh int idxLru; 2734ceea3321Sdrh struct yColCache *p; 2735ceea3321Sdrh 2736ce8f53d4Sdan /* Unless an error has occurred, register numbers are always positive. */ 2737ce8f53d4Sdan assert( iReg>0 || pParse->nErr || pParse->db->mallocFailed ); 273820411ea7Sdrh assert( iCol>=-1 && iCol<32768 ); /* Finite column numbers */ 273920411ea7Sdrh 2740b6da74ebSdrh /* The SQLITE_ColumnCache flag disables the column cache. This is used 2741b6da74ebSdrh ** for testing only - to verify that SQLite always gets the same answer 2742b6da74ebSdrh ** with and without the column cache. 2743b6da74ebSdrh */ 27447e5418e4Sdrh if( OptimizationDisabled(pParse->db, SQLITE_ColumnCache) ) return; 2745b6da74ebSdrh 274627ee406eSdrh /* First replace any existing entry. 274727ee406eSdrh ** 274827ee406eSdrh ** Actually, the way the column cache is currently used, we are guaranteed 274927ee406eSdrh ** that the object will never already be in cache. Verify this guarantee. 275027ee406eSdrh */ 275127ee406eSdrh #ifndef NDEBUG 2752ceea3321Sdrh for(i=0, p=pParse->aColCache; i<SQLITE_N_COLCACHE; i++, p++){ 275327ee406eSdrh assert( p->iReg==0 || p->iTable!=iTab || p->iColumn!=iCol ); 2754ceea3321Sdrh } 275527ee406eSdrh #endif 2756ceea3321Sdrh 2757ceea3321Sdrh /* Find an empty slot and replace it */ 2758ceea3321Sdrh for(i=0, p=pParse->aColCache; i<SQLITE_N_COLCACHE; i++, p++){ 2759ceea3321Sdrh if( p->iReg==0 ){ 2760ceea3321Sdrh p->iLevel = pParse->iCacheLevel; 2761ceea3321Sdrh p->iTable = iTab; 2762ceea3321Sdrh p->iColumn = iCol; 2763ceea3321Sdrh p->iReg = iReg; 2764ceea3321Sdrh p->tempReg = 0; 2765ceea3321Sdrh p->lru = pParse->iCacheCnt++; 2766bea119cdSdrh pParse->nColCache++; 2767ee65eea4Sdan assert( pParse->db->mallocFailed || cacheIsValid(pParse) ); 2768ceea3321Sdrh return; 2769ceea3321Sdrh } 2770ceea3321Sdrh } 2771ceea3321Sdrh 2772ceea3321Sdrh /* Replace the last recently used */ 2773ceea3321Sdrh minLru = 0x7fffffff; 2774ceea3321Sdrh idxLru = -1; 2775ceea3321Sdrh for(i=0, p=pParse->aColCache; i<SQLITE_N_COLCACHE; i++, p++){ 2776ceea3321Sdrh if( p->lru<minLru ){ 2777ceea3321Sdrh idxLru = i; 2778ceea3321Sdrh minLru = p->lru; 2779ceea3321Sdrh } 2780ceea3321Sdrh } 278120411ea7Sdrh if( ALWAYS(idxLru>=0) ){ 2782ceea3321Sdrh p = &pParse->aColCache[idxLru]; 2783ceea3321Sdrh p->iLevel = pParse->iCacheLevel; 2784ceea3321Sdrh p->iTable = iTab; 2785ceea3321Sdrh p->iColumn = iCol; 2786ceea3321Sdrh p->iReg = iReg; 2787ceea3321Sdrh p->tempReg = 0; 2788ceea3321Sdrh p->lru = pParse->iCacheCnt++; 2789bea119cdSdrh assert( cacheIsValid(pParse) ); 2790ceea3321Sdrh return; 2791ceea3321Sdrh } 2792ceea3321Sdrh } 2793ceea3321Sdrh 2794ceea3321Sdrh /* 2795f49f3523Sdrh ** Indicate that registers between iReg..iReg+nReg-1 are being overwritten. 2796f49f3523Sdrh ** Purge the range of registers from the column cache. 2797ceea3321Sdrh */ 2798f49f3523Sdrh void sqlite3ExprCacheRemove(Parse *pParse, int iReg, int nReg){ 2799ceea3321Sdrh struct yColCache *p; 2800bea119cdSdrh if( iReg<=0 || pParse->nColCache==0 ) return; 2801bea119cdSdrh p = &pParse->aColCache[SQLITE_N_COLCACHE-1]; 2802bea119cdSdrh while(1){ 2803bea119cdSdrh if( p->iReg >= iReg && p->iReg < iReg+nReg ) cacheEntryClear(pParse, p); 2804bea119cdSdrh if( p==pParse->aColCache ) break; 2805bea119cdSdrh p--; 2806ceea3321Sdrh } 2807ceea3321Sdrh } 2808ceea3321Sdrh 2809ceea3321Sdrh /* 2810ceea3321Sdrh ** Remember the current column cache context. Any new entries added 2811ceea3321Sdrh ** added to the column cache after this call are removed when the 2812ceea3321Sdrh ** corresponding pop occurs. 2813ceea3321Sdrh */ 2814ceea3321Sdrh void sqlite3ExprCachePush(Parse *pParse){ 2815ceea3321Sdrh pParse->iCacheLevel++; 28169ac7962aSdrh #ifdef SQLITE_DEBUG 28179ac7962aSdrh if( pParse->db->flags & SQLITE_VdbeAddopTrace ){ 28189ac7962aSdrh printf("PUSH to %d\n", pParse->iCacheLevel); 28199ac7962aSdrh } 28209ac7962aSdrh #endif 2821ceea3321Sdrh } 2822ceea3321Sdrh 2823ceea3321Sdrh /* 2824ceea3321Sdrh ** Remove from the column cache any entries that were added since the 2825d2490904Sdrh ** the previous sqlite3ExprCachePush operation. In other words, restore 2826d2490904Sdrh ** the cache to the state it was in prior the most recent Push. 2827ceea3321Sdrh */ 2828d2490904Sdrh void sqlite3ExprCachePop(Parse *pParse){ 2829ceea3321Sdrh int i; 2830ceea3321Sdrh struct yColCache *p; 2831d2490904Sdrh assert( pParse->iCacheLevel>=1 ); 2832d2490904Sdrh pParse->iCacheLevel--; 28339ac7962aSdrh #ifdef SQLITE_DEBUG 28349ac7962aSdrh if( pParse->db->flags & SQLITE_VdbeAddopTrace ){ 28359ac7962aSdrh printf("POP to %d\n", pParse->iCacheLevel); 28369ac7962aSdrh } 28379ac7962aSdrh #endif 2838ceea3321Sdrh for(i=0, p=pParse->aColCache; i<SQLITE_N_COLCACHE; i++, p++){ 2839ceea3321Sdrh if( p->iReg && p->iLevel>pParse->iCacheLevel ){ 2840ceea3321Sdrh cacheEntryClear(pParse, p); 2841ceea3321Sdrh } 2842ceea3321Sdrh } 2843ceea3321Sdrh } 2844945498f3Sdrh 2845945498f3Sdrh /* 28465cd79239Sdrh ** When a cached column is reused, make sure that its register is 28475cd79239Sdrh ** no longer available as a temp register. ticket #3879: that same 28485cd79239Sdrh ** register might be in the cache in multiple places, so be sure to 28495cd79239Sdrh ** get them all. 28505cd79239Sdrh */ 28515cd79239Sdrh static void sqlite3ExprCachePinRegister(Parse *pParse, int iReg){ 28525cd79239Sdrh int i; 28535cd79239Sdrh struct yColCache *p; 28545cd79239Sdrh for(i=0, p=pParse->aColCache; i<SQLITE_N_COLCACHE; i++, p++){ 28555cd79239Sdrh if( p->iReg==iReg ){ 28565cd79239Sdrh p->tempReg = 0; 28575cd79239Sdrh } 28585cd79239Sdrh } 28595cd79239Sdrh } 28605cd79239Sdrh 28611f9ca2c8Sdrh /* Generate code that will load into register regOut a value that is 28621f9ca2c8Sdrh ** appropriate for the iIdxCol-th column of index pIdx. 28631f9ca2c8Sdrh */ 28641f9ca2c8Sdrh void sqlite3ExprCodeLoadIndexColumn( 28651f9ca2c8Sdrh Parse *pParse, /* The parsing context */ 28661f9ca2c8Sdrh Index *pIdx, /* The index whose column is to be loaded */ 28671f9ca2c8Sdrh int iTabCur, /* Cursor pointing to a table row */ 28681f9ca2c8Sdrh int iIdxCol, /* The column of the index to be loaded */ 28691f9ca2c8Sdrh int regOut /* Store the index column value in this register */ 28701f9ca2c8Sdrh ){ 28711f9ca2c8Sdrh i16 iTabCol = pIdx->aiColumn[iIdxCol]; 28724b92f98cSdrh if( iTabCol==XN_EXPR ){ 28731f9ca2c8Sdrh assert( pIdx->aColExpr ); 28741f9ca2c8Sdrh assert( pIdx->aColExpr->nExpr>iIdxCol ); 28751f9ca2c8Sdrh pParse->iSelfTab = iTabCur; 28761c75c9d7Sdrh sqlite3ExprCodeCopy(pParse, pIdx->aColExpr->a[iIdxCol].pExpr, regOut); 28774b92f98cSdrh }else{ 28784b92f98cSdrh sqlite3ExprCodeGetColumnOfTable(pParse->pVdbe, pIdx->pTable, iTabCur, 28794b92f98cSdrh iTabCol, regOut); 28804b92f98cSdrh } 28811f9ca2c8Sdrh } 28821f9ca2c8Sdrh 28835cd79239Sdrh /* 28845c092e8aSdrh ** Generate code to extract the value of the iCol-th column of a table. 28855c092e8aSdrh */ 28865c092e8aSdrh void sqlite3ExprCodeGetColumnOfTable( 28875c092e8aSdrh Vdbe *v, /* The VDBE under construction */ 28885c092e8aSdrh Table *pTab, /* The table containing the value */ 2889313619f5Sdrh int iTabCur, /* The table cursor. Or the PK cursor for WITHOUT ROWID */ 28905c092e8aSdrh int iCol, /* Index of the column to extract */ 2891313619f5Sdrh int regOut /* Extract the value into this register */ 28925c092e8aSdrh ){ 28935c092e8aSdrh if( iCol<0 || iCol==pTab->iPKey ){ 28945c092e8aSdrh sqlite3VdbeAddOp2(v, OP_Rowid, iTabCur, regOut); 28955c092e8aSdrh }else{ 28965c092e8aSdrh int op = IsVirtual(pTab) ? OP_VColumn : OP_Column; 2897ee0ec8e1Sdrh int x = iCol; 289835db31b2Sdrh if( !HasRowid(pTab) && !IsVirtual(pTab) ){ 2899ee0ec8e1Sdrh x = sqlite3ColumnOfIndex(sqlite3PrimaryKeyIndex(pTab), iCol); 2900ee0ec8e1Sdrh } 2901ee0ec8e1Sdrh sqlite3VdbeAddOp3(v, op, iTabCur, x, regOut); 29025c092e8aSdrh } 29035c092e8aSdrh if( iCol>=0 ){ 29045c092e8aSdrh sqlite3ColumnDefault(v, pTab, iCol, regOut); 29055c092e8aSdrh } 29065c092e8aSdrh } 29075c092e8aSdrh 29085c092e8aSdrh /* 2909945498f3Sdrh ** Generate code that will extract the iColumn-th column from 2910ce78bc6eSdrh ** table pTab and store the column value in a register. 2911ce78bc6eSdrh ** 2912ce78bc6eSdrh ** An effort is made to store the column value in register iReg. This 2913ce78bc6eSdrh ** is not garanteeed for GetColumn() - the result can be stored in 2914ce78bc6eSdrh ** any register. But the result is guaranteed to land in register iReg 2915ce78bc6eSdrh ** for GetColumnToReg(). 2916e55cbd72Sdrh ** 2917e55cbd72Sdrh ** There must be an open cursor to pTab in iTable when this routine 2918e55cbd72Sdrh ** is called. If iColumn<0 then code is generated that extracts the rowid. 2919945498f3Sdrh */ 2920e55cbd72Sdrh int sqlite3ExprCodeGetColumn( 2921e55cbd72Sdrh Parse *pParse, /* Parsing and code generating context */ 29222133d822Sdrh Table *pTab, /* Description of the table we are reading from */ 29232133d822Sdrh int iColumn, /* Index of the table column */ 29242133d822Sdrh int iTable, /* The cursor pointing to the table */ 2925a748fdccSdrh int iReg, /* Store results here */ 2926ce78bc6eSdrh u8 p5 /* P5 value for OP_Column + FLAGS */ 29272133d822Sdrh ){ 2928e55cbd72Sdrh Vdbe *v = pParse->pVdbe; 2929e55cbd72Sdrh int i; 2930da250ea5Sdrh struct yColCache *p; 2931e55cbd72Sdrh 2932ceea3321Sdrh for(i=0, p=pParse->aColCache; i<SQLITE_N_COLCACHE; i++, p++){ 2933b6da74ebSdrh if( p->iReg>0 && p->iTable==iTable && p->iColumn==iColumn ){ 2934ceea3321Sdrh p->lru = pParse->iCacheCnt++; 29355cd79239Sdrh sqlite3ExprCachePinRegister(pParse, p->iReg); 2936da250ea5Sdrh return p->iReg; 2937e55cbd72Sdrh } 2938e55cbd72Sdrh } 2939e55cbd72Sdrh assert( v!=0 ); 29405c092e8aSdrh sqlite3ExprCodeGetColumnOfTable(v, pTab, iTable, iColumn, iReg); 2941a748fdccSdrh if( p5 ){ 2942a748fdccSdrh sqlite3VdbeChangeP5(v, p5); 2943a748fdccSdrh }else{ 2944ceea3321Sdrh sqlite3ExprCacheStore(pParse, iTable, iColumn, iReg); 2945a748fdccSdrh } 2946e55cbd72Sdrh return iReg; 2947e55cbd72Sdrh } 2948ce78bc6eSdrh void sqlite3ExprCodeGetColumnToReg( 2949ce78bc6eSdrh Parse *pParse, /* Parsing and code generating context */ 2950ce78bc6eSdrh Table *pTab, /* Description of the table we are reading from */ 2951ce78bc6eSdrh int iColumn, /* Index of the table column */ 2952ce78bc6eSdrh int iTable, /* The cursor pointing to the table */ 2953ce78bc6eSdrh int iReg /* Store results here */ 2954ce78bc6eSdrh ){ 2955ce78bc6eSdrh int r1 = sqlite3ExprCodeGetColumn(pParse, pTab, iColumn, iTable, iReg, 0); 2956ce78bc6eSdrh if( r1!=iReg ) sqlite3VdbeAddOp2(pParse->pVdbe, OP_SCopy, r1, iReg); 2957ce78bc6eSdrh } 2958ce78bc6eSdrh 2959e55cbd72Sdrh 2960e55cbd72Sdrh /* 2961ceea3321Sdrh ** Clear all column cache entries. 2962e55cbd72Sdrh */ 2963ceea3321Sdrh void sqlite3ExprCacheClear(Parse *pParse){ 2964e55cbd72Sdrh int i; 2965ceea3321Sdrh struct yColCache *p; 2966ceea3321Sdrh 29679ac7962aSdrh #if SQLITE_DEBUG 29689ac7962aSdrh if( pParse->db->flags & SQLITE_VdbeAddopTrace ){ 29699ac7962aSdrh printf("CLEAR\n"); 29709ac7962aSdrh } 29719ac7962aSdrh #endif 2972ceea3321Sdrh for(i=0, p=pParse->aColCache; i<SQLITE_N_COLCACHE; i++, p++){ 2973ceea3321Sdrh if( p->iReg ){ 2974ceea3321Sdrh cacheEntryClear(pParse, p); 2975e55cbd72Sdrh } 2976da250ea5Sdrh } 2977da250ea5Sdrh } 2978e55cbd72Sdrh 2979e55cbd72Sdrh /* 2980da250ea5Sdrh ** Record the fact that an affinity change has occurred on iCount 2981da250ea5Sdrh ** registers starting with iStart. 2982e55cbd72Sdrh */ 2983da250ea5Sdrh void sqlite3ExprCacheAffinityChange(Parse *pParse, int iStart, int iCount){ 2984f49f3523Sdrh sqlite3ExprCacheRemove(pParse, iStart, iCount); 2985e55cbd72Sdrh } 2986e55cbd72Sdrh 2987e55cbd72Sdrh /* 2988b21e7c70Sdrh ** Generate code to move content from registers iFrom...iFrom+nReg-1 2989b21e7c70Sdrh ** over to iTo..iTo+nReg-1. Keep the column cache up-to-date. 2990e55cbd72Sdrh */ 2991b21e7c70Sdrh void sqlite3ExprCodeMove(Parse *pParse, int iFrom, int iTo, int nReg){ 2992e8e4af76Sdrh assert( iFrom>=iTo+nReg || iFrom+nReg<=iTo ); 2993079a3072Sdrh sqlite3VdbeAddOp3(pParse->pVdbe, OP_Move, iFrom, iTo, nReg); 2994236241aeSdrh sqlite3ExprCacheRemove(pParse, iFrom, nReg); 2995945498f3Sdrh } 2996945498f3Sdrh 2997f49f3523Sdrh #if defined(SQLITE_DEBUG) || defined(SQLITE_COVERAGE_TEST) 299892b01d53Sdrh /* 2999652fbf55Sdrh ** Return true if any register in the range iFrom..iTo (inclusive) 3000652fbf55Sdrh ** is used as part of the column cache. 3001f49f3523Sdrh ** 3002f49f3523Sdrh ** This routine is used within assert() and testcase() macros only 3003f49f3523Sdrh ** and does not appear in a normal build. 3004652fbf55Sdrh */ 3005652fbf55Sdrh static int usedAsColumnCache(Parse *pParse, int iFrom, int iTo){ 3006652fbf55Sdrh int i; 3007ceea3321Sdrh struct yColCache *p; 3008ceea3321Sdrh for(i=0, p=pParse->aColCache; i<SQLITE_N_COLCACHE; i++, p++){ 3009ceea3321Sdrh int r = p->iReg; 3010f49f3523Sdrh if( r>=iFrom && r<=iTo ) return 1; /*NO_TEST*/ 3011652fbf55Sdrh } 3012652fbf55Sdrh return 0; 3013652fbf55Sdrh } 3014f49f3523Sdrh #endif /* SQLITE_DEBUG || SQLITE_COVERAGE_TEST */ 3015652fbf55Sdrh 3016bea119cdSdrh 3017652fbf55Sdrh /* 3018a4c3c87eSdrh ** Convert an expression node to a TK_REGISTER 3019a4c3c87eSdrh */ 3020a4c3c87eSdrh static void exprToRegister(Expr *p, int iReg){ 3021a4c3c87eSdrh p->op2 = p->op; 3022a4c3c87eSdrh p->op = TK_REGISTER; 3023a4c3c87eSdrh p->iTable = iReg; 3024a4c3c87eSdrh ExprClearProperty(p, EP_Skip); 3025a4c3c87eSdrh } 3026a4c3c87eSdrh 302771c57db0Sdan static void exprCodeBetween(Parse*,Expr*,int,void(*)(Parse*,Expr*,int,int),int); 302871c57db0Sdan 3029a4c3c87eSdrh /* 3030cce7d176Sdrh ** Generate code into the current Vdbe to evaluate the given 30312dcef11bSdrh ** expression. Attempt to store the results in register "target". 30322dcef11bSdrh ** Return the register where results are stored. 3033389a1adbSdrh ** 30348b213899Sdrh ** With this routine, there is no guarantee that results will 30352dcef11bSdrh ** be stored in target. The result might be stored in some other 30362dcef11bSdrh ** register if it is convenient to do so. The calling function 30372dcef11bSdrh ** must check the return code and move the results to the desired 30382dcef11bSdrh ** register. 3039cce7d176Sdrh */ 3040678ccce8Sdrh int sqlite3ExprCodeTarget(Parse *pParse, Expr *pExpr, int target){ 30412dcef11bSdrh Vdbe *v = pParse->pVdbe; /* The VM under construction */ 30422dcef11bSdrh int op; /* The opcode being coded */ 30432dcef11bSdrh int inReg = target; /* Results stored in register inReg */ 30442dcef11bSdrh int regFree1 = 0; /* If non-zero free this temporary register */ 30452dcef11bSdrh int regFree2 = 0; /* If non-zero free this temporary register */ 30467b35a77bSdan int r1, r2; /* Various register numbers */ 304720411ea7Sdrh sqlite3 *db = pParse->db; /* The database connection */ 304810d1edf0Sdrh Expr tempX; /* Temporary expression node */ 304971c57db0Sdan int p5 = 0; 3050ffe07b2dSdrh 30519cbf3425Sdrh assert( target>0 && target<=pParse->nMem ); 305220411ea7Sdrh if( v==0 ){ 305320411ea7Sdrh assert( pParse->db->mallocFailed ); 305420411ea7Sdrh return 0; 305520411ea7Sdrh } 3056389a1adbSdrh 3057389a1adbSdrh if( pExpr==0 ){ 3058389a1adbSdrh op = TK_NULL; 3059389a1adbSdrh }else{ 3060f2bc013cSdrh op = pExpr->op; 3061389a1adbSdrh } 3062f2bc013cSdrh switch( op ){ 306313449892Sdrh case TK_AGG_COLUMN: { 306413449892Sdrh AggInfo *pAggInfo = pExpr->pAggInfo; 306513449892Sdrh struct AggInfo_col *pCol = &pAggInfo->aCol[pExpr->iAgg]; 306613449892Sdrh if( !pAggInfo->directMode ){ 30679de221dfSdrh assert( pCol->iMem>0 ); 30689de221dfSdrh inReg = pCol->iMem; 306913449892Sdrh break; 307013449892Sdrh }else if( pAggInfo->useSortingIdx ){ 30715134d135Sdan sqlite3VdbeAddOp3(v, OP_Column, pAggInfo->sortingIdxPTab, 3072389a1adbSdrh pCol->iSorterColumn, target); 307313449892Sdrh break; 307413449892Sdrh } 307513449892Sdrh /* Otherwise, fall thru into the TK_COLUMN case */ 307613449892Sdrh } 3077967e8b73Sdrh case TK_COLUMN: { 3078b2b9d3d7Sdrh int iTab = pExpr->iTable; 3079b2b9d3d7Sdrh if( iTab<0 ){ 3080b2b9d3d7Sdrh if( pParse->ckBase>0 ){ 3081b2b9d3d7Sdrh /* Generating CHECK constraints or inserting into partial index */ 3082aa9b8963Sdrh inReg = pExpr->iColumn + pParse->ckBase; 3083b2b9d3d7Sdrh break; 3084c4a3c779Sdrh }else{ 30851f9ca2c8Sdrh /* Coding an expression that is part of an index where column names 30861f9ca2c8Sdrh ** in the index refer to the table to which the index belongs */ 30871f9ca2c8Sdrh iTab = pParse->iSelfTab; 30882282792aSdrh } 3089b2b9d3d7Sdrh } 3090b2b9d3d7Sdrh inReg = sqlite3ExprCodeGetColumn(pParse, pExpr->pTab, 3091b2b9d3d7Sdrh pExpr->iColumn, iTab, target, 3092b2b9d3d7Sdrh pExpr->op2); 3093cce7d176Sdrh break; 3094cce7d176Sdrh } 3095cce7d176Sdrh case TK_INTEGER: { 309613573c71Sdrh codeInteger(pParse, pExpr, 0, target); 3097fec19aadSdrh break; 309851e9a445Sdrh } 309913573c71Sdrh #ifndef SQLITE_OMIT_FLOATING_POINT 3100598f1340Sdrh case TK_FLOAT: { 310133e619fcSdrh assert( !ExprHasProperty(pExpr, EP_IntValue) ); 310233e619fcSdrh codeReal(v, pExpr->u.zToken, 0, target); 3103598f1340Sdrh break; 3104598f1340Sdrh } 310513573c71Sdrh #endif 3106fec19aadSdrh case TK_STRING: { 310733e619fcSdrh assert( !ExprHasProperty(pExpr, EP_IntValue) ); 3108076e85f5Sdrh sqlite3VdbeLoadString(v, target, pExpr->u.zToken); 3109cce7d176Sdrh break; 3110cce7d176Sdrh } 3111f0863fe5Sdrh case TK_NULL: { 31129de221dfSdrh sqlite3VdbeAddOp2(v, OP_Null, 0, target); 3113f0863fe5Sdrh break; 3114f0863fe5Sdrh } 31155338a5f7Sdanielk1977 #ifndef SQLITE_OMIT_BLOB_LITERAL 3116c572ef7fSdanielk1977 case TK_BLOB: { 31176c8c6cecSdrh int n; 31186c8c6cecSdrh const char *z; 3119ca48c90fSdrh char *zBlob; 312033e619fcSdrh assert( !ExprHasProperty(pExpr, EP_IntValue) ); 312133e619fcSdrh assert( pExpr->u.zToken[0]=='x' || pExpr->u.zToken[0]=='X' ); 312233e619fcSdrh assert( pExpr->u.zToken[1]=='\'' ); 312333e619fcSdrh z = &pExpr->u.zToken[2]; 3124b7916a78Sdrh n = sqlite3Strlen30(z) - 1; 3125b7916a78Sdrh assert( z[n]=='\'' ); 3126ca48c90fSdrh zBlob = sqlite3HexToBlob(sqlite3VdbeDb(v), z, n); 3127ca48c90fSdrh sqlite3VdbeAddOp4(v, OP_Blob, n/2, target, 0, zBlob, P4_DYNAMIC); 3128c572ef7fSdanielk1977 break; 3129c572ef7fSdanielk1977 } 31305338a5f7Sdanielk1977 #endif 313150457896Sdrh case TK_VARIABLE: { 313233e619fcSdrh assert( !ExprHasProperty(pExpr, EP_IntValue) ); 313333e619fcSdrh assert( pExpr->u.zToken!=0 ); 313433e619fcSdrh assert( pExpr->u.zToken[0]!=0 ); 3135eaf52d88Sdrh sqlite3VdbeAddOp2(v, OP_Variable, pExpr->iColumn, target); 313633e619fcSdrh if( pExpr->u.zToken[1]!=0 ){ 313704e9eeadSdrh assert( pExpr->u.zToken[0]=='?' 313804e9eeadSdrh || strcmp(pExpr->u.zToken, pParse->azVar[pExpr->iColumn-1])==0 ); 313904e9eeadSdrh sqlite3VdbeChangeP4(v, -1, pParse->azVar[pExpr->iColumn-1], P4_STATIC); 3140895d7472Sdrh } 314150457896Sdrh break; 314250457896Sdrh } 31434e0cff60Sdrh case TK_REGISTER: { 31449de221dfSdrh inReg = pExpr->iTable; 31454e0cff60Sdrh break; 31464e0cff60Sdrh } 3147487e262fSdrh #ifndef SQLITE_OMIT_CAST 3148487e262fSdrh case TK_CAST: { 3149487e262fSdrh /* Expressions of the form: CAST(pLeft AS token) */ 31502dcef11bSdrh inReg = sqlite3ExprCodeTarget(pParse, pExpr->pLeft, target); 31511735fa88Sdrh if( inReg!=target ){ 31521735fa88Sdrh sqlite3VdbeAddOp2(v, OP_SCopy, inReg, target); 31531735fa88Sdrh inReg = target; 31541735fa88Sdrh } 31554169e430Sdrh sqlite3VdbeAddOp2(v, OP_Cast, target, 31564169e430Sdrh sqlite3AffinityType(pExpr->u.zToken, 0)); 3157c5499befSdrh testcase( usedAsColumnCache(pParse, inReg, inReg) ); 3158b3843a82Sdrh sqlite3ExprCacheAffinityChange(pParse, inReg, 1); 3159487e262fSdrh break; 3160487e262fSdrh } 3161487e262fSdrh #endif /* SQLITE_OMIT_CAST */ 316271c57db0Sdan case TK_IS: 316371c57db0Sdan case TK_ISNOT: 316471c57db0Sdan op = (op==TK_IS) ? TK_EQ : TK_NE; 316571c57db0Sdan p5 = SQLITE_NULLEQ; 316671c57db0Sdan /* fall-through */ 3167c9b84a1fSdrh case TK_LT: 3168c9b84a1fSdrh case TK_LE: 3169c9b84a1fSdrh case TK_GT: 3170c9b84a1fSdrh case TK_GE: 3171c9b84a1fSdrh case TK_NE: 3172c9b84a1fSdrh case TK_EQ: { 317371c57db0Sdan Expr *pLeft = pExpr->pLeft; 317471c57db0Sdan if( (pLeft->flags & EP_Vector) ){ 317571c57db0Sdan codeVectorCompare(pParse, pExpr, target); 317671c57db0Sdan }else{ 317771c57db0Sdan r1 = sqlite3ExprCodeTemp(pParse, pLeft, ®Free1); 3178b6da74ebSdrh r2 = sqlite3ExprCodeTemp(pParse, pExpr->pRight, ®Free2); 317971c57db0Sdan codeCompare(pParse, pLeft, pExpr->pRight, op, 318071c57db0Sdan r1, r2, inReg, SQLITE_STOREP2 | p5); 31817d176105Sdrh assert(TK_LT==OP_Lt); testcase(op==OP_Lt); VdbeCoverageIf(v,op==OP_Lt); 31827d176105Sdrh assert(TK_LE==OP_Le); testcase(op==OP_Le); VdbeCoverageIf(v,op==OP_Le); 31837d176105Sdrh assert(TK_GT==OP_Gt); testcase(op==OP_Gt); VdbeCoverageIf(v,op==OP_Gt); 31847d176105Sdrh assert(TK_GE==OP_Ge); testcase(op==OP_Ge); VdbeCoverageIf(v,op==OP_Ge); 31857d176105Sdrh assert(TK_EQ==OP_Eq); testcase(op==OP_Eq); VdbeCoverageIf(v,op==OP_Eq); 31867d176105Sdrh assert(TK_NE==OP_Ne); testcase(op==OP_Ne); VdbeCoverageIf(v,op==OP_Ne); 3187c5499befSdrh testcase( regFree1==0 ); 3188c5499befSdrh testcase( regFree2==0 ); 3189c9b84a1fSdrh } 31906a2fe093Sdrh break; 31916a2fe093Sdrh } 3192cce7d176Sdrh case TK_AND: 3193cce7d176Sdrh case TK_OR: 3194cce7d176Sdrh case TK_PLUS: 3195cce7d176Sdrh case TK_STAR: 3196cce7d176Sdrh case TK_MINUS: 3197bf4133cbSdrh case TK_REM: 3198bf4133cbSdrh case TK_BITAND: 3199bf4133cbSdrh case TK_BITOR: 320017c40294Sdrh case TK_SLASH: 3201bf4133cbSdrh case TK_LSHIFT: 3202855eb1cfSdrh case TK_RSHIFT: 32030040077dSdrh case TK_CONCAT: { 32047d176105Sdrh assert( TK_AND==OP_And ); testcase( op==TK_AND ); 32057d176105Sdrh assert( TK_OR==OP_Or ); testcase( op==TK_OR ); 32067d176105Sdrh assert( TK_PLUS==OP_Add ); testcase( op==TK_PLUS ); 32077d176105Sdrh assert( TK_MINUS==OP_Subtract ); testcase( op==TK_MINUS ); 32087d176105Sdrh assert( TK_REM==OP_Remainder ); testcase( op==TK_REM ); 32097d176105Sdrh assert( TK_BITAND==OP_BitAnd ); testcase( op==TK_BITAND ); 32107d176105Sdrh assert( TK_BITOR==OP_BitOr ); testcase( op==TK_BITOR ); 32117d176105Sdrh assert( TK_SLASH==OP_Divide ); testcase( op==TK_SLASH ); 32127d176105Sdrh assert( TK_LSHIFT==OP_ShiftLeft ); testcase( op==TK_LSHIFT ); 32137d176105Sdrh assert( TK_RSHIFT==OP_ShiftRight ); testcase( op==TK_RSHIFT ); 32147d176105Sdrh assert( TK_CONCAT==OP_Concat ); testcase( op==TK_CONCAT ); 32152dcef11bSdrh r1 = sqlite3ExprCodeTemp(pParse, pExpr->pLeft, ®Free1); 32162dcef11bSdrh r2 = sqlite3ExprCodeTemp(pParse, pExpr->pRight, ®Free2); 32175b6afba9Sdrh sqlite3VdbeAddOp3(v, op, r2, r1, target); 3218c5499befSdrh testcase( regFree1==0 ); 3219c5499befSdrh testcase( regFree2==0 ); 32200040077dSdrh break; 32210040077dSdrh } 3222cce7d176Sdrh case TK_UMINUS: { 3223fec19aadSdrh Expr *pLeft = pExpr->pLeft; 3224fec19aadSdrh assert( pLeft ); 322513573c71Sdrh if( pLeft->op==TK_INTEGER ){ 322613573c71Sdrh codeInteger(pParse, pLeft, 1, target); 322713573c71Sdrh #ifndef SQLITE_OMIT_FLOATING_POINT 322813573c71Sdrh }else if( pLeft->op==TK_FLOAT ){ 322933e619fcSdrh assert( !ExprHasProperty(pExpr, EP_IntValue) ); 323033e619fcSdrh codeReal(v, pLeft->u.zToken, 1, target); 323113573c71Sdrh #endif 32323c84ddffSdrh }else{ 323310d1edf0Sdrh tempX.op = TK_INTEGER; 323410d1edf0Sdrh tempX.flags = EP_IntValue|EP_TokenOnly; 323510d1edf0Sdrh tempX.u.iValue = 0; 323610d1edf0Sdrh r1 = sqlite3ExprCodeTemp(pParse, &tempX, ®Free1); 3237e55cbd72Sdrh r2 = sqlite3ExprCodeTemp(pParse, pExpr->pLeft, ®Free2); 32382dcef11bSdrh sqlite3VdbeAddOp3(v, OP_Subtract, r2, r1, target); 3239c5499befSdrh testcase( regFree2==0 ); 32403c84ddffSdrh } 32419de221dfSdrh inReg = target; 32426e142f54Sdrh break; 32436e142f54Sdrh } 3244bf4133cbSdrh case TK_BITNOT: 32456e142f54Sdrh case TK_NOT: { 32467d176105Sdrh assert( TK_BITNOT==OP_BitNot ); testcase( op==TK_BITNOT ); 32477d176105Sdrh assert( TK_NOT==OP_Not ); testcase( op==TK_NOT ); 3248e99fa2afSdrh r1 = sqlite3ExprCodeTemp(pParse, pExpr->pLeft, ®Free1); 3249e99fa2afSdrh testcase( regFree1==0 ); 3250e99fa2afSdrh inReg = target; 3251e99fa2afSdrh sqlite3VdbeAddOp2(v, op, r1, inReg); 3252cce7d176Sdrh break; 3253cce7d176Sdrh } 3254cce7d176Sdrh case TK_ISNULL: 3255cce7d176Sdrh case TK_NOTNULL: { 32566a288a33Sdrh int addr; 32577d176105Sdrh assert( TK_ISNULL==OP_IsNull ); testcase( op==TK_ISNULL ); 32587d176105Sdrh assert( TK_NOTNULL==OP_NotNull ); testcase( op==TK_NOTNULL ); 32599de221dfSdrh sqlite3VdbeAddOp2(v, OP_Integer, 1, target); 32602dcef11bSdrh r1 = sqlite3ExprCodeTemp(pParse, pExpr->pLeft, ®Free1); 3261c5499befSdrh testcase( regFree1==0 ); 32622dcef11bSdrh addr = sqlite3VdbeAddOp1(v, op, r1); 32637d176105Sdrh VdbeCoverageIf(v, op==TK_ISNULL); 32647d176105Sdrh VdbeCoverageIf(v, op==TK_NOTNULL); 3265a976979bSdrh sqlite3VdbeAddOp2(v, OP_Integer, 0, target); 32666a288a33Sdrh sqlite3VdbeJumpHere(v, addr); 3267a37cdde0Sdanielk1977 break; 3268f2bc013cSdrh } 32692282792aSdrh case TK_AGG_FUNCTION: { 327013449892Sdrh AggInfo *pInfo = pExpr->pAggInfo; 32717e56e711Sdrh if( pInfo==0 ){ 327233e619fcSdrh assert( !ExprHasProperty(pExpr, EP_IntValue) ); 327333e619fcSdrh sqlite3ErrorMsg(pParse, "misuse of aggregate: %s()", pExpr->u.zToken); 32747e56e711Sdrh }else{ 32759de221dfSdrh inReg = pInfo->aFunc[pExpr->iAgg].iMem; 32767e56e711Sdrh } 32772282792aSdrh break; 32782282792aSdrh } 3279cce7d176Sdrh case TK_FUNCTION: { 328012ffee8cSdrh ExprList *pFarg; /* List of function arguments */ 328112ffee8cSdrh int nFarg; /* Number of function arguments */ 328212ffee8cSdrh FuncDef *pDef; /* The function definition object */ 328312ffee8cSdrh const char *zId; /* The function name */ 3284693e6719Sdrh u32 constMask = 0; /* Mask of function arguments that are constant */ 328512ffee8cSdrh int i; /* Loop counter */ 328612ffee8cSdrh u8 enc = ENC(db); /* The text encoding used by this database */ 328712ffee8cSdrh CollSeq *pColl = 0; /* A collating sequence */ 328817435752Sdrh 32896ab3a2ecSdanielk1977 assert( !ExprHasProperty(pExpr, EP_xIsSelect) ); 3290c5cd1249Sdrh if( ExprHasProperty(pExpr, EP_TokenOnly) ){ 329112ffee8cSdrh pFarg = 0; 329212ffee8cSdrh }else{ 329312ffee8cSdrh pFarg = pExpr->x.pList; 329412ffee8cSdrh } 329512ffee8cSdrh nFarg = pFarg ? pFarg->nExpr : 0; 329633e619fcSdrh assert( !ExprHasProperty(pExpr, EP_IntValue) ); 329733e619fcSdrh zId = pExpr->u.zToken; 329880738d9cSdrh pDef = sqlite3FindFunction(db, zId, nFarg, enc, 0); 32992d80151fSdrh if( pDef==0 || pDef->xFinalize!=0 ){ 330080738d9cSdrh sqlite3ErrorMsg(pParse, "unknown function: %s()", zId); 3301feb306f5Sdrh break; 3302feb306f5Sdrh } 3303ae6bb957Sdrh 3304ae6bb957Sdrh /* Attempt a direct implementation of the built-in COALESCE() and 330560ec914cSpeter.d.reid ** IFNULL() functions. This avoids unnecessary evaluation of 3306ae6bb957Sdrh ** arguments past the first non-NULL argument. 3307ae6bb957Sdrh */ 3308d36e1041Sdrh if( pDef->funcFlags & SQLITE_FUNC_COALESCE ){ 3309ae6bb957Sdrh int endCoalesce = sqlite3VdbeMakeLabel(v); 3310ae6bb957Sdrh assert( nFarg>=2 ); 3311ae6bb957Sdrh sqlite3ExprCode(pParse, pFarg->a[0].pExpr, target); 3312ae6bb957Sdrh for(i=1; i<nFarg; i++){ 3313ae6bb957Sdrh sqlite3VdbeAddOp2(v, OP_NotNull, target, endCoalesce); 3314688852abSdrh VdbeCoverage(v); 3315f49f3523Sdrh sqlite3ExprCacheRemove(pParse, target, 1); 3316ae6bb957Sdrh sqlite3ExprCachePush(pParse); 3317ae6bb957Sdrh sqlite3ExprCode(pParse, pFarg->a[i].pExpr, target); 3318d2490904Sdrh sqlite3ExprCachePop(pParse); 3319ae6bb957Sdrh } 3320ae6bb957Sdrh sqlite3VdbeResolveLabel(v, endCoalesce); 3321ae6bb957Sdrh break; 3322ae6bb957Sdrh } 3323ae6bb957Sdrh 3324cca9f3d2Sdrh /* The UNLIKELY() function is a no-op. The result is the value 3325cca9f3d2Sdrh ** of the first argument. 3326cca9f3d2Sdrh */ 3327cca9f3d2Sdrh if( pDef->funcFlags & SQLITE_FUNC_UNLIKELY ){ 3328cca9f3d2Sdrh assert( nFarg>=1 ); 33295f02ab09Sdrh inReg = sqlite3ExprCodeTarget(pParse, pFarg->a[0].pExpr, target); 3330cca9f3d2Sdrh break; 3331cca9f3d2Sdrh } 3332ae6bb957Sdrh 3333d1a01edaSdrh for(i=0; i<nFarg; i++){ 3334d1a01edaSdrh if( i<32 && sqlite3ExprIsConstant(pFarg->a[i].pExpr) ){ 3335693e6719Sdrh testcase( i==31 ); 3336693e6719Sdrh constMask |= MASKBIT32(i); 3337d1a01edaSdrh } 3338d1a01edaSdrh if( (pDef->funcFlags & SQLITE_FUNC_NEEDCOLL)!=0 && !pColl ){ 3339d1a01edaSdrh pColl = sqlite3ExprCollSeq(pParse, pFarg->a[i].pExpr); 3340d1a01edaSdrh } 3341d1a01edaSdrh } 334212ffee8cSdrh if( pFarg ){ 3343d1a01edaSdrh if( constMask ){ 3344d1a01edaSdrh r1 = pParse->nMem+1; 3345d1a01edaSdrh pParse->nMem += nFarg; 3346d1a01edaSdrh }else{ 334712ffee8cSdrh r1 = sqlite3GetTempRange(pParse, nFarg); 3348d1a01edaSdrh } 3349a748fdccSdrh 3350a748fdccSdrh /* For length() and typeof() functions with a column argument, 3351a748fdccSdrh ** set the P5 parameter to the OP_Column opcode to OPFLAG_LENGTHARG 3352a748fdccSdrh ** or OPFLAG_TYPEOFARG respectively, to avoid unnecessary data 3353a748fdccSdrh ** loading. 3354a748fdccSdrh */ 3355d36e1041Sdrh if( (pDef->funcFlags & (SQLITE_FUNC_LENGTH|SQLITE_FUNC_TYPEOF))!=0 ){ 33564e245a4cSdrh u8 exprOp; 3357a748fdccSdrh assert( nFarg==1 ); 3358a748fdccSdrh assert( pFarg->a[0].pExpr!=0 ); 33594e245a4cSdrh exprOp = pFarg->a[0].pExpr->op; 33604e245a4cSdrh if( exprOp==TK_COLUMN || exprOp==TK_AGG_COLUMN ){ 3361a748fdccSdrh assert( SQLITE_FUNC_LENGTH==OPFLAG_LENGTHARG ); 3362a748fdccSdrh assert( SQLITE_FUNC_TYPEOF==OPFLAG_TYPEOFARG ); 3363b1fba286Sdrh testcase( pDef->funcFlags & OPFLAG_LENGTHARG ); 3364b1fba286Sdrh pFarg->a[0].pExpr->op2 = 3365b1fba286Sdrh pDef->funcFlags & (OPFLAG_LENGTHARG|OPFLAG_TYPEOFARG); 3366a748fdccSdrh } 3367a748fdccSdrh } 3368a748fdccSdrh 3369d7d385ddSdrh sqlite3ExprCachePush(pParse); /* Ticket 2ea2425d34be */ 33705579d59fSdrh sqlite3ExprCodeExprList(pParse, pFarg, r1, 0, 3371d1a01edaSdrh SQLITE_ECEL_DUP|SQLITE_ECEL_FACTOR); 3372d2490904Sdrh sqlite3ExprCachePop(pParse); /* Ticket 2ea2425d34be */ 3373892d3179Sdrh }else{ 337412ffee8cSdrh r1 = 0; 3375892d3179Sdrh } 3376b7f6f68fSdrh #ifndef SQLITE_OMIT_VIRTUALTABLE 3377a43fa227Sdrh /* Possibly overload the function if the first argument is 3378a43fa227Sdrh ** a virtual table column. 3379a43fa227Sdrh ** 3380a43fa227Sdrh ** For infix functions (LIKE, GLOB, REGEXP, and MATCH) use the 3381a43fa227Sdrh ** second argument, not the first, as the argument to test to 3382a43fa227Sdrh ** see if it is a column in a virtual table. This is done because 3383a43fa227Sdrh ** the left operand of infix functions (the operand we want to 3384a43fa227Sdrh ** control overloading) ends up as the second argument to the 3385a43fa227Sdrh ** function. The expression "A glob B" is equivalent to 3386a43fa227Sdrh ** "glob(B,A). We want to use the A in "A glob B" to test 3387a43fa227Sdrh ** for function overloading. But we use the B term in "glob(B,A)". 3388a43fa227Sdrh */ 338912ffee8cSdrh if( nFarg>=2 && (pExpr->flags & EP_InfixFunc) ){ 339012ffee8cSdrh pDef = sqlite3VtabOverloadFunction(db, pDef, nFarg, pFarg->a[1].pExpr); 339112ffee8cSdrh }else if( nFarg>0 ){ 339212ffee8cSdrh pDef = sqlite3VtabOverloadFunction(db, pDef, nFarg, pFarg->a[0].pExpr); 3393b7f6f68fSdrh } 3394b7f6f68fSdrh #endif 3395d36e1041Sdrh if( pDef->funcFlags & SQLITE_FUNC_NEEDCOLL ){ 33968b213899Sdrh if( !pColl ) pColl = db->pDfltColl; 339766a5167bSdrh sqlite3VdbeAddOp4(v, OP_CollSeq, 0, 0, 0, (char *)pColl, P4_COLLSEQ); 3398682f68b0Sdanielk1977 } 33999c7c913cSdrh sqlite3VdbeAddOp4(v, OP_Function0, constMask, r1, target, 340066a5167bSdrh (char*)pDef, P4_FUNCDEF); 340112ffee8cSdrh sqlite3VdbeChangeP5(v, (u8)nFarg); 3402d1a01edaSdrh if( nFarg && constMask==0 ){ 340312ffee8cSdrh sqlite3ReleaseTempRange(pParse, r1, nFarg); 34042dcef11bSdrh } 34056ec2733bSdrh break; 34066ec2733bSdrh } 3407fe2093d7Sdrh #ifndef SQLITE_OMIT_SUBQUERY 3408fe2093d7Sdrh case TK_EXISTS: 340919a775c2Sdrh case TK_SELECT: { 34108da209b1Sdan int nCol; 3411c5499befSdrh testcase( op==TK_EXISTS ); 3412c5499befSdrh testcase( op==TK_SELECT ); 34138da209b1Sdan if( op==TK_SELECT && (nCol = pExpr->x.pSelect->pEList->nExpr)!=1 ){ 34148da209b1Sdan sqlite3SubselectError(pParse, nCol, 1); 34158da209b1Sdan }else{ 34161450bc6eSdrh inReg = sqlite3CodeSubselect(pParse, pExpr, 0, 0); 34178da209b1Sdan } 341819a775c2Sdrh break; 341919a775c2Sdrh } 3420fef5208cSdrh case TK_IN: { 3421e3365e6cSdrh int destIfFalse = sqlite3VdbeMakeLabel(v); 3422e3365e6cSdrh int destIfNull = sqlite3VdbeMakeLabel(v); 3423e3365e6cSdrh sqlite3VdbeAddOp2(v, OP_Null, 0, target); 3424e3365e6cSdrh sqlite3ExprCodeIN(pParse, pExpr, destIfFalse, destIfNull); 342566ba23ceSdrh sqlite3VdbeAddOp2(v, OP_Integer, 1, target); 3426e3365e6cSdrh sqlite3VdbeResolveLabel(v, destIfFalse); 3427e3365e6cSdrh sqlite3VdbeAddOp2(v, OP_AddImm, target, 0); 3428e3365e6cSdrh sqlite3VdbeResolveLabel(v, destIfNull); 3429fef5208cSdrh break; 3430fef5208cSdrh } 3431e3365e6cSdrh #endif /* SQLITE_OMIT_SUBQUERY */ 3432e3365e6cSdrh 3433e3365e6cSdrh 34342dcef11bSdrh /* 34352dcef11bSdrh ** x BETWEEN y AND z 34362dcef11bSdrh ** 34372dcef11bSdrh ** This is equivalent to 34382dcef11bSdrh ** 34392dcef11bSdrh ** x>=y AND x<=z 34402dcef11bSdrh ** 34412dcef11bSdrh ** X is stored in pExpr->pLeft. 34422dcef11bSdrh ** Y is stored in pExpr->pList->a[0].pExpr. 34432dcef11bSdrh ** Z is stored in pExpr->pList->a[1].pExpr. 34442dcef11bSdrh */ 3445fef5208cSdrh case TK_BETWEEN: { 344671c57db0Sdan exprCodeBetween(pParse, pExpr, target, 0, 0); 3447fef5208cSdrh break; 3448fef5208cSdrh } 344994fa9c41Sdrh case TK_SPAN: 3450ae80ddeaSdrh case TK_COLLATE: 34514f07e5fbSdrh case TK_UPLUS: { 34522dcef11bSdrh inReg = sqlite3ExprCodeTarget(pParse, pExpr->pLeft, target); 3453a2e00042Sdrh break; 3454a2e00042Sdrh } 34552dcef11bSdrh 3456165921a7Sdan case TK_TRIGGER: { 345765a7cd16Sdan /* If the opcode is TK_TRIGGER, then the expression is a reference 345865a7cd16Sdan ** to a column in the new.* or old.* pseudo-tables available to 345965a7cd16Sdan ** trigger programs. In this case Expr.iTable is set to 1 for the 346065a7cd16Sdan ** new.* pseudo-table, or 0 for the old.* pseudo-table. Expr.iColumn 346165a7cd16Sdan ** is set to the column of the pseudo-table to read, or to -1 to 346265a7cd16Sdan ** read the rowid field. 346365a7cd16Sdan ** 346465a7cd16Sdan ** The expression is implemented using an OP_Param opcode. The p1 346565a7cd16Sdan ** parameter is set to 0 for an old.rowid reference, or to (i+1) 346665a7cd16Sdan ** to reference another column of the old.* pseudo-table, where 346765a7cd16Sdan ** i is the index of the column. For a new.rowid reference, p1 is 346865a7cd16Sdan ** set to (n+1), where n is the number of columns in each pseudo-table. 346965a7cd16Sdan ** For a reference to any other column in the new.* pseudo-table, p1 347065a7cd16Sdan ** is set to (n+2+i), where n and i are as defined previously. For 347165a7cd16Sdan ** example, if the table on which triggers are being fired is 347265a7cd16Sdan ** declared as: 347365a7cd16Sdan ** 347465a7cd16Sdan ** CREATE TABLE t1(a, b); 347565a7cd16Sdan ** 347665a7cd16Sdan ** Then p1 is interpreted as follows: 347765a7cd16Sdan ** 347865a7cd16Sdan ** p1==0 -> old.rowid p1==3 -> new.rowid 347965a7cd16Sdan ** p1==1 -> old.a p1==4 -> new.a 348065a7cd16Sdan ** p1==2 -> old.b p1==5 -> new.b 348165a7cd16Sdan */ 34822832ad42Sdan Table *pTab = pExpr->pTab; 348365a7cd16Sdan int p1 = pExpr->iTable * (pTab->nCol+1) + 1 + pExpr->iColumn; 348465a7cd16Sdan 348565a7cd16Sdan assert( pExpr->iTable==0 || pExpr->iTable==1 ); 348665a7cd16Sdan assert( pExpr->iColumn>=-1 && pExpr->iColumn<pTab->nCol ); 348765a7cd16Sdan assert( pTab->iPKey<0 || pExpr->iColumn!=pTab->iPKey ); 348865a7cd16Sdan assert( p1>=0 && p1<(pTab->nCol*2+2) ); 348965a7cd16Sdan 349065a7cd16Sdan sqlite3VdbeAddOp2(v, OP_Param, p1, target); 349176d462eeSdan VdbeComment((v, "%s.%s -> $%d", 3492165921a7Sdan (pExpr->iTable ? "new" : "old"), 349376d462eeSdan (pExpr->iColumn<0 ? "rowid" : pExpr->pTab->aCol[pExpr->iColumn].zName), 349476d462eeSdan target 3495165921a7Sdan )); 349665a7cd16Sdan 349744dbca83Sdrh #ifndef SQLITE_OMIT_FLOATING_POINT 349865a7cd16Sdan /* If the column has REAL affinity, it may currently be stored as an 3499113762a2Sdrh ** integer. Use OP_RealAffinity to make sure it is really real. 3500113762a2Sdrh ** 3501113762a2Sdrh ** EVIDENCE-OF: R-60985-57662 SQLite will convert the value back to 3502113762a2Sdrh ** floating point when extracting it from the record. */ 35032832ad42Sdan if( pExpr->iColumn>=0 35042832ad42Sdan && pTab->aCol[pExpr->iColumn].affinity==SQLITE_AFF_REAL 35052832ad42Sdan ){ 35062832ad42Sdan sqlite3VdbeAddOp1(v, OP_RealAffinity, target); 35072832ad42Sdan } 350844dbca83Sdrh #endif 3509165921a7Sdan break; 3510165921a7Sdan } 3511165921a7Sdan 351271c57db0Sdan case TK_VECTOR: { 3513d49fd4e8Sdan sqlite3ErrorMsg(pParse, "invalid use of row value"); 351471c57db0Sdan break; 351571c57db0Sdan } 351671c57db0Sdan 35172dcef11bSdrh /* 35182dcef11bSdrh ** Form A: 35192dcef11bSdrh ** CASE x WHEN e1 THEN r1 WHEN e2 THEN r2 ... WHEN eN THEN rN ELSE y END 35202dcef11bSdrh ** 35212dcef11bSdrh ** Form B: 35222dcef11bSdrh ** CASE WHEN e1 THEN r1 WHEN e2 THEN r2 ... WHEN eN THEN rN ELSE y END 35232dcef11bSdrh ** 35242dcef11bSdrh ** Form A is can be transformed into the equivalent form B as follows: 35252dcef11bSdrh ** CASE WHEN x=e1 THEN r1 WHEN x=e2 THEN r2 ... 35262dcef11bSdrh ** WHEN x=eN THEN rN ELSE y END 35272dcef11bSdrh ** 35282dcef11bSdrh ** X (if it exists) is in pExpr->pLeft. 3529c5cd1249Sdrh ** Y is in the last element of pExpr->x.pList if pExpr->x.pList->nExpr is 3530c5cd1249Sdrh ** odd. The Y is also optional. If the number of elements in x.pList 3531c5cd1249Sdrh ** is even, then Y is omitted and the "otherwise" result is NULL. 35322dcef11bSdrh ** Ei is in pExpr->pList->a[i*2] and Ri is pExpr->pList->a[i*2+1]. 35332dcef11bSdrh ** 35342dcef11bSdrh ** The result of the expression is the Ri for the first matching Ei, 35352dcef11bSdrh ** or if there is no matching Ei, the ELSE term Y, or if there is 35362dcef11bSdrh ** no ELSE term, NULL. 35372dcef11bSdrh */ 353833cd4909Sdrh default: assert( op==TK_CASE ); { 35392dcef11bSdrh int endLabel; /* GOTO label for end of CASE stmt */ 35402dcef11bSdrh int nextCase; /* GOTO label for next WHEN clause */ 35412dcef11bSdrh int nExpr; /* 2x number of WHEN terms */ 35422dcef11bSdrh int i; /* Loop counter */ 35432dcef11bSdrh ExprList *pEList; /* List of WHEN terms */ 35442dcef11bSdrh struct ExprList_item *aListelem; /* Array of WHEN terms */ 35452dcef11bSdrh Expr opCompare; /* The X==Ei expression */ 35462dcef11bSdrh Expr *pX; /* The X expression */ 35471bd10f8aSdrh Expr *pTest = 0; /* X==Ei (form A) or just Ei (form B) */ 3548ceea3321Sdrh VVA_ONLY( int iCacheLevel = pParse->iCacheLevel; ) 354917a7f8ddSdrh 35506ab3a2ecSdanielk1977 assert( !ExprHasProperty(pExpr, EP_xIsSelect) && pExpr->x.pList ); 35516ab3a2ecSdanielk1977 assert(pExpr->x.pList->nExpr > 0); 35526ab3a2ecSdanielk1977 pEList = pExpr->x.pList; 3553be5c89acSdrh aListelem = pEList->a; 3554be5c89acSdrh nExpr = pEList->nExpr; 35552dcef11bSdrh endLabel = sqlite3VdbeMakeLabel(v); 35562dcef11bSdrh if( (pX = pExpr->pLeft)!=0 ){ 355710d1edf0Sdrh tempX = *pX; 355833cd4909Sdrh testcase( pX->op==TK_COLUMN ); 355910d1edf0Sdrh exprToRegister(&tempX, sqlite3ExprCodeTemp(pParse, pX, ®Free1)); 3560c5499befSdrh testcase( regFree1==0 ); 35612dcef11bSdrh opCompare.op = TK_EQ; 356210d1edf0Sdrh opCompare.pLeft = &tempX; 35632dcef11bSdrh pTest = &opCompare; 35648b1db07fSdrh /* Ticket b351d95f9cd5ef17e9d9dbae18f5ca8611190001: 35658b1db07fSdrh ** The value in regFree1 might get SCopy-ed into the file result. 35668b1db07fSdrh ** So make sure that the regFree1 register is not reused for other 35678b1db07fSdrh ** purposes and possibly overwritten. */ 35688b1db07fSdrh regFree1 = 0; 3569cce7d176Sdrh } 3570c5cd1249Sdrh for(i=0; i<nExpr-1; i=i+2){ 3571ceea3321Sdrh sqlite3ExprCachePush(pParse); 35722dcef11bSdrh if( pX ){ 35731bd10f8aSdrh assert( pTest!=0 ); 35742dcef11bSdrh opCompare.pRight = aListelem[i].pExpr; 3575f5905aa7Sdrh }else{ 35762dcef11bSdrh pTest = aListelem[i].pExpr; 357717a7f8ddSdrh } 35782dcef11bSdrh nextCase = sqlite3VdbeMakeLabel(v); 357933cd4909Sdrh testcase( pTest->op==TK_COLUMN ); 35802dcef11bSdrh sqlite3ExprIfFalse(pParse, pTest, nextCase, SQLITE_JUMPIFNULL); 3581c5499befSdrh testcase( aListelem[i+1].pExpr->op==TK_COLUMN ); 35829de221dfSdrh sqlite3ExprCode(pParse, aListelem[i+1].pExpr, target); 3583076e85f5Sdrh sqlite3VdbeGoto(v, endLabel); 3584d2490904Sdrh sqlite3ExprCachePop(pParse); 35852dcef11bSdrh sqlite3VdbeResolveLabel(v, nextCase); 3586f570f011Sdrh } 3587c5cd1249Sdrh if( (nExpr&1)!=0 ){ 3588ceea3321Sdrh sqlite3ExprCachePush(pParse); 3589c5cd1249Sdrh sqlite3ExprCode(pParse, pEList->a[nExpr-1].pExpr, target); 3590d2490904Sdrh sqlite3ExprCachePop(pParse); 359117a7f8ddSdrh }else{ 35929de221dfSdrh sqlite3VdbeAddOp2(v, OP_Null, 0, target); 359317a7f8ddSdrh } 3594c1f4a19bSdanielk1977 assert( db->mallocFailed || pParse->nErr>0 3595c1f4a19bSdanielk1977 || pParse->iCacheLevel==iCacheLevel ); 35962dcef11bSdrh sqlite3VdbeResolveLabel(v, endLabel); 35976f34903eSdanielk1977 break; 35986f34903eSdanielk1977 } 35995338a5f7Sdanielk1977 #ifndef SQLITE_OMIT_TRIGGER 36006f34903eSdanielk1977 case TK_RAISE: { 3601165921a7Sdan assert( pExpr->affinity==OE_Rollback 3602165921a7Sdan || pExpr->affinity==OE_Abort 3603165921a7Sdan || pExpr->affinity==OE_Fail 3604165921a7Sdan || pExpr->affinity==OE_Ignore 3605165921a7Sdan ); 3606e0af83acSdan if( !pParse->pTriggerTab ){ 3607e0af83acSdan sqlite3ErrorMsg(pParse, 3608e0af83acSdan "RAISE() may only be used within a trigger-program"); 3609e0af83acSdan return 0; 3610e0af83acSdan } 3611e0af83acSdan if( pExpr->affinity==OE_Abort ){ 3612e0af83acSdan sqlite3MayAbort(pParse); 3613e0af83acSdan } 361433e619fcSdrh assert( !ExprHasProperty(pExpr, EP_IntValue) ); 3615e0af83acSdan if( pExpr->affinity==OE_Ignore ){ 3616e0af83acSdan sqlite3VdbeAddOp4( 3617e0af83acSdan v, OP_Halt, SQLITE_OK, OE_Ignore, 0, pExpr->u.zToken,0); 3618688852abSdrh VdbeCoverage(v); 3619e0af83acSdan }else{ 3620433dccfbSdrh sqlite3HaltConstraint(pParse, SQLITE_CONSTRAINT_TRIGGER, 3621f9c8ce3cSdrh pExpr->affinity, pExpr->u.zToken, 0, 0); 3622e0af83acSdan } 3623e0af83acSdan 3624ffe07b2dSdrh break; 362517a7f8ddSdrh } 36265338a5f7Sdanielk1977 #endif 3627ffe07b2dSdrh } 36282dcef11bSdrh sqlite3ReleaseTempReg(pParse, regFree1); 36292dcef11bSdrh sqlite3ReleaseTempReg(pParse, regFree2); 36302dcef11bSdrh return inReg; 36315b6afba9Sdrh } 36322dcef11bSdrh 36332dcef11bSdrh /* 3634d1a01edaSdrh ** Factor out the code of the given expression to initialization time. 3635d1a01edaSdrh */ 3636d673cddaSdrh void sqlite3ExprCodeAtInit( 3637d673cddaSdrh Parse *pParse, /* Parsing context */ 3638d673cddaSdrh Expr *pExpr, /* The expression to code when the VDBE initializes */ 3639d673cddaSdrh int regDest, /* Store the value in this register */ 3640d673cddaSdrh u8 reusable /* True if this expression is reusable */ 3641d673cddaSdrh ){ 3642d1a01edaSdrh ExprList *p; 3643d9f158e7Sdrh assert( ConstFactorOk(pParse) ); 3644d1a01edaSdrh p = pParse->pConstExpr; 3645d1a01edaSdrh pExpr = sqlite3ExprDup(pParse->db, pExpr, 0); 3646d1a01edaSdrh p = sqlite3ExprListAppend(pParse, p, pExpr); 3647d673cddaSdrh if( p ){ 3648d673cddaSdrh struct ExprList_item *pItem = &p->a[p->nExpr-1]; 3649d673cddaSdrh pItem->u.iConstExprReg = regDest; 3650d673cddaSdrh pItem->reusable = reusable; 3651d673cddaSdrh } 3652d1a01edaSdrh pParse->pConstExpr = p; 3653d1a01edaSdrh } 3654d1a01edaSdrh 3655d1a01edaSdrh /* 36562dcef11bSdrh ** Generate code to evaluate an expression and store the results 36572dcef11bSdrh ** into a register. Return the register number where the results 36582dcef11bSdrh ** are stored. 36592dcef11bSdrh ** 36602dcef11bSdrh ** If the register is a temporary register that can be deallocated, 3661678ccce8Sdrh ** then write its number into *pReg. If the result register is not 36622dcef11bSdrh ** a temporary, then set *pReg to zero. 3663f30a969bSdrh ** 3664f30a969bSdrh ** If pExpr is a constant, then this routine might generate this 3665f30a969bSdrh ** code to fill the register in the initialization section of the 3666f30a969bSdrh ** VDBE program, in order to factor it out of the evaluation loop. 36672dcef11bSdrh */ 36682dcef11bSdrh int sqlite3ExprCodeTemp(Parse *pParse, Expr *pExpr, int *pReg){ 3669f30a969bSdrh int r2; 3670f30a969bSdrh pExpr = sqlite3ExprSkipCollate(pExpr); 3671d9f158e7Sdrh if( ConstFactorOk(pParse) 3672f30a969bSdrh && pExpr->op!=TK_REGISTER 3673f30a969bSdrh && sqlite3ExprIsConstantNotJoin(pExpr) 3674f30a969bSdrh ){ 3675f30a969bSdrh ExprList *p = pParse->pConstExpr; 3676f30a969bSdrh int i; 3677f30a969bSdrh *pReg = 0; 3678f30a969bSdrh if( p ){ 3679d673cddaSdrh struct ExprList_item *pItem; 3680d673cddaSdrh for(pItem=p->a, i=p->nExpr; i>0; pItem++, i--){ 3681d673cddaSdrh if( pItem->reusable && sqlite3ExprCompare(pItem->pExpr,pExpr,-1)==0 ){ 3682d673cddaSdrh return pItem->u.iConstExprReg; 3683f30a969bSdrh } 3684f30a969bSdrh } 3685f30a969bSdrh } 3686f30a969bSdrh r2 = ++pParse->nMem; 3687d673cddaSdrh sqlite3ExprCodeAtInit(pParse, pExpr, r2, 1); 3688f30a969bSdrh }else{ 36892dcef11bSdrh int r1 = sqlite3GetTempReg(pParse); 3690f30a969bSdrh r2 = sqlite3ExprCodeTarget(pParse, pExpr, r1); 36912dcef11bSdrh if( r2==r1 ){ 36922dcef11bSdrh *pReg = r1; 36932dcef11bSdrh }else{ 36942dcef11bSdrh sqlite3ReleaseTempReg(pParse, r1); 36952dcef11bSdrh *pReg = 0; 36962dcef11bSdrh } 3697f30a969bSdrh } 36982dcef11bSdrh return r2; 36992dcef11bSdrh } 37002dcef11bSdrh 37012dcef11bSdrh /* 37022dcef11bSdrh ** Generate code that will evaluate expression pExpr and store the 37032dcef11bSdrh ** results in register target. The results are guaranteed to appear 37042dcef11bSdrh ** in register target. 37052dcef11bSdrh */ 370605a86c5cSdrh void sqlite3ExprCode(Parse *pParse, Expr *pExpr, int target){ 37079cbf3425Sdrh int inReg; 37089cbf3425Sdrh 37099cbf3425Sdrh assert( target>0 && target<=pParse->nMem ); 3710ebc16717Sdrh if( pExpr && pExpr->op==TK_REGISTER ){ 3711ebc16717Sdrh sqlite3VdbeAddOp2(pParse->pVdbe, OP_Copy, pExpr->iTable, target); 3712ebc16717Sdrh }else{ 37139cbf3425Sdrh inReg = sqlite3ExprCodeTarget(pParse, pExpr, target); 37141c75c9d7Sdrh assert( pParse->pVdbe!=0 || pParse->db->mallocFailed ); 37150e359b30Sdrh if( inReg!=target && pParse->pVdbe ){ 37169cbf3425Sdrh sqlite3VdbeAddOp2(pParse->pVdbe, OP_SCopy, inReg, target); 371717a7f8ddSdrh } 3718ebc16717Sdrh } 3719cce7d176Sdrh } 3720cce7d176Sdrh 3721cce7d176Sdrh /* 37221c75c9d7Sdrh ** Make a transient copy of expression pExpr and then code it using 37231c75c9d7Sdrh ** sqlite3ExprCode(). This routine works just like sqlite3ExprCode() 37241c75c9d7Sdrh ** except that the input expression is guaranteed to be unchanged. 37251c75c9d7Sdrh */ 37261c75c9d7Sdrh void sqlite3ExprCodeCopy(Parse *pParse, Expr *pExpr, int target){ 37271c75c9d7Sdrh sqlite3 *db = pParse->db; 37281c75c9d7Sdrh pExpr = sqlite3ExprDup(db, pExpr, 0); 37291c75c9d7Sdrh if( !db->mallocFailed ) sqlite3ExprCode(pParse, pExpr, target); 37301c75c9d7Sdrh sqlite3ExprDelete(db, pExpr); 37311c75c9d7Sdrh } 37321c75c9d7Sdrh 37331c75c9d7Sdrh /* 373405a86c5cSdrh ** Generate code that will evaluate expression pExpr and store the 373505a86c5cSdrh ** results in register target. The results are guaranteed to appear 373605a86c5cSdrh ** in register target. If the expression is constant, then this routine 373705a86c5cSdrh ** might choose to code the expression at initialization time. 373805a86c5cSdrh */ 373905a86c5cSdrh void sqlite3ExprCodeFactorable(Parse *pParse, Expr *pExpr, int target){ 374005a86c5cSdrh if( pParse->okConstFactor && sqlite3ExprIsConstant(pExpr) ){ 374105a86c5cSdrh sqlite3ExprCodeAtInit(pParse, pExpr, target, 0); 374205a86c5cSdrh }else{ 374305a86c5cSdrh sqlite3ExprCode(pParse, pExpr, target); 374405a86c5cSdrh } 3745cce7d176Sdrh } 3746cce7d176Sdrh 3747cce7d176Sdrh /* 374860ec914cSpeter.d.reid ** Generate code that evaluates the given expression and puts the result 3749de4fcfddSdrh ** in register target. 375025303780Sdrh ** 37512dcef11bSdrh ** Also make a copy of the expression results into another "cache" register 37522dcef11bSdrh ** and modify the expression so that the next time it is evaluated, 37532dcef11bSdrh ** the result is a copy of the cache register. 37542dcef11bSdrh ** 37552dcef11bSdrh ** This routine is used for expressions that are used multiple 37562dcef11bSdrh ** times. They are evaluated once and the results of the expression 37572dcef11bSdrh ** are reused. 375825303780Sdrh */ 375905a86c5cSdrh void sqlite3ExprCodeAndCache(Parse *pParse, Expr *pExpr, int target){ 376025303780Sdrh Vdbe *v = pParse->pVdbe; 376125303780Sdrh int iMem; 376205a86c5cSdrh 376305a86c5cSdrh assert( target>0 ); 376405a86c5cSdrh assert( pExpr->op!=TK_REGISTER ); 376505a86c5cSdrh sqlite3ExprCode(pParse, pExpr, target); 37662dcef11bSdrh iMem = ++pParse->nMem; 376705a86c5cSdrh sqlite3VdbeAddOp2(v, OP_Copy, target, iMem); 3768a4c3c87eSdrh exprToRegister(pExpr, iMem); 376925303780Sdrh } 37707e02e5e6Sdrh 3771678ccce8Sdrh /* 3772268380caSdrh ** Generate code that pushes the value of every element of the given 37739cbf3425Sdrh ** expression list into a sequence of registers beginning at target. 3774268380caSdrh ** 3775892d3179Sdrh ** Return the number of elements evaluated. 3776d1a01edaSdrh ** 3777d1a01edaSdrh ** The SQLITE_ECEL_DUP flag prevents the arguments from being 3778d1a01edaSdrh ** filled using OP_SCopy. OP_Copy must be used instead. 3779d1a01edaSdrh ** 3780d1a01edaSdrh ** The SQLITE_ECEL_FACTOR argument allows constant arguments to be 3781d1a01edaSdrh ** factored out into initialization code. 3782b0df9634Sdrh ** 3783b0df9634Sdrh ** The SQLITE_ECEL_REF flag means that expressions in the list with 3784b0df9634Sdrh ** ExprList.a[].u.x.iOrderByCol>0 have already been evaluated and stored 3785b0df9634Sdrh ** in registers at srcReg, and so the value can be copied from there. 3786268380caSdrh */ 37874adee20fSdanielk1977 int sqlite3ExprCodeExprList( 3788268380caSdrh Parse *pParse, /* Parsing context */ 3789389a1adbSdrh ExprList *pList, /* The expression list to be coded */ 3790191b54cbSdrh int target, /* Where to write results */ 37915579d59fSdrh int srcReg, /* Source registers if SQLITE_ECEL_REF */ 3792d1a01edaSdrh u8 flags /* SQLITE_ECEL_* flags */ 3793268380caSdrh ){ 3794268380caSdrh struct ExprList_item *pItem; 37955579d59fSdrh int i, j, n; 3796d1a01edaSdrh u8 copyOp = (flags & SQLITE_ECEL_DUP) ? OP_Copy : OP_SCopy; 37975579d59fSdrh Vdbe *v = pParse->pVdbe; 37989d8b3072Sdrh assert( pList!=0 ); 37999cbf3425Sdrh assert( target>0 ); 3800d81a142bSdrh assert( pParse->pVdbe!=0 ); /* Never gets this far otherwise */ 3801268380caSdrh n = pList->nExpr; 3802d9f158e7Sdrh if( !ConstFactorOk(pParse) ) flags &= ~SQLITE_ECEL_FACTOR; 3803191b54cbSdrh for(pItem=pList->a, i=0; i<n; i++, pItem++){ 38047445ffe2Sdrh Expr *pExpr = pItem->pExpr; 38055579d59fSdrh if( (flags & SQLITE_ECEL_REF)!=0 && (j = pList->a[i].u.x.iOrderByCol)>0 ){ 38065579d59fSdrh sqlite3VdbeAddOp2(v, copyOp, j+srcReg-1, target+i); 38075579d59fSdrh }else if( (flags & SQLITE_ECEL_FACTOR)!=0 && sqlite3ExprIsConstant(pExpr) ){ 3808d673cddaSdrh sqlite3ExprCodeAtInit(pParse, pExpr, target+i, 0); 3809d1a01edaSdrh }else{ 38107445ffe2Sdrh int inReg = sqlite3ExprCodeTarget(pParse, pExpr, target+i); 3811746fd9ccSdrh if( inReg!=target+i ){ 38124eded604Sdrh VdbeOp *pOp; 38134eded604Sdrh if( copyOp==OP_Copy 38144eded604Sdrh && (pOp=sqlite3VdbeGetOp(v, -1))->opcode==OP_Copy 38154eded604Sdrh && pOp->p1+pOp->p3+1==inReg 38164eded604Sdrh && pOp->p2+pOp->p3+1==target+i 38174eded604Sdrh ){ 38184eded604Sdrh pOp->p3++; 38194eded604Sdrh }else{ 38204eded604Sdrh sqlite3VdbeAddOp2(v, copyOp, inReg, target+i); 38214eded604Sdrh } 3822d1a01edaSdrh } 3823d176611bSdrh } 3824268380caSdrh } 3825f9b596ebSdrh return n; 3826268380caSdrh } 3827268380caSdrh 3828268380caSdrh /* 382936c563a2Sdrh ** Generate code for a BETWEEN operator. 383036c563a2Sdrh ** 383136c563a2Sdrh ** x BETWEEN y AND z 383236c563a2Sdrh ** 383336c563a2Sdrh ** The above is equivalent to 383436c563a2Sdrh ** 383536c563a2Sdrh ** x>=y AND x<=z 383636c563a2Sdrh ** 383736c563a2Sdrh ** Code it as such, taking care to do the common subexpression 383860ec914cSpeter.d.reid ** elimination of x. 383936c563a2Sdrh */ 384036c563a2Sdrh static void exprCodeBetween( 384136c563a2Sdrh Parse *pParse, /* Parsing and code generating context */ 384236c563a2Sdrh Expr *pExpr, /* The BETWEEN expression */ 384336c563a2Sdrh int dest, /* Jump here if the jump is taken */ 384471c57db0Sdan void (*xJumpIf)(Parse*,Expr*,int,int), 384536c563a2Sdrh int jumpIfNull /* Take the jump if the BETWEEN is NULL */ 384636c563a2Sdrh ){ 384736c563a2Sdrh Expr exprAnd; /* The AND operator in x>=y AND x<=z */ 384836c563a2Sdrh Expr compLeft; /* The x>=y term */ 384936c563a2Sdrh Expr compRight; /* The x<=z term */ 385036c563a2Sdrh Expr exprX; /* The x subexpression */ 385136c563a2Sdrh int regFree1 = 0; /* Temporary use register */ 385236c563a2Sdrh 385371c57db0Sdan memset(&compLeft, 0, sizeof(Expr)); 385471c57db0Sdan memset(&compRight, 0, sizeof(Expr)); 385571c57db0Sdan memset(&exprAnd, 0, sizeof(Expr)); 385671c57db0Sdan 385736c563a2Sdrh assert( !ExprHasProperty(pExpr, EP_xIsSelect) ); 385836c563a2Sdrh exprX = *pExpr->pLeft; 385936c563a2Sdrh exprAnd.op = TK_AND; 386036c563a2Sdrh exprAnd.pLeft = &compLeft; 386136c563a2Sdrh exprAnd.pRight = &compRight; 386236c563a2Sdrh compLeft.op = TK_GE; 386336c563a2Sdrh compLeft.pLeft = &exprX; 386436c563a2Sdrh compLeft.pRight = pExpr->x.pList->a[0].pExpr; 386536c563a2Sdrh compRight.op = TK_LE; 386636c563a2Sdrh compRight.pLeft = &exprX; 386736c563a2Sdrh compRight.pRight = pExpr->x.pList->a[1].pExpr; 386871c57db0Sdan if( (exprX.flags & EP_Vector)==0 ){ 3869a4c3c87eSdrh exprToRegister(&exprX, sqlite3ExprCodeTemp(pParse, &exprX, ®Free1)); 387071c57db0Sdan } 387171c57db0Sdan if( xJumpIf ){ 387271c57db0Sdan xJumpIf(pParse, &exprAnd, dest, jumpIfNull); 387336c563a2Sdrh }else{ 387471c57db0Sdan exprX.flags |= EP_FromJoin; 387571c57db0Sdan sqlite3ExprCodeTarget(pParse, &exprAnd, dest); 387636c563a2Sdrh } 387736c563a2Sdrh sqlite3ReleaseTempReg(pParse, regFree1); 387836c563a2Sdrh 387936c563a2Sdrh /* Ensure adequate test coverage */ 388036c563a2Sdrh testcase( jumpIfTrue==0 && jumpIfNull==0 && regFree1==0 ); 388136c563a2Sdrh testcase( jumpIfTrue==0 && jumpIfNull==0 && regFree1!=0 ); 388236c563a2Sdrh testcase( jumpIfTrue==0 && jumpIfNull!=0 && regFree1==0 ); 388336c563a2Sdrh testcase( jumpIfTrue==0 && jumpIfNull!=0 && regFree1!=0 ); 388436c563a2Sdrh testcase( jumpIfTrue!=0 && jumpIfNull==0 && regFree1==0 ); 388536c563a2Sdrh testcase( jumpIfTrue!=0 && jumpIfNull==0 && regFree1!=0 ); 388636c563a2Sdrh testcase( jumpIfTrue!=0 && jumpIfNull!=0 && regFree1==0 ); 388736c563a2Sdrh testcase( jumpIfTrue!=0 && jumpIfNull!=0 && regFree1!=0 ); 388836c563a2Sdrh } 388936c563a2Sdrh 389036c563a2Sdrh /* 3891cce7d176Sdrh ** Generate code for a boolean expression such that a jump is made 3892cce7d176Sdrh ** to the label "dest" if the expression is true but execution 3893cce7d176Sdrh ** continues straight thru if the expression is false. 3894f5905aa7Sdrh ** 3895f5905aa7Sdrh ** If the expression evaluates to NULL (neither true nor false), then 389635573356Sdrh ** take the jump if the jumpIfNull flag is SQLITE_JUMPIFNULL. 3897f2bc013cSdrh ** 3898f2bc013cSdrh ** This code depends on the fact that certain token values (ex: TK_EQ) 3899f2bc013cSdrh ** are the same as opcode values (ex: OP_Eq) that implement the corresponding 3900f2bc013cSdrh ** operation. Special comments in vdbe.c and the mkopcodeh.awk script in 3901f2bc013cSdrh ** the make process cause these values to align. Assert()s in the code 3902f2bc013cSdrh ** below verify that the numbers are aligned correctly. 3903cce7d176Sdrh */ 39044adee20fSdanielk1977 void sqlite3ExprIfTrue(Parse *pParse, Expr *pExpr, int dest, int jumpIfNull){ 3905cce7d176Sdrh Vdbe *v = pParse->pVdbe; 3906cce7d176Sdrh int op = 0; 39072dcef11bSdrh int regFree1 = 0; 39082dcef11bSdrh int regFree2 = 0; 39092dcef11bSdrh int r1, r2; 39102dcef11bSdrh 391135573356Sdrh assert( jumpIfNull==SQLITE_JUMPIFNULL || jumpIfNull==0 ); 391248864df9Smistachkin if( NEVER(v==0) ) return; /* Existence of VDBE checked by caller */ 391333cd4909Sdrh if( NEVER(pExpr==0) ) return; /* No way this can happen */ 3914f2bc013cSdrh op = pExpr->op; 39157b35a77bSdan switch( op ){ 3916cce7d176Sdrh case TK_AND: { 39174adee20fSdanielk1977 int d2 = sqlite3VdbeMakeLabel(v); 3918c5499befSdrh testcase( jumpIfNull==0 ); 391935573356Sdrh sqlite3ExprIfFalse(pParse, pExpr->pLeft, d2,jumpIfNull^SQLITE_JUMPIFNULL); 392054e2adb5Sdrh sqlite3ExprCachePush(pParse); 39214adee20fSdanielk1977 sqlite3ExprIfTrue(pParse, pExpr->pRight, dest, jumpIfNull); 39224adee20fSdanielk1977 sqlite3VdbeResolveLabel(v, d2); 3923d2490904Sdrh sqlite3ExprCachePop(pParse); 3924cce7d176Sdrh break; 3925cce7d176Sdrh } 3926cce7d176Sdrh case TK_OR: { 3927c5499befSdrh testcase( jumpIfNull==0 ); 39284adee20fSdanielk1977 sqlite3ExprIfTrue(pParse, pExpr->pLeft, dest, jumpIfNull); 392954e2adb5Sdrh sqlite3ExprCachePush(pParse); 39304adee20fSdanielk1977 sqlite3ExprIfTrue(pParse, pExpr->pRight, dest, jumpIfNull); 3931d2490904Sdrh sqlite3ExprCachePop(pParse); 3932cce7d176Sdrh break; 3933cce7d176Sdrh } 3934cce7d176Sdrh case TK_NOT: { 3935c5499befSdrh testcase( jumpIfNull==0 ); 39364adee20fSdanielk1977 sqlite3ExprIfFalse(pParse, pExpr->pLeft, dest, jumpIfNull); 3937cce7d176Sdrh break; 3938cce7d176Sdrh } 3939de845c2fSdrh case TK_IS: 3940de845c2fSdrh case TK_ISNOT: 3941de845c2fSdrh testcase( op==TK_IS ); 3942de845c2fSdrh testcase( op==TK_ISNOT ); 3943de845c2fSdrh op = (op==TK_IS) ? TK_EQ : TK_NE; 3944de845c2fSdrh jumpIfNull = SQLITE_NULLEQ; 3945de845c2fSdrh /* Fall thru */ 3946cce7d176Sdrh case TK_LT: 3947cce7d176Sdrh case TK_LE: 3948cce7d176Sdrh case TK_GT: 3949cce7d176Sdrh case TK_GE: 3950cce7d176Sdrh case TK_NE: 39510ac65892Sdrh case TK_EQ: { 39527b35a77bSdan if( pExpr->pLeft->flags & EP_Vector ) goto default_expr; 3953c5499befSdrh testcase( jumpIfNull==0 ); 3954b6da74ebSdrh r1 = sqlite3ExprCodeTemp(pParse, pExpr->pLeft, ®Free1); 3955b6da74ebSdrh r2 = sqlite3ExprCodeTemp(pParse, pExpr->pRight, ®Free2); 395635573356Sdrh codeCompare(pParse, pExpr->pLeft, pExpr->pRight, op, 39572dcef11bSdrh r1, r2, dest, jumpIfNull); 39587d176105Sdrh assert(TK_LT==OP_Lt); testcase(op==OP_Lt); VdbeCoverageIf(v,op==OP_Lt); 39597d176105Sdrh assert(TK_LE==OP_Le); testcase(op==OP_Le); VdbeCoverageIf(v,op==OP_Le); 39607d176105Sdrh assert(TK_GT==OP_Gt); testcase(op==OP_Gt); VdbeCoverageIf(v,op==OP_Gt); 39617d176105Sdrh assert(TK_GE==OP_Ge); testcase(op==OP_Ge); VdbeCoverageIf(v,op==OP_Ge); 3962de845c2fSdrh assert(TK_EQ==OP_Eq); testcase(op==OP_Eq); 3963de845c2fSdrh VdbeCoverageIf(v, op==OP_Eq && jumpIfNull==SQLITE_NULLEQ); 3964de845c2fSdrh VdbeCoverageIf(v, op==OP_Eq && jumpIfNull!=SQLITE_NULLEQ); 3965de845c2fSdrh assert(TK_NE==OP_Ne); testcase(op==OP_Ne); 3966de845c2fSdrh VdbeCoverageIf(v, op==OP_Ne && jumpIfNull==SQLITE_NULLEQ); 3967de845c2fSdrh VdbeCoverageIf(v, op==OP_Ne && jumpIfNull!=SQLITE_NULLEQ); 39686a2fe093Sdrh testcase( regFree1==0 ); 39696a2fe093Sdrh testcase( regFree2==0 ); 39706a2fe093Sdrh break; 39716a2fe093Sdrh } 3972cce7d176Sdrh case TK_ISNULL: 3973cce7d176Sdrh case TK_NOTNULL: { 39747d176105Sdrh assert( TK_ISNULL==OP_IsNull ); testcase( op==TK_ISNULL ); 39757d176105Sdrh assert( TK_NOTNULL==OP_NotNull ); testcase( op==TK_NOTNULL ); 39762dcef11bSdrh r1 = sqlite3ExprCodeTemp(pParse, pExpr->pLeft, ®Free1); 39772dcef11bSdrh sqlite3VdbeAddOp2(v, op, r1, dest); 39787d176105Sdrh VdbeCoverageIf(v, op==TK_ISNULL); 39797d176105Sdrh VdbeCoverageIf(v, op==TK_NOTNULL); 3980c5499befSdrh testcase( regFree1==0 ); 3981cce7d176Sdrh break; 3982cce7d176Sdrh } 3983fef5208cSdrh case TK_BETWEEN: { 39845c03f30aSdrh testcase( jumpIfNull==0 ); 398571c57db0Sdan exprCodeBetween(pParse, pExpr, dest, sqlite3ExprIfTrue, jumpIfNull); 3986fef5208cSdrh break; 3987fef5208cSdrh } 3988bb201344Sshaneh #ifndef SQLITE_OMIT_SUBQUERY 3989e3365e6cSdrh case TK_IN: { 3990e3365e6cSdrh int destIfFalse = sqlite3VdbeMakeLabel(v); 3991e3365e6cSdrh int destIfNull = jumpIfNull ? dest : destIfFalse; 3992e3365e6cSdrh sqlite3ExprCodeIN(pParse, pExpr, destIfFalse, destIfNull); 3993076e85f5Sdrh sqlite3VdbeGoto(v, dest); 3994e3365e6cSdrh sqlite3VdbeResolveLabel(v, destIfFalse); 3995e3365e6cSdrh break; 3996e3365e6cSdrh } 3997bb201344Sshaneh #endif 3998cce7d176Sdrh default: { 39997b35a77bSdan default_expr: 4000991a1985Sdrh if( exprAlwaysTrue(pExpr) ){ 4001076e85f5Sdrh sqlite3VdbeGoto(v, dest); 4002991a1985Sdrh }else if( exprAlwaysFalse(pExpr) ){ 4003991a1985Sdrh /* No-op */ 4004991a1985Sdrh }else{ 40052dcef11bSdrh r1 = sqlite3ExprCodeTemp(pParse, pExpr, ®Free1); 40062dcef11bSdrh sqlite3VdbeAddOp3(v, OP_If, r1, dest, jumpIfNull!=0); 4007688852abSdrh VdbeCoverage(v); 4008c5499befSdrh testcase( regFree1==0 ); 4009c5499befSdrh testcase( jumpIfNull==0 ); 4010991a1985Sdrh } 4011cce7d176Sdrh break; 4012cce7d176Sdrh } 4013cce7d176Sdrh } 40142dcef11bSdrh sqlite3ReleaseTempReg(pParse, regFree1); 40152dcef11bSdrh sqlite3ReleaseTempReg(pParse, regFree2); 4016cce7d176Sdrh } 4017cce7d176Sdrh 4018cce7d176Sdrh /* 401966b89c8fSdrh ** Generate code for a boolean expression such that a jump is made 4020cce7d176Sdrh ** to the label "dest" if the expression is false but execution 4021cce7d176Sdrh ** continues straight thru if the expression is true. 4022f5905aa7Sdrh ** 4023f5905aa7Sdrh ** If the expression evaluates to NULL (neither true nor false) then 402435573356Sdrh ** jump if jumpIfNull is SQLITE_JUMPIFNULL or fall through if jumpIfNull 402535573356Sdrh ** is 0. 4026cce7d176Sdrh */ 40274adee20fSdanielk1977 void sqlite3ExprIfFalse(Parse *pParse, Expr *pExpr, int dest, int jumpIfNull){ 4028cce7d176Sdrh Vdbe *v = pParse->pVdbe; 4029cce7d176Sdrh int op = 0; 40302dcef11bSdrh int regFree1 = 0; 40312dcef11bSdrh int regFree2 = 0; 40322dcef11bSdrh int r1, r2; 40332dcef11bSdrh 403435573356Sdrh assert( jumpIfNull==SQLITE_JUMPIFNULL || jumpIfNull==0 ); 403548864df9Smistachkin if( NEVER(v==0) ) return; /* Existence of VDBE checked by caller */ 403633cd4909Sdrh if( pExpr==0 ) return; 4037f2bc013cSdrh 4038f2bc013cSdrh /* The value of pExpr->op and op are related as follows: 4039f2bc013cSdrh ** 4040f2bc013cSdrh ** pExpr->op op 4041f2bc013cSdrh ** --------- ---------- 4042f2bc013cSdrh ** TK_ISNULL OP_NotNull 4043f2bc013cSdrh ** TK_NOTNULL OP_IsNull 4044f2bc013cSdrh ** TK_NE OP_Eq 4045f2bc013cSdrh ** TK_EQ OP_Ne 4046f2bc013cSdrh ** TK_GT OP_Le 4047f2bc013cSdrh ** TK_LE OP_Gt 4048f2bc013cSdrh ** TK_GE OP_Lt 4049f2bc013cSdrh ** TK_LT OP_Ge 4050f2bc013cSdrh ** 4051f2bc013cSdrh ** For other values of pExpr->op, op is undefined and unused. 4052f2bc013cSdrh ** The value of TK_ and OP_ constants are arranged such that we 4053f2bc013cSdrh ** can compute the mapping above using the following expression. 4054f2bc013cSdrh ** Assert()s verify that the computation is correct. 4055f2bc013cSdrh */ 4056f2bc013cSdrh op = ((pExpr->op+(TK_ISNULL&1))^1)-(TK_ISNULL&1); 4057f2bc013cSdrh 4058f2bc013cSdrh /* Verify correct alignment of TK_ and OP_ constants 4059f2bc013cSdrh */ 4060f2bc013cSdrh assert( pExpr->op!=TK_ISNULL || op==OP_NotNull ); 4061f2bc013cSdrh assert( pExpr->op!=TK_NOTNULL || op==OP_IsNull ); 4062f2bc013cSdrh assert( pExpr->op!=TK_NE || op==OP_Eq ); 4063f2bc013cSdrh assert( pExpr->op!=TK_EQ || op==OP_Ne ); 4064f2bc013cSdrh assert( pExpr->op!=TK_LT || op==OP_Ge ); 4065f2bc013cSdrh assert( pExpr->op!=TK_LE || op==OP_Gt ); 4066f2bc013cSdrh assert( pExpr->op!=TK_GT || op==OP_Le ); 4067f2bc013cSdrh assert( pExpr->op!=TK_GE || op==OP_Lt ); 4068f2bc013cSdrh 4069ba00e30aSdan switch( pExpr->op ){ 4070cce7d176Sdrh case TK_AND: { 4071c5499befSdrh testcase( jumpIfNull==0 ); 40724adee20fSdanielk1977 sqlite3ExprIfFalse(pParse, pExpr->pLeft, dest, jumpIfNull); 407354e2adb5Sdrh sqlite3ExprCachePush(pParse); 40744adee20fSdanielk1977 sqlite3ExprIfFalse(pParse, pExpr->pRight, dest, jumpIfNull); 4075d2490904Sdrh sqlite3ExprCachePop(pParse); 4076cce7d176Sdrh break; 4077cce7d176Sdrh } 4078cce7d176Sdrh case TK_OR: { 40794adee20fSdanielk1977 int d2 = sqlite3VdbeMakeLabel(v); 4080c5499befSdrh testcase( jumpIfNull==0 ); 408135573356Sdrh sqlite3ExprIfTrue(pParse, pExpr->pLeft, d2, jumpIfNull^SQLITE_JUMPIFNULL); 408254e2adb5Sdrh sqlite3ExprCachePush(pParse); 40834adee20fSdanielk1977 sqlite3ExprIfFalse(pParse, pExpr->pRight, dest, jumpIfNull); 40844adee20fSdanielk1977 sqlite3VdbeResolveLabel(v, d2); 4085d2490904Sdrh sqlite3ExprCachePop(pParse); 4086cce7d176Sdrh break; 4087cce7d176Sdrh } 4088cce7d176Sdrh case TK_NOT: { 40895c03f30aSdrh testcase( jumpIfNull==0 ); 40904adee20fSdanielk1977 sqlite3ExprIfTrue(pParse, pExpr->pLeft, dest, jumpIfNull); 4091cce7d176Sdrh break; 4092cce7d176Sdrh } 4093de845c2fSdrh case TK_IS: 4094de845c2fSdrh case TK_ISNOT: 4095de845c2fSdrh testcase( pExpr->op==TK_IS ); 4096de845c2fSdrh testcase( pExpr->op==TK_ISNOT ); 4097de845c2fSdrh op = (pExpr->op==TK_IS) ? TK_NE : TK_EQ; 4098de845c2fSdrh jumpIfNull = SQLITE_NULLEQ; 4099de845c2fSdrh /* Fall thru */ 4100cce7d176Sdrh case TK_LT: 4101cce7d176Sdrh case TK_LE: 4102cce7d176Sdrh case TK_GT: 4103cce7d176Sdrh case TK_GE: 4104cce7d176Sdrh case TK_NE: 4105cce7d176Sdrh case TK_EQ: { 4106ba00e30aSdan if( pExpr->pLeft->flags & EP_Vector ) goto default_expr; 4107ba00e30aSdan 4108c5499befSdrh testcase( jumpIfNull==0 ); 4109b6da74ebSdrh r1 = sqlite3ExprCodeTemp(pParse, pExpr->pLeft, ®Free1); 4110b6da74ebSdrh r2 = sqlite3ExprCodeTemp(pParse, pExpr->pRight, ®Free2); 411135573356Sdrh codeCompare(pParse, pExpr->pLeft, pExpr->pRight, op, 41122dcef11bSdrh r1, r2, dest, jumpIfNull); 41137d176105Sdrh assert(TK_LT==OP_Lt); testcase(op==OP_Lt); VdbeCoverageIf(v,op==OP_Lt); 41147d176105Sdrh assert(TK_LE==OP_Le); testcase(op==OP_Le); VdbeCoverageIf(v,op==OP_Le); 41157d176105Sdrh assert(TK_GT==OP_Gt); testcase(op==OP_Gt); VdbeCoverageIf(v,op==OP_Gt); 41167d176105Sdrh assert(TK_GE==OP_Ge); testcase(op==OP_Ge); VdbeCoverageIf(v,op==OP_Ge); 4117de845c2fSdrh assert(TK_EQ==OP_Eq); testcase(op==OP_Eq); 4118de845c2fSdrh VdbeCoverageIf(v, op==OP_Eq && jumpIfNull!=SQLITE_NULLEQ); 4119de845c2fSdrh VdbeCoverageIf(v, op==OP_Eq && jumpIfNull==SQLITE_NULLEQ); 4120de845c2fSdrh assert(TK_NE==OP_Ne); testcase(op==OP_Ne); 4121de845c2fSdrh VdbeCoverageIf(v, op==OP_Ne && jumpIfNull!=SQLITE_NULLEQ); 4122de845c2fSdrh VdbeCoverageIf(v, op==OP_Ne && jumpIfNull==SQLITE_NULLEQ); 41236a2fe093Sdrh testcase( regFree1==0 ); 41246a2fe093Sdrh testcase( regFree2==0 ); 41256a2fe093Sdrh break; 41266a2fe093Sdrh } 4127cce7d176Sdrh case TK_ISNULL: 4128cce7d176Sdrh case TK_NOTNULL: { 41292dcef11bSdrh r1 = sqlite3ExprCodeTemp(pParse, pExpr->pLeft, ®Free1); 41302dcef11bSdrh sqlite3VdbeAddOp2(v, op, r1, dest); 41317d176105Sdrh testcase( op==TK_ISNULL ); VdbeCoverageIf(v, op==TK_ISNULL); 41327d176105Sdrh testcase( op==TK_NOTNULL ); VdbeCoverageIf(v, op==TK_NOTNULL); 4133c5499befSdrh testcase( regFree1==0 ); 4134cce7d176Sdrh break; 4135cce7d176Sdrh } 4136fef5208cSdrh case TK_BETWEEN: { 41375c03f30aSdrh testcase( jumpIfNull==0 ); 413871c57db0Sdan exprCodeBetween(pParse, pExpr, dest, sqlite3ExprIfFalse, jumpIfNull); 4139fef5208cSdrh break; 4140fef5208cSdrh } 4141bb201344Sshaneh #ifndef SQLITE_OMIT_SUBQUERY 4142e3365e6cSdrh case TK_IN: { 4143e3365e6cSdrh if( jumpIfNull ){ 4144e3365e6cSdrh sqlite3ExprCodeIN(pParse, pExpr, dest, dest); 4145e3365e6cSdrh }else{ 4146e3365e6cSdrh int destIfNull = sqlite3VdbeMakeLabel(v); 4147e3365e6cSdrh sqlite3ExprCodeIN(pParse, pExpr, dest, destIfNull); 4148e3365e6cSdrh sqlite3VdbeResolveLabel(v, destIfNull); 4149e3365e6cSdrh } 4150e3365e6cSdrh break; 4151e3365e6cSdrh } 4152bb201344Sshaneh #endif 4153cce7d176Sdrh default: { 4154ba00e30aSdan default_expr: 4155991a1985Sdrh if( exprAlwaysFalse(pExpr) ){ 4156076e85f5Sdrh sqlite3VdbeGoto(v, dest); 4157991a1985Sdrh }else if( exprAlwaysTrue(pExpr) ){ 4158991a1985Sdrh /* no-op */ 4159991a1985Sdrh }else{ 41602dcef11bSdrh r1 = sqlite3ExprCodeTemp(pParse, pExpr, ®Free1); 41612dcef11bSdrh sqlite3VdbeAddOp3(v, OP_IfNot, r1, dest, jumpIfNull!=0); 4162688852abSdrh VdbeCoverage(v); 4163c5499befSdrh testcase( regFree1==0 ); 4164c5499befSdrh testcase( jumpIfNull==0 ); 4165991a1985Sdrh } 4166cce7d176Sdrh break; 4167cce7d176Sdrh } 4168cce7d176Sdrh } 41692dcef11bSdrh sqlite3ReleaseTempReg(pParse, regFree1); 41702dcef11bSdrh sqlite3ReleaseTempReg(pParse, regFree2); 4171cce7d176Sdrh } 41722282792aSdrh 41732282792aSdrh /* 417472bc8208Sdrh ** Like sqlite3ExprIfFalse() except that a copy is made of pExpr before 417572bc8208Sdrh ** code generation, and that copy is deleted after code generation. This 417672bc8208Sdrh ** ensures that the original pExpr is unchanged. 417772bc8208Sdrh */ 417872bc8208Sdrh void sqlite3ExprIfFalseDup(Parse *pParse, Expr *pExpr, int dest,int jumpIfNull){ 417972bc8208Sdrh sqlite3 *db = pParse->db; 418072bc8208Sdrh Expr *pCopy = sqlite3ExprDup(db, pExpr, 0); 418172bc8208Sdrh if( db->mallocFailed==0 ){ 418272bc8208Sdrh sqlite3ExprIfFalse(pParse, pCopy, dest, jumpIfNull); 418372bc8208Sdrh } 418472bc8208Sdrh sqlite3ExprDelete(db, pCopy); 418572bc8208Sdrh } 418672bc8208Sdrh 418772bc8208Sdrh 418872bc8208Sdrh /* 41891d9da70aSdrh ** Do a deep comparison of two expression trees. Return 0 if the two 41901d9da70aSdrh ** expressions are completely identical. Return 1 if they differ only 41911d9da70aSdrh ** by a COLLATE operator at the top level. Return 2 if there are differences 41921d9da70aSdrh ** other than the top-level COLLATE operator. 4193d40aab0eSdrh ** 4194619a1305Sdrh ** If any subelement of pB has Expr.iTable==(-1) then it is allowed 4195619a1305Sdrh ** to compare equal to an equivalent element in pA with Expr.iTable==iTab. 4196619a1305Sdrh ** 419766518ca7Sdrh ** The pA side might be using TK_REGISTER. If that is the case and pB is 419866518ca7Sdrh ** not using TK_REGISTER but is otherwise equivalent, then still return 0. 419966518ca7Sdrh ** 42001d9da70aSdrh ** Sometimes this routine will return 2 even if the two expressions 4201d40aab0eSdrh ** really are equivalent. If we cannot prove that the expressions are 42021d9da70aSdrh ** identical, we return 2 just to be safe. So if this routine 42031d9da70aSdrh ** returns 2, then you do not really know for certain if the two 42041d9da70aSdrh ** expressions are the same. But if you get a 0 or 1 return, then you 4205d40aab0eSdrh ** can be sure the expressions are the same. In the places where 42061d9da70aSdrh ** this routine is used, it does not hurt to get an extra 2 - that 4207d40aab0eSdrh ** just might result in some slightly slower code. But returning 42081d9da70aSdrh ** an incorrect 0 or 1 could lead to a malfunction. 42092282792aSdrh */ 4210619a1305Sdrh int sqlite3ExprCompare(Expr *pA, Expr *pB, int iTab){ 421110d1edf0Sdrh u32 combinedFlags; 42124b202ae2Sdanielk1977 if( pA==0 || pB==0 ){ 42131d9da70aSdrh return pB==pA ? 0 : 2; 42142282792aSdrh } 421510d1edf0Sdrh combinedFlags = pA->flags | pB->flags; 421610d1edf0Sdrh if( combinedFlags & EP_IntValue ){ 421710d1edf0Sdrh if( (pA->flags&pB->flags&EP_IntValue)!=0 && pA->u.iValue==pB->u.iValue ){ 421810d1edf0Sdrh return 0; 421910d1edf0Sdrh } 42201d9da70aSdrh return 2; 42216ab3a2ecSdanielk1977 } 4222c2acc4e4Sdrh if( pA->op!=pB->op ){ 4223619a1305Sdrh if( pA->op==TK_COLLATE && sqlite3ExprCompare(pA->pLeft, pB, iTab)<2 ){ 4224ae80ddeaSdrh return 1; 4225ae80ddeaSdrh } 4226619a1305Sdrh if( pB->op==TK_COLLATE && sqlite3ExprCompare(pA, pB->pLeft, iTab)<2 ){ 4227ae80ddeaSdrh return 1; 4228ae80ddeaSdrh } 4229ae80ddeaSdrh return 2; 4230ae80ddeaSdrh } 42312edc5fd7Sdrh if( pA->op!=TK_COLUMN && pA->op!=TK_AGG_COLUMN && pA->u.zToken ){ 4232390b88a4Sdrh if( pA->op==TK_FUNCTION ){ 4233390b88a4Sdrh if( sqlite3StrICmp(pA->u.zToken,pB->u.zToken)!=0 ) return 2; 4234390b88a4Sdrh }else if( strcmp(pA->u.zToken,pB->u.zToken)!=0 ){ 423510d1edf0Sdrh return pA->op==TK_COLLATE ? 1 : 2; 423610d1edf0Sdrh } 423710d1edf0Sdrh } 423810d1edf0Sdrh if( (pA->flags & EP_Distinct)!=(pB->flags & EP_Distinct) ) return 2; 423985f8aa79Sdrh if( ALWAYS((combinedFlags & EP_TokenOnly)==0) ){ 424010d1edf0Sdrh if( combinedFlags & EP_xIsSelect ) return 2; 4241619a1305Sdrh if( sqlite3ExprCompare(pA->pLeft, pB->pLeft, iTab) ) return 2; 4242619a1305Sdrh if( sqlite3ExprCompare(pA->pRight, pB->pRight, iTab) ) return 2; 4243619a1305Sdrh if( sqlite3ExprListCompare(pA->x.pList, pB->x.pList, iTab) ) return 2; 42447693c42fSdrh if( ALWAYS((combinedFlags & EP_Reduced)==0) && pA->op!=TK_STRING ){ 4245619a1305Sdrh if( pA->iColumn!=pB->iColumn ) return 2; 424666518ca7Sdrh if( pA->iTable!=pB->iTable 424785f8aa79Sdrh && (pA->iTable!=iTab || NEVER(pB->iTable>=0)) ) return 2; 42481d9da70aSdrh } 42491d9da70aSdrh } 42502646da7eSdrh return 0; 42512646da7eSdrh } 42522282792aSdrh 42538c6f666bSdrh /* 42548c6f666bSdrh ** Compare two ExprList objects. Return 0 if they are identical and 42558c6f666bSdrh ** non-zero if they differ in any way. 42568c6f666bSdrh ** 4257619a1305Sdrh ** If any subelement of pB has Expr.iTable==(-1) then it is allowed 4258619a1305Sdrh ** to compare equal to an equivalent element in pA with Expr.iTable==iTab. 4259619a1305Sdrh ** 42608c6f666bSdrh ** This routine might return non-zero for equivalent ExprLists. The 42618c6f666bSdrh ** only consequence will be disabled optimizations. But this routine 42628c6f666bSdrh ** must never return 0 if the two ExprList objects are different, or 42638c6f666bSdrh ** a malfunction will result. 42648c6f666bSdrh ** 42658c6f666bSdrh ** Two NULL pointers are considered to be the same. But a NULL pointer 42668c6f666bSdrh ** always differs from a non-NULL pointer. 42678c6f666bSdrh */ 4268619a1305Sdrh int sqlite3ExprListCompare(ExprList *pA, ExprList *pB, int iTab){ 42698c6f666bSdrh int i; 42708c6f666bSdrh if( pA==0 && pB==0 ) return 0; 42718c6f666bSdrh if( pA==0 || pB==0 ) return 1; 42728c6f666bSdrh if( pA->nExpr!=pB->nExpr ) return 1; 42738c6f666bSdrh for(i=0; i<pA->nExpr; i++){ 42748c6f666bSdrh Expr *pExprA = pA->a[i].pExpr; 42758c6f666bSdrh Expr *pExprB = pB->a[i].pExpr; 42768c6f666bSdrh if( pA->a[i].sortOrder!=pB->a[i].sortOrder ) return 1; 4277619a1305Sdrh if( sqlite3ExprCompare(pExprA, pExprB, iTab) ) return 1; 42788c6f666bSdrh } 42798c6f666bSdrh return 0; 42808c6f666bSdrh } 428113449892Sdrh 42822282792aSdrh /* 42834bd5f73fSdrh ** Return true if we can prove the pE2 will always be true if pE1 is 42844bd5f73fSdrh ** true. Return false if we cannot complete the proof or if pE2 might 42854bd5f73fSdrh ** be false. Examples: 42864bd5f73fSdrh ** 4287619a1305Sdrh ** pE1: x==5 pE2: x==5 Result: true 42884bd5f73fSdrh ** pE1: x>0 pE2: x==5 Result: false 4289619a1305Sdrh ** pE1: x=21 pE2: x=21 OR y=43 Result: true 42904bd5f73fSdrh ** pE1: x!=123 pE2: x IS NOT NULL Result: true 4291619a1305Sdrh ** pE1: x!=?1 pE2: x IS NOT NULL Result: true 4292619a1305Sdrh ** pE1: x IS NULL pE2: x IS NOT NULL Result: false 4293619a1305Sdrh ** pE1: x IS ?2 pE2: x IS NOT NULL Reuslt: false 42944bd5f73fSdrh ** 42954bd5f73fSdrh ** When comparing TK_COLUMN nodes between pE1 and pE2, if pE2 has 42964bd5f73fSdrh ** Expr.iTable<0 then assume a table number given by iTab. 42974bd5f73fSdrh ** 42984bd5f73fSdrh ** When in doubt, return false. Returning true might give a performance 42994bd5f73fSdrh ** improvement. Returning false might cause a performance reduction, but 43004bd5f73fSdrh ** it will always give the correct answer and is hence always safe. 43014bd5f73fSdrh */ 43024bd5f73fSdrh int sqlite3ExprImpliesExpr(Expr *pE1, Expr *pE2, int iTab){ 4303619a1305Sdrh if( sqlite3ExprCompare(pE1, pE2, iTab)==0 ){ 4304619a1305Sdrh return 1; 4305619a1305Sdrh } 4306619a1305Sdrh if( pE2->op==TK_OR 4307619a1305Sdrh && (sqlite3ExprImpliesExpr(pE1, pE2->pLeft, iTab) 4308619a1305Sdrh || sqlite3ExprImpliesExpr(pE1, pE2->pRight, iTab) ) 4309619a1305Sdrh ){ 4310619a1305Sdrh return 1; 4311619a1305Sdrh } 4312619a1305Sdrh if( pE2->op==TK_NOTNULL 4313619a1305Sdrh && sqlite3ExprCompare(pE1->pLeft, pE2->pLeft, iTab)==0 4314619a1305Sdrh && (pE1->op!=TK_ISNULL && pE1->op!=TK_IS) 4315619a1305Sdrh ){ 4316619a1305Sdrh return 1; 4317619a1305Sdrh } 4318619a1305Sdrh return 0; 43194bd5f73fSdrh } 43204bd5f73fSdrh 43214bd5f73fSdrh /* 4322030796dfSdrh ** An instance of the following structure is used by the tree walker 43232409f8a1Sdrh ** to determine if an expression can be evaluated by reference to the 43242409f8a1Sdrh ** index only, without having to do a search for the corresponding 43252409f8a1Sdrh ** table entry. The IdxCover.pIdx field is the index. IdxCover.iCur 43262409f8a1Sdrh ** is the cursor for the table. 43272409f8a1Sdrh */ 43282409f8a1Sdrh struct IdxCover { 43292409f8a1Sdrh Index *pIdx; /* The index to be tested for coverage */ 43302409f8a1Sdrh int iCur; /* Cursor number for the table corresponding to the index */ 43312409f8a1Sdrh }; 43322409f8a1Sdrh 43332409f8a1Sdrh /* 43342409f8a1Sdrh ** Check to see if there are references to columns in table 43352409f8a1Sdrh ** pWalker->u.pIdxCover->iCur can be satisfied using the index 43362409f8a1Sdrh ** pWalker->u.pIdxCover->pIdx. 43372409f8a1Sdrh */ 43382409f8a1Sdrh static int exprIdxCover(Walker *pWalker, Expr *pExpr){ 43392409f8a1Sdrh if( pExpr->op==TK_COLUMN 43402409f8a1Sdrh && pExpr->iTable==pWalker->u.pIdxCover->iCur 43412409f8a1Sdrh && sqlite3ColumnOfIndex(pWalker->u.pIdxCover->pIdx, pExpr->iColumn)<0 43422409f8a1Sdrh ){ 43432409f8a1Sdrh pWalker->eCode = 1; 43442409f8a1Sdrh return WRC_Abort; 43452409f8a1Sdrh } 43462409f8a1Sdrh return WRC_Continue; 43472409f8a1Sdrh } 43482409f8a1Sdrh 43492409f8a1Sdrh /* 4350e604ec0bSdrh ** Determine if an index pIdx on table with cursor iCur contains will 4351e604ec0bSdrh ** the expression pExpr. Return true if the index does cover the 4352e604ec0bSdrh ** expression and false if the pExpr expression references table columns 4353e604ec0bSdrh ** that are not found in the index pIdx. 43542409f8a1Sdrh ** 43552409f8a1Sdrh ** An index covering an expression means that the expression can be 43562409f8a1Sdrh ** evaluated using only the index and without having to lookup the 43572409f8a1Sdrh ** corresponding table entry. 43582409f8a1Sdrh */ 43592409f8a1Sdrh int sqlite3ExprCoveredByIndex( 43602409f8a1Sdrh Expr *pExpr, /* The index to be tested */ 43612409f8a1Sdrh int iCur, /* The cursor number for the corresponding table */ 43622409f8a1Sdrh Index *pIdx /* The index that might be used for coverage */ 43632409f8a1Sdrh ){ 43642409f8a1Sdrh Walker w; 43652409f8a1Sdrh struct IdxCover xcov; 43662409f8a1Sdrh memset(&w, 0, sizeof(w)); 43672409f8a1Sdrh xcov.iCur = iCur; 43682409f8a1Sdrh xcov.pIdx = pIdx; 43692409f8a1Sdrh w.xExprCallback = exprIdxCover; 43702409f8a1Sdrh w.u.pIdxCover = &xcov; 43712409f8a1Sdrh sqlite3WalkExpr(&w, pExpr); 43722409f8a1Sdrh return !w.eCode; 43732409f8a1Sdrh } 43742409f8a1Sdrh 43752409f8a1Sdrh 43762409f8a1Sdrh /* 43772409f8a1Sdrh ** An instance of the following structure is used by the tree walker 4378030796dfSdrh ** to count references to table columns in the arguments of an 4379ed551b95Sdrh ** aggregate function, in order to implement the 4380ed551b95Sdrh ** sqlite3FunctionThisSrc() routine. 4381374fdce4Sdrh */ 4382030796dfSdrh struct SrcCount { 4383030796dfSdrh SrcList *pSrc; /* One particular FROM clause in a nested query */ 4384030796dfSdrh int nThis; /* Number of references to columns in pSrcList */ 4385030796dfSdrh int nOther; /* Number of references to columns in other FROM clauses */ 4386030796dfSdrh }; 4387030796dfSdrh 4388030796dfSdrh /* 4389030796dfSdrh ** Count the number of references to columns. 4390030796dfSdrh */ 4391030796dfSdrh static int exprSrcCount(Walker *pWalker, Expr *pExpr){ 4392fb0a6081Sdrh /* The NEVER() on the second term is because sqlite3FunctionUsesThisSrc() 4393fb0a6081Sdrh ** is always called before sqlite3ExprAnalyzeAggregates() and so the 4394fb0a6081Sdrh ** TK_COLUMNs have not yet been converted into TK_AGG_COLUMN. If 4395fb0a6081Sdrh ** sqlite3FunctionUsesThisSrc() is used differently in the future, the 4396fb0a6081Sdrh ** NEVER() will need to be removed. */ 4397fb0a6081Sdrh if( pExpr->op==TK_COLUMN || NEVER(pExpr->op==TK_AGG_COLUMN) ){ 4398374fdce4Sdrh int i; 4399030796dfSdrh struct SrcCount *p = pWalker->u.pSrcCount; 4400030796dfSdrh SrcList *pSrc = p->pSrc; 4401655814d2Sdrh int nSrc = pSrc ? pSrc->nSrc : 0; 4402655814d2Sdrh for(i=0; i<nSrc; i++){ 4403030796dfSdrh if( pExpr->iTable==pSrc->a[i].iCursor ) break; 4404374fdce4Sdrh } 4405655814d2Sdrh if( i<nSrc ){ 4406030796dfSdrh p->nThis++; 4407374fdce4Sdrh }else{ 4408030796dfSdrh p->nOther++; 4409374fdce4Sdrh } 4410374fdce4Sdrh } 4411030796dfSdrh return WRC_Continue; 4412030796dfSdrh } 4413374fdce4Sdrh 4414374fdce4Sdrh /* 4415030796dfSdrh ** Determine if any of the arguments to the pExpr Function reference 4416030796dfSdrh ** pSrcList. Return true if they do. Also return true if the function 4417030796dfSdrh ** has no arguments or has only constant arguments. Return false if pExpr 4418030796dfSdrh ** references columns but not columns of tables found in pSrcList. 4419374fdce4Sdrh */ 4420030796dfSdrh int sqlite3FunctionUsesThisSrc(Expr *pExpr, SrcList *pSrcList){ 4421374fdce4Sdrh Walker w; 4422030796dfSdrh struct SrcCount cnt; 4423374fdce4Sdrh assert( pExpr->op==TK_AGG_FUNCTION ); 4424374fdce4Sdrh memset(&w, 0, sizeof(w)); 4425030796dfSdrh w.xExprCallback = exprSrcCount; 4426030796dfSdrh w.u.pSrcCount = &cnt; 4427030796dfSdrh cnt.pSrc = pSrcList; 4428030796dfSdrh cnt.nThis = 0; 4429030796dfSdrh cnt.nOther = 0; 4430030796dfSdrh sqlite3WalkExprList(&w, pExpr->x.pList); 4431030796dfSdrh return cnt.nThis>0 || cnt.nOther==0; 4432374fdce4Sdrh } 4433374fdce4Sdrh 4434374fdce4Sdrh /* 443513449892Sdrh ** Add a new element to the pAggInfo->aCol[] array. Return the index of 443613449892Sdrh ** the new element. Return a negative number if malloc fails. 44372282792aSdrh */ 443817435752Sdrh static int addAggInfoColumn(sqlite3 *db, AggInfo *pInfo){ 443913449892Sdrh int i; 4440cf643729Sdrh pInfo->aCol = sqlite3ArrayAllocate( 444117435752Sdrh db, 4442cf643729Sdrh pInfo->aCol, 4443cf643729Sdrh sizeof(pInfo->aCol[0]), 4444cf643729Sdrh &pInfo->nColumn, 4445cf643729Sdrh &i 4446cf643729Sdrh ); 444713449892Sdrh return i; 44482282792aSdrh } 444913449892Sdrh 445013449892Sdrh /* 445113449892Sdrh ** Add a new element to the pAggInfo->aFunc[] array. Return the index of 445213449892Sdrh ** the new element. Return a negative number if malloc fails. 445313449892Sdrh */ 445417435752Sdrh static int addAggInfoFunc(sqlite3 *db, AggInfo *pInfo){ 445513449892Sdrh int i; 4456cf643729Sdrh pInfo->aFunc = sqlite3ArrayAllocate( 445717435752Sdrh db, 4458cf643729Sdrh pInfo->aFunc, 4459cf643729Sdrh sizeof(pInfo->aFunc[0]), 4460cf643729Sdrh &pInfo->nFunc, 4461cf643729Sdrh &i 4462cf643729Sdrh ); 446313449892Sdrh return i; 44642282792aSdrh } 44652282792aSdrh 44662282792aSdrh /* 44677d10d5a6Sdrh ** This is the xExprCallback for a tree walker. It is used to 44687d10d5a6Sdrh ** implement sqlite3ExprAnalyzeAggregates(). See sqlite3ExprAnalyzeAggregates 4469626a879aSdrh ** for additional information. 44702282792aSdrh */ 44717d10d5a6Sdrh static int analyzeAggregate(Walker *pWalker, Expr *pExpr){ 44722282792aSdrh int i; 44737d10d5a6Sdrh NameContext *pNC = pWalker->u.pNC; 4474a58fdfb1Sdanielk1977 Parse *pParse = pNC->pParse; 4475a58fdfb1Sdanielk1977 SrcList *pSrcList = pNC->pSrcList; 447613449892Sdrh AggInfo *pAggInfo = pNC->pAggInfo; 447713449892Sdrh 44782282792aSdrh switch( pExpr->op ){ 447989c69d00Sdrh case TK_AGG_COLUMN: 4480967e8b73Sdrh case TK_COLUMN: { 44818b213899Sdrh testcase( pExpr->op==TK_AGG_COLUMN ); 44828b213899Sdrh testcase( pExpr->op==TK_COLUMN ); 448313449892Sdrh /* Check to see if the column is in one of the tables in the FROM 448413449892Sdrh ** clause of the aggregate query */ 448520bc393cSdrh if( ALWAYS(pSrcList!=0) ){ 448613449892Sdrh struct SrcList_item *pItem = pSrcList->a; 448713449892Sdrh for(i=0; i<pSrcList->nSrc; i++, pItem++){ 448813449892Sdrh struct AggInfo_col *pCol; 4489c5cd1249Sdrh assert( !ExprHasProperty(pExpr, EP_TokenOnly|EP_Reduced) ); 449013449892Sdrh if( pExpr->iTable==pItem->iCursor ){ 449113449892Sdrh /* If we reach this point, it means that pExpr refers to a table 449213449892Sdrh ** that is in the FROM clause of the aggregate query. 449313449892Sdrh ** 449413449892Sdrh ** Make an entry for the column in pAggInfo->aCol[] if there 449513449892Sdrh ** is not an entry there already. 449613449892Sdrh */ 44977f906d63Sdrh int k; 449813449892Sdrh pCol = pAggInfo->aCol; 44997f906d63Sdrh for(k=0; k<pAggInfo->nColumn; k++, pCol++){ 450013449892Sdrh if( pCol->iTable==pExpr->iTable && 450113449892Sdrh pCol->iColumn==pExpr->iColumn ){ 45022282792aSdrh break; 45032282792aSdrh } 45042282792aSdrh } 45051e536953Sdanielk1977 if( (k>=pAggInfo->nColumn) 45061e536953Sdanielk1977 && (k = addAggInfoColumn(pParse->db, pAggInfo))>=0 45071e536953Sdanielk1977 ){ 45087f906d63Sdrh pCol = &pAggInfo->aCol[k]; 45090817d0dfSdanielk1977 pCol->pTab = pExpr->pTab; 451013449892Sdrh pCol->iTable = pExpr->iTable; 451113449892Sdrh pCol->iColumn = pExpr->iColumn; 45120a07c107Sdrh pCol->iMem = ++pParse->nMem; 451313449892Sdrh pCol->iSorterColumn = -1; 45145774b806Sdrh pCol->pExpr = pExpr; 451513449892Sdrh if( pAggInfo->pGroupBy ){ 451613449892Sdrh int j, n; 451713449892Sdrh ExprList *pGB = pAggInfo->pGroupBy; 451813449892Sdrh struct ExprList_item *pTerm = pGB->a; 451913449892Sdrh n = pGB->nExpr; 452013449892Sdrh for(j=0; j<n; j++, pTerm++){ 452113449892Sdrh Expr *pE = pTerm->pExpr; 452213449892Sdrh if( pE->op==TK_COLUMN && pE->iTable==pExpr->iTable && 452313449892Sdrh pE->iColumn==pExpr->iColumn ){ 452413449892Sdrh pCol->iSorterColumn = j; 452513449892Sdrh break; 45262282792aSdrh } 452713449892Sdrh } 452813449892Sdrh } 452913449892Sdrh if( pCol->iSorterColumn<0 ){ 453013449892Sdrh pCol->iSorterColumn = pAggInfo->nSortingColumn++; 453113449892Sdrh } 453213449892Sdrh } 453313449892Sdrh /* There is now an entry for pExpr in pAggInfo->aCol[] (either 453413449892Sdrh ** because it was there before or because we just created it). 453513449892Sdrh ** Convert the pExpr to be a TK_AGG_COLUMN referring to that 453613449892Sdrh ** pAggInfo->aCol[] entry. 453713449892Sdrh */ 4538ebb6a65dSdrh ExprSetVVAProperty(pExpr, EP_NoReduce); 453913449892Sdrh pExpr->pAggInfo = pAggInfo; 454013449892Sdrh pExpr->op = TK_AGG_COLUMN; 4541cf697396Sshane pExpr->iAgg = (i16)k; 454213449892Sdrh break; 454313449892Sdrh } /* endif pExpr->iTable==pItem->iCursor */ 454413449892Sdrh } /* end loop over pSrcList */ 4545a58fdfb1Sdanielk1977 } 45467d10d5a6Sdrh return WRC_Prune; 45472282792aSdrh } 45482282792aSdrh case TK_AGG_FUNCTION: { 45493a8c4be7Sdrh if( (pNC->ncFlags & NC_InAggFunc)==0 4550ed551b95Sdrh && pWalker->walkerDepth==pExpr->op2 45513a8c4be7Sdrh ){ 455213449892Sdrh /* Check to see if pExpr is a duplicate of another aggregate 455313449892Sdrh ** function that is already in the pAggInfo structure 455413449892Sdrh */ 455513449892Sdrh struct AggInfo_func *pItem = pAggInfo->aFunc; 455613449892Sdrh for(i=0; i<pAggInfo->nFunc; i++, pItem++){ 4557619a1305Sdrh if( sqlite3ExprCompare(pItem->pExpr, pExpr, -1)==0 ){ 45582282792aSdrh break; 45592282792aSdrh } 45602282792aSdrh } 456113449892Sdrh if( i>=pAggInfo->nFunc ){ 456213449892Sdrh /* pExpr is original. Make a new entry in pAggInfo->aFunc[] 456313449892Sdrh */ 456414db2665Sdanielk1977 u8 enc = ENC(pParse->db); 45651e536953Sdanielk1977 i = addAggInfoFunc(pParse->db, pAggInfo); 456613449892Sdrh if( i>=0 ){ 45676ab3a2ecSdanielk1977 assert( !ExprHasProperty(pExpr, EP_xIsSelect) ); 456813449892Sdrh pItem = &pAggInfo->aFunc[i]; 456913449892Sdrh pItem->pExpr = pExpr; 45700a07c107Sdrh pItem->iMem = ++pParse->nMem; 457133e619fcSdrh assert( !ExprHasProperty(pExpr, EP_IntValue) ); 457213449892Sdrh pItem->pFunc = sqlite3FindFunction(pParse->db, 457380738d9cSdrh pExpr->u.zToken, 45746ab3a2ecSdanielk1977 pExpr->x.pList ? pExpr->x.pList->nExpr : 0, enc, 0); 4575fd357974Sdrh if( pExpr->flags & EP_Distinct ){ 4576fd357974Sdrh pItem->iDistinct = pParse->nTab++; 4577fd357974Sdrh }else{ 4578fd357974Sdrh pItem->iDistinct = -1; 4579fd357974Sdrh } 45802282792aSdrh } 458113449892Sdrh } 458213449892Sdrh /* Make pExpr point to the appropriate pAggInfo->aFunc[] entry 458313449892Sdrh */ 4584c5cd1249Sdrh assert( !ExprHasProperty(pExpr, EP_TokenOnly|EP_Reduced) ); 4585ebb6a65dSdrh ExprSetVVAProperty(pExpr, EP_NoReduce); 4586cf697396Sshane pExpr->iAgg = (i16)i; 458713449892Sdrh pExpr->pAggInfo = pAggInfo; 45883a8c4be7Sdrh return WRC_Prune; 45896e83a57fSdrh }else{ 45906e83a57fSdrh return WRC_Continue; 45916e83a57fSdrh } 45922282792aSdrh } 4593a58fdfb1Sdanielk1977 } 45947d10d5a6Sdrh return WRC_Continue; 45957d10d5a6Sdrh } 45967d10d5a6Sdrh static int analyzeAggregatesInSelect(Walker *pWalker, Select *pSelect){ 4597d5a336efSdrh UNUSED_PARAMETER(pWalker); 4598d5a336efSdrh UNUSED_PARAMETER(pSelect); 45997d10d5a6Sdrh return WRC_Continue; 4600a58fdfb1Sdanielk1977 } 4601626a879aSdrh 4602626a879aSdrh /* 4603e8abb4caSdrh ** Analyze the pExpr expression looking for aggregate functions and 4604e8abb4caSdrh ** for variables that need to be added to AggInfo object that pNC->pAggInfo 4605e8abb4caSdrh ** points to. Additional entries are made on the AggInfo object as 4606e8abb4caSdrh ** necessary. 4607626a879aSdrh ** 4608626a879aSdrh ** This routine should only be called after the expression has been 46097d10d5a6Sdrh ** analyzed by sqlite3ResolveExprNames(). 4610626a879aSdrh */ 4611d2b3e23bSdrh void sqlite3ExprAnalyzeAggregates(NameContext *pNC, Expr *pExpr){ 46127d10d5a6Sdrh Walker w; 4613374fdce4Sdrh memset(&w, 0, sizeof(w)); 46147d10d5a6Sdrh w.xExprCallback = analyzeAggregate; 46157d10d5a6Sdrh w.xSelectCallback = analyzeAggregatesInSelect; 46167d10d5a6Sdrh w.u.pNC = pNC; 461720bc393cSdrh assert( pNC->pSrcList!=0 ); 46187d10d5a6Sdrh sqlite3WalkExpr(&w, pExpr); 46192282792aSdrh } 46205d9a4af9Sdrh 46215d9a4af9Sdrh /* 46225d9a4af9Sdrh ** Call sqlite3ExprAnalyzeAggregates() for every expression in an 46235d9a4af9Sdrh ** expression list. Return the number of errors. 46245d9a4af9Sdrh ** 46255d9a4af9Sdrh ** If an error is found, the analysis is cut short. 46265d9a4af9Sdrh */ 4627d2b3e23bSdrh void sqlite3ExprAnalyzeAggList(NameContext *pNC, ExprList *pList){ 46285d9a4af9Sdrh struct ExprList_item *pItem; 46295d9a4af9Sdrh int i; 46305d9a4af9Sdrh if( pList ){ 4631d2b3e23bSdrh for(pItem=pList->a, i=0; i<pList->nExpr; i++, pItem++){ 4632d2b3e23bSdrh sqlite3ExprAnalyzeAggregates(pNC, pItem->pExpr); 46335d9a4af9Sdrh } 46345d9a4af9Sdrh } 46355d9a4af9Sdrh } 4636892d3179Sdrh 4637892d3179Sdrh /* 4638ceea3321Sdrh ** Allocate a single new register for use to hold some intermediate result. 4639892d3179Sdrh */ 4640892d3179Sdrh int sqlite3GetTempReg(Parse *pParse){ 4641e55cbd72Sdrh if( pParse->nTempReg==0 ){ 4642892d3179Sdrh return ++pParse->nMem; 4643892d3179Sdrh } 46442f425f6bSdanielk1977 return pParse->aTempReg[--pParse->nTempReg]; 4645892d3179Sdrh } 4646ceea3321Sdrh 4647ceea3321Sdrh /* 4648ceea3321Sdrh ** Deallocate a register, making available for reuse for some other 4649ceea3321Sdrh ** purpose. 4650ceea3321Sdrh ** 4651ceea3321Sdrh ** If a register is currently being used by the column cache, then 465260ec914cSpeter.d.reid ** the deallocation is deferred until the column cache line that uses 4653ceea3321Sdrh ** the register becomes stale. 4654ceea3321Sdrh */ 4655892d3179Sdrh void sqlite3ReleaseTempReg(Parse *pParse, int iReg){ 46562dcef11bSdrh if( iReg && pParse->nTempReg<ArraySize(pParse->aTempReg) ){ 4657ceea3321Sdrh int i; 4658ceea3321Sdrh struct yColCache *p; 4659ceea3321Sdrh for(i=0, p=pParse->aColCache; i<SQLITE_N_COLCACHE; i++, p++){ 4660ceea3321Sdrh if( p->iReg==iReg ){ 4661ceea3321Sdrh p->tempReg = 1; 4662ceea3321Sdrh return; 4663ceea3321Sdrh } 4664ceea3321Sdrh } 4665892d3179Sdrh pParse->aTempReg[pParse->nTempReg++] = iReg; 4666892d3179Sdrh } 4667892d3179Sdrh } 4668892d3179Sdrh 4669892d3179Sdrh /* 4670892d3179Sdrh ** Allocate or deallocate a block of nReg consecutive registers 4671892d3179Sdrh */ 4672892d3179Sdrh int sqlite3GetTempRange(Parse *pParse, int nReg){ 4673e55cbd72Sdrh int i, n; 4674892d3179Sdrh i = pParse->iRangeReg; 4675e55cbd72Sdrh n = pParse->nRangeReg; 4676f49f3523Sdrh if( nReg<=n ){ 4677f49f3523Sdrh assert( !usedAsColumnCache(pParse, i, i+n-1) ); 4678892d3179Sdrh pParse->iRangeReg += nReg; 4679892d3179Sdrh pParse->nRangeReg -= nReg; 4680892d3179Sdrh }else{ 4681892d3179Sdrh i = pParse->nMem+1; 4682892d3179Sdrh pParse->nMem += nReg; 4683892d3179Sdrh } 4684892d3179Sdrh return i; 4685892d3179Sdrh } 4686892d3179Sdrh void sqlite3ReleaseTempRange(Parse *pParse, int iReg, int nReg){ 4687f49f3523Sdrh sqlite3ExprCacheRemove(pParse, iReg, nReg); 4688892d3179Sdrh if( nReg>pParse->nRangeReg ){ 4689892d3179Sdrh pParse->nRangeReg = nReg; 4690892d3179Sdrh pParse->iRangeReg = iReg; 4691892d3179Sdrh } 4692892d3179Sdrh } 4693cdc69557Sdrh 4694cdc69557Sdrh /* 4695cdc69557Sdrh ** Mark all temporary registers as being unavailable for reuse. 4696cdc69557Sdrh */ 4697cdc69557Sdrh void sqlite3ClearTempRegCache(Parse *pParse){ 4698cdc69557Sdrh pParse->nTempReg = 0; 4699cdc69557Sdrh pParse->nRangeReg = 0; 4700cdc69557Sdrh } 4701bb9b5f26Sdrh 4702bb9b5f26Sdrh /* 4703bb9b5f26Sdrh ** Validate that no temporary register falls within the range of 4704bb9b5f26Sdrh ** iFirst..iLast, inclusive. This routine is only call from within assert() 4705bb9b5f26Sdrh ** statements. 4706bb9b5f26Sdrh */ 4707bb9b5f26Sdrh #ifdef SQLITE_DEBUG 4708bb9b5f26Sdrh int sqlite3NoTempsInRange(Parse *pParse, int iFirst, int iLast){ 4709bb9b5f26Sdrh int i; 4710bb9b5f26Sdrh if( pParse->nRangeReg>0 4711bb9b5f26Sdrh && pParse->iRangeReg+pParse->nRangeReg<iLast 4712bb9b5f26Sdrh && pParse->iRangeReg>=iFirst 4713bb9b5f26Sdrh ){ 4714bb9b5f26Sdrh return 0; 4715bb9b5f26Sdrh } 4716bb9b5f26Sdrh for(i=0; i<pParse->nTempReg; i++){ 4717bb9b5f26Sdrh if( pParse->aTempReg[i]>=iFirst && pParse->aTempReg[i]<=iLast ){ 4718bb9b5f26Sdrh return 0; 4719bb9b5f26Sdrh } 4720bb9b5f26Sdrh } 4721bb9b5f26Sdrh return 1; 4722bb9b5f26Sdrh } 4723bb9b5f26Sdrh #endif /* SQLITE_DEBUG */ 4724