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 342*8da209b1Sdan static int exprVectorSubselect(Parse *pParse, Expr *pExpr){ 343*8da209b1Sdan int reg = 0; 344*8da209b1Sdan if( pExpr->flags & EP_xIsSelect ){ 345*8da209b1Sdan assert( pExpr->op==TK_REGISTER || pExpr->op==TK_SELECT ); 346*8da209b1Sdan if( pExpr->op==TK_REGISTER ){ 347*8da209b1Sdan reg = pExpr->iTable; 348*8da209b1Sdan }else{ 349*8da209b1Sdan reg = sqlite3CodeSubselect(pParse, pExpr, 0, 0); 350*8da209b1Sdan } 351*8da209b1Sdan } 352*8da209b1Sdan return reg; 353*8da209b1Sdan } 354*8da209b1Sdan 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 405*8da209b1Sdan regLeft = exprVectorSubselect(pParse, pLeft); 406*8da209b1Sdan 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 41471c57db0Sdan if( regLeft ){ 41571c57db0Sdan pL = pLeft->x.pSelect->pEList->a[i].pExpr; 41671c57db0Sdan r1 = regLeft+i; 41771c57db0Sdan }else{ 41871c57db0Sdan pL = pLeft->x.pList->a[i].pExpr; 41971c57db0Sdan r1 = sqlite3ExprCodeTemp(pParse, pL, ®Free1); 42071c57db0Sdan } 42171c57db0Sdan 42271c57db0Sdan if( regRight ){ 42371c57db0Sdan pR = pRight->x.pSelect->pEList->a[i].pExpr; 42471c57db0Sdan r2 = regRight+i; 42571c57db0Sdan }else{ 42671c57db0Sdan pR = pRight->x.pList->a[i].pExpr; 42771c57db0Sdan r2 = sqlite3ExprCodeTemp(pParse, pR, ®Free1); 42871c57db0Sdan } 42971c57db0Sdan 43071c57db0Sdan codeCompare(pParse, pL, pR, opCmp, r1, r2, dest, SQLITE_STOREP2 | p5); 43171c57db0Sdan sqlite3VdbeAddOp3(v, opTest, dest, addr, p3); 43271c57db0Sdan sqlite3ReleaseTempReg(pParse, regFree1); 43371c57db0Sdan sqlite3ReleaseTempReg(pParse, regFree2); 43471c57db0Sdan } 43571c57db0Sdan } 43671c57db0Sdan 43771c57db0Sdan sqlite3VdbeResolveLabel(v, addr); 43871c57db0Sdan } 43971c57db0Sdan 4404b5255acSdanielk1977 #if SQLITE_MAX_EXPR_DEPTH>0 4414b5255acSdanielk1977 /* 4424b5255acSdanielk1977 ** Check that argument nHeight is less than or equal to the maximum 4434b5255acSdanielk1977 ** expression depth allowed. If it is not, leave an error message in 4444b5255acSdanielk1977 ** pParse. 4454b5255acSdanielk1977 */ 4467d10d5a6Sdrh int sqlite3ExprCheckHeight(Parse *pParse, int nHeight){ 4474b5255acSdanielk1977 int rc = SQLITE_OK; 4484b5255acSdanielk1977 int mxHeight = pParse->db->aLimit[SQLITE_LIMIT_EXPR_DEPTH]; 4494b5255acSdanielk1977 if( nHeight>mxHeight ){ 4504b5255acSdanielk1977 sqlite3ErrorMsg(pParse, 4514b5255acSdanielk1977 "Expression tree is too large (maximum depth %d)", mxHeight 4524b5255acSdanielk1977 ); 4534b5255acSdanielk1977 rc = SQLITE_ERROR; 4544b5255acSdanielk1977 } 4554b5255acSdanielk1977 return rc; 4564b5255acSdanielk1977 } 4574b5255acSdanielk1977 4584b5255acSdanielk1977 /* The following three functions, heightOfExpr(), heightOfExprList() 4594b5255acSdanielk1977 ** and heightOfSelect(), are used to determine the maximum height 4604b5255acSdanielk1977 ** of any expression tree referenced by the structure passed as the 4614b5255acSdanielk1977 ** first argument. 4624b5255acSdanielk1977 ** 4634b5255acSdanielk1977 ** If this maximum height is greater than the current value pointed 4644b5255acSdanielk1977 ** to by pnHeight, the second parameter, then set *pnHeight to that 4654b5255acSdanielk1977 ** value. 4664b5255acSdanielk1977 */ 4674b5255acSdanielk1977 static void heightOfExpr(Expr *p, int *pnHeight){ 4684b5255acSdanielk1977 if( p ){ 4694b5255acSdanielk1977 if( p->nHeight>*pnHeight ){ 4704b5255acSdanielk1977 *pnHeight = p->nHeight; 4714b5255acSdanielk1977 } 4724b5255acSdanielk1977 } 4734b5255acSdanielk1977 } 4744b5255acSdanielk1977 static void heightOfExprList(ExprList *p, int *pnHeight){ 4754b5255acSdanielk1977 if( p ){ 4764b5255acSdanielk1977 int i; 4774b5255acSdanielk1977 for(i=0; i<p->nExpr; i++){ 4784b5255acSdanielk1977 heightOfExpr(p->a[i].pExpr, pnHeight); 4794b5255acSdanielk1977 } 4804b5255acSdanielk1977 } 4814b5255acSdanielk1977 } 4824b5255acSdanielk1977 static void heightOfSelect(Select *p, int *pnHeight){ 4834b5255acSdanielk1977 if( p ){ 4844b5255acSdanielk1977 heightOfExpr(p->pWhere, pnHeight); 4854b5255acSdanielk1977 heightOfExpr(p->pHaving, pnHeight); 4864b5255acSdanielk1977 heightOfExpr(p->pLimit, pnHeight); 4874b5255acSdanielk1977 heightOfExpr(p->pOffset, pnHeight); 4884b5255acSdanielk1977 heightOfExprList(p->pEList, pnHeight); 4894b5255acSdanielk1977 heightOfExprList(p->pGroupBy, pnHeight); 4904b5255acSdanielk1977 heightOfExprList(p->pOrderBy, pnHeight); 4914b5255acSdanielk1977 heightOfSelect(p->pPrior, pnHeight); 4924b5255acSdanielk1977 } 4934b5255acSdanielk1977 } 4944b5255acSdanielk1977 4954b5255acSdanielk1977 /* 4964b5255acSdanielk1977 ** Set the Expr.nHeight variable in the structure passed as an 4974b5255acSdanielk1977 ** argument. An expression with no children, Expr.pList or 4984b5255acSdanielk1977 ** Expr.pSelect member has a height of 1. Any other expression 4994b5255acSdanielk1977 ** has a height equal to the maximum height of any other 5004b5255acSdanielk1977 ** referenced Expr plus one. 5012308ed38Sdrh ** 5022308ed38Sdrh ** Also propagate EP_Propagate flags up from Expr.x.pList to Expr.flags, 5032308ed38Sdrh ** if appropriate. 5044b5255acSdanielk1977 */ 5054b5255acSdanielk1977 static void exprSetHeight(Expr *p){ 5064b5255acSdanielk1977 int nHeight = 0; 5074b5255acSdanielk1977 heightOfExpr(p->pLeft, &nHeight); 5084b5255acSdanielk1977 heightOfExpr(p->pRight, &nHeight); 5096ab3a2ecSdanielk1977 if( ExprHasProperty(p, EP_xIsSelect) ){ 5106ab3a2ecSdanielk1977 heightOfSelect(p->x.pSelect, &nHeight); 5112308ed38Sdrh }else if( p->x.pList ){ 5126ab3a2ecSdanielk1977 heightOfExprList(p->x.pList, &nHeight); 5132308ed38Sdrh p->flags |= EP_Propagate & sqlite3ExprListFlags(p->x.pList); 5146ab3a2ecSdanielk1977 } 5154b5255acSdanielk1977 p->nHeight = nHeight + 1; 5164b5255acSdanielk1977 } 5174b5255acSdanielk1977 5184b5255acSdanielk1977 /* 5194b5255acSdanielk1977 ** Set the Expr.nHeight variable using the exprSetHeight() function. If 5204b5255acSdanielk1977 ** the height is greater than the maximum allowed expression depth, 5214b5255acSdanielk1977 ** leave an error in pParse. 5222308ed38Sdrh ** 5232308ed38Sdrh ** Also propagate all EP_Propagate flags from the Expr.x.pList into 5242308ed38Sdrh ** Expr.flags. 5254b5255acSdanielk1977 */ 5262308ed38Sdrh void sqlite3ExprSetHeightAndFlags(Parse *pParse, Expr *p){ 52774893a4cSdrh if( pParse->nErr ) return; 5284b5255acSdanielk1977 exprSetHeight(p); 5297d10d5a6Sdrh sqlite3ExprCheckHeight(pParse, p->nHeight); 5304b5255acSdanielk1977 } 5314b5255acSdanielk1977 5324b5255acSdanielk1977 /* 5334b5255acSdanielk1977 ** Return the maximum height of any expression tree referenced 5344b5255acSdanielk1977 ** by the select statement passed as an argument. 5354b5255acSdanielk1977 */ 5364b5255acSdanielk1977 int sqlite3SelectExprHeight(Select *p){ 5374b5255acSdanielk1977 int nHeight = 0; 5384b5255acSdanielk1977 heightOfSelect(p, &nHeight); 5394b5255acSdanielk1977 return nHeight; 5404b5255acSdanielk1977 } 5412308ed38Sdrh #else /* ABOVE: Height enforcement enabled. BELOW: Height enforcement off */ 5422308ed38Sdrh /* 5432308ed38Sdrh ** Propagate all EP_Propagate flags from the Expr.x.pList into 5442308ed38Sdrh ** Expr.flags. 5452308ed38Sdrh */ 5462308ed38Sdrh void sqlite3ExprSetHeightAndFlags(Parse *pParse, Expr *p){ 5472308ed38Sdrh if( p && p->x.pList && !ExprHasProperty(p, EP_xIsSelect) ){ 5482308ed38Sdrh p->flags |= EP_Propagate & sqlite3ExprListFlags(p->x.pList); 5492308ed38Sdrh } 5502308ed38Sdrh } 5514b5255acSdanielk1977 #define exprSetHeight(y) 5524b5255acSdanielk1977 #endif /* SQLITE_MAX_EXPR_DEPTH>0 */ 5534b5255acSdanielk1977 554be5c89acSdrh /* 555b7916a78Sdrh ** This routine is the core allocator for Expr nodes. 556b7916a78Sdrh ** 557a76b5dfcSdrh ** Construct a new expression node and return a pointer to it. Memory 558b7916a78Sdrh ** for this node and for the pToken argument is a single allocation 559b7916a78Sdrh ** obtained from sqlite3DbMalloc(). The calling function 560a76b5dfcSdrh ** is responsible for making sure the node eventually gets freed. 561b7916a78Sdrh ** 562b7916a78Sdrh ** If dequote is true, then the token (if it exists) is dequoted. 563e792b5b4Sdrh ** If dequote is false, no dequoting is performed. The deQuote 564b7916a78Sdrh ** parameter is ignored if pToken is NULL or if the token does not 565b7916a78Sdrh ** appear to be quoted. If the quotes were of the form "..." (double-quotes) 566b7916a78Sdrh ** then the EP_DblQuoted flag is set on the expression node. 56733e619fcSdrh ** 56833e619fcSdrh ** Special case: If op==TK_INTEGER and pToken points to a string that 56933e619fcSdrh ** can be translated into a 32-bit integer, then the token is not 57033e619fcSdrh ** stored in u.zToken. Instead, the integer values is written 57133e619fcSdrh ** into u.iValue and the EP_IntValue flag is set. No extra storage 57233e619fcSdrh ** is allocated to hold the integer text and the dequote flag is ignored. 573a76b5dfcSdrh */ 574b7916a78Sdrh Expr *sqlite3ExprAlloc( 575a1644fd8Sdanielk1977 sqlite3 *db, /* Handle for sqlite3DbMallocZero() (may be null) */ 57617435752Sdrh int op, /* Expression opcode */ 577b7916a78Sdrh const Token *pToken, /* Token argument. Might be NULL */ 578b7916a78Sdrh int dequote /* True to dequote */ 57917435752Sdrh ){ 580a76b5dfcSdrh Expr *pNew; 58133e619fcSdrh int nExtra = 0; 582cf697396Sshane int iValue = 0; 583b7916a78Sdrh 584575fad65Sdrh assert( db!=0 ); 585b7916a78Sdrh if( pToken ){ 58633e619fcSdrh if( op!=TK_INTEGER || pToken->z==0 58733e619fcSdrh || sqlite3GetInt32(pToken->z, &iValue)==0 ){ 588b7916a78Sdrh nExtra = pToken->n+1; 589d50ffc41Sdrh assert( iValue>=0 ); 59033e619fcSdrh } 591a76b5dfcSdrh } 592575fad65Sdrh pNew = sqlite3DbMallocRawNN(db, sizeof(Expr)+nExtra); 593b7916a78Sdrh if( pNew ){ 594ca3862dcSdrh memset(pNew, 0, sizeof(Expr)); 5951bd10f8aSdrh pNew->op = (u8)op; 596a58fdfb1Sdanielk1977 pNew->iAgg = -1; 597a76b5dfcSdrh if( pToken ){ 59833e619fcSdrh if( nExtra==0 ){ 59933e619fcSdrh pNew->flags |= EP_IntValue; 60033e619fcSdrh pNew->u.iValue = iValue; 60133e619fcSdrh }else{ 60233e619fcSdrh pNew->u.zToken = (char*)&pNew[1]; 603b07028f7Sdrh assert( pToken->z!=0 || pToken->n==0 ); 604b07028f7Sdrh if( pToken->n ) memcpy(pNew->u.zToken, pToken->z, pToken->n); 60533e619fcSdrh pNew->u.zToken[pToken->n] = 0; 606244b9d6eSdrh if( dequote && sqlite3Isquote(pNew->u.zToken[0]) ){ 607244b9d6eSdrh if( pNew->u.zToken[0]=='"' ) pNew->flags |= EP_DblQuoted; 60833e619fcSdrh sqlite3Dequote(pNew->u.zToken); 609a34001c9Sdrh } 610a34001c9Sdrh } 61133e619fcSdrh } 612b7916a78Sdrh #if SQLITE_MAX_EXPR_DEPTH>0 613b7916a78Sdrh pNew->nHeight = 1; 614b7916a78Sdrh #endif 615a34001c9Sdrh } 616a76b5dfcSdrh return pNew; 617a76b5dfcSdrh } 618a76b5dfcSdrh 619a76b5dfcSdrh /* 620b7916a78Sdrh ** Allocate a new expression node from a zero-terminated token that has 621b7916a78Sdrh ** already been dequoted. 622b7916a78Sdrh */ 623b7916a78Sdrh Expr *sqlite3Expr( 624b7916a78Sdrh sqlite3 *db, /* Handle for sqlite3DbMallocZero() (may be null) */ 625b7916a78Sdrh int op, /* Expression opcode */ 626b7916a78Sdrh const char *zToken /* Token argument. Might be NULL */ 627b7916a78Sdrh ){ 628b7916a78Sdrh Token x; 629b7916a78Sdrh x.z = zToken; 630b7916a78Sdrh x.n = zToken ? sqlite3Strlen30(zToken) : 0; 631b7916a78Sdrh return sqlite3ExprAlloc(db, op, &x, 0); 632b7916a78Sdrh } 633b7916a78Sdrh 634b7916a78Sdrh /* 635b7916a78Sdrh ** Attach subtrees pLeft and pRight to the Expr node pRoot. 636b7916a78Sdrh ** 637b7916a78Sdrh ** If pRoot==NULL that means that a memory allocation error has occurred. 638b7916a78Sdrh ** In that case, delete the subtrees pLeft and pRight. 639b7916a78Sdrh */ 640b7916a78Sdrh void sqlite3ExprAttachSubtrees( 641b7916a78Sdrh sqlite3 *db, 642b7916a78Sdrh Expr *pRoot, 643b7916a78Sdrh Expr *pLeft, 644b7916a78Sdrh Expr *pRight 645b7916a78Sdrh ){ 646b7916a78Sdrh if( pRoot==0 ){ 647b7916a78Sdrh assert( db->mallocFailed ); 648b7916a78Sdrh sqlite3ExprDelete(db, pLeft); 649b7916a78Sdrh sqlite3ExprDelete(db, pRight); 650b7916a78Sdrh }else{ 651b7916a78Sdrh if( pRight ){ 652b7916a78Sdrh pRoot->pRight = pRight; 653885a5b03Sdrh pRoot->flags |= EP_Propagate & pRight->flags; 654b7916a78Sdrh } 655b7916a78Sdrh if( pLeft ){ 656b7916a78Sdrh pRoot->pLeft = pLeft; 657885a5b03Sdrh pRoot->flags |= EP_Propagate & pLeft->flags; 658b7916a78Sdrh } 659b7916a78Sdrh exprSetHeight(pRoot); 660b7916a78Sdrh } 661b7916a78Sdrh } 662b7916a78Sdrh 663b7916a78Sdrh /* 66460ec914cSpeter.d.reid ** Allocate an Expr node which joins as many as two subtrees. 665b7916a78Sdrh ** 666bf664469Sdrh ** One or both of the subtrees can be NULL. Return a pointer to the new 667bf664469Sdrh ** Expr node. Or, if an OOM error occurs, set pParse->db->mallocFailed, 668bf664469Sdrh ** free the subtrees and return NULL. 669206f3d96Sdrh */ 67017435752Sdrh Expr *sqlite3PExpr( 67117435752Sdrh Parse *pParse, /* Parsing context */ 67217435752Sdrh int op, /* Expression opcode */ 67317435752Sdrh Expr *pLeft, /* Left operand */ 67417435752Sdrh Expr *pRight, /* Right operand */ 67517435752Sdrh const Token *pToken /* Argument token */ 67617435752Sdrh ){ 6775fb52caaSdrh Expr *p; 6781167d327Sdrh if( op==TK_AND && pParse->nErr==0 ){ 6795fb52caaSdrh /* Take advantage of short-circuit false optimization for AND */ 6805fb52caaSdrh p = sqlite3ExprAnd(pParse->db, pLeft, pRight); 6815fb52caaSdrh }else{ 6821167d327Sdrh p = sqlite3ExprAlloc(pParse->db, op & TKFLG_MASK, pToken, 1); 683b7916a78Sdrh sqlite3ExprAttachSubtrees(pParse->db, p, pLeft, pRight); 6845fb52caaSdrh } 6852b359bdbSdan if( p ) { 6862b359bdbSdan sqlite3ExprCheckHeight(pParse, p->nHeight); 6872b359bdbSdan } 6884e0cff60Sdrh return p; 6894e0cff60Sdrh } 6904e0cff60Sdrh 6914e0cff60Sdrh /* 69208de4f79Sdrh ** Add pSelect to the Expr.x.pSelect field. Or, if pExpr is NULL (due 69308de4f79Sdrh ** do a memory allocation failure) then delete the pSelect object. 69408de4f79Sdrh */ 69508de4f79Sdrh void sqlite3PExprAddSelect(Parse *pParse, Expr *pExpr, Select *pSelect){ 69608de4f79Sdrh if( pExpr ){ 69708de4f79Sdrh pExpr->x.pSelect = pSelect; 69808de4f79Sdrh ExprSetProperty(pExpr, EP_xIsSelect|EP_Subquery); 69908de4f79Sdrh sqlite3ExprSetHeightAndFlags(pParse, pExpr); 70008de4f79Sdrh }else{ 70108de4f79Sdrh assert( pParse->db->mallocFailed ); 70208de4f79Sdrh sqlite3SelectDelete(pParse->db, pSelect); 70308de4f79Sdrh } 70408de4f79Sdrh } 70508de4f79Sdrh 70608de4f79Sdrh 70708de4f79Sdrh /* 708991a1985Sdrh ** If the expression is always either TRUE or FALSE (respectively), 709991a1985Sdrh ** then return 1. If one cannot determine the truth value of the 710991a1985Sdrh ** expression at compile-time return 0. 711991a1985Sdrh ** 712991a1985Sdrh ** This is an optimization. If is OK to return 0 here even if 713991a1985Sdrh ** the expression really is always false or false (a false negative). 714991a1985Sdrh ** But it is a bug to return 1 if the expression might have different 715991a1985Sdrh ** boolean values in different circumstances (a false positive.) 7165fb52caaSdrh ** 7175fb52caaSdrh ** Note that if the expression is part of conditional for a 7185fb52caaSdrh ** LEFT JOIN, then we cannot determine at compile-time whether or not 7195fb52caaSdrh ** is it true or false, so always return 0. 7205fb52caaSdrh */ 721991a1985Sdrh static int exprAlwaysTrue(Expr *p){ 722991a1985Sdrh int v = 0; 723991a1985Sdrh if( ExprHasProperty(p, EP_FromJoin) ) return 0; 724991a1985Sdrh if( !sqlite3ExprIsInteger(p, &v) ) return 0; 725991a1985Sdrh return v!=0; 726991a1985Sdrh } 7275fb52caaSdrh static int exprAlwaysFalse(Expr *p){ 7285fb52caaSdrh int v = 0; 7295fb52caaSdrh if( ExprHasProperty(p, EP_FromJoin) ) return 0; 7305fb52caaSdrh if( !sqlite3ExprIsInteger(p, &v) ) return 0; 7315fb52caaSdrh return v==0; 7325fb52caaSdrh } 7335fb52caaSdrh 7345fb52caaSdrh /* 73591bb0eedSdrh ** Join two expressions using an AND operator. If either expression is 73691bb0eedSdrh ** NULL, then just return the other expression. 7375fb52caaSdrh ** 7385fb52caaSdrh ** If one side or the other of the AND is known to be false, then instead 7395fb52caaSdrh ** of returning an AND expression, just return a constant expression with 7405fb52caaSdrh ** a value of false. 74191bb0eedSdrh */ 7421e536953Sdanielk1977 Expr *sqlite3ExprAnd(sqlite3 *db, Expr *pLeft, Expr *pRight){ 74391bb0eedSdrh if( pLeft==0 ){ 74491bb0eedSdrh return pRight; 74591bb0eedSdrh }else if( pRight==0 ){ 74691bb0eedSdrh return pLeft; 7475fb52caaSdrh }else if( exprAlwaysFalse(pLeft) || exprAlwaysFalse(pRight) ){ 7485fb52caaSdrh sqlite3ExprDelete(db, pLeft); 7495fb52caaSdrh sqlite3ExprDelete(db, pRight); 7505fb52caaSdrh return sqlite3ExprAlloc(db, TK_INTEGER, &sqlite3IntTokens[0], 0); 75191bb0eedSdrh }else{ 752b7916a78Sdrh Expr *pNew = sqlite3ExprAlloc(db, TK_AND, 0, 0); 753b7916a78Sdrh sqlite3ExprAttachSubtrees(db, pNew, pLeft, pRight); 754b7916a78Sdrh return pNew; 755a76b5dfcSdrh } 756a76b5dfcSdrh } 757a76b5dfcSdrh 758a76b5dfcSdrh /* 759a76b5dfcSdrh ** Construct a new expression node for a function with multiple 760a76b5dfcSdrh ** arguments. 761a76b5dfcSdrh */ 76217435752Sdrh Expr *sqlite3ExprFunction(Parse *pParse, ExprList *pList, Token *pToken){ 763a76b5dfcSdrh Expr *pNew; 764633e6d57Sdrh sqlite3 *db = pParse->db; 7654b202ae2Sdanielk1977 assert( pToken ); 766b7916a78Sdrh pNew = sqlite3ExprAlloc(db, TK_FUNCTION, pToken, 1); 767a76b5dfcSdrh if( pNew==0 ){ 768d9da78a2Sdrh sqlite3ExprListDelete(db, pList); /* Avoid memory leak when malloc fails */ 769a76b5dfcSdrh return 0; 770a76b5dfcSdrh } 7716ab3a2ecSdanielk1977 pNew->x.pList = pList; 7726ab3a2ecSdanielk1977 assert( !ExprHasProperty(pNew, EP_xIsSelect) ); 7732308ed38Sdrh sqlite3ExprSetHeightAndFlags(pParse, pNew); 774a76b5dfcSdrh return pNew; 775a76b5dfcSdrh } 776a76b5dfcSdrh 777a76b5dfcSdrh /* 778fa6bc000Sdrh ** Assign a variable number to an expression that encodes a wildcard 779fa6bc000Sdrh ** in the original SQL statement. 780fa6bc000Sdrh ** 781fa6bc000Sdrh ** Wildcards consisting of a single "?" are assigned the next sequential 782fa6bc000Sdrh ** variable number. 783fa6bc000Sdrh ** 784fa6bc000Sdrh ** Wildcards of the form "?nnn" are assigned the number "nnn". We make 785fa6bc000Sdrh ** sure "nnn" is not too be to avoid a denial of service attack when 786fa6bc000Sdrh ** the SQL statement comes from an external source. 787fa6bc000Sdrh ** 78851f49f17Sdrh ** Wildcards of the form ":aaa", "@aaa", or "$aaa" are assigned the same number 789fa6bc000Sdrh ** as the previous instance of the same wildcard. Or if this is the first 79060ec914cSpeter.d.reid ** instance of the wildcard, the next sequential variable number is 791fa6bc000Sdrh ** assigned. 792fa6bc000Sdrh */ 793fa6bc000Sdrh void sqlite3ExprAssignVarNumber(Parse *pParse, Expr *pExpr){ 79417435752Sdrh sqlite3 *db = pParse->db; 795b7916a78Sdrh const char *z; 79617435752Sdrh 797fa6bc000Sdrh if( pExpr==0 ) return; 798c5cd1249Sdrh assert( !ExprHasProperty(pExpr, EP_IntValue|EP_Reduced|EP_TokenOnly) ); 79933e619fcSdrh z = pExpr->u.zToken; 800b7916a78Sdrh assert( z!=0 ); 801b7916a78Sdrh assert( z[0]!=0 ); 802b7916a78Sdrh if( z[1]==0 ){ 803fa6bc000Sdrh /* Wildcard of the form "?". Assign the next variable number */ 804b7916a78Sdrh assert( z[0]=='?' ); 8058677d308Sdrh pExpr->iColumn = (ynVar)(++pParse->nVar); 806124c0b49Sdrh }else{ 807124c0b49Sdrh ynVar x = 0; 808124c0b49Sdrh u32 n = sqlite3Strlen30(z); 809124c0b49Sdrh if( z[0]=='?' ){ 810fa6bc000Sdrh /* Wildcard of the form "?nnn". Convert "nnn" to an integer and 811fa6bc000Sdrh ** use it as the variable number */ 812c8d735aeSdan i64 i; 813124c0b49Sdrh int bOk = 0==sqlite3Atoi64(&z[1], &i, n-1, SQLITE_UTF8); 814124c0b49Sdrh pExpr->iColumn = x = (ynVar)i; 815c5499befSdrh testcase( i==0 ); 816c5499befSdrh testcase( i==1 ); 817c5499befSdrh testcase( i==db->aLimit[SQLITE_LIMIT_VARIABLE_NUMBER]-1 ); 818c5499befSdrh testcase( i==db->aLimit[SQLITE_LIMIT_VARIABLE_NUMBER] ); 819c8d735aeSdan if( bOk==0 || i<1 || i>db->aLimit[SQLITE_LIMIT_VARIABLE_NUMBER] ){ 820fa6bc000Sdrh sqlite3ErrorMsg(pParse, "variable number must be between ?1 and ?%d", 821bb4957f8Sdrh db->aLimit[SQLITE_LIMIT_VARIABLE_NUMBER]); 822124c0b49Sdrh x = 0; 823fa6bc000Sdrh } 824fa6bc000Sdrh if( i>pParse->nVar ){ 8251df2db7fSshaneh pParse->nVar = (int)i; 826fa6bc000Sdrh } 827fa6bc000Sdrh }else{ 82851f49f17Sdrh /* Wildcards like ":aaa", "$aaa" or "@aaa". Reuse the same variable 829fa6bc000Sdrh ** number as the prior appearance of the same name, or if the name 830fa6bc000Sdrh ** has never appeared before, reuse the same variable number 831fa6bc000Sdrh */ 832124c0b49Sdrh ynVar i; 833124c0b49Sdrh for(i=0; i<pParse->nzVar; i++){ 834503a686eSdrh if( pParse->azVar[i] && strcmp(pParse->azVar[i],z)==0 ){ 835124c0b49Sdrh pExpr->iColumn = x = (ynVar)i+1; 836fa6bc000Sdrh break; 837fa6bc000Sdrh } 838fa6bc000Sdrh } 839124c0b49Sdrh if( x==0 ) x = pExpr->iColumn = (ynVar)(++pParse->nVar); 840fa6bc000Sdrh } 841124c0b49Sdrh if( x>0 ){ 842124c0b49Sdrh if( x>pParse->nzVar ){ 843124c0b49Sdrh char **a; 844124c0b49Sdrh a = sqlite3DbRealloc(db, pParse->azVar, x*sizeof(a[0])); 8454a642b60Sdrh if( a==0 ){ 8464a642b60Sdrh assert( db->mallocFailed ); /* Error reported through mallocFailed */ 8474a642b60Sdrh return; 8484a642b60Sdrh } 849124c0b49Sdrh pParse->azVar = a; 850124c0b49Sdrh memset(&a[pParse->nzVar], 0, (x-pParse->nzVar)*sizeof(a[0])); 851124c0b49Sdrh pParse->nzVar = x; 852124c0b49Sdrh } 853124c0b49Sdrh if( z[0]!='?' || pParse->azVar[x-1]==0 ){ 854124c0b49Sdrh sqlite3DbFree(db, pParse->azVar[x-1]); 855124c0b49Sdrh pParse->azVar[x-1] = sqlite3DbStrNDup(db, z, n); 856fa6bc000Sdrh } 857fa6bc000Sdrh } 858fa6bc000Sdrh } 859bb4957f8Sdrh if( !pParse->nErr && pParse->nVar>db->aLimit[SQLITE_LIMIT_VARIABLE_NUMBER] ){ 860832b2664Sdanielk1977 sqlite3ErrorMsg(pParse, "too many SQL variables"); 861832b2664Sdanielk1977 } 862fa6bc000Sdrh } 863fa6bc000Sdrh 864fa6bc000Sdrh /* 865f6963f99Sdan ** Recursively delete an expression tree. 866a2e00042Sdrh */ 8674f0010b1Sdrh static SQLITE_NOINLINE void sqlite3ExprDeleteNN(sqlite3 *db, Expr *p){ 8684f0010b1Sdrh assert( p!=0 ); 869d50ffc41Sdrh /* Sanity check: Assert that the IntValue is non-negative if it exists */ 870d50ffc41Sdrh assert( !ExprHasProperty(p, EP_IntValue) || p->u.iValue>=0 ); 871c5cd1249Sdrh if( !ExprHasProperty(p, EP_TokenOnly) ){ 872c5cd1249Sdrh /* The Expr.x union is never used at the same time as Expr.pRight */ 873c5cd1249Sdrh assert( p->x.pList==0 || p->pRight==0 ); 87471c57db0Sdan if( p->op!=TK_SELECT_COLUMN ) sqlite3ExprDelete(db, p->pLeft); 875633e6d57Sdrh sqlite3ExprDelete(db, p->pRight); 876c5cd1249Sdrh if( ExprHasProperty(p, EP_MemToken) ) sqlite3DbFree(db, p->u.zToken); 8776ab3a2ecSdanielk1977 if( ExprHasProperty(p, EP_xIsSelect) ){ 8786ab3a2ecSdanielk1977 sqlite3SelectDelete(db, p->x.pSelect); 8796ab3a2ecSdanielk1977 }else{ 8806ab3a2ecSdanielk1977 sqlite3ExprListDelete(db, p->x.pList); 8816ab3a2ecSdanielk1977 } 8826ab3a2ecSdanielk1977 } 88333e619fcSdrh if( !ExprHasProperty(p, EP_Static) ){ 884633e6d57Sdrh sqlite3DbFree(db, p); 885a2e00042Sdrh } 88633e619fcSdrh } 8874f0010b1Sdrh void sqlite3ExprDelete(sqlite3 *db, Expr *p){ 8884f0010b1Sdrh if( p ) sqlite3ExprDeleteNN(db, p); 8894f0010b1Sdrh } 890a2e00042Sdrh 891d2687b77Sdrh /* 8926ab3a2ecSdanielk1977 ** Return the number of bytes allocated for the expression structure 8936ab3a2ecSdanielk1977 ** passed as the first argument. This is always one of EXPR_FULLSIZE, 8946ab3a2ecSdanielk1977 ** EXPR_REDUCEDSIZE or EXPR_TOKENONLYSIZE. 8956ab3a2ecSdanielk1977 */ 8966ab3a2ecSdanielk1977 static int exprStructSize(Expr *p){ 8976ab3a2ecSdanielk1977 if( ExprHasProperty(p, EP_TokenOnly) ) return EXPR_TOKENONLYSIZE; 8986ab3a2ecSdanielk1977 if( ExprHasProperty(p, EP_Reduced) ) return EXPR_REDUCEDSIZE; 8996ab3a2ecSdanielk1977 return EXPR_FULLSIZE; 9006ab3a2ecSdanielk1977 } 9016ab3a2ecSdanielk1977 9026ab3a2ecSdanielk1977 /* 90333e619fcSdrh ** The dupedExpr*Size() routines each return the number of bytes required 90433e619fcSdrh ** to store a copy of an expression or expression tree. They differ in 90533e619fcSdrh ** how much of the tree is measured. 90633e619fcSdrh ** 90733e619fcSdrh ** dupedExprStructSize() Size of only the Expr structure 90833e619fcSdrh ** dupedExprNodeSize() Size of Expr + space for token 90933e619fcSdrh ** dupedExprSize() Expr + token + subtree components 91033e619fcSdrh ** 91133e619fcSdrh *************************************************************************** 91233e619fcSdrh ** 91333e619fcSdrh ** The dupedExprStructSize() function returns two values OR-ed together: 91433e619fcSdrh ** (1) the space required for a copy of the Expr structure only and 91533e619fcSdrh ** (2) the EP_xxx flags that indicate what the structure size should be. 91633e619fcSdrh ** The return values is always one of: 91733e619fcSdrh ** 91833e619fcSdrh ** EXPR_FULLSIZE 91933e619fcSdrh ** EXPR_REDUCEDSIZE | EP_Reduced 92033e619fcSdrh ** EXPR_TOKENONLYSIZE | EP_TokenOnly 92133e619fcSdrh ** 92233e619fcSdrh ** The size of the structure can be found by masking the return value 92333e619fcSdrh ** of this routine with 0xfff. The flags can be found by masking the 92433e619fcSdrh ** return value with EP_Reduced|EP_TokenOnly. 92533e619fcSdrh ** 92633e619fcSdrh ** Note that with flags==EXPRDUP_REDUCE, this routines works on full-size 92733e619fcSdrh ** (unreduced) Expr objects as they or originally constructed by the parser. 92833e619fcSdrh ** During expression analysis, extra information is computed and moved into 92933e619fcSdrh ** later parts of teh Expr object and that extra information might get chopped 93033e619fcSdrh ** off if the expression is reduced. Note also that it does not work to 93160ec914cSpeter.d.reid ** make an EXPRDUP_REDUCE copy of a reduced expression. It is only legal 93233e619fcSdrh ** to reduce a pristine expression tree from the parser. The implementation 93333e619fcSdrh ** of dupedExprStructSize() contain multiple assert() statements that attempt 93433e619fcSdrh ** to enforce this constraint. 9356ab3a2ecSdanielk1977 */ 9366ab3a2ecSdanielk1977 static int dupedExprStructSize(Expr *p, int flags){ 9376ab3a2ecSdanielk1977 int nSize; 93833e619fcSdrh assert( flags==EXPRDUP_REDUCE || flags==0 ); /* Only one flag value allowed */ 939aecd8021Sdrh assert( EXPR_FULLSIZE<=0xfff ); 940aecd8021Sdrh assert( (0xfff & (EP_Reduced|EP_TokenOnly))==0 ); 9413c19469cSdrh if( 0==flags ){ 9426ab3a2ecSdanielk1977 nSize = EXPR_FULLSIZE; 9436ab3a2ecSdanielk1977 }else{ 944c5cd1249Sdrh assert( !ExprHasProperty(p, EP_TokenOnly|EP_Reduced) ); 94533e619fcSdrh assert( !ExprHasProperty(p, EP_FromJoin) ); 946c5cd1249Sdrh assert( !ExprHasProperty(p, EP_MemToken) ); 947ebb6a65dSdrh assert( !ExprHasProperty(p, EP_NoReduce) ); 948aecd8021Sdrh if( p->pLeft || p->x.pList ){ 94933e619fcSdrh nSize = EXPR_REDUCEDSIZE | EP_Reduced; 95033e619fcSdrh }else{ 951aecd8021Sdrh assert( p->pRight==0 ); 95233e619fcSdrh nSize = EXPR_TOKENONLYSIZE | EP_TokenOnly; 95333e619fcSdrh } 9546ab3a2ecSdanielk1977 } 9556ab3a2ecSdanielk1977 return nSize; 9566ab3a2ecSdanielk1977 } 9576ab3a2ecSdanielk1977 9586ab3a2ecSdanielk1977 /* 95933e619fcSdrh ** This function returns the space in bytes required to store the copy 96033e619fcSdrh ** of the Expr structure and a copy of the Expr.u.zToken string (if that 96133e619fcSdrh ** string is defined.) 9626ab3a2ecSdanielk1977 */ 9636ab3a2ecSdanielk1977 static int dupedExprNodeSize(Expr *p, int flags){ 96433e619fcSdrh int nByte = dupedExprStructSize(p, flags) & 0xfff; 96533e619fcSdrh if( !ExprHasProperty(p, EP_IntValue) && p->u.zToken ){ 96633e619fcSdrh nByte += sqlite3Strlen30(p->u.zToken)+1; 9676ab3a2ecSdanielk1977 } 968bc73971dSdanielk1977 return ROUND8(nByte); 9696ab3a2ecSdanielk1977 } 9706ab3a2ecSdanielk1977 9716ab3a2ecSdanielk1977 /* 9726ab3a2ecSdanielk1977 ** Return the number of bytes required to create a duplicate of the 9736ab3a2ecSdanielk1977 ** expression passed as the first argument. The second argument is a 9746ab3a2ecSdanielk1977 ** mask containing EXPRDUP_XXX flags. 9756ab3a2ecSdanielk1977 ** 9766ab3a2ecSdanielk1977 ** The value returned includes space to create a copy of the Expr struct 97733e619fcSdrh ** itself and the buffer referred to by Expr.u.zToken, if any. 9786ab3a2ecSdanielk1977 ** 9796ab3a2ecSdanielk1977 ** If the EXPRDUP_REDUCE flag is set, then the return value includes 9806ab3a2ecSdanielk1977 ** space to duplicate all Expr nodes in the tree formed by Expr.pLeft 9816ab3a2ecSdanielk1977 ** and Expr.pRight variables (but not for any structures pointed to or 9826ab3a2ecSdanielk1977 ** descended from the Expr.x.pList or Expr.x.pSelect variables). 9836ab3a2ecSdanielk1977 */ 9846ab3a2ecSdanielk1977 static int dupedExprSize(Expr *p, int flags){ 9856ab3a2ecSdanielk1977 int nByte = 0; 9866ab3a2ecSdanielk1977 if( p ){ 9876ab3a2ecSdanielk1977 nByte = dupedExprNodeSize(p, flags); 9886ab3a2ecSdanielk1977 if( flags&EXPRDUP_REDUCE ){ 989b7916a78Sdrh nByte += dupedExprSize(p->pLeft, flags) + dupedExprSize(p->pRight, flags); 9906ab3a2ecSdanielk1977 } 9916ab3a2ecSdanielk1977 } 9926ab3a2ecSdanielk1977 return nByte; 9936ab3a2ecSdanielk1977 } 9946ab3a2ecSdanielk1977 9956ab3a2ecSdanielk1977 /* 9966ab3a2ecSdanielk1977 ** This function is similar to sqlite3ExprDup(), except that if pzBuffer 9976ab3a2ecSdanielk1977 ** is not NULL then *pzBuffer is assumed to point to a buffer large enough 99833e619fcSdrh ** to store the copy of expression p, the copies of p->u.zToken 9996ab3a2ecSdanielk1977 ** (if applicable), and the copies of the p->pLeft and p->pRight expressions, 100060ec914cSpeter.d.reid ** if any. Before returning, *pzBuffer is set to the first byte past the 10016ab3a2ecSdanielk1977 ** portion of the buffer copied into by this function. 10026ab3a2ecSdanielk1977 */ 10033c19469cSdrh static Expr *exprDup(sqlite3 *db, Expr *p, int dupFlags, u8 **pzBuffer){ 10043c19469cSdrh Expr *pNew; /* Value to return */ 10053c19469cSdrh u8 *zAlloc; /* Memory space from which to build Expr object */ 10063c19469cSdrh u32 staticFlag; /* EP_Static if space not obtained from malloc */ 10076ab3a2ecSdanielk1977 10083c19469cSdrh assert( db!=0 ); 10093c19469cSdrh assert( p ); 10103c19469cSdrh assert( dupFlags==0 || dupFlags==EXPRDUP_REDUCE ); 10113c19469cSdrh assert( pzBuffer==0 || dupFlags==EXPRDUP_REDUCE ); 10126ab3a2ecSdanielk1977 10136ab3a2ecSdanielk1977 /* Figure out where to write the new Expr structure. */ 10146ab3a2ecSdanielk1977 if( pzBuffer ){ 10156ab3a2ecSdanielk1977 zAlloc = *pzBuffer; 101633e619fcSdrh staticFlag = EP_Static; 10176ab3a2ecSdanielk1977 }else{ 10183c19469cSdrh zAlloc = sqlite3DbMallocRawNN(db, dupedExprSize(p, dupFlags)); 10193c19469cSdrh staticFlag = 0; 10206ab3a2ecSdanielk1977 } 10216ab3a2ecSdanielk1977 pNew = (Expr *)zAlloc; 10226ab3a2ecSdanielk1977 10236ab3a2ecSdanielk1977 if( pNew ){ 10246ab3a2ecSdanielk1977 /* Set nNewSize to the size allocated for the structure pointed to 10256ab3a2ecSdanielk1977 ** by pNew. This is either EXPR_FULLSIZE, EXPR_REDUCEDSIZE or 10266ab3a2ecSdanielk1977 ** EXPR_TOKENONLYSIZE. nToken is set to the number of bytes consumed 102733e619fcSdrh ** by the copy of the p->u.zToken string (if any). 10286ab3a2ecSdanielk1977 */ 10293c19469cSdrh const unsigned nStructSize = dupedExprStructSize(p, dupFlags); 103033e619fcSdrh const int nNewSize = nStructSize & 0xfff; 103133e619fcSdrh int nToken; 103233e619fcSdrh if( !ExprHasProperty(p, EP_IntValue) && p->u.zToken ){ 103333e619fcSdrh nToken = sqlite3Strlen30(p->u.zToken) + 1; 103433e619fcSdrh }else{ 103533e619fcSdrh nToken = 0; 103633e619fcSdrh } 10373c19469cSdrh if( dupFlags ){ 10386ab3a2ecSdanielk1977 assert( ExprHasProperty(p, EP_Reduced)==0 ); 10396ab3a2ecSdanielk1977 memcpy(zAlloc, p, nNewSize); 10406ab3a2ecSdanielk1977 }else{ 10413e6a1411Sdan u32 nSize = (u32)exprStructSize(p); 10426ab3a2ecSdanielk1977 memcpy(zAlloc, p, nSize); 104372ea29d7Sdrh if( nSize<EXPR_FULLSIZE ){ 10446ab3a2ecSdanielk1977 memset(&zAlloc[nSize], 0, EXPR_FULLSIZE-nSize); 10456ab3a2ecSdanielk1977 } 104672ea29d7Sdrh } 10476ab3a2ecSdanielk1977 104833e619fcSdrh /* Set the EP_Reduced, EP_TokenOnly, and EP_Static flags appropriately. */ 1049c5cd1249Sdrh pNew->flags &= ~(EP_Reduced|EP_TokenOnly|EP_Static|EP_MemToken); 105033e619fcSdrh pNew->flags |= nStructSize & (EP_Reduced|EP_TokenOnly); 105133e619fcSdrh pNew->flags |= staticFlag; 10526ab3a2ecSdanielk1977 105333e619fcSdrh /* Copy the p->u.zToken string, if any. */ 10546ab3a2ecSdanielk1977 if( nToken ){ 105533e619fcSdrh char *zToken = pNew->u.zToken = (char*)&zAlloc[nNewSize]; 105633e619fcSdrh memcpy(zToken, p->u.zToken, nToken); 10576ab3a2ecSdanielk1977 } 10586ab3a2ecSdanielk1977 10596ab3a2ecSdanielk1977 if( 0==((p->flags|pNew->flags) & EP_TokenOnly) ){ 10606ab3a2ecSdanielk1977 /* Fill in the pNew->x.pSelect or pNew->x.pList member. */ 10616ab3a2ecSdanielk1977 if( ExprHasProperty(p, EP_xIsSelect) ){ 10623c19469cSdrh pNew->x.pSelect = sqlite3SelectDup(db, p->x.pSelect, dupFlags); 10636ab3a2ecSdanielk1977 }else{ 10643c19469cSdrh pNew->x.pList = sqlite3ExprListDup(db, p->x.pList, dupFlags); 10656ab3a2ecSdanielk1977 } 10666ab3a2ecSdanielk1977 } 10676ab3a2ecSdanielk1977 10686ab3a2ecSdanielk1977 /* Fill in pNew->pLeft and pNew->pRight. */ 1069c5cd1249Sdrh if( ExprHasProperty(pNew, EP_Reduced|EP_TokenOnly) ){ 10703c19469cSdrh zAlloc += dupedExprNodeSize(p, dupFlags); 10716ab3a2ecSdanielk1977 if( ExprHasProperty(pNew, EP_Reduced) ){ 10723c19469cSdrh pNew->pLeft = p->pLeft ? 10733c19469cSdrh exprDup(db, p->pLeft, EXPRDUP_REDUCE, &zAlloc) : 0; 10743c19469cSdrh pNew->pRight = p->pRight ? 10753c19469cSdrh exprDup(db, p->pRight, EXPRDUP_REDUCE, &zAlloc) : 0; 10766ab3a2ecSdanielk1977 } 10776ab3a2ecSdanielk1977 if( pzBuffer ){ 10786ab3a2ecSdanielk1977 *pzBuffer = zAlloc; 10796ab3a2ecSdanielk1977 } 1080b7916a78Sdrh }else{ 1081c5cd1249Sdrh if( !ExprHasProperty(p, EP_TokenOnly) ){ 10826ab3a2ecSdanielk1977 pNew->pLeft = sqlite3ExprDup(db, p->pLeft, 0); 10836ab3a2ecSdanielk1977 pNew->pRight = sqlite3ExprDup(db, p->pRight, 0); 10846ab3a2ecSdanielk1977 } 10856ab3a2ecSdanielk1977 } 10866ab3a2ecSdanielk1977 } 10876ab3a2ecSdanielk1977 return pNew; 10886ab3a2ecSdanielk1977 } 10896ab3a2ecSdanielk1977 10906ab3a2ecSdanielk1977 /* 1091bfe31e7fSdan ** Create and return a deep copy of the object passed as the second 1092bfe31e7fSdan ** argument. If an OOM condition is encountered, NULL is returned 1093bfe31e7fSdan ** and the db->mallocFailed flag set. 1094bfe31e7fSdan */ 1095eede6a53Sdan #ifndef SQLITE_OMIT_CTE 1096bfe31e7fSdan static With *withDup(sqlite3 *db, With *p){ 10974e9119d9Sdan With *pRet = 0; 10984e9119d9Sdan if( p ){ 10994e9119d9Sdan int nByte = sizeof(*p) + sizeof(p->a[0]) * (p->nCte-1); 11004e9119d9Sdan pRet = sqlite3DbMallocZero(db, nByte); 11014e9119d9Sdan if( pRet ){ 11024e9119d9Sdan int i; 11034e9119d9Sdan pRet->nCte = p->nCte; 11044e9119d9Sdan for(i=0; i<p->nCte; i++){ 11054e9119d9Sdan pRet->a[i].pSelect = sqlite3SelectDup(db, p->a[i].pSelect, 0); 11064e9119d9Sdan pRet->a[i].pCols = sqlite3ExprListDup(db, p->a[i].pCols, 0); 11074e9119d9Sdan pRet->a[i].zName = sqlite3DbStrDup(db, p->a[i].zName); 11084e9119d9Sdan } 11094e9119d9Sdan } 11104e9119d9Sdan } 11114e9119d9Sdan return pRet; 11124e9119d9Sdan } 1113eede6a53Sdan #else 1114eede6a53Sdan # define withDup(x,y) 0 1115eede6a53Sdan #endif 11164e9119d9Sdan 1117a76b5dfcSdrh /* 1118ff78bd2fSdrh ** The following group of routines make deep copies of expressions, 1119ff78bd2fSdrh ** expression lists, ID lists, and select statements. The copies can 1120ff78bd2fSdrh ** be deleted (by being passed to their respective ...Delete() routines) 1121ff78bd2fSdrh ** without effecting the originals. 1122ff78bd2fSdrh ** 11234adee20fSdanielk1977 ** The expression list, ID, and source lists return by sqlite3ExprListDup(), 11244adee20fSdanielk1977 ** sqlite3IdListDup(), and sqlite3SrcListDup() can not be further expanded 1125ad3cab52Sdrh ** by subsequent calls to sqlite*ListAppend() routines. 1126ff78bd2fSdrh ** 1127ad3cab52Sdrh ** Any tables that the SrcList might point to are not duplicated. 11286ab3a2ecSdanielk1977 ** 1129b7916a78Sdrh ** The flags parameter contains a combination of the EXPRDUP_XXX flags. 11306ab3a2ecSdanielk1977 ** If the EXPRDUP_REDUCE flag is set, then the structure returned is a 11316ab3a2ecSdanielk1977 ** truncated version of the usual Expr structure that will be stored as 11326ab3a2ecSdanielk1977 ** part of the in-memory representation of the database schema. 1133ff78bd2fSdrh */ 11346ab3a2ecSdanielk1977 Expr *sqlite3ExprDup(sqlite3 *db, Expr *p, int flags){ 113572ea29d7Sdrh assert( flags==0 || flags==EXPRDUP_REDUCE ); 11363c19469cSdrh return p ? exprDup(db, p, flags, 0) : 0; 1137ff78bd2fSdrh } 11386ab3a2ecSdanielk1977 ExprList *sqlite3ExprListDup(sqlite3 *db, ExprList *p, int flags){ 1139ff78bd2fSdrh ExprList *pNew; 1140145716b3Sdrh struct ExprList_item *pItem, *pOldItem; 1141ff78bd2fSdrh int i; 1142575fad65Sdrh assert( db!=0 ); 1143ff78bd2fSdrh if( p==0 ) return 0; 1144575fad65Sdrh pNew = sqlite3DbMallocRawNN(db, sizeof(*pNew) ); 1145ff78bd2fSdrh if( pNew==0 ) return 0; 1146d872bb18Sdrh pNew->nExpr = i = p->nExpr; 1147d872bb18Sdrh if( (flags & EXPRDUP_REDUCE)==0 ) for(i=1; i<p->nExpr; i+=i){} 1148575fad65Sdrh pNew->a = pItem = sqlite3DbMallocRawNN(db, i*sizeof(p->a[0]) ); 1149e0048400Sdanielk1977 if( pItem==0 ){ 1150633e6d57Sdrh sqlite3DbFree(db, pNew); 1151e0048400Sdanielk1977 return 0; 1152e0048400Sdanielk1977 } 1153145716b3Sdrh pOldItem = p->a; 1154145716b3Sdrh for(i=0; i<p->nExpr; i++, pItem++, pOldItem++){ 11556ab3a2ecSdanielk1977 Expr *pOldExpr = pOldItem->pExpr; 1156b5526ea6Sdrh pItem->pExpr = sqlite3ExprDup(db, pOldExpr, flags); 115717435752Sdrh pItem->zName = sqlite3DbStrDup(db, pOldItem->zName); 1158b7916a78Sdrh pItem->zSpan = sqlite3DbStrDup(db, pOldItem->zSpan); 1159145716b3Sdrh pItem->sortOrder = pOldItem->sortOrder; 11603e7bc9caSdrh pItem->done = 0; 11612c036cffSdrh pItem->bSpanIsTab = pOldItem->bSpanIsTab; 1162c2acc4e4Sdrh pItem->u = pOldItem->u; 1163ff78bd2fSdrh } 1164ff78bd2fSdrh return pNew; 1165ff78bd2fSdrh } 116693758c8dSdanielk1977 116793758c8dSdanielk1977 /* 116893758c8dSdanielk1977 ** If cursors, triggers, views and subqueries are all omitted from 116993758c8dSdanielk1977 ** the build, then none of the following routines, except for 117093758c8dSdanielk1977 ** sqlite3SelectDup(), can be called. sqlite3SelectDup() is sometimes 117193758c8dSdanielk1977 ** called with a NULL argument. 117293758c8dSdanielk1977 */ 11736a67fe8eSdanielk1977 #if !defined(SQLITE_OMIT_VIEW) || !defined(SQLITE_OMIT_TRIGGER) \ 11746a67fe8eSdanielk1977 || !defined(SQLITE_OMIT_SUBQUERY) 11756ab3a2ecSdanielk1977 SrcList *sqlite3SrcListDup(sqlite3 *db, SrcList *p, int flags){ 1176ad3cab52Sdrh SrcList *pNew; 1177ad3cab52Sdrh int i; 1178113088ecSdrh int nByte; 1179575fad65Sdrh assert( db!=0 ); 1180ad3cab52Sdrh if( p==0 ) return 0; 1181113088ecSdrh nByte = sizeof(*p) + (p->nSrc>0 ? sizeof(p->a[0]) * (p->nSrc-1) : 0); 1182575fad65Sdrh pNew = sqlite3DbMallocRawNN(db, nByte ); 1183ad3cab52Sdrh if( pNew==0 ) return 0; 11844305d103Sdrh pNew->nSrc = pNew->nAlloc = p->nSrc; 1185ad3cab52Sdrh for(i=0; i<p->nSrc; i++){ 11864efc4754Sdrh struct SrcList_item *pNewItem = &pNew->a[i]; 11874efc4754Sdrh struct SrcList_item *pOldItem = &p->a[i]; 1188ed8a3bb1Sdrh Table *pTab; 118941fb5cd1Sdan pNewItem->pSchema = pOldItem->pSchema; 119017435752Sdrh pNewItem->zDatabase = sqlite3DbStrDup(db, pOldItem->zDatabase); 119117435752Sdrh pNewItem->zName = sqlite3DbStrDup(db, pOldItem->zName); 119217435752Sdrh pNewItem->zAlias = sqlite3DbStrDup(db, pOldItem->zAlias); 11938a48b9c0Sdrh pNewItem->fg = pOldItem->fg; 11944efc4754Sdrh pNewItem->iCursor = pOldItem->iCursor; 11955b6a9ed4Sdrh pNewItem->addrFillSub = pOldItem->addrFillSub; 11965b6a9ed4Sdrh pNewItem->regReturn = pOldItem->regReturn; 11978a48b9c0Sdrh if( pNewItem->fg.isIndexedBy ){ 11988a48b9c0Sdrh pNewItem->u1.zIndexedBy = sqlite3DbStrDup(db, pOldItem->u1.zIndexedBy); 11998a48b9c0Sdrh } 12008a48b9c0Sdrh pNewItem->pIBIndex = pOldItem->pIBIndex; 12018a48b9c0Sdrh if( pNewItem->fg.isTabFunc ){ 12028a48b9c0Sdrh pNewItem->u1.pFuncArg = 12038a48b9c0Sdrh sqlite3ExprListDup(db, pOldItem->u1.pFuncArg, flags); 12048a48b9c0Sdrh } 1205ed8a3bb1Sdrh pTab = pNewItem->pTab = pOldItem->pTab; 1206ed8a3bb1Sdrh if( pTab ){ 1207ed8a3bb1Sdrh pTab->nRef++; 1208a1cb183dSdanielk1977 } 12096ab3a2ecSdanielk1977 pNewItem->pSelect = sqlite3SelectDup(db, pOldItem->pSelect, flags); 12106ab3a2ecSdanielk1977 pNewItem->pOn = sqlite3ExprDup(db, pOldItem->pOn, flags); 121117435752Sdrh pNewItem->pUsing = sqlite3IdListDup(db, pOldItem->pUsing); 12126c18b6e0Sdanielk1977 pNewItem->colUsed = pOldItem->colUsed; 1213ad3cab52Sdrh } 1214ad3cab52Sdrh return pNew; 1215ad3cab52Sdrh } 121617435752Sdrh IdList *sqlite3IdListDup(sqlite3 *db, IdList *p){ 1217ff78bd2fSdrh IdList *pNew; 1218ff78bd2fSdrh int i; 1219575fad65Sdrh assert( db!=0 ); 1220ff78bd2fSdrh if( p==0 ) return 0; 1221575fad65Sdrh pNew = sqlite3DbMallocRawNN(db, sizeof(*pNew) ); 1222ff78bd2fSdrh if( pNew==0 ) return 0; 12236c535158Sdrh pNew->nId = p->nId; 1224575fad65Sdrh pNew->a = sqlite3DbMallocRawNN(db, p->nId*sizeof(p->a[0]) ); 1225d5d56523Sdanielk1977 if( pNew->a==0 ){ 1226633e6d57Sdrh sqlite3DbFree(db, pNew); 1227d5d56523Sdanielk1977 return 0; 1228d5d56523Sdanielk1977 } 12296c535158Sdrh /* Note that because the size of the allocation for p->a[] is not 12306c535158Sdrh ** necessarily a power of two, sqlite3IdListAppend() may not be called 12316c535158Sdrh ** on the duplicate created by this function. */ 1232ff78bd2fSdrh for(i=0; i<p->nId; i++){ 12334efc4754Sdrh struct IdList_item *pNewItem = &pNew->a[i]; 12344efc4754Sdrh struct IdList_item *pOldItem = &p->a[i]; 123517435752Sdrh pNewItem->zName = sqlite3DbStrDup(db, pOldItem->zName); 12364efc4754Sdrh pNewItem->idx = pOldItem->idx; 1237ff78bd2fSdrh } 1238ff78bd2fSdrh return pNew; 1239ff78bd2fSdrh } 12406ab3a2ecSdanielk1977 Select *sqlite3SelectDup(sqlite3 *db, Select *p, int flags){ 124123b1b372Sdrh Select *pNew, *pPrior; 1242575fad65Sdrh assert( db!=0 ); 1243ff78bd2fSdrh if( p==0 ) return 0; 1244575fad65Sdrh pNew = sqlite3DbMallocRawNN(db, sizeof(*p) ); 1245ff78bd2fSdrh if( pNew==0 ) return 0; 1246b7916a78Sdrh pNew->pEList = sqlite3ExprListDup(db, p->pEList, flags); 12476ab3a2ecSdanielk1977 pNew->pSrc = sqlite3SrcListDup(db, p->pSrc, flags); 12486ab3a2ecSdanielk1977 pNew->pWhere = sqlite3ExprDup(db, p->pWhere, flags); 12496ab3a2ecSdanielk1977 pNew->pGroupBy = sqlite3ExprListDup(db, p->pGroupBy, flags); 12506ab3a2ecSdanielk1977 pNew->pHaving = sqlite3ExprDup(db, p->pHaving, flags); 12516ab3a2ecSdanielk1977 pNew->pOrderBy = sqlite3ExprListDup(db, p->pOrderBy, flags); 1252ff78bd2fSdrh pNew->op = p->op; 125323b1b372Sdrh pNew->pPrior = pPrior = sqlite3SelectDup(db, p->pPrior, flags); 125423b1b372Sdrh if( pPrior ) pPrior->pNext = pNew; 125523b1b372Sdrh pNew->pNext = 0; 12566ab3a2ecSdanielk1977 pNew->pLimit = sqlite3ExprDup(db, p->pLimit, flags); 12576ab3a2ecSdanielk1977 pNew->pOffset = sqlite3ExprDup(db, p->pOffset, flags); 125892b01d53Sdrh pNew->iLimit = 0; 125992b01d53Sdrh pNew->iOffset = 0; 12607d10d5a6Sdrh pNew->selFlags = p->selFlags & ~SF_UsesEphemeral; 1261b9bb7c18Sdrh pNew->addrOpenEphm[0] = -1; 1262b9bb7c18Sdrh pNew->addrOpenEphm[1] = -1; 1263ec2da854Sdrh pNew->nSelectRow = p->nSelectRow; 12644e9119d9Sdan pNew->pWith = withDup(db, p->pWith); 1265eb9b884cSdrh sqlite3SelectSetName(pNew, p->zSelName); 1266ff78bd2fSdrh return pNew; 1267ff78bd2fSdrh } 126893758c8dSdanielk1977 #else 12696ab3a2ecSdanielk1977 Select *sqlite3SelectDup(sqlite3 *db, Select *p, int flags){ 127093758c8dSdanielk1977 assert( p==0 ); 127193758c8dSdanielk1977 return 0; 127293758c8dSdanielk1977 } 127393758c8dSdanielk1977 #endif 1274ff78bd2fSdrh 1275ff78bd2fSdrh 1276ff78bd2fSdrh /* 1277a76b5dfcSdrh ** Add a new element to the end of an expression list. If pList is 1278a76b5dfcSdrh ** initially NULL, then create a new expression list. 1279b7916a78Sdrh ** 1280b7916a78Sdrh ** If a memory allocation error occurs, the entire list is freed and 1281b7916a78Sdrh ** NULL is returned. If non-NULL is returned, then it is guaranteed 1282b7916a78Sdrh ** that the new entry was successfully appended. 1283a76b5dfcSdrh */ 128417435752Sdrh ExprList *sqlite3ExprListAppend( 128517435752Sdrh Parse *pParse, /* Parsing context */ 128617435752Sdrh ExprList *pList, /* List to which to append. Might be NULL */ 1287b7916a78Sdrh Expr *pExpr /* Expression to be appended. Might be NULL */ 128817435752Sdrh ){ 128917435752Sdrh sqlite3 *db = pParse->db; 1290575fad65Sdrh assert( db!=0 ); 1291a76b5dfcSdrh if( pList==0 ){ 1292575fad65Sdrh pList = sqlite3DbMallocRawNN(db, sizeof(ExprList) ); 1293a76b5dfcSdrh if( pList==0 ){ 1294d5d56523Sdanielk1977 goto no_mem; 1295a76b5dfcSdrh } 1296c263f7c4Sdrh pList->nExpr = 0; 1297575fad65Sdrh pList->a = sqlite3DbMallocRawNN(db, sizeof(pList->a[0])); 1298d872bb18Sdrh if( pList->a==0 ) goto no_mem; 1299d872bb18Sdrh }else if( (pList->nExpr & (pList->nExpr-1))==0 ){ 1300d5d56523Sdanielk1977 struct ExprList_item *a; 1301d872bb18Sdrh assert( pList->nExpr>0 ); 1302d872bb18Sdrh a = sqlite3DbRealloc(db, pList->a, pList->nExpr*2*sizeof(pList->a[0])); 1303d5d56523Sdanielk1977 if( a==0 ){ 1304d5d56523Sdanielk1977 goto no_mem; 1305a76b5dfcSdrh } 1306d5d56523Sdanielk1977 pList->a = a; 1307a76b5dfcSdrh } 13084efc4754Sdrh assert( pList->a!=0 ); 1309b7916a78Sdrh if( 1 ){ 13104efc4754Sdrh struct ExprList_item *pItem = &pList->a[pList->nExpr++]; 13114efc4754Sdrh memset(pItem, 0, sizeof(*pItem)); 1312e94ddc9eSdanielk1977 pItem->pExpr = pExpr; 1313a76b5dfcSdrh } 1314a76b5dfcSdrh return pList; 1315d5d56523Sdanielk1977 1316d5d56523Sdanielk1977 no_mem: 1317d5d56523Sdanielk1977 /* Avoid leaking memory if malloc has failed. */ 1318633e6d57Sdrh sqlite3ExprDelete(db, pExpr); 1319633e6d57Sdrh sqlite3ExprListDelete(db, pList); 1320d5d56523Sdanielk1977 return 0; 1321a76b5dfcSdrh } 1322a76b5dfcSdrh 1323a76b5dfcSdrh /* 1324bc622bc0Sdrh ** Set the sort order for the last element on the given ExprList. 1325bc622bc0Sdrh */ 1326bc622bc0Sdrh void sqlite3ExprListSetSortOrder(ExprList *p, int iSortOrder){ 1327bc622bc0Sdrh if( p==0 ) return; 1328bc622bc0Sdrh assert( SQLITE_SO_UNDEFINED<0 && SQLITE_SO_ASC>=0 && SQLITE_SO_DESC>0 ); 1329bc622bc0Sdrh assert( p->nExpr>0 ); 1330bc622bc0Sdrh if( iSortOrder<0 ){ 1331bc622bc0Sdrh assert( p->a[p->nExpr-1].sortOrder==SQLITE_SO_ASC ); 1332bc622bc0Sdrh return; 1333bc622bc0Sdrh } 1334bc622bc0Sdrh p->a[p->nExpr-1].sortOrder = (u8)iSortOrder; 1335bc622bc0Sdrh } 1336bc622bc0Sdrh 1337bc622bc0Sdrh /* 1338b7916a78Sdrh ** Set the ExprList.a[].zName element of the most recently added item 1339b7916a78Sdrh ** on the expression list. 1340b7916a78Sdrh ** 1341b7916a78Sdrh ** pList might be NULL following an OOM error. But pName should never be 1342b7916a78Sdrh ** NULL. If a memory allocation fails, the pParse->db->mallocFailed flag 1343b7916a78Sdrh ** is set. 1344b7916a78Sdrh */ 1345b7916a78Sdrh void sqlite3ExprListSetName( 1346b7916a78Sdrh Parse *pParse, /* Parsing context */ 1347b7916a78Sdrh ExprList *pList, /* List to which to add the span. */ 1348b7916a78Sdrh Token *pName, /* Name to be added */ 1349b7916a78Sdrh int dequote /* True to cause the name to be dequoted */ 1350b7916a78Sdrh ){ 1351b7916a78Sdrh assert( pList!=0 || pParse->db->mallocFailed!=0 ); 1352b7916a78Sdrh if( pList ){ 1353b7916a78Sdrh struct ExprList_item *pItem; 1354b7916a78Sdrh assert( pList->nExpr>0 ); 1355b7916a78Sdrh pItem = &pList->a[pList->nExpr-1]; 1356b7916a78Sdrh assert( pItem->zName==0 ); 1357b7916a78Sdrh pItem->zName = sqlite3DbStrNDup(pParse->db, pName->z, pName->n); 1358244b9d6eSdrh if( dequote ) sqlite3Dequote(pItem->zName); 1359b7916a78Sdrh } 1360b7916a78Sdrh } 1361b7916a78Sdrh 1362b7916a78Sdrh /* 1363b7916a78Sdrh ** Set the ExprList.a[].zSpan element of the most recently added item 1364b7916a78Sdrh ** on the expression list. 1365b7916a78Sdrh ** 1366b7916a78Sdrh ** pList might be NULL following an OOM error. But pSpan should never be 1367b7916a78Sdrh ** NULL. If a memory allocation fails, the pParse->db->mallocFailed flag 1368b7916a78Sdrh ** is set. 1369b7916a78Sdrh */ 1370b7916a78Sdrh void sqlite3ExprListSetSpan( 1371b7916a78Sdrh Parse *pParse, /* Parsing context */ 1372b7916a78Sdrh ExprList *pList, /* List to which to add the span. */ 1373b7916a78Sdrh ExprSpan *pSpan /* The span to be added */ 1374b7916a78Sdrh ){ 1375b7916a78Sdrh sqlite3 *db = pParse->db; 1376b7916a78Sdrh assert( pList!=0 || db->mallocFailed!=0 ); 1377b7916a78Sdrh if( pList ){ 1378b7916a78Sdrh struct ExprList_item *pItem = &pList->a[pList->nExpr-1]; 1379b7916a78Sdrh assert( pList->nExpr>0 ); 1380b7916a78Sdrh assert( db->mallocFailed || pItem->pExpr==pSpan->pExpr ); 1381b7916a78Sdrh sqlite3DbFree(db, pItem->zSpan); 1382b7916a78Sdrh pItem->zSpan = sqlite3DbStrNDup(db, (char*)pSpan->zStart, 1383cf697396Sshane (int)(pSpan->zEnd - pSpan->zStart)); 1384b7916a78Sdrh } 1385b7916a78Sdrh } 1386b7916a78Sdrh 1387b7916a78Sdrh /* 13887a15a4beSdanielk1977 ** If the expression list pEList contains more than iLimit elements, 13897a15a4beSdanielk1977 ** leave an error message in pParse. 13907a15a4beSdanielk1977 */ 13917a15a4beSdanielk1977 void sqlite3ExprListCheckLength( 13927a15a4beSdanielk1977 Parse *pParse, 13937a15a4beSdanielk1977 ExprList *pEList, 13947a15a4beSdanielk1977 const char *zObject 13957a15a4beSdanielk1977 ){ 1396b1a6c3c1Sdrh int mx = pParse->db->aLimit[SQLITE_LIMIT_COLUMN]; 1397c5499befSdrh testcase( pEList && pEList->nExpr==mx ); 1398c5499befSdrh testcase( pEList && pEList->nExpr==mx+1 ); 1399b1a6c3c1Sdrh if( pEList && pEList->nExpr>mx ){ 14007a15a4beSdanielk1977 sqlite3ErrorMsg(pParse, "too many columns in %s", zObject); 14017a15a4beSdanielk1977 } 14027a15a4beSdanielk1977 } 14037a15a4beSdanielk1977 14047a15a4beSdanielk1977 /* 1405a76b5dfcSdrh ** Delete an entire expression list. 1406a76b5dfcSdrh */ 1407affa855cSdrh static SQLITE_NOINLINE void exprListDeleteNN(sqlite3 *db, ExprList *pList){ 1408a76b5dfcSdrh int i; 1409be5c89acSdrh struct ExprList_item *pItem; 1410d872bb18Sdrh assert( pList->a!=0 || pList->nExpr==0 ); 1411be5c89acSdrh for(pItem=pList->a, i=0; i<pList->nExpr; i++, pItem++){ 1412633e6d57Sdrh sqlite3ExprDelete(db, pItem->pExpr); 1413633e6d57Sdrh sqlite3DbFree(db, pItem->zName); 1414b7916a78Sdrh sqlite3DbFree(db, pItem->zSpan); 1415a76b5dfcSdrh } 1416633e6d57Sdrh sqlite3DbFree(db, pList->a); 1417633e6d57Sdrh sqlite3DbFree(db, pList); 1418a76b5dfcSdrh } 1419affa855cSdrh void sqlite3ExprListDelete(sqlite3 *db, ExprList *pList){ 1420affa855cSdrh if( pList ) exprListDeleteNN(db, pList); 1421affa855cSdrh } 1422a76b5dfcSdrh 1423a76b5dfcSdrh /* 14242308ed38Sdrh ** Return the bitwise-OR of all Expr.flags fields in the given 14252308ed38Sdrh ** ExprList. 1426885a5b03Sdrh */ 14272308ed38Sdrh u32 sqlite3ExprListFlags(const ExprList *pList){ 1428885a5b03Sdrh int i; 14292308ed38Sdrh u32 m = 0; 14302308ed38Sdrh if( pList ){ 1431885a5b03Sdrh for(i=0; i<pList->nExpr; i++){ 1432d0c73053Sdrh Expr *pExpr = pList->a[i].pExpr; 1433de845c2fSdrh assert( pExpr!=0 ); 1434de845c2fSdrh m |= pExpr->flags; 1435885a5b03Sdrh } 14362308ed38Sdrh } 14372308ed38Sdrh return m; 1438885a5b03Sdrh } 1439885a5b03Sdrh 1440885a5b03Sdrh /* 1441059b2d50Sdrh ** These routines are Walker callbacks used to check expressions to 1442059b2d50Sdrh ** see if they are "constant" for some definition of constant. The 1443059b2d50Sdrh ** Walker.eCode value determines the type of "constant" we are looking 1444059b2d50Sdrh ** for. 144573b211abSdrh ** 14467d10d5a6Sdrh ** These callback routines are used to implement the following: 1447626a879aSdrh ** 1448059b2d50Sdrh ** sqlite3ExprIsConstant() pWalker->eCode==1 1449059b2d50Sdrh ** sqlite3ExprIsConstantNotJoin() pWalker->eCode==2 1450fcb9f4f3Sdrh ** sqlite3ExprIsTableConstant() pWalker->eCode==3 1451059b2d50Sdrh ** sqlite3ExprIsConstantOrFunction() pWalker->eCode==4 or 5 145287abf5c0Sdrh ** 1453059b2d50Sdrh ** In all cases, the callbacks set Walker.eCode=0 and abort if the expression 1454059b2d50Sdrh ** is found to not be a constant. 145587abf5c0Sdrh ** 1456feada2dfSdrh ** The sqlite3ExprIsConstantOrFunction() is used for evaluating expressions 1457059b2d50Sdrh ** in a CREATE TABLE statement. The Walker.eCode value is 5 when parsing 1458059b2d50Sdrh ** an existing schema and 4 when processing a new statement. A bound 1459feada2dfSdrh ** parameter raises an error for new statements, but is silently converted 1460feada2dfSdrh ** to NULL for existing schemas. This allows sqlite_master tables that 1461feada2dfSdrh ** contain a bound parameter because they were generated by older versions 1462feada2dfSdrh ** of SQLite to be parsed by newer versions of SQLite without raising a 1463feada2dfSdrh ** malformed schema error. 1464626a879aSdrh */ 14657d10d5a6Sdrh static int exprNodeIsConstant(Walker *pWalker, Expr *pExpr){ 1466626a879aSdrh 1467059b2d50Sdrh /* If pWalker->eCode is 2 then any term of the expression that comes from 1468059b2d50Sdrh ** the ON or USING clauses of a left join disqualifies the expression 14690a168377Sdrh ** from being considered constant. */ 1470059b2d50Sdrh if( pWalker->eCode==2 && ExprHasProperty(pExpr, EP_FromJoin) ){ 1471059b2d50Sdrh pWalker->eCode = 0; 14727d10d5a6Sdrh return WRC_Abort; 14730a168377Sdrh } 14740a168377Sdrh 1475626a879aSdrh switch( pExpr->op ){ 1476eb55bd2fSdrh /* Consider functions to be constant if all their arguments are constant 1477059b2d50Sdrh ** and either pWalker->eCode==4 or 5 or the function has the 1478059b2d50Sdrh ** SQLITE_FUNC_CONST flag. */ 1479eb55bd2fSdrh case TK_FUNCTION: 148063f84573Sdrh if( pWalker->eCode>=4 || ExprHasProperty(pExpr,EP_ConstFunc) ){ 1481b1fba286Sdrh return WRC_Continue; 1482059b2d50Sdrh }else{ 1483059b2d50Sdrh pWalker->eCode = 0; 1484059b2d50Sdrh return WRC_Abort; 1485b1fba286Sdrh } 1486626a879aSdrh case TK_ID: 1487626a879aSdrh case TK_COLUMN: 1488626a879aSdrh case TK_AGG_FUNCTION: 148913449892Sdrh case TK_AGG_COLUMN: 1490c5499befSdrh testcase( pExpr->op==TK_ID ); 1491c5499befSdrh testcase( pExpr->op==TK_COLUMN ); 1492c5499befSdrh testcase( pExpr->op==TK_AGG_FUNCTION ); 1493c5499befSdrh testcase( pExpr->op==TK_AGG_COLUMN ); 1494059b2d50Sdrh if( pWalker->eCode==3 && pExpr->iTable==pWalker->u.iCur ){ 1495059b2d50Sdrh return WRC_Continue; 1496059b2d50Sdrh }else{ 1497059b2d50Sdrh pWalker->eCode = 0; 14987d10d5a6Sdrh return WRC_Abort; 1499059b2d50Sdrh } 1500feada2dfSdrh case TK_VARIABLE: 1501059b2d50Sdrh if( pWalker->eCode==5 ){ 1502feada2dfSdrh /* Silently convert bound parameters that appear inside of CREATE 1503feada2dfSdrh ** statements into a NULL when parsing the CREATE statement text out 1504feada2dfSdrh ** of the sqlite_master table */ 1505feada2dfSdrh pExpr->op = TK_NULL; 1506059b2d50Sdrh }else if( pWalker->eCode==4 ){ 1507feada2dfSdrh /* A bound parameter in a CREATE statement that originates from 1508feada2dfSdrh ** sqlite3_prepare() causes an error */ 1509059b2d50Sdrh pWalker->eCode = 0; 1510feada2dfSdrh return WRC_Abort; 1511feada2dfSdrh } 1512feada2dfSdrh /* Fall through */ 1513626a879aSdrh default: 1514b74b1017Sdrh testcase( pExpr->op==TK_SELECT ); /* selectNodeIsConstant will disallow */ 1515b74b1017Sdrh testcase( pExpr->op==TK_EXISTS ); /* selectNodeIsConstant will disallow */ 15167d10d5a6Sdrh return WRC_Continue; 1517626a879aSdrh } 1518626a879aSdrh } 151962c14b34Sdanielk1977 static int selectNodeIsConstant(Walker *pWalker, Select *NotUsed){ 152062c14b34Sdanielk1977 UNUSED_PARAMETER(NotUsed); 1521059b2d50Sdrh pWalker->eCode = 0; 15227d10d5a6Sdrh return WRC_Abort; 15237d10d5a6Sdrh } 1524059b2d50Sdrh static int exprIsConst(Expr *p, int initFlag, int iCur){ 15257d10d5a6Sdrh Walker w; 1526aa87f9a6Sdrh memset(&w, 0, sizeof(w)); 1527059b2d50Sdrh w.eCode = initFlag; 15287d10d5a6Sdrh w.xExprCallback = exprNodeIsConstant; 15297d10d5a6Sdrh w.xSelectCallback = selectNodeIsConstant; 1530059b2d50Sdrh w.u.iCur = iCur; 15317d10d5a6Sdrh sqlite3WalkExpr(&w, p); 1532059b2d50Sdrh return w.eCode; 15337d10d5a6Sdrh } 1534626a879aSdrh 1535626a879aSdrh /* 1536059b2d50Sdrh ** Walk an expression tree. Return non-zero if the expression is constant 1537eb55bd2fSdrh ** and 0 if it involves variables or function calls. 15382398937bSdrh ** 15392398937bSdrh ** For the purposes of this function, a double-quoted string (ex: "abc") 15402398937bSdrh ** is considered a variable but a single-quoted string (ex: 'abc') is 15412398937bSdrh ** a constant. 1542fef5208cSdrh */ 15434adee20fSdanielk1977 int sqlite3ExprIsConstant(Expr *p){ 1544059b2d50Sdrh return exprIsConst(p, 1, 0); 1545fef5208cSdrh } 1546fef5208cSdrh 1547fef5208cSdrh /* 1548059b2d50Sdrh ** Walk an expression tree. Return non-zero if the expression is constant 15490a168377Sdrh ** that does no originate from the ON or USING clauses of a join. 15500a168377Sdrh ** Return 0 if it involves variables or function calls or terms from 15510a168377Sdrh ** an ON or USING clause. 15520a168377Sdrh */ 15530a168377Sdrh int sqlite3ExprIsConstantNotJoin(Expr *p){ 1554059b2d50Sdrh return exprIsConst(p, 2, 0); 15550a168377Sdrh } 15560a168377Sdrh 15570a168377Sdrh /* 1558fcb9f4f3Sdrh ** Walk an expression tree. Return non-zero if the expression is constant 1559059b2d50Sdrh ** for any single row of the table with cursor iCur. In other words, the 1560059b2d50Sdrh ** expression must not refer to any non-deterministic function nor any 1561059b2d50Sdrh ** table other than iCur. 1562059b2d50Sdrh */ 1563059b2d50Sdrh int sqlite3ExprIsTableConstant(Expr *p, int iCur){ 1564059b2d50Sdrh return exprIsConst(p, 3, iCur); 1565059b2d50Sdrh } 1566059b2d50Sdrh 1567059b2d50Sdrh /* 1568059b2d50Sdrh ** Walk an expression tree. Return non-zero if the expression is constant 1569eb55bd2fSdrh ** or a function call with constant arguments. Return and 0 if there 1570eb55bd2fSdrh ** are any variables. 1571eb55bd2fSdrh ** 1572eb55bd2fSdrh ** For the purposes of this function, a double-quoted string (ex: "abc") 1573eb55bd2fSdrh ** is considered a variable but a single-quoted string (ex: 'abc') is 1574eb55bd2fSdrh ** a constant. 1575eb55bd2fSdrh */ 1576feada2dfSdrh int sqlite3ExprIsConstantOrFunction(Expr *p, u8 isInit){ 1577feada2dfSdrh assert( isInit==0 || isInit==1 ); 1578059b2d50Sdrh return exprIsConst(p, 4+isInit, 0); 1579eb55bd2fSdrh } 1580eb55bd2fSdrh 15815b88bc4bSdrh #ifdef SQLITE_ENABLE_CURSOR_HINTS 15825b88bc4bSdrh /* 15835b88bc4bSdrh ** Walk an expression tree. Return 1 if the expression contains a 15845b88bc4bSdrh ** subquery of some kind. Return 0 if there are no subqueries. 15855b88bc4bSdrh */ 15865b88bc4bSdrh int sqlite3ExprContainsSubquery(Expr *p){ 15875b88bc4bSdrh Walker w; 15885b88bc4bSdrh memset(&w, 0, sizeof(w)); 1589bec2476aSdrh w.eCode = 1; 15905b88bc4bSdrh w.xExprCallback = sqlite3ExprWalkNoop; 15915b88bc4bSdrh w.xSelectCallback = selectNodeIsConstant; 15925b88bc4bSdrh sqlite3WalkExpr(&w, p); 159307194bffSdrh return w.eCode==0; 15945b88bc4bSdrh } 15955b88bc4bSdrh #endif 15965b88bc4bSdrh 1597eb55bd2fSdrh /* 159873b211abSdrh ** If the expression p codes a constant integer that is small enough 1599202b2df7Sdrh ** to fit in a 32-bit integer, return 1 and put the value of the integer 1600202b2df7Sdrh ** in *pValue. If the expression is not an integer or if it is too big 1601202b2df7Sdrh ** to fit in a signed 32-bit integer, return 0 and leave *pValue unchanged. 1602e4de1febSdrh */ 16034adee20fSdanielk1977 int sqlite3ExprIsInteger(Expr *p, int *pValue){ 160492b01d53Sdrh int rc = 0; 1605cd92e84dSdrh 1606cd92e84dSdrh /* If an expression is an integer literal that fits in a signed 32-bit 1607cd92e84dSdrh ** integer, then the EP_IntValue flag will have already been set */ 1608cd92e84dSdrh assert( p->op!=TK_INTEGER || (p->flags & EP_IntValue)!=0 1609cd92e84dSdrh || sqlite3GetInt32(p->u.zToken, &rc)==0 ); 1610cd92e84dSdrh 161192b01d53Sdrh if( p->flags & EP_IntValue ){ 161233e619fcSdrh *pValue = p->u.iValue; 1613e4de1febSdrh return 1; 1614e4de1febSdrh } 161592b01d53Sdrh switch( p->op ){ 16164b59ab5eSdrh case TK_UPLUS: { 161792b01d53Sdrh rc = sqlite3ExprIsInteger(p->pLeft, pValue); 1618f6e369a1Sdrh break; 16194b59ab5eSdrh } 1620e4de1febSdrh case TK_UMINUS: { 1621e4de1febSdrh int v; 16224adee20fSdanielk1977 if( sqlite3ExprIsInteger(p->pLeft, &v) ){ 1623f6418891Smistachkin assert( v!=(-2147483647-1) ); 1624e4de1febSdrh *pValue = -v; 162592b01d53Sdrh rc = 1; 1626e4de1febSdrh } 1627e4de1febSdrh break; 1628e4de1febSdrh } 1629e4de1febSdrh default: break; 1630e4de1febSdrh } 163192b01d53Sdrh return rc; 1632e4de1febSdrh } 1633e4de1febSdrh 1634e4de1febSdrh /* 1635039fc32eSdrh ** Return FALSE if there is no chance that the expression can be NULL. 1636039fc32eSdrh ** 1637039fc32eSdrh ** If the expression might be NULL or if the expression is too complex 1638039fc32eSdrh ** to tell return TRUE. 1639039fc32eSdrh ** 1640039fc32eSdrh ** This routine is used as an optimization, to skip OP_IsNull opcodes 1641039fc32eSdrh ** when we know that a value cannot be NULL. Hence, a false positive 1642039fc32eSdrh ** (returning TRUE when in fact the expression can never be NULL) might 1643039fc32eSdrh ** be a small performance hit but is otherwise harmless. On the other 1644039fc32eSdrh ** hand, a false negative (returning FALSE when the result could be NULL) 1645039fc32eSdrh ** will likely result in an incorrect answer. So when in doubt, return 1646039fc32eSdrh ** TRUE. 1647039fc32eSdrh */ 1648039fc32eSdrh int sqlite3ExprCanBeNull(const Expr *p){ 1649039fc32eSdrh u8 op; 1650cd7f457eSdrh while( p->op==TK_UPLUS || p->op==TK_UMINUS ){ p = p->pLeft; } 1651039fc32eSdrh op = p->op; 1652039fc32eSdrh if( op==TK_REGISTER ) op = p->op2; 1653039fc32eSdrh switch( op ){ 1654039fc32eSdrh case TK_INTEGER: 1655039fc32eSdrh case TK_STRING: 1656039fc32eSdrh case TK_FLOAT: 1657039fc32eSdrh case TK_BLOB: 1658039fc32eSdrh return 0; 16597248a8b2Sdrh case TK_COLUMN: 16607248a8b2Sdrh assert( p->pTab!=0 ); 166172673a24Sdrh return ExprHasProperty(p, EP_CanBeNull) || 166272673a24Sdrh (p->iColumn>=0 && p->pTab->aCol[p->iColumn].notNull==0); 1663039fc32eSdrh default: 1664039fc32eSdrh return 1; 1665039fc32eSdrh } 1666039fc32eSdrh } 1667039fc32eSdrh 1668039fc32eSdrh /* 1669039fc32eSdrh ** Return TRUE if the given expression is a constant which would be 1670039fc32eSdrh ** unchanged by OP_Affinity with the affinity given in the second 1671039fc32eSdrh ** argument. 1672039fc32eSdrh ** 1673039fc32eSdrh ** This routine is used to determine if the OP_Affinity operation 1674039fc32eSdrh ** can be omitted. When in doubt return FALSE. A false negative 1675039fc32eSdrh ** is harmless. A false positive, however, can result in the wrong 1676039fc32eSdrh ** answer. 1677039fc32eSdrh */ 1678039fc32eSdrh int sqlite3ExprNeedsNoAffinityChange(const Expr *p, char aff){ 1679039fc32eSdrh u8 op; 168005883a34Sdrh if( aff==SQLITE_AFF_BLOB ) return 1; 1681cd7f457eSdrh while( p->op==TK_UPLUS || p->op==TK_UMINUS ){ p = p->pLeft; } 1682039fc32eSdrh op = p->op; 1683039fc32eSdrh if( op==TK_REGISTER ) op = p->op2; 1684039fc32eSdrh switch( op ){ 1685039fc32eSdrh case TK_INTEGER: { 1686039fc32eSdrh return aff==SQLITE_AFF_INTEGER || aff==SQLITE_AFF_NUMERIC; 1687039fc32eSdrh } 1688039fc32eSdrh case TK_FLOAT: { 1689039fc32eSdrh return aff==SQLITE_AFF_REAL || aff==SQLITE_AFF_NUMERIC; 1690039fc32eSdrh } 1691039fc32eSdrh case TK_STRING: { 1692039fc32eSdrh return aff==SQLITE_AFF_TEXT; 1693039fc32eSdrh } 1694039fc32eSdrh case TK_BLOB: { 1695039fc32eSdrh return 1; 1696039fc32eSdrh } 16972f2855b6Sdrh case TK_COLUMN: { 169888376ca7Sdrh assert( p->iTable>=0 ); /* p cannot be part of a CHECK constraint */ 169988376ca7Sdrh return p->iColumn<0 17002f2855b6Sdrh && (aff==SQLITE_AFF_INTEGER || aff==SQLITE_AFF_NUMERIC); 17012f2855b6Sdrh } 1702039fc32eSdrh default: { 1703039fc32eSdrh return 0; 1704039fc32eSdrh } 1705039fc32eSdrh } 1706039fc32eSdrh } 1707039fc32eSdrh 1708039fc32eSdrh /* 1709c4a3c779Sdrh ** Return TRUE if the given string is a row-id column name. 1710c4a3c779Sdrh */ 17114adee20fSdanielk1977 int sqlite3IsRowid(const char *z){ 17124adee20fSdanielk1977 if( sqlite3StrICmp(z, "_ROWID_")==0 ) return 1; 17134adee20fSdanielk1977 if( sqlite3StrICmp(z, "ROWID")==0 ) return 1; 17144adee20fSdanielk1977 if( sqlite3StrICmp(z, "OID")==0 ) return 1; 1715c4a3c779Sdrh return 0; 1716c4a3c779Sdrh } 1717c4a3c779Sdrh 17189a96b668Sdanielk1977 /* 171969c355bdSdrh ** pX is the RHS of an IN operator. If pX is a SELECT statement 172069c355bdSdrh ** that can be simplified to a direct table access, then return 172169c355bdSdrh ** a pointer to the SELECT statement. If pX is not a SELECT statement, 172269c355bdSdrh ** or if the SELECT statement needs to be manifested into a transient 172369c355bdSdrh ** table, then return NULL. 1724ba00e30aSdan ** 1725ba00e30aSdan ** If parameter bNullSensitive is 0, then this operation will be 1726ba00e30aSdan ** used in a context in which there is no difference between a result 1727ba00e30aSdan ** of 0 and one of NULL. For example: 1728ba00e30aSdan ** 1729ba00e30aSdan ** ... WHERE (?,?) IN (SELECT ...) 1730ba00e30aSdan ** 1731b287f4b6Sdrh */ 1732b287f4b6Sdrh #ifndef SQLITE_OMIT_SUBQUERY 1733cfbb5e82Sdan static Select *isCandidateForInOpt(Expr *pX, int bNullSensitive){ 173469c355bdSdrh Select *p; 1735b287f4b6Sdrh SrcList *pSrc; 1736b287f4b6Sdrh ExprList *pEList; 1737b287f4b6Sdrh Table *pTab; 1738cfbb5e82Sdan int i; 173969c355bdSdrh if( !ExprHasProperty(pX, EP_xIsSelect) ) return 0; /* Not a subquery */ 174069c355bdSdrh if( ExprHasProperty(pX, EP_VarSelect) ) return 0; /* Correlated subq */ 174169c355bdSdrh p = pX->x.pSelect; 1742b287f4b6Sdrh if( p->pPrior ) return 0; /* Not a compound SELECT */ 17437d10d5a6Sdrh if( p->selFlags & (SF_Distinct|SF_Aggregate) ){ 1744b74b1017Sdrh testcase( (p->selFlags & (SF_Distinct|SF_Aggregate))==SF_Distinct ); 1745b74b1017Sdrh testcase( (p->selFlags & (SF_Distinct|SF_Aggregate))==SF_Aggregate ); 17467d10d5a6Sdrh return 0; /* No DISTINCT keyword and no aggregate functions */ 17477d10d5a6Sdrh } 1748b74b1017Sdrh assert( p->pGroupBy==0 ); /* Has no GROUP BY clause */ 1749b287f4b6Sdrh if( p->pLimit ) return 0; /* Has no LIMIT clause */ 1750b74b1017Sdrh assert( p->pOffset==0 ); /* No LIMIT means no OFFSET */ 1751b287f4b6Sdrh if( p->pWhere ) return 0; /* Has no WHERE clause */ 1752b287f4b6Sdrh pSrc = p->pSrc; 1753d1fa7bcaSdrh assert( pSrc!=0 ); 1754d1fa7bcaSdrh if( pSrc->nSrc!=1 ) return 0; /* Single term in FROM clause */ 1755b74b1017Sdrh if( pSrc->a[0].pSelect ) return 0; /* FROM is not a subquery or view */ 1756b287f4b6Sdrh pTab = pSrc->a[0].pTab; 175769c355bdSdrh assert( pTab!=0 ); 1758b74b1017Sdrh assert( pTab->pSelect==0 ); /* FROM clause is not a view */ 1759b287f4b6Sdrh if( IsVirtual(pTab) ) return 0; /* FROM clause not a virtual table */ 1760b287f4b6Sdrh pEList = p->pEList; 1761cfbb5e82Sdan 1762cfbb5e82Sdan /* All SELECT results must be columns. If the SELECT returns more than 1763cfbb5e82Sdan ** one column and the bNullSensitive flag is set, all returned columns 1764cfbb5e82Sdan ** must be declared NOT NULL. */ 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 if( pEList->nExpr>1 && bNullSensitive ){ 1770cfbb5e82Sdan if( pTab->aCol[pRes->iColumn].notNull==0 ) return 0; 1771cfbb5e82Sdan } 1772cfbb5e82Sdan } 177369c355bdSdrh return p; 1774b287f4b6Sdrh } 1775b287f4b6Sdrh #endif /* SQLITE_OMIT_SUBQUERY */ 1776b287f4b6Sdrh 1777b287f4b6Sdrh /* 17781d8cb21fSdan ** Code an OP_Once instruction and allocate space for its flag. Return the 17791d8cb21fSdan ** address of the new instruction. 17801d8cb21fSdan */ 17811d8cb21fSdan int sqlite3CodeOnce(Parse *pParse){ 17821d8cb21fSdan Vdbe *v = sqlite3GetVdbe(pParse); /* Virtual machine being coded */ 17831d8cb21fSdan return sqlite3VdbeAddOp1(v, OP_Once, pParse->nOnce++); 17841d8cb21fSdan } 17851d8cb21fSdan 17861d8cb21fSdan /* 17874c259e9fSdrh ** Generate code that checks the left-most column of index table iCur to see if 17884c259e9fSdrh ** it contains any NULL entries. Cause the register at regHasNull to be set 17896be515ebSdrh ** to a non-NULL value if iCur contains no NULLs. Cause register regHasNull 17906be515ebSdrh ** to be set to NULL if iCur contains one or more NULL values. 17916be515ebSdrh */ 17926be515ebSdrh static void sqlite3SetHasNullFlag(Vdbe *v, int iCur, int regHasNull){ 1793728e0f91Sdrh int addr1; 17946be515ebSdrh sqlite3VdbeAddOp2(v, OP_Integer, 0, regHasNull); 1795728e0f91Sdrh addr1 = sqlite3VdbeAddOp1(v, OP_Rewind, iCur); VdbeCoverage(v); 17966be515ebSdrh sqlite3VdbeAddOp3(v, OP_Column, iCur, 0, regHasNull); 17976be515ebSdrh sqlite3VdbeChangeP5(v, OPFLAG_TYPEOFARG); 17984c259e9fSdrh VdbeComment((v, "first_entry_in(%d)", iCur)); 1799728e0f91Sdrh sqlite3VdbeJumpHere(v, addr1); 18006be515ebSdrh } 18016be515ebSdrh 1802bb53ecb1Sdrh 1803bb53ecb1Sdrh #ifndef SQLITE_OMIT_SUBQUERY 1804bb53ecb1Sdrh /* 1805bb53ecb1Sdrh ** The argument is an IN operator with a list (not a subquery) on the 1806bb53ecb1Sdrh ** right-hand side. Return TRUE if that list is constant. 1807bb53ecb1Sdrh */ 1808bb53ecb1Sdrh static int sqlite3InRhsIsConstant(Expr *pIn){ 1809bb53ecb1Sdrh Expr *pLHS; 1810bb53ecb1Sdrh int res; 1811bb53ecb1Sdrh assert( !ExprHasProperty(pIn, EP_xIsSelect) ); 1812bb53ecb1Sdrh pLHS = pIn->pLeft; 1813bb53ecb1Sdrh pIn->pLeft = 0; 1814bb53ecb1Sdrh res = sqlite3ExprIsConstant(pIn); 1815bb53ecb1Sdrh pIn->pLeft = pLHS; 1816bb53ecb1Sdrh return res; 1817bb53ecb1Sdrh } 1818bb53ecb1Sdrh #endif 1819bb53ecb1Sdrh 18206be515ebSdrh /* 18219a96b668Sdanielk1977 ** This function is used by the implementation of the IN (...) operator. 1822d4305ca6Sdrh ** The pX parameter is the expression on the RHS of the IN operator, which 1823d4305ca6Sdrh ** might be either a list of expressions or a subquery. 18249a96b668Sdanielk1977 ** 1825d4305ca6Sdrh ** The job of this routine is to find or create a b-tree object that can 1826d4305ca6Sdrh ** be used either to test for membership in the RHS set or to iterate through 1827d4305ca6Sdrh ** all members of the RHS set, skipping duplicates. 1828d4305ca6Sdrh ** 18293a85625dSdrh ** A cursor is opened on the b-tree object that is the RHS of the IN operator 1830d4305ca6Sdrh ** and pX->iTable is set to the index of that cursor. 1831d4305ca6Sdrh ** 1832b74b1017Sdrh ** The returned value of this function indicates the b-tree type, as follows: 18339a96b668Sdanielk1977 ** 18349a96b668Sdanielk1977 ** IN_INDEX_ROWID - The cursor was opened on a database table. 18351ccce449Sdrh ** IN_INDEX_INDEX_ASC - The cursor was opened on an ascending index. 18361ccce449Sdrh ** IN_INDEX_INDEX_DESC - The cursor was opened on a descending index. 18379a96b668Sdanielk1977 ** IN_INDEX_EPH - The cursor was opened on a specially created and 18389a96b668Sdanielk1977 ** populated epheremal table. 1839bb53ecb1Sdrh ** IN_INDEX_NOOP - No cursor was allocated. The IN operator must be 1840bb53ecb1Sdrh ** implemented as a sequence of comparisons. 18419a96b668Sdanielk1977 ** 1842d4305ca6Sdrh ** An existing b-tree might be used if the RHS expression pX is a simple 1843d4305ca6Sdrh ** subquery such as: 18449a96b668Sdanielk1977 ** 18459a96b668Sdanielk1977 ** SELECT <column> FROM <table> 18469a96b668Sdanielk1977 ** 1847d4305ca6Sdrh ** If the RHS of the IN operator is a list or a more complex subquery, then 1848d4305ca6Sdrh ** an ephemeral table might need to be generated from the RHS and then 184960ec914cSpeter.d.reid ** pX->iTable made to point to the ephemeral table instead of an 1850d4305ca6Sdrh ** existing table. 1851d4305ca6Sdrh ** 18523a85625dSdrh ** The inFlags parameter must contain exactly one of the bits 18533a85625dSdrh ** IN_INDEX_MEMBERSHIP or IN_INDEX_LOOP. If inFlags contains 18543a85625dSdrh ** IN_INDEX_MEMBERSHIP, then the generated table will be used for a 18553a85625dSdrh ** fast membership test. When the IN_INDEX_LOOP bit is set, the 18563a85625dSdrh ** IN index will be used to loop over all values of the RHS of the 18573a85625dSdrh ** IN operator. 18583a85625dSdrh ** 18593a85625dSdrh ** When IN_INDEX_LOOP is used (and the b-tree will be used to iterate 18603a85625dSdrh ** through the set members) then the b-tree must not contain duplicates. 18613a85625dSdrh ** An epheremal table must be used unless the selected <column> is guaranteed 18629a96b668Sdanielk1977 ** to be unique - either because it is an INTEGER PRIMARY KEY or it 1863b74b1017Sdrh ** has a UNIQUE constraint or UNIQUE index. 18640cdc022eSdanielk1977 ** 18653a85625dSdrh ** When IN_INDEX_MEMBERSHIP is used (and the b-tree will be used 18663a85625dSdrh ** for fast set membership tests) then an epheremal table must 18670cdc022eSdanielk1977 ** be used unless <column> is an INTEGER PRIMARY KEY or an index can 18680cdc022eSdanielk1977 ** be found with <column> as its left-most column. 18690cdc022eSdanielk1977 ** 1870bb53ecb1Sdrh ** If the IN_INDEX_NOOP_OK and IN_INDEX_MEMBERSHIP are both set and 1871bb53ecb1Sdrh ** if the RHS of the IN operator is a list (not a subquery) then this 1872bb53ecb1Sdrh ** routine might decide that creating an ephemeral b-tree for membership 1873bb53ecb1Sdrh ** testing is too expensive and return IN_INDEX_NOOP. In that case, the 1874bb53ecb1Sdrh ** calling routine should implement the IN operator using a sequence 1875bb53ecb1Sdrh ** of Eq or Ne comparison operations. 1876bb53ecb1Sdrh ** 1877b74b1017Sdrh ** When the b-tree is being used for membership tests, the calling function 18783a85625dSdrh ** might need to know whether or not the RHS side of the IN operator 1879e21a6e1dSdrh ** contains a NULL. If prRhsHasNull is not a NULL pointer and 18803a85625dSdrh ** if there is any chance that the (...) might contain a NULL value at 18810cdc022eSdanielk1977 ** runtime, then a register is allocated and the register number written 1882e21a6e1dSdrh ** to *prRhsHasNull. If there is no chance that the (...) contains a 1883e21a6e1dSdrh ** NULL value, then *prRhsHasNull is left unchanged. 18840cdc022eSdanielk1977 ** 1885e21a6e1dSdrh ** If a register is allocated and its location stored in *prRhsHasNull, then 18866be515ebSdrh ** the value in that register will be NULL if the b-tree contains one or more 18876be515ebSdrh ** NULL values, and it will be some non-NULL value if the b-tree contains no 18886be515ebSdrh ** NULL values. 18899a96b668Sdanielk1977 */ 1890284f4acaSdanielk1977 #ifndef SQLITE_OMIT_SUBQUERY 1891ba00e30aSdan int sqlite3FindInIndex( 1892ba00e30aSdan Parse *pParse, 1893ba00e30aSdan Expr *pX, 1894ba00e30aSdan u32 inFlags, 1895ba00e30aSdan int *prRhsHasNull, 1896ba00e30aSdan int *aiMap 1897ba00e30aSdan ){ 1898b74b1017Sdrh Select *p; /* SELECT to the right of IN operator */ 1899b74b1017Sdrh int eType = 0; /* Type of RHS table. IN_INDEX_* */ 1900b74b1017Sdrh int iTab = pParse->nTab++; /* Cursor of the RHS table */ 19013a85625dSdrh int mustBeUnique; /* True if RHS must be unique */ 1902b8475df8Sdrh Vdbe *v = sqlite3GetVdbe(pParse); /* Virtual machine being coded */ 19039a96b668Sdanielk1977 19041450bc6eSdrh assert( pX->op==TK_IN ); 19053a85625dSdrh mustBeUnique = (inFlags & IN_INDEX_LOOP)!=0; 19061450bc6eSdrh 1907b74b1017Sdrh /* Check to see if an existing table or index can be used to 1908b74b1017Sdrh ** satisfy the query. This is preferable to generating a new 1909b74b1017Sdrh ** ephemeral table. 19109a96b668Sdanielk1977 */ 1911cfbb5e82Sdan if( pParse->nErr==0 && (p = isCandidateForInOpt(pX, prRhsHasNull!=0))!=0 ){ 1912e1fb65a0Sdanielk1977 sqlite3 *db = pParse->db; /* Database connection */ 1913b07028f7Sdrh Table *pTab; /* Table <table>. */ 1914ba00e30aSdan i16 iDb; /* Database idx for pTab */ 1915cfbb5e82Sdan ExprList *pEList = p->pEList; 1916cfbb5e82Sdan int nExpr = pEList->nExpr; 1917e1fb65a0Sdanielk1977 1918b07028f7Sdrh assert( p->pEList!=0 ); /* Because of isCandidateForInOpt(p) */ 1919b07028f7Sdrh assert( p->pEList->a[0].pExpr!=0 ); /* Because of isCandidateForInOpt(p) */ 1920b07028f7Sdrh assert( p->pSrc!=0 ); /* Because of isCandidateForInOpt(p) */ 1921b07028f7Sdrh pTab = p->pSrc->a[0].pTab; 1922b07028f7Sdrh 1923b22f7c83Sdrh /* Code an OP_Transaction and OP_TableLock for <table>. */ 1924e1fb65a0Sdanielk1977 iDb = sqlite3SchemaToIndex(db, pTab->pSchema); 1925e1fb65a0Sdanielk1977 sqlite3CodeVerifySchema(pParse, iDb); 1926e1fb65a0Sdanielk1977 sqlite3TableLock(pParse, iDb, pTab->tnum, 0, pTab->zName); 19279a96b668Sdanielk1977 19289a96b668Sdanielk1977 /* This function is only called from two places. In both cases the vdbe 19299a96b668Sdanielk1977 ** has already been allocated. So assume sqlite3GetVdbe() is always 19309a96b668Sdanielk1977 ** successful here. 19319a96b668Sdanielk1977 */ 19329a96b668Sdanielk1977 assert(v); 1933cfbb5e82Sdan if( nExpr==1 && pEList->a[0].pExpr->iColumn<0 ){ 19347d176105Sdrh int iAddr = sqlite3CodeOnce(pParse); 19357d176105Sdrh VdbeCoverage(v); 19369a96b668Sdanielk1977 19379a96b668Sdanielk1977 sqlite3OpenTable(pParse, iTab, iDb, pTab, OP_OpenRead); 19389a96b668Sdanielk1977 eType = IN_INDEX_ROWID; 19399a96b668Sdanielk1977 19409a96b668Sdanielk1977 sqlite3VdbeJumpHere(v, iAddr); 19419a96b668Sdanielk1977 }else{ 1942e1fb65a0Sdanielk1977 Index *pIdx; /* Iterator variable */ 1943cfbb5e82Sdan int affinity_ok = 1; 1944cfbb5e82Sdan int i; 1945cfbb5e82Sdan 1946cfbb5e82Sdan /* Check that the affinity that will be used to perform each 1947cfbb5e82Sdan ** comparison is the same as the affinity of each column. If 1948cfbb5e82Sdan ** it not, it is not possible to use any index. */ 1949cfbb5e82Sdan for(i=0; i<nExpr && affinity_ok; i++){ 1950cfbb5e82Sdan Expr *pLhs = exprVectorField(pX->pLeft, i); 1951cfbb5e82Sdan int iCol = pEList->a[i].pExpr->iColumn; 1952cfbb5e82Sdan char idxaff = pTab->aCol[iCol].affinity; 1953cfbb5e82Sdan char cmpaff = sqlite3CompareAffinity(pLhs, idxaff); 1954cfbb5e82Sdan switch( cmpaff ){ 1955cfbb5e82Sdan case SQLITE_AFF_BLOB: 1956cfbb5e82Sdan break; 1957cfbb5e82Sdan case SQLITE_AFF_TEXT: 1958cfbb5e82Sdan affinity_ok = (idxaff==SQLITE_AFF_TEXT); 1959cfbb5e82Sdan break; 1960cfbb5e82Sdan default: 1961cfbb5e82Sdan affinity_ok = sqlite3IsNumericAffinity(idxaff); 1962cfbb5e82Sdan } 1963cfbb5e82Sdan } 1964e1fb65a0Sdanielk1977 19659a96b668Sdanielk1977 /* The collation sequence used by the comparison. If an index is to 19669a96b668Sdanielk1977 ** be used in place of a temp-table, it must be ordered according 1967e1fb65a0Sdanielk1977 ** to this collation sequence. */ 19689a96b668Sdanielk1977 19699a96b668Sdanielk1977 for(pIdx=pTab->pIndex; pIdx && eType==0 && affinity_ok; pIdx=pIdx->pNext){ 1970cfbb5e82Sdan if( pIdx->nKeyCol<nExpr ) continue; 1971cfbb5e82Sdan if( mustBeUnique && (pIdx->nKeyCol!=nExpr || !IsUniqueIndex(pIdx)) ){ 1972cfbb5e82Sdan continue; 1973cfbb5e82Sdan } 1974cfbb5e82Sdan 1975cfbb5e82Sdan for(i=0; i<nExpr; i++){ 1976cfbb5e82Sdan Expr *pLhs = exprVectorField(pX->pLeft, i); 1977cfbb5e82Sdan Expr *pRhs = pEList->a[i].pExpr; 1978cfbb5e82Sdan CollSeq *pReq = sqlite3BinaryCompareCollSeq(pParse, pLhs, pRhs); 1979cfbb5e82Sdan int j; 1980cfbb5e82Sdan 1981cfbb5e82Sdan for(j=0; j<nExpr; j++){ 1982cfbb5e82Sdan if( pIdx->aiColumn[j]!=pRhs->iColumn ) continue; 1983cfbb5e82Sdan assert( pIdx->azColl[j] ); 1984cfbb5e82Sdan if( sqlite3StrICmp(pReq->zName, pIdx->azColl[j])!=0 ) continue; 1985cfbb5e82Sdan break; 1986cfbb5e82Sdan } 1987cfbb5e82Sdan if( j==nExpr ) break; 1988ba00e30aSdan if( aiMap ) aiMap[i] = j; 1989cfbb5e82Sdan } 1990cfbb5e82Sdan 1991cfbb5e82Sdan if( i==nExpr ){ 19927d176105Sdrh int iAddr = sqlite3CodeOnce(pParse); VdbeCoverage(v); 19932ec2fb22Sdrh sqlite3VdbeAddOp3(v, OP_OpenRead, iTab, pIdx->tnum, iDb); 19942ec2fb22Sdrh sqlite3VdbeSetP4KeyInfo(pParse, pIdx); 1995207872a4Sdanielk1977 VdbeComment((v, "%s", pIdx->zName)); 19961ccce449Sdrh assert( IN_INDEX_INDEX_DESC == IN_INDEX_INDEX_ASC+1 ); 19971ccce449Sdrh eType = IN_INDEX_INDEX_ASC + pIdx->aSortOrder[0]; 19989a96b668Sdanielk1977 1999cfbb5e82Sdan if( prRhsHasNull && nExpr==1 2000cfbb5e82Sdan && !pTab->aCol[pEList->a[0].pExpr->iColumn].notNull 2001cfbb5e82Sdan ){ 20023480bfdaSdan #ifdef SQLITE_ENABLE_COLUMN_USED_MASK 2003cfbb5e82Sdan i64 mask = (1<<nExpr)-1; 20043480bfdaSdan sqlite3VdbeAddOp4Dup8(v, OP_ColumnsUsed, 2005cfbb5e82Sdan iTab, 0, 0, (u8*)&mask, P4_INT64); 20063480bfdaSdan #endif 2007e21a6e1dSdrh *prRhsHasNull = ++pParse->nMem; 20086be515ebSdrh sqlite3SetHasNullFlag(v, iTab, *prRhsHasNull); 20090cdc022eSdanielk1977 } 2010552fd454Sdrh sqlite3VdbeJumpHere(v, iAddr); 20119a96b668Sdanielk1977 } 20129a96b668Sdanielk1977 } 20139a96b668Sdanielk1977 } 20149a96b668Sdanielk1977 } 20159a96b668Sdanielk1977 2016bb53ecb1Sdrh /* If no preexisting index is available for the IN clause 2017bb53ecb1Sdrh ** and IN_INDEX_NOOP is an allowed reply 2018bb53ecb1Sdrh ** and the RHS of the IN operator is a list, not a subquery 201971c57db0Sdan ** and the RHS is not constant or has two or fewer terms, 202060ec914cSpeter.d.reid ** then it is not worth creating an ephemeral table to evaluate 2021bb53ecb1Sdrh ** the IN operator so return IN_INDEX_NOOP. 2022bb53ecb1Sdrh */ 2023bb53ecb1Sdrh if( eType==0 2024bb53ecb1Sdrh && (inFlags & IN_INDEX_NOOP_OK) 2025bb53ecb1Sdrh && !ExprHasProperty(pX, EP_xIsSelect) 2026bb53ecb1Sdrh && (!sqlite3InRhsIsConstant(pX) || pX->x.pList->nExpr<=2) 2027bb53ecb1Sdrh ){ 2028bb53ecb1Sdrh eType = IN_INDEX_NOOP; 2029bb53ecb1Sdrh } 2030bb53ecb1Sdrh 20319a96b668Sdanielk1977 if( eType==0 ){ 20324387006cSdrh /* Could not find an existing table or index to use as the RHS b-tree. 2033b74b1017Sdrh ** We will have to generate an ephemeral table to do the job. 2034b74b1017Sdrh */ 20358e23daf3Sdrh u32 savedNQueryLoop = pParse->nQueryLoop; 20360cdc022eSdanielk1977 int rMayHaveNull = 0; 203741a05b7bSdanielk1977 eType = IN_INDEX_EPH; 20383a85625dSdrh if( inFlags & IN_INDEX_LOOP ){ 20394a5acf8eSdrh pParse->nQueryLoop = 0; 2040c5cd1249Sdrh if( pX->pLeft->iColumn<0 && !ExprHasProperty(pX, EP_xIsSelect) ){ 204141a05b7bSdanielk1977 eType = IN_INDEX_ROWID; 20420cdc022eSdanielk1977 } 2043e21a6e1dSdrh }else if( prRhsHasNull ){ 2044e21a6e1dSdrh *prRhsHasNull = rMayHaveNull = ++pParse->nMem; 2045cf4d38aaSdrh } 204641a05b7bSdanielk1977 sqlite3CodeSubselect(pParse, pX, rMayHaveNull, eType==IN_INDEX_ROWID); 2047cf4d38aaSdrh pParse->nQueryLoop = savedNQueryLoop; 20489a96b668Sdanielk1977 }else{ 20499a96b668Sdanielk1977 pX->iTable = iTab; 20509a96b668Sdanielk1977 } 2051ba00e30aSdan 2052ba00e30aSdan if( aiMap && eType!=IN_INDEX_INDEX_ASC && eType!=IN_INDEX_INDEX_DESC ){ 2053ba00e30aSdan int i, n; 2054ba00e30aSdan n = sqlite3ExprVectorSize(pX->pLeft); 2055ba00e30aSdan for(i=0; i<n; i++) aiMap[i] = i; 2056ba00e30aSdan } 20579a96b668Sdanielk1977 return eType; 20589a96b668Sdanielk1977 } 2059284f4acaSdanielk1977 #endif 2060626a879aSdrh 206171c57db0Sdan static char *exprINAffinity(Parse *pParse, Expr *pExpr){ 206271c57db0Sdan Expr *pLeft = pExpr->pLeft; 206371c57db0Sdan int nVal = sqlite3ExprVectorSize(pLeft); 206471c57db0Sdan char *zRet; 206571c57db0Sdan 206671c57db0Sdan zRet = sqlite3DbMallocZero(pParse->db, nVal+1); 206771c57db0Sdan if( zRet ){ 206871c57db0Sdan int i; 206971c57db0Sdan for(i=0; i<nVal; i++){ 207071c57db0Sdan Expr *pA; 207171c57db0Sdan char a; 2072*8da209b1Sdan if( nVal==1 && 0 ){ 207371c57db0Sdan pA = pLeft; 207471c57db0Sdan }else{ 207571c57db0Sdan pA = exprVectorField(pLeft, i); 207671c57db0Sdan } 207771c57db0Sdan a = sqlite3ExprAffinity(pA); 207871c57db0Sdan zRet[i] = sqlite3CompareAffinity(pExpr->x.pSelect->pEList->a[i].pExpr, a); 207971c57db0Sdan } 208071c57db0Sdan zRet[nVal] = '\0'; 208171c57db0Sdan } 208271c57db0Sdan return zRet; 208371c57db0Sdan } 208471c57db0Sdan 2085*8da209b1Sdan #ifndef SQLITE_OMIT_SUBQUERY 2086*8da209b1Sdan /* 2087*8da209b1Sdan ** Load the Parse object passed as the first argument with an error 2088*8da209b1Sdan ** message of the form: 2089*8da209b1Sdan ** 2090*8da209b1Sdan ** "sub-select returns N columns - expected M" 2091*8da209b1Sdan */ 2092*8da209b1Sdan void sqlite3SubselectError(Parse *pParse, int nActual, int nExpect){ 2093*8da209b1Sdan const char *zFmt = "sub-select returns %d columns - expected %d"; 2094*8da209b1Sdan sqlite3ErrorMsg(pParse, zFmt, nActual, nExpect); 2095*8da209b1Sdan } 2096*8da209b1Sdan #endif 2097*8da209b1Sdan 2098626a879aSdrh /* 2099d4187c71Sdrh ** Generate code for scalar subqueries used as a subquery expression, EXISTS, 2100d4187c71Sdrh ** or IN operators. Examples: 2101626a879aSdrh ** 21029cbe6352Sdrh ** (SELECT a FROM b) -- subquery 21039cbe6352Sdrh ** EXISTS (SELECT a FROM b) -- EXISTS subquery 21049cbe6352Sdrh ** x IN (4,5,11) -- IN operator with list on right-hand side 21059cbe6352Sdrh ** x IN (SELECT a FROM b) -- IN operator with subquery on the right 2106fef5208cSdrh ** 21079cbe6352Sdrh ** The pExpr parameter describes the expression that contains the IN 21089cbe6352Sdrh ** operator or subquery. 210941a05b7bSdanielk1977 ** 211041a05b7bSdanielk1977 ** If parameter isRowid is non-zero, then expression pExpr is guaranteed 211141a05b7bSdanielk1977 ** to be of the form "<rowid> IN (?, ?, ?)", where <rowid> is a reference 211241a05b7bSdanielk1977 ** to some integer key column of a table B-Tree. In this case, use an 211341a05b7bSdanielk1977 ** intkey B-Tree to store the set of IN(...) values instead of the usual 211441a05b7bSdanielk1977 ** (slower) variable length keys B-Tree. 2115fd773cf9Sdrh ** 2116fd773cf9Sdrh ** If rMayHaveNull is non-zero, that means that the operation is an IN 2117fd773cf9Sdrh ** (not a SELECT or EXISTS) and that the RHS might contains NULLs. 21183a85625dSdrh ** All this routine does is initialize the register given by rMayHaveNull 21193a85625dSdrh ** to NULL. Calling routines will take care of changing this register 21203a85625dSdrh ** value to non-NULL if the RHS is NULL-free. 21211450bc6eSdrh ** 21221450bc6eSdrh ** For a SELECT or EXISTS operator, return the register that holds the 21231450bc6eSdrh ** result. For IN operators or if an error occurs, the return value is 0. 2124cce7d176Sdrh */ 212551522cd3Sdrh #ifndef SQLITE_OMIT_SUBQUERY 21261450bc6eSdrh int sqlite3CodeSubselect( 2127fd773cf9Sdrh Parse *pParse, /* Parsing context */ 2128fd773cf9Sdrh Expr *pExpr, /* The IN, SELECT, or EXISTS operator */ 21296be515ebSdrh int rHasNullFlag, /* Register that records whether NULLs exist in RHS */ 2130fd773cf9Sdrh int isRowid /* If true, LHS of IN operator is a rowid */ 213141a05b7bSdanielk1977 ){ 21326be515ebSdrh int jmpIfDynamic = -1; /* One-time test address */ 21331450bc6eSdrh int rReg = 0; /* Register storing resulting */ 2134b3bce662Sdanielk1977 Vdbe *v = sqlite3GetVdbe(pParse); 21351450bc6eSdrh if( NEVER(v==0) ) return 0; 2136ceea3321Sdrh sqlite3ExprCachePush(pParse); 2137fc976065Sdanielk1977 213857dbd7b3Sdrh /* This code must be run in its entirety every time it is encountered 213957dbd7b3Sdrh ** if any of the following is true: 214057dbd7b3Sdrh ** 214157dbd7b3Sdrh ** * The right-hand side is a correlated subquery 214257dbd7b3Sdrh ** * The right-hand side is an expression list containing variables 214357dbd7b3Sdrh ** * We are inside a trigger 214457dbd7b3Sdrh ** 214557dbd7b3Sdrh ** If all of the above are false, then we can run this code just once 214657dbd7b3Sdrh ** save the results, and reuse the same result on subsequent invocations. 2147b3bce662Sdanielk1977 */ 2148c5cd1249Sdrh if( !ExprHasProperty(pExpr, EP_VarSelect) ){ 21496be515ebSdrh jmpIfDynamic = sqlite3CodeOnce(pParse); VdbeCoverage(v); 2150b3bce662Sdanielk1977 } 2151b3bce662Sdanielk1977 21524a07e3dbSdan #ifndef SQLITE_OMIT_EXPLAIN 21534a07e3dbSdan if( pParse->explain==2 ){ 215462aaa6caSdrh char *zMsg = sqlite3MPrintf(pParse->db, "EXECUTE %s%s SUBQUERY %d", 215562aaa6caSdrh jmpIfDynamic>=0?"":"CORRELATED ", 215662aaa6caSdrh pExpr->op==TK_IN?"LIST":"SCALAR", 215762aaa6caSdrh pParse->iNextSelectId 21584a07e3dbSdan ); 21594a07e3dbSdan sqlite3VdbeAddOp4(v, OP_Explain, pParse->iSelectId, 0, 0, zMsg, P4_DYNAMIC); 21604a07e3dbSdan } 21614a07e3dbSdan #endif 21624a07e3dbSdan 2163cce7d176Sdrh switch( pExpr->op ){ 2164fef5208cSdrh case TK_IN: { 2165b9bb7c18Sdrh int addr; /* Address of OP_OpenEphemeral instruction */ 2166d4187c71Sdrh Expr *pLeft = pExpr->pLeft; /* the LHS of the IN operator */ 2167323df790Sdrh KeyInfo *pKeyInfo = 0; /* Key information */ 216871c57db0Sdan int nVal; /* Size of vector pLeft */ 2169d3d39e93Sdrh 217071c57db0Sdan nVal = sqlite3ExprVectorSize(pLeft); 2171e014a838Sdanielk1977 2172e014a838Sdanielk1977 /* Whether this is an 'x IN(SELECT...)' or an 'x IN(<exprlist>)' 21738cff69dfSdrh ** expression it is handled the same way. An ephemeral table is 2174e014a838Sdanielk1977 ** filled with single-field index keys representing the results 2175e014a838Sdanielk1977 ** from the SELECT or the <exprlist>. 2176fef5208cSdrh ** 2177e014a838Sdanielk1977 ** If the 'x' expression is a column value, or the SELECT... 2178e014a838Sdanielk1977 ** statement returns a column value, then the affinity of that 2179e014a838Sdanielk1977 ** column is used to build the index keys. If both 'x' and the 2180e014a838Sdanielk1977 ** SELECT... statement are columns, then numeric affinity is used 2181e014a838Sdanielk1977 ** if either column has NUMERIC or INTEGER affinity. If neither 2182e014a838Sdanielk1977 ** 'x' nor the SELECT... statement are columns, then numeric affinity 2183e014a838Sdanielk1977 ** is used. 2184fef5208cSdrh */ 2185832508b7Sdrh pExpr->iTable = pParse->nTab++; 218671c57db0Sdan addr = sqlite3VdbeAddOp2(v, OP_OpenEphemeral, 218771c57db0Sdan pExpr->iTable, (isRowid?0:nVal)); 218871c57db0Sdan pKeyInfo = isRowid ? 0 : sqlite3KeyInfoAlloc(pParse->db, nVal, 1); 2189e014a838Sdanielk1977 21906ab3a2ecSdanielk1977 if( ExprHasProperty(pExpr, EP_xIsSelect) ){ 2191e014a838Sdanielk1977 /* Case 1: expr IN (SELECT ...) 2192e014a838Sdanielk1977 ** 2193e014a838Sdanielk1977 ** Generate code to write the results of the select into the temporary 2194e014a838Sdanielk1977 ** table allocated and opened above. 2195e014a838Sdanielk1977 */ 21964387006cSdrh Select *pSelect = pExpr->x.pSelect; 219771c57db0Sdan ExprList *pEList = pSelect->pEList; 21981013c932Sdrh 219941a05b7bSdanielk1977 assert( !isRowid ); 220071c57db0Sdan if( pEList->nExpr!=nVal ){ 2201*8da209b1Sdan sqlite3SubselectError(pParse, pEList->nExpr, nVal); 220271c57db0Sdan }else{ 220371c57db0Sdan SelectDest dest; 220471c57db0Sdan int i; 22051013c932Sdrh sqlite3SelectDestInit(&dest, SRT_Set, pExpr->iTable); 220671c57db0Sdan dest.zAffSdst = exprINAffinity(pParse, pExpr); 2207e014a838Sdanielk1977 assert( (pExpr->iTable&0x0000FFFF)==pExpr->iTable ); 22084387006cSdrh pSelect->iLimit = 0; 22094387006cSdrh testcase( pSelect->selFlags & SF_Distinct ); 2210812ea833Sdrh testcase( pKeyInfo==0 ); /* Caused by OOM in sqlite3KeyInfoAlloc() */ 22114387006cSdrh if( sqlite3Select(pParse, pSelect, &dest) ){ 221271c57db0Sdan sqlite3DbFree(pParse->db, dest.zAffSdst); 22132ec2fb22Sdrh sqlite3KeyInfoUnref(pKeyInfo); 22141450bc6eSdrh return 0; 221594ccde58Sdrh } 221671c57db0Sdan sqlite3DbFree(pParse->db, dest.zAffSdst); 2217812ea833Sdrh assert( pKeyInfo!=0 ); /* OOM will cause exit after sqlite3Select() */ 22183535ec3eSdrh assert( pEList!=0 ); 22193535ec3eSdrh assert( pEList->nExpr>0 ); 22202ec2fb22Sdrh assert( sqlite3KeyInfoIsWriteable(pKeyInfo) ); 222171c57db0Sdan for(i=0; i<nVal; i++){ 222271c57db0Sdan Expr *p = (nVal>1) ? exprVectorField(pLeft, i) : pLeft; 222371c57db0Sdan pKeyInfo->aColl[i] = sqlite3BinaryCompareCollSeq( 222471c57db0Sdan pParse, p, pEList->a[i].pExpr 222571c57db0Sdan ); 222671c57db0Sdan } 222771c57db0Sdan } 2228a7d2db17Sdrh }else if( ALWAYS(pExpr->x.pList!=0) ){ 2229fef5208cSdrh /* Case 2: expr IN (exprlist) 2230fef5208cSdrh ** 2231e014a838Sdanielk1977 ** For each expression, build an index key from the evaluation and 2232e014a838Sdanielk1977 ** store it in the temporary table. If <expr> is a column, then use 2233e014a838Sdanielk1977 ** that columns affinity when building index keys. If <expr> is not 2234e014a838Sdanielk1977 ** a column, use numeric affinity. 2235fef5208cSdrh */ 223671c57db0Sdan char affinity; /* Affinity of the LHS of the IN */ 2237e014a838Sdanielk1977 int i; 22386ab3a2ecSdanielk1977 ExprList *pList = pExpr->x.pList; 223957dbd7b3Sdrh struct ExprList_item *pItem; 2240ecc31805Sdrh int r1, r2, r3; 224157dbd7b3Sdrh 224271c57db0Sdan affinity = sqlite3ExprAffinity(pLeft); 2243e014a838Sdanielk1977 if( !affinity ){ 224405883a34Sdrh affinity = SQLITE_AFF_BLOB; 2245e014a838Sdanielk1977 } 2246323df790Sdrh if( pKeyInfo ){ 22472ec2fb22Sdrh assert( sqlite3KeyInfoIsWriteable(pKeyInfo) ); 2248323df790Sdrh pKeyInfo->aColl[0] = sqlite3ExprCollSeq(pParse, pExpr->pLeft); 2249323df790Sdrh } 2250e014a838Sdanielk1977 2251e014a838Sdanielk1977 /* Loop through each expression in <exprlist>. */ 22522d401ab8Sdrh r1 = sqlite3GetTempReg(pParse); 22532d401ab8Sdrh r2 = sqlite3GetTempReg(pParse); 225437e08081Sdrh if( isRowid ) sqlite3VdbeAddOp2(v, OP_Null, 0, r2); 225557dbd7b3Sdrh for(i=pList->nExpr, pItem=pList->a; i>0; i--, pItem++){ 225657dbd7b3Sdrh Expr *pE2 = pItem->pExpr; 2257e05c929bSdrh int iValToIns; 2258e014a838Sdanielk1977 225957dbd7b3Sdrh /* If the expression is not constant then we will need to 226057dbd7b3Sdrh ** disable the test that was generated above that makes sure 226157dbd7b3Sdrh ** this code only executes once. Because for a non-constant 226257dbd7b3Sdrh ** expression we need to rerun this code each time. 226357dbd7b3Sdrh */ 22646be515ebSdrh if( jmpIfDynamic>=0 && !sqlite3ExprIsConstant(pE2) ){ 22656be515ebSdrh sqlite3VdbeChangeToNoop(v, jmpIfDynamic); 22666be515ebSdrh jmpIfDynamic = -1; 22674794b980Sdrh } 2268e014a838Sdanielk1977 2269e014a838Sdanielk1977 /* Evaluate the expression and insert it into the temp table */ 2270e05c929bSdrh if( isRowid && sqlite3ExprIsInteger(pE2, &iValToIns) ){ 2271e05c929bSdrh sqlite3VdbeAddOp3(v, OP_InsertInt, pExpr->iTable, r2, iValToIns); 2272e05c929bSdrh }else{ 2273ecc31805Sdrh r3 = sqlite3ExprCodeTarget(pParse, pE2, r1); 227441a05b7bSdanielk1977 if( isRowid ){ 2275e05c929bSdrh sqlite3VdbeAddOp2(v, OP_MustBeInt, r3, 2276e05c929bSdrh sqlite3VdbeCurrentAddr(v)+2); 2277688852abSdrh VdbeCoverage(v); 227841a05b7bSdanielk1977 sqlite3VdbeAddOp3(v, OP_Insert, pExpr->iTable, r2, r3); 227941a05b7bSdanielk1977 }else{ 2280ecc31805Sdrh sqlite3VdbeAddOp4(v, OP_MakeRecord, r3, 1, r2, &affinity, 1); 22813c31fc23Sdrh sqlite3ExprCacheAffinityChange(pParse, r3, 1); 22822d401ab8Sdrh sqlite3VdbeAddOp2(v, OP_IdxInsert, pExpr->iTable, r2); 2283fef5208cSdrh } 228441a05b7bSdanielk1977 } 2285e05c929bSdrh } 22862d401ab8Sdrh sqlite3ReleaseTempReg(pParse, r1); 22872d401ab8Sdrh sqlite3ReleaseTempReg(pParse, r2); 2288fef5208cSdrh } 2289323df790Sdrh if( pKeyInfo ){ 22902ec2fb22Sdrh sqlite3VdbeChangeP4(v, addr, (void *)pKeyInfo, P4_KEYINFO); 229141a05b7bSdanielk1977 } 2292b3bce662Sdanielk1977 break; 2293fef5208cSdrh } 2294fef5208cSdrh 229551522cd3Sdrh case TK_EXISTS: 2296fd773cf9Sdrh case TK_SELECT: 2297fd773cf9Sdrh default: { 2298fd773cf9Sdrh /* If this has to be a scalar SELECT. Generate code to put the 2299fef5208cSdrh ** value of this select in a memory cell and record the number 2300fd773cf9Sdrh ** of the memory cell in iColumn. If this is an EXISTS, write 2301fd773cf9Sdrh ** an integer 0 (not exists) or 1 (exists) into a memory cell 2302fd773cf9Sdrh ** and record that memory cell in iColumn. 2303fef5208cSdrh */ 2304fd773cf9Sdrh Select *pSel; /* SELECT statement to encode */ 2305fd773cf9Sdrh SelectDest dest; /* How to deal with SELECt result */ 230671c57db0Sdan int nReg; /* Registers to allocate */ 23071398ad36Sdrh 2308cf697396Sshane testcase( pExpr->op==TK_EXISTS ); 2309cf697396Sshane testcase( pExpr->op==TK_SELECT ); 2310cf697396Sshane assert( pExpr->op==TK_EXISTS || pExpr->op==TK_SELECT ); 23116ab3a2ecSdanielk1977 assert( ExprHasProperty(pExpr, EP_xIsSelect) ); 231271c57db0Sdan 23136ab3a2ecSdanielk1977 pSel = pExpr->x.pSelect; 231471c57db0Sdan nReg = pExpr->op==TK_SELECT ? pSel->pEList->nExpr : 1; 231571c57db0Sdan sqlite3SelectDestInit(&dest, 0, pParse->nMem+1); 231671c57db0Sdan pParse->nMem += nReg; 231751522cd3Sdrh if( pExpr->op==TK_SELECT ){ 23186c8c8ce0Sdanielk1977 dest.eDest = SRT_Mem; 231953932ce8Sdrh dest.iSdst = dest.iSDParm; 232071c57db0Sdan dest.nSdst = nReg; 232171c57db0Sdan sqlite3VdbeAddOp3(v, OP_Null, 0, dest.iSDParm, dest.iSDParm+nReg-1); 2322d4e70ebdSdrh VdbeComment((v, "Init subquery result")); 232351522cd3Sdrh }else{ 23246c8c8ce0Sdanielk1977 dest.eDest = SRT_Exists; 23252b596da8Sdrh sqlite3VdbeAddOp2(v, OP_Integer, 0, dest.iSDParm); 2326d4e70ebdSdrh VdbeComment((v, "Init EXISTS result")); 232751522cd3Sdrh } 2328633e6d57Sdrh sqlite3ExprDelete(pParse->db, pSel->pLimit); 2329094430ebSdrh pSel->pLimit = sqlite3PExpr(pParse, TK_INTEGER, 0, 0, 2330094430ebSdrh &sqlite3IntTokens[1]); 233148b5b041Sdrh pSel->iLimit = 0; 2332772460fdSdrh pSel->selFlags &= ~SF_MultiValue; 23337d10d5a6Sdrh if( sqlite3Select(pParse, pSel, &dest) ){ 23341450bc6eSdrh return 0; 233594ccde58Sdrh } 23362b596da8Sdrh rReg = dest.iSDParm; 2337ebb6a65dSdrh ExprSetVVAProperty(pExpr, EP_NoReduce); 2338b3bce662Sdanielk1977 break; 233919a775c2Sdrh } 2340cce7d176Sdrh } 2341b3bce662Sdanielk1977 23426be515ebSdrh if( rHasNullFlag ){ 23436be515ebSdrh sqlite3SetHasNullFlag(v, pExpr->iTable, rHasNullFlag); 2344b3bce662Sdanielk1977 } 23456be515ebSdrh 23466be515ebSdrh if( jmpIfDynamic>=0 ){ 23476be515ebSdrh sqlite3VdbeJumpHere(v, jmpIfDynamic); 2348b3bce662Sdanielk1977 } 2349d2490904Sdrh sqlite3ExprCachePop(pParse); 2350fc976065Sdanielk1977 23511450bc6eSdrh return rReg; 2352cce7d176Sdrh } 235351522cd3Sdrh #endif /* SQLITE_OMIT_SUBQUERY */ 2354cce7d176Sdrh 2355e3365e6cSdrh #ifndef SQLITE_OMIT_SUBQUERY 2356e3365e6cSdrh /* 2357e3365e6cSdrh ** Generate code for an IN expression. 2358e3365e6cSdrh ** 2359e3365e6cSdrh ** x IN (SELECT ...) 2360e3365e6cSdrh ** x IN (value, value, ...) 2361e3365e6cSdrh ** 2362e3365e6cSdrh ** The left-hand side (LHS) is a scalar expression. The right-hand side (RHS) 2363e3365e6cSdrh ** is an array of zero or more values. The expression is true if the LHS is 2364e3365e6cSdrh ** contained within the RHS. The value of the expression is unknown (NULL) 2365e3365e6cSdrh ** if the LHS is NULL or if the LHS is not contained within the RHS and the 2366e3365e6cSdrh ** RHS contains one or more NULL values. 2367e3365e6cSdrh ** 23686be515ebSdrh ** This routine generates code that jumps to destIfFalse if the LHS is not 2369e3365e6cSdrh ** contained within the RHS. If due to NULLs we cannot determine if the LHS 2370e3365e6cSdrh ** is contained in the RHS then jump to destIfNull. If the LHS is contained 2371e3365e6cSdrh ** within the RHS then fall through. 2372e3365e6cSdrh */ 2373e3365e6cSdrh static void sqlite3ExprCodeIN( 2374e3365e6cSdrh Parse *pParse, /* Parsing and code generating context */ 2375e3365e6cSdrh Expr *pExpr, /* The IN expression */ 2376e3365e6cSdrh int destIfFalse, /* Jump here if LHS is not contained in the RHS */ 2377e3365e6cSdrh int destIfNull /* Jump here if the results are unknown due to NULLs */ 2378e3365e6cSdrh ){ 2379e3365e6cSdrh int rRhsHasNull = 0; /* Register that is true if RHS contains NULL values */ 2380e3365e6cSdrh int eType; /* Type of the RHS */ 2381e3365e6cSdrh int r1; /* Temporary use register */ 2382e3365e6cSdrh Vdbe *v; /* Statement under construction */ 2383ba00e30aSdan int *aiMap = 0; /* Map from vector field to index column */ 2384ba00e30aSdan char *zAff = 0; /* Affinity string for comparisons */ 2385ba00e30aSdan int nVector; /* Size of vectors for this IN(...) op */ 2386ba00e30aSdan int regSelect = 0; 2387ba00e30aSdan Expr *pLeft = pExpr->pLeft; 2388ba00e30aSdan int i; 2389e3365e6cSdrh 2390ba00e30aSdan nVector = sqlite3ExprVectorSize(pExpr->pLeft); 2391ba00e30aSdan aiMap = (int*)sqlite3DbMallocZero( 2392ba00e30aSdan pParse->db, nVector*(sizeof(int) + sizeof(char)) + 1 2393ba00e30aSdan ); 2394ba00e30aSdan if( !aiMap ) return; 2395ba00e30aSdan zAff = (char*)&aiMap[nVector]; 239671c57db0Sdan 2397ba00e30aSdan /* Attempt to compute the RHS. After this step, if anything other than 2398ba00e30aSdan ** IN_INDEX_NOOP is returned, the table opened ith cursor pExpr->iTable 2399ba00e30aSdan ** contains the values that make up the RHS. If IN_INDEX_NOOP is returned, 2400ba00e30aSdan ** the RHS has not yet been coded. */ 2401e3365e6cSdrh v = pParse->pVdbe; 2402e3365e6cSdrh assert( v!=0 ); /* OOM detected prior to this routine */ 2403e3365e6cSdrh VdbeNoopComment((v, "begin IN expr")); 2404bb53ecb1Sdrh eType = sqlite3FindInIndex(pParse, pExpr, 2405bb53ecb1Sdrh IN_INDEX_MEMBERSHIP | IN_INDEX_NOOP_OK, 2406ba00e30aSdan destIfFalse==destIfNull ? 0 : &rRhsHasNull, aiMap); 2407e3365e6cSdrh 2408ba00e30aSdan assert( pParse->nErr || nVector==1 || eType==IN_INDEX_EPH 2409ba00e30aSdan || eType==IN_INDEX_INDEX_ASC || eType==IN_INDEX_INDEX_DESC 2410ba00e30aSdan ); 2411e3365e6cSdrh 2412ba00e30aSdan /* Code the LHS, the <expr> from "<expr> IN (...)". If the LHS is a 2413ba00e30aSdan ** vector, then it is stored in an array of nVector registers starting 2414ba00e30aSdan ** at r1. 2415e3365e6cSdrh */ 2416ba00e30aSdan r1 = sqlite3GetTempRange(pParse, nVector); 2417e3365e6cSdrh sqlite3ExprCachePush(pParse); 2418ba00e30aSdan if( nVector>1 && (pLeft->flags & EP_xIsSelect) ){ 2419ba00e30aSdan regSelect = sqlite3CodeSubselect(pParse, pLeft, 0, 0); 2420ba00e30aSdan } 2421ba00e30aSdan for(i=0; i<nVector; i++){ 2422ba00e30aSdan int iCol = aiMap[i]; 2423ba00e30aSdan Expr *pLhs = exprVectorField(pLeft, i); 2424ba00e30aSdan 2425ba00e30aSdan if( regSelect ){ 2426ba00e30aSdan sqlite3VdbeAddOp3(v, OP_Copy, regSelect+i, r1+iCol, 0); 2427ba00e30aSdan }else{ 2428ba00e30aSdan sqlite3ExprCode(pParse, pLhs, r1+iCol); 2429ba00e30aSdan } 2430ba00e30aSdan 2431ba00e30aSdan zAff[iCol] = sqlite3ExprAffinity(pLhs); 2432ba00e30aSdan if( pExpr->flags & EP_xIsSelect ){ 2433ba00e30aSdan zAff[iCol] = sqlite3CompareAffinity( 2434ba00e30aSdan pExpr->x.pSelect->pEList->a[iCol].pExpr, zAff[iCol] 2435ba00e30aSdan ); 2436ba00e30aSdan } 2437ba00e30aSdan } 2438e3365e6cSdrh 2439bb53ecb1Sdrh /* If sqlite3FindInIndex() did not find or create an index that is 2440bb53ecb1Sdrh ** suitable for evaluating the IN operator, then evaluate using a 2441bb53ecb1Sdrh ** sequence of comparisons. 2442bb53ecb1Sdrh */ 2443bb53ecb1Sdrh if( eType==IN_INDEX_NOOP ){ 2444bb53ecb1Sdrh ExprList *pList = pExpr->x.pList; 2445bb53ecb1Sdrh CollSeq *pColl = sqlite3ExprCollSeq(pParse, pExpr->pLeft); 2446bb53ecb1Sdrh int labelOk = sqlite3VdbeMakeLabel(v); 2447bb53ecb1Sdrh int r2, regToFree; 2448bb53ecb1Sdrh int regCkNull = 0; 2449bb53ecb1Sdrh int ii; 2450bb53ecb1Sdrh assert( !ExprHasProperty(pExpr, EP_xIsSelect) ); 2451bb53ecb1Sdrh if( destIfNull!=destIfFalse ){ 2452bb53ecb1Sdrh regCkNull = sqlite3GetTempReg(pParse); 2453a976979bSdrh sqlite3VdbeAddOp3(v, OP_BitAnd, r1, r1, regCkNull); 2454bb53ecb1Sdrh } 2455bb53ecb1Sdrh for(ii=0; ii<pList->nExpr; ii++){ 2456bb53ecb1Sdrh r2 = sqlite3ExprCodeTemp(pParse, pList->a[ii].pExpr, ®ToFree); 2457a976979bSdrh if( regCkNull && sqlite3ExprCanBeNull(pList->a[ii].pExpr) ){ 2458bb53ecb1Sdrh sqlite3VdbeAddOp3(v, OP_BitAnd, regCkNull, r2, regCkNull); 2459bb53ecb1Sdrh } 2460bb53ecb1Sdrh if( ii<pList->nExpr-1 || destIfNull!=destIfFalse ){ 2461bb53ecb1Sdrh sqlite3VdbeAddOp4(v, OP_Eq, r1, labelOk, r2, 24624336b0e6Sdrh (void*)pColl, P4_COLLSEQ); 24634336b0e6Sdrh VdbeCoverageIf(v, ii<pList->nExpr-1); 24644336b0e6Sdrh VdbeCoverageIf(v, ii==pList->nExpr-1); 2465ba00e30aSdan sqlite3VdbeChangeP5(v, zAff[0]); 2466bb53ecb1Sdrh }else{ 2467bb53ecb1Sdrh assert( destIfNull==destIfFalse ); 2468bb53ecb1Sdrh sqlite3VdbeAddOp4(v, OP_Ne, r1, destIfFalse, r2, 2469bb53ecb1Sdrh (void*)pColl, P4_COLLSEQ); VdbeCoverage(v); 2470ba00e30aSdan sqlite3VdbeChangeP5(v, zAff[0] | SQLITE_JUMPIFNULL); 2471bb53ecb1Sdrh } 2472bb53ecb1Sdrh sqlite3ReleaseTempReg(pParse, regToFree); 2473bb53ecb1Sdrh } 2474bb53ecb1Sdrh if( regCkNull ){ 2475bb53ecb1Sdrh sqlite3VdbeAddOp2(v, OP_IsNull, regCkNull, destIfNull); VdbeCoverage(v); 2476076e85f5Sdrh sqlite3VdbeGoto(v, destIfFalse); 2477bb53ecb1Sdrh } 2478bb53ecb1Sdrh sqlite3VdbeResolveLabel(v, labelOk); 2479bb53ecb1Sdrh sqlite3ReleaseTempReg(pParse, regCkNull); 2480bb53ecb1Sdrh }else{ 2481bb53ecb1Sdrh 2482094430ebSdrh /* If the LHS is NULL, then the result is either false or NULL depending 2483d49fd4e8Sdan ** on whether the RHS is empty or not, respectively. */ 2484094430ebSdrh if( destIfNull==destIfFalse ){ 2485d49fd4e8Sdan for(i=0; i<nVector; i++){ 2486d49fd4e8Sdan Expr *p = exprVectorField(pExpr->pLeft, i); 2487d49fd4e8Sdan if( sqlite3ExprCanBeNull(p) ){ 2488d49fd4e8Sdan sqlite3VdbeAddOp2(v, OP_IsNull, r1+aiMap[i], destIfNull); 2489d49fd4e8Sdan } 2490d49fd4e8Sdan } 2491d49fd4e8Sdan }else if( nVector==1 && sqlite3ExprCanBeNull(pExpr->pLeft) ){ 2492688852abSdrh int addr1 = sqlite3VdbeAddOp1(v, OP_NotNull, r1); VdbeCoverage(v); 2493094430ebSdrh sqlite3VdbeAddOp2(v, OP_Rewind, pExpr->iTable, destIfFalse); 2494688852abSdrh VdbeCoverage(v); 2495076e85f5Sdrh sqlite3VdbeGoto(v, destIfNull); 2496094430ebSdrh sqlite3VdbeJumpHere(v, addr1); 2497094430ebSdrh } 2498e3365e6cSdrh 2499e3365e6cSdrh if( eType==IN_INDEX_ROWID ){ 2500e3365e6cSdrh /* In this case, the RHS is the ROWID of table b-tree 2501e3365e6cSdrh */ 2502eeb9565aSdrh sqlite3VdbeAddOp3(v, OP_SeekRowid, pExpr->iTable, destIfFalse, r1); 2503688852abSdrh VdbeCoverage(v); 2504d49fd4e8Sdan }else if( nVector>1 && eType==IN_INDEX_EPH && destIfNull!=destIfFalse ){ 2505ba00e30aSdan int regNull = sqlite3GetTempReg(pParse); 2506ba00e30aSdan int r2 = sqlite3GetTempReg(pParse); 2507ba00e30aSdan int r3 = sqlite3GetTempReg(pParse); 2508ba00e30aSdan int r4 = sqlite3GetTempReg(pParse); 2509ba00e30aSdan int addrNext; 2510ba00e30aSdan int addrIf; 2511ba00e30aSdan 2512ba00e30aSdan if( destIfFalse!=destIfNull ){ 2513ba00e30aSdan sqlite3VdbeAddOp2(v, OP_Integer, 0, regNull); 2514ba00e30aSdan } 2515ba00e30aSdan addrNext = sqlite3VdbeAddOp2(v, OP_Rewind, pExpr->iTable, destIfFalse); 2516ba00e30aSdan for(i=0; i<nVector; i++){ 2517ba00e30aSdan Expr *p; 2518ba00e30aSdan CollSeq *pColl; 2519ba00e30aSdan p = exprVectorField(pLeft, i); 2520ba00e30aSdan pColl = sqlite3ExprCollSeq(pParse, p); 2521ba00e30aSdan 2522ba00e30aSdan sqlite3VdbeAddOp3(v, OP_Column, pExpr->iTable, i, r2); 2523ba00e30aSdan sqlite3VdbeAddOp4(v, OP_Eq, r1+i, i?r3:r4, r2, (void*)pColl,P4_COLLSEQ); 2524ba00e30aSdan sqlite3VdbeChangeP5(v, SQLITE_STOREP2); 2525ba00e30aSdan if( i!=0 ){ 2526ba00e30aSdan sqlite3VdbeAddOp3(v, OP_And, r3, r4, r4); 2527ba00e30aSdan } 2528ba00e30aSdan } 2529ba00e30aSdan addrIf = sqlite3VdbeAddOp1(v, OP_If, r4); 2530ba00e30aSdan if( destIfNull!=destIfFalse ){ 2531ba00e30aSdan sqlite3VdbeAddOp2(v, OP_IfNot, r4, sqlite3VdbeCurrentAddr(v)+2); 2532ba00e30aSdan sqlite3VdbeAddOp2(v, OP_Integer, 1, regNull); 2533ba00e30aSdan } 2534ba00e30aSdan sqlite3VdbeAddOp2(v, OP_Next, pExpr->iTable, addrNext+1); 2535ba00e30aSdan if( destIfNull!=destIfFalse ){ 2536ba00e30aSdan sqlite3VdbeAddOp2(v, OP_If, regNull, destIfNull); 2537ba00e30aSdan } 2538ba00e30aSdan sqlite3VdbeAddOp2(v, OP_Goto, 0, destIfFalse); 2539ba00e30aSdan sqlite3VdbeChangeP2(v, addrIf, sqlite3VdbeCurrentAddr(v)); 2540ba00e30aSdan sqlite3ReleaseTempReg(pParse, regNull); 2541ba00e30aSdan sqlite3ReleaseTempReg(pParse, r2); 2542ba00e30aSdan sqlite3ReleaseTempReg(pParse, r3); 2543ba00e30aSdan sqlite3ReleaseTempReg(pParse, r4); 2544e3365e6cSdrh }else{ 2545e3365e6cSdrh /* In this case, the RHS is an index b-tree. 2546e3365e6cSdrh */ 2547ba00e30aSdan sqlite3VdbeAddOp4(v, OP_Affinity, r1, nVector, 0, zAff, nVector); 2548e3365e6cSdrh 2549e3365e6cSdrh /* If the set membership test fails, then the result of the 2550e3365e6cSdrh ** "x IN (...)" expression must be either 0 or NULL. If the set 2551e3365e6cSdrh ** contains no NULL values, then the result is 0. If the set 2552e3365e6cSdrh ** contains one or more NULL values, then the result of the 2553e3365e6cSdrh ** expression is also NULL. 2554e3365e6cSdrh */ 2555e80c9b9aSdrh assert( destIfFalse!=destIfNull || rRhsHasNull==0 ); 2556e80c9b9aSdrh if( rRhsHasNull==0 ){ 2557e3365e6cSdrh /* This branch runs if it is known at compile time that the RHS 2558e3365e6cSdrh ** cannot contain NULL values. This happens as the result 2559e3365e6cSdrh ** of a "NOT NULL" constraint in the database schema. 2560e3365e6cSdrh ** 2561e3365e6cSdrh ** Also run this branch if NULL is equivalent to FALSE 2562e3365e6cSdrh ** for this particular IN operator. 2563e3365e6cSdrh */ 2564ba00e30aSdan sqlite3VdbeAddOp4Int( 2565ba00e30aSdan v, OP_NotFound, pExpr->iTable, destIfFalse, r1, nVector 2566ba00e30aSdan ); 2567688852abSdrh VdbeCoverage(v); 2568e3365e6cSdrh }else{ 2569e3365e6cSdrh /* In this branch, the RHS of the IN might contain a NULL and 2570e3365e6cSdrh ** the presence of a NULL on the RHS makes a difference in the 2571e3365e6cSdrh ** outcome. 2572e3365e6cSdrh */ 2573728e0f91Sdrh int addr1; 2574e3365e6cSdrh 2575e3365e6cSdrh /* First check to see if the LHS is contained in the RHS. If so, 25766be515ebSdrh ** then the answer is TRUE the presence of NULLs in the RHS does 25776be515ebSdrh ** not matter. If the LHS is not contained in the RHS, then the 25786be515ebSdrh ** answer is NULL if the RHS contains NULLs and the answer is 25796be515ebSdrh ** FALSE if the RHS is NULL-free. 2580e3365e6cSdrh */ 2581728e0f91Sdrh addr1 = sqlite3VdbeAddOp4Int(v, OP_Found, pExpr->iTable, 0, r1, 1); 2582688852abSdrh VdbeCoverage(v); 25836be515ebSdrh sqlite3VdbeAddOp2(v, OP_IsNull, rRhsHasNull, destIfNull); 2584552fd454Sdrh VdbeCoverage(v); 2585076e85f5Sdrh sqlite3VdbeGoto(v, destIfFalse); 2586728e0f91Sdrh sqlite3VdbeJumpHere(v, addr1); 2587e3365e6cSdrh } 2588e3365e6cSdrh } 2589bb53ecb1Sdrh } 2590e3365e6cSdrh sqlite3ReleaseTempReg(pParse, r1); 2591d2490904Sdrh sqlite3ExprCachePop(pParse); 2592ba00e30aSdan sqlite3DbFree(pParse->db, aiMap); 2593e3365e6cSdrh VdbeComment((v, "end IN expr")); 2594e3365e6cSdrh } 2595e3365e6cSdrh #endif /* SQLITE_OMIT_SUBQUERY */ 2596e3365e6cSdrh 259713573c71Sdrh #ifndef SQLITE_OMIT_FLOATING_POINT 2598598f1340Sdrh /* 2599598f1340Sdrh ** Generate an instruction that will put the floating point 26009cbf3425Sdrh ** value described by z[0..n-1] into register iMem. 26010cf19ed8Sdrh ** 26020cf19ed8Sdrh ** The z[] string will probably not be zero-terminated. But the 26030cf19ed8Sdrh ** z[n] character is guaranteed to be something that does not look 26040cf19ed8Sdrh ** like the continuation of the number. 2605598f1340Sdrh */ 2606b7916a78Sdrh static void codeReal(Vdbe *v, const char *z, int negateFlag, int iMem){ 2607fd773cf9Sdrh if( ALWAYS(z!=0) ){ 2608598f1340Sdrh double value; 26099339da1fSdrh sqlite3AtoF(z, &value, sqlite3Strlen30(z), SQLITE_UTF8); 2610d0015161Sdrh assert( !sqlite3IsNaN(value) ); /* The new AtoF never returns NaN */ 2611598f1340Sdrh if( negateFlag ) value = -value; 261297bae794Sdrh sqlite3VdbeAddOp4Dup8(v, OP_Real, 0, iMem, 0, (u8*)&value, P4_REAL); 2613598f1340Sdrh } 2614598f1340Sdrh } 261513573c71Sdrh #endif 2616598f1340Sdrh 2617598f1340Sdrh 2618598f1340Sdrh /* 2619fec19aadSdrh ** Generate an instruction that will put the integer describe by 26209cbf3425Sdrh ** text z[0..n-1] into register iMem. 26210cf19ed8Sdrh ** 26225f1d6b61Sshaneh ** Expr.u.zToken is always UTF8 and zero-terminated. 2623fec19aadSdrh */ 262413573c71Sdrh static void codeInteger(Parse *pParse, Expr *pExpr, int negFlag, int iMem){ 262513573c71Sdrh Vdbe *v = pParse->pVdbe; 262692b01d53Sdrh if( pExpr->flags & EP_IntValue ){ 262733e619fcSdrh int i = pExpr->u.iValue; 2628d50ffc41Sdrh assert( i>=0 ); 262992b01d53Sdrh if( negFlag ) i = -i; 263092b01d53Sdrh sqlite3VdbeAddOp2(v, OP_Integer, i, iMem); 2631fd773cf9Sdrh }else{ 26325f1d6b61Sshaneh int c; 26335f1d6b61Sshaneh i64 value; 2634fd773cf9Sdrh const char *z = pExpr->u.zToken; 2635fd773cf9Sdrh assert( z!=0 ); 26369296c18aSdrh c = sqlite3DecOrHexToI64(z, &value); 26375f1d6b61Sshaneh if( c==0 || (c==2 && negFlag) ){ 2638158b9cb9Sdrh if( negFlag ){ value = c==2 ? SMALLEST_INT64 : -value; } 263997bae794Sdrh sqlite3VdbeAddOp4Dup8(v, OP_Int64, 0, iMem, 0, (u8*)&value, P4_INT64); 2640fec19aadSdrh }else{ 264113573c71Sdrh #ifdef SQLITE_OMIT_FLOATING_POINT 264213573c71Sdrh sqlite3ErrorMsg(pParse, "oversized integer: %s%s", negFlag ? "-" : "", z); 264313573c71Sdrh #else 26441b7ddc59Sdrh #ifndef SQLITE_OMIT_HEX_INTEGER 26459296c18aSdrh if( sqlite3_strnicmp(z,"0x",2)==0 ){ 26469296c18aSdrh sqlite3ErrorMsg(pParse, "hex literal too big: %s", z); 26471b7ddc59Sdrh }else 26481b7ddc59Sdrh #endif 26491b7ddc59Sdrh { 2650b7916a78Sdrh codeReal(v, z, negFlag, iMem); 26519296c18aSdrh } 265213573c71Sdrh #endif 2653fec19aadSdrh } 2654fec19aadSdrh } 2655c9cf901dSdanielk1977 } 2656fec19aadSdrh 2657bea119cdSdrh #if defined(SQLITE_DEBUG) 2658bea119cdSdrh /* 2659bea119cdSdrh ** Verify the consistency of the column cache 2660bea119cdSdrh */ 2661bea119cdSdrh static int cacheIsValid(Parse *pParse){ 2662bea119cdSdrh int i, n; 2663bea119cdSdrh for(i=n=0; i<SQLITE_N_COLCACHE; i++){ 2664bea119cdSdrh if( pParse->aColCache[i].iReg>0 ) n++; 2665bea119cdSdrh } 2666bea119cdSdrh return n==pParse->nColCache; 2667bea119cdSdrh } 2668bea119cdSdrh #endif 2669bea119cdSdrh 2670ceea3321Sdrh /* 2671ceea3321Sdrh ** Clear a cache entry. 2672ceea3321Sdrh */ 2673ceea3321Sdrh static void cacheEntryClear(Parse *pParse, struct yColCache *p){ 2674ceea3321Sdrh if( p->tempReg ){ 2675ceea3321Sdrh if( pParse->nTempReg<ArraySize(pParse->aTempReg) ){ 2676ceea3321Sdrh pParse->aTempReg[pParse->nTempReg++] = p->iReg; 2677ceea3321Sdrh } 2678ceea3321Sdrh p->tempReg = 0; 2679ceea3321Sdrh } 2680bea119cdSdrh p->iReg = 0; 2681bea119cdSdrh pParse->nColCache--; 2682ee65eea4Sdan assert( pParse->db->mallocFailed || cacheIsValid(pParse) ); 2683ceea3321Sdrh } 2684ceea3321Sdrh 2685ceea3321Sdrh 2686ceea3321Sdrh /* 2687ceea3321Sdrh ** Record in the column cache that a particular column from a 2688ceea3321Sdrh ** particular table is stored in a particular register. 2689ceea3321Sdrh */ 2690ceea3321Sdrh void sqlite3ExprCacheStore(Parse *pParse, int iTab, int iCol, int iReg){ 2691ceea3321Sdrh int i; 2692ceea3321Sdrh int minLru; 2693ceea3321Sdrh int idxLru; 2694ceea3321Sdrh struct yColCache *p; 2695ceea3321Sdrh 2696ce8f53d4Sdan /* Unless an error has occurred, register numbers are always positive. */ 2697ce8f53d4Sdan assert( iReg>0 || pParse->nErr || pParse->db->mallocFailed ); 269820411ea7Sdrh assert( iCol>=-1 && iCol<32768 ); /* Finite column numbers */ 269920411ea7Sdrh 2700b6da74ebSdrh /* The SQLITE_ColumnCache flag disables the column cache. This is used 2701b6da74ebSdrh ** for testing only - to verify that SQLite always gets the same answer 2702b6da74ebSdrh ** with and without the column cache. 2703b6da74ebSdrh */ 27047e5418e4Sdrh if( OptimizationDisabled(pParse->db, SQLITE_ColumnCache) ) return; 2705b6da74ebSdrh 270627ee406eSdrh /* First replace any existing entry. 270727ee406eSdrh ** 270827ee406eSdrh ** Actually, the way the column cache is currently used, we are guaranteed 270927ee406eSdrh ** that the object will never already be in cache. Verify this guarantee. 271027ee406eSdrh */ 271127ee406eSdrh #ifndef NDEBUG 2712ceea3321Sdrh for(i=0, p=pParse->aColCache; i<SQLITE_N_COLCACHE; i++, p++){ 271327ee406eSdrh assert( p->iReg==0 || p->iTable!=iTab || p->iColumn!=iCol ); 2714ceea3321Sdrh } 271527ee406eSdrh #endif 2716ceea3321Sdrh 2717ceea3321Sdrh /* Find an empty slot and replace it */ 2718ceea3321Sdrh for(i=0, p=pParse->aColCache; i<SQLITE_N_COLCACHE; i++, p++){ 2719ceea3321Sdrh if( p->iReg==0 ){ 2720ceea3321Sdrh p->iLevel = pParse->iCacheLevel; 2721ceea3321Sdrh p->iTable = iTab; 2722ceea3321Sdrh p->iColumn = iCol; 2723ceea3321Sdrh p->iReg = iReg; 2724ceea3321Sdrh p->tempReg = 0; 2725ceea3321Sdrh p->lru = pParse->iCacheCnt++; 2726bea119cdSdrh pParse->nColCache++; 2727ee65eea4Sdan assert( pParse->db->mallocFailed || cacheIsValid(pParse) ); 2728ceea3321Sdrh return; 2729ceea3321Sdrh } 2730ceea3321Sdrh } 2731ceea3321Sdrh 2732ceea3321Sdrh /* Replace the last recently used */ 2733ceea3321Sdrh minLru = 0x7fffffff; 2734ceea3321Sdrh idxLru = -1; 2735ceea3321Sdrh for(i=0, p=pParse->aColCache; i<SQLITE_N_COLCACHE; i++, p++){ 2736ceea3321Sdrh if( p->lru<minLru ){ 2737ceea3321Sdrh idxLru = i; 2738ceea3321Sdrh minLru = p->lru; 2739ceea3321Sdrh } 2740ceea3321Sdrh } 274120411ea7Sdrh if( ALWAYS(idxLru>=0) ){ 2742ceea3321Sdrh p = &pParse->aColCache[idxLru]; 2743ceea3321Sdrh p->iLevel = pParse->iCacheLevel; 2744ceea3321Sdrh p->iTable = iTab; 2745ceea3321Sdrh p->iColumn = iCol; 2746ceea3321Sdrh p->iReg = iReg; 2747ceea3321Sdrh p->tempReg = 0; 2748ceea3321Sdrh p->lru = pParse->iCacheCnt++; 2749bea119cdSdrh assert( cacheIsValid(pParse) ); 2750ceea3321Sdrh return; 2751ceea3321Sdrh } 2752ceea3321Sdrh } 2753ceea3321Sdrh 2754ceea3321Sdrh /* 2755f49f3523Sdrh ** Indicate that registers between iReg..iReg+nReg-1 are being overwritten. 2756f49f3523Sdrh ** Purge the range of registers from the column cache. 2757ceea3321Sdrh */ 2758f49f3523Sdrh void sqlite3ExprCacheRemove(Parse *pParse, int iReg, int nReg){ 2759ceea3321Sdrh struct yColCache *p; 2760bea119cdSdrh if( iReg<=0 || pParse->nColCache==0 ) return; 2761bea119cdSdrh p = &pParse->aColCache[SQLITE_N_COLCACHE-1]; 2762bea119cdSdrh while(1){ 2763bea119cdSdrh if( p->iReg >= iReg && p->iReg < iReg+nReg ) cacheEntryClear(pParse, p); 2764bea119cdSdrh if( p==pParse->aColCache ) break; 2765bea119cdSdrh p--; 2766ceea3321Sdrh } 2767ceea3321Sdrh } 2768ceea3321Sdrh 2769ceea3321Sdrh /* 2770ceea3321Sdrh ** Remember the current column cache context. Any new entries added 2771ceea3321Sdrh ** added to the column cache after this call are removed when the 2772ceea3321Sdrh ** corresponding pop occurs. 2773ceea3321Sdrh */ 2774ceea3321Sdrh void sqlite3ExprCachePush(Parse *pParse){ 2775ceea3321Sdrh pParse->iCacheLevel++; 27769ac7962aSdrh #ifdef SQLITE_DEBUG 27779ac7962aSdrh if( pParse->db->flags & SQLITE_VdbeAddopTrace ){ 27789ac7962aSdrh printf("PUSH to %d\n", pParse->iCacheLevel); 27799ac7962aSdrh } 27809ac7962aSdrh #endif 2781ceea3321Sdrh } 2782ceea3321Sdrh 2783ceea3321Sdrh /* 2784ceea3321Sdrh ** Remove from the column cache any entries that were added since the 2785d2490904Sdrh ** the previous sqlite3ExprCachePush operation. In other words, restore 2786d2490904Sdrh ** the cache to the state it was in prior the most recent Push. 2787ceea3321Sdrh */ 2788d2490904Sdrh void sqlite3ExprCachePop(Parse *pParse){ 2789ceea3321Sdrh int i; 2790ceea3321Sdrh struct yColCache *p; 2791d2490904Sdrh assert( pParse->iCacheLevel>=1 ); 2792d2490904Sdrh pParse->iCacheLevel--; 27939ac7962aSdrh #ifdef SQLITE_DEBUG 27949ac7962aSdrh if( pParse->db->flags & SQLITE_VdbeAddopTrace ){ 27959ac7962aSdrh printf("POP to %d\n", pParse->iCacheLevel); 27969ac7962aSdrh } 27979ac7962aSdrh #endif 2798ceea3321Sdrh for(i=0, p=pParse->aColCache; i<SQLITE_N_COLCACHE; i++, p++){ 2799ceea3321Sdrh if( p->iReg && p->iLevel>pParse->iCacheLevel ){ 2800ceea3321Sdrh cacheEntryClear(pParse, p); 2801ceea3321Sdrh } 2802ceea3321Sdrh } 2803ceea3321Sdrh } 2804945498f3Sdrh 2805945498f3Sdrh /* 28065cd79239Sdrh ** When a cached column is reused, make sure that its register is 28075cd79239Sdrh ** no longer available as a temp register. ticket #3879: that same 28085cd79239Sdrh ** register might be in the cache in multiple places, so be sure to 28095cd79239Sdrh ** get them all. 28105cd79239Sdrh */ 28115cd79239Sdrh static void sqlite3ExprCachePinRegister(Parse *pParse, int iReg){ 28125cd79239Sdrh int i; 28135cd79239Sdrh struct yColCache *p; 28145cd79239Sdrh for(i=0, p=pParse->aColCache; i<SQLITE_N_COLCACHE; i++, p++){ 28155cd79239Sdrh if( p->iReg==iReg ){ 28165cd79239Sdrh p->tempReg = 0; 28175cd79239Sdrh } 28185cd79239Sdrh } 28195cd79239Sdrh } 28205cd79239Sdrh 28211f9ca2c8Sdrh /* Generate code that will load into register regOut a value that is 28221f9ca2c8Sdrh ** appropriate for the iIdxCol-th column of index pIdx. 28231f9ca2c8Sdrh */ 28241f9ca2c8Sdrh void sqlite3ExprCodeLoadIndexColumn( 28251f9ca2c8Sdrh Parse *pParse, /* The parsing context */ 28261f9ca2c8Sdrh Index *pIdx, /* The index whose column is to be loaded */ 28271f9ca2c8Sdrh int iTabCur, /* Cursor pointing to a table row */ 28281f9ca2c8Sdrh int iIdxCol, /* The column of the index to be loaded */ 28291f9ca2c8Sdrh int regOut /* Store the index column value in this register */ 28301f9ca2c8Sdrh ){ 28311f9ca2c8Sdrh i16 iTabCol = pIdx->aiColumn[iIdxCol]; 28324b92f98cSdrh if( iTabCol==XN_EXPR ){ 28331f9ca2c8Sdrh assert( pIdx->aColExpr ); 28341f9ca2c8Sdrh assert( pIdx->aColExpr->nExpr>iIdxCol ); 28351f9ca2c8Sdrh pParse->iSelfTab = iTabCur; 28361c75c9d7Sdrh sqlite3ExprCodeCopy(pParse, pIdx->aColExpr->a[iIdxCol].pExpr, regOut); 28374b92f98cSdrh }else{ 28384b92f98cSdrh sqlite3ExprCodeGetColumnOfTable(pParse->pVdbe, pIdx->pTable, iTabCur, 28394b92f98cSdrh iTabCol, regOut); 28404b92f98cSdrh } 28411f9ca2c8Sdrh } 28421f9ca2c8Sdrh 28435cd79239Sdrh /* 28445c092e8aSdrh ** Generate code to extract the value of the iCol-th column of a table. 28455c092e8aSdrh */ 28465c092e8aSdrh void sqlite3ExprCodeGetColumnOfTable( 28475c092e8aSdrh Vdbe *v, /* The VDBE under construction */ 28485c092e8aSdrh Table *pTab, /* The table containing the value */ 2849313619f5Sdrh int iTabCur, /* The table cursor. Or the PK cursor for WITHOUT ROWID */ 28505c092e8aSdrh int iCol, /* Index of the column to extract */ 2851313619f5Sdrh int regOut /* Extract the value into this register */ 28525c092e8aSdrh ){ 28535c092e8aSdrh if( iCol<0 || iCol==pTab->iPKey ){ 28545c092e8aSdrh sqlite3VdbeAddOp2(v, OP_Rowid, iTabCur, regOut); 28555c092e8aSdrh }else{ 28565c092e8aSdrh int op = IsVirtual(pTab) ? OP_VColumn : OP_Column; 2857ee0ec8e1Sdrh int x = iCol; 285835db31b2Sdrh if( !HasRowid(pTab) && !IsVirtual(pTab) ){ 2859ee0ec8e1Sdrh x = sqlite3ColumnOfIndex(sqlite3PrimaryKeyIndex(pTab), iCol); 2860ee0ec8e1Sdrh } 2861ee0ec8e1Sdrh sqlite3VdbeAddOp3(v, op, iTabCur, x, regOut); 28625c092e8aSdrh } 28635c092e8aSdrh if( iCol>=0 ){ 28645c092e8aSdrh sqlite3ColumnDefault(v, pTab, iCol, regOut); 28655c092e8aSdrh } 28665c092e8aSdrh } 28675c092e8aSdrh 28685c092e8aSdrh /* 2869945498f3Sdrh ** Generate code that will extract the iColumn-th column from 2870ce78bc6eSdrh ** table pTab and store the column value in a register. 2871ce78bc6eSdrh ** 2872ce78bc6eSdrh ** An effort is made to store the column value in register iReg. This 2873ce78bc6eSdrh ** is not garanteeed for GetColumn() - the result can be stored in 2874ce78bc6eSdrh ** any register. But the result is guaranteed to land in register iReg 2875ce78bc6eSdrh ** for GetColumnToReg(). 2876e55cbd72Sdrh ** 2877e55cbd72Sdrh ** There must be an open cursor to pTab in iTable when this routine 2878e55cbd72Sdrh ** is called. If iColumn<0 then code is generated that extracts the rowid. 2879945498f3Sdrh */ 2880e55cbd72Sdrh int sqlite3ExprCodeGetColumn( 2881e55cbd72Sdrh Parse *pParse, /* Parsing and code generating context */ 28822133d822Sdrh Table *pTab, /* Description of the table we are reading from */ 28832133d822Sdrh int iColumn, /* Index of the table column */ 28842133d822Sdrh int iTable, /* The cursor pointing to the table */ 2885a748fdccSdrh int iReg, /* Store results here */ 2886ce78bc6eSdrh u8 p5 /* P5 value for OP_Column + FLAGS */ 28872133d822Sdrh ){ 2888e55cbd72Sdrh Vdbe *v = pParse->pVdbe; 2889e55cbd72Sdrh int i; 2890da250ea5Sdrh struct yColCache *p; 2891e55cbd72Sdrh 2892ceea3321Sdrh for(i=0, p=pParse->aColCache; i<SQLITE_N_COLCACHE; i++, p++){ 2893b6da74ebSdrh if( p->iReg>0 && p->iTable==iTable && p->iColumn==iColumn ){ 2894ceea3321Sdrh p->lru = pParse->iCacheCnt++; 28955cd79239Sdrh sqlite3ExprCachePinRegister(pParse, p->iReg); 2896da250ea5Sdrh return p->iReg; 2897e55cbd72Sdrh } 2898e55cbd72Sdrh } 2899e55cbd72Sdrh assert( v!=0 ); 29005c092e8aSdrh sqlite3ExprCodeGetColumnOfTable(v, pTab, iTable, iColumn, iReg); 2901a748fdccSdrh if( p5 ){ 2902a748fdccSdrh sqlite3VdbeChangeP5(v, p5); 2903a748fdccSdrh }else{ 2904ceea3321Sdrh sqlite3ExprCacheStore(pParse, iTable, iColumn, iReg); 2905a748fdccSdrh } 2906e55cbd72Sdrh return iReg; 2907e55cbd72Sdrh } 2908ce78bc6eSdrh void sqlite3ExprCodeGetColumnToReg( 2909ce78bc6eSdrh Parse *pParse, /* Parsing and code generating context */ 2910ce78bc6eSdrh Table *pTab, /* Description of the table we are reading from */ 2911ce78bc6eSdrh int iColumn, /* Index of the table column */ 2912ce78bc6eSdrh int iTable, /* The cursor pointing to the table */ 2913ce78bc6eSdrh int iReg /* Store results here */ 2914ce78bc6eSdrh ){ 2915ce78bc6eSdrh int r1 = sqlite3ExprCodeGetColumn(pParse, pTab, iColumn, iTable, iReg, 0); 2916ce78bc6eSdrh if( r1!=iReg ) sqlite3VdbeAddOp2(pParse->pVdbe, OP_SCopy, r1, iReg); 2917ce78bc6eSdrh } 2918ce78bc6eSdrh 2919e55cbd72Sdrh 2920e55cbd72Sdrh /* 2921ceea3321Sdrh ** Clear all column cache entries. 2922e55cbd72Sdrh */ 2923ceea3321Sdrh void sqlite3ExprCacheClear(Parse *pParse){ 2924e55cbd72Sdrh int i; 2925ceea3321Sdrh struct yColCache *p; 2926ceea3321Sdrh 29279ac7962aSdrh #if SQLITE_DEBUG 29289ac7962aSdrh if( pParse->db->flags & SQLITE_VdbeAddopTrace ){ 29299ac7962aSdrh printf("CLEAR\n"); 29309ac7962aSdrh } 29319ac7962aSdrh #endif 2932ceea3321Sdrh for(i=0, p=pParse->aColCache; i<SQLITE_N_COLCACHE; i++, p++){ 2933ceea3321Sdrh if( p->iReg ){ 2934ceea3321Sdrh cacheEntryClear(pParse, p); 2935e55cbd72Sdrh } 2936da250ea5Sdrh } 2937da250ea5Sdrh } 2938e55cbd72Sdrh 2939e55cbd72Sdrh /* 2940da250ea5Sdrh ** Record the fact that an affinity change has occurred on iCount 2941da250ea5Sdrh ** registers starting with iStart. 2942e55cbd72Sdrh */ 2943da250ea5Sdrh void sqlite3ExprCacheAffinityChange(Parse *pParse, int iStart, int iCount){ 2944f49f3523Sdrh sqlite3ExprCacheRemove(pParse, iStart, iCount); 2945e55cbd72Sdrh } 2946e55cbd72Sdrh 2947e55cbd72Sdrh /* 2948b21e7c70Sdrh ** Generate code to move content from registers iFrom...iFrom+nReg-1 2949b21e7c70Sdrh ** over to iTo..iTo+nReg-1. Keep the column cache up-to-date. 2950e55cbd72Sdrh */ 2951b21e7c70Sdrh void sqlite3ExprCodeMove(Parse *pParse, int iFrom, int iTo, int nReg){ 2952e8e4af76Sdrh assert( iFrom>=iTo+nReg || iFrom+nReg<=iTo ); 2953079a3072Sdrh sqlite3VdbeAddOp3(pParse->pVdbe, OP_Move, iFrom, iTo, nReg); 2954236241aeSdrh sqlite3ExprCacheRemove(pParse, iFrom, nReg); 2955945498f3Sdrh } 2956945498f3Sdrh 2957f49f3523Sdrh #if defined(SQLITE_DEBUG) || defined(SQLITE_COVERAGE_TEST) 295892b01d53Sdrh /* 2959652fbf55Sdrh ** Return true if any register in the range iFrom..iTo (inclusive) 2960652fbf55Sdrh ** is used as part of the column cache. 2961f49f3523Sdrh ** 2962f49f3523Sdrh ** This routine is used within assert() and testcase() macros only 2963f49f3523Sdrh ** and does not appear in a normal build. 2964652fbf55Sdrh */ 2965652fbf55Sdrh static int usedAsColumnCache(Parse *pParse, int iFrom, int iTo){ 2966652fbf55Sdrh int i; 2967ceea3321Sdrh struct yColCache *p; 2968ceea3321Sdrh for(i=0, p=pParse->aColCache; i<SQLITE_N_COLCACHE; i++, p++){ 2969ceea3321Sdrh int r = p->iReg; 2970f49f3523Sdrh if( r>=iFrom && r<=iTo ) return 1; /*NO_TEST*/ 2971652fbf55Sdrh } 2972652fbf55Sdrh return 0; 2973652fbf55Sdrh } 2974f49f3523Sdrh #endif /* SQLITE_DEBUG || SQLITE_COVERAGE_TEST */ 2975652fbf55Sdrh 2976bea119cdSdrh 2977652fbf55Sdrh /* 2978a4c3c87eSdrh ** Convert an expression node to a TK_REGISTER 2979a4c3c87eSdrh */ 2980a4c3c87eSdrh static void exprToRegister(Expr *p, int iReg){ 2981a4c3c87eSdrh p->op2 = p->op; 2982a4c3c87eSdrh p->op = TK_REGISTER; 2983a4c3c87eSdrh p->iTable = iReg; 2984a4c3c87eSdrh ExprClearProperty(p, EP_Skip); 2985a4c3c87eSdrh } 2986a4c3c87eSdrh 298771c57db0Sdan static void exprCodeBetween(Parse*,Expr*,int,void(*)(Parse*,Expr*,int,int),int); 298871c57db0Sdan 2989a4c3c87eSdrh /* 2990cce7d176Sdrh ** Generate code into the current Vdbe to evaluate the given 29912dcef11bSdrh ** expression. Attempt to store the results in register "target". 29922dcef11bSdrh ** Return the register where results are stored. 2993389a1adbSdrh ** 29948b213899Sdrh ** With this routine, there is no guarantee that results will 29952dcef11bSdrh ** be stored in target. The result might be stored in some other 29962dcef11bSdrh ** register if it is convenient to do so. The calling function 29972dcef11bSdrh ** must check the return code and move the results to the desired 29982dcef11bSdrh ** register. 2999cce7d176Sdrh */ 3000678ccce8Sdrh int sqlite3ExprCodeTarget(Parse *pParse, Expr *pExpr, int target){ 30012dcef11bSdrh Vdbe *v = pParse->pVdbe; /* The VM under construction */ 30022dcef11bSdrh int op; /* The opcode being coded */ 30032dcef11bSdrh int inReg = target; /* Results stored in register inReg */ 30042dcef11bSdrh int regFree1 = 0; /* If non-zero free this temporary register */ 30052dcef11bSdrh int regFree2 = 0; /* If non-zero free this temporary register */ 3006678ccce8Sdrh int r1, r2, r3, r4; /* Various register numbers */ 300720411ea7Sdrh sqlite3 *db = pParse->db; /* The database connection */ 300810d1edf0Sdrh Expr tempX; /* Temporary expression node */ 300971c57db0Sdan int p5 = 0; 3010ffe07b2dSdrh 30119cbf3425Sdrh assert( target>0 && target<=pParse->nMem ); 301220411ea7Sdrh if( v==0 ){ 301320411ea7Sdrh assert( pParse->db->mallocFailed ); 301420411ea7Sdrh return 0; 301520411ea7Sdrh } 3016389a1adbSdrh 3017389a1adbSdrh if( pExpr==0 ){ 3018389a1adbSdrh op = TK_NULL; 3019389a1adbSdrh }else{ 3020f2bc013cSdrh op = pExpr->op; 3021389a1adbSdrh } 3022f2bc013cSdrh switch( op ){ 302313449892Sdrh case TK_AGG_COLUMN: { 302413449892Sdrh AggInfo *pAggInfo = pExpr->pAggInfo; 302513449892Sdrh struct AggInfo_col *pCol = &pAggInfo->aCol[pExpr->iAgg]; 302613449892Sdrh if( !pAggInfo->directMode ){ 30279de221dfSdrh assert( pCol->iMem>0 ); 30289de221dfSdrh inReg = pCol->iMem; 302913449892Sdrh break; 303013449892Sdrh }else if( pAggInfo->useSortingIdx ){ 30315134d135Sdan sqlite3VdbeAddOp3(v, OP_Column, pAggInfo->sortingIdxPTab, 3032389a1adbSdrh pCol->iSorterColumn, target); 303313449892Sdrh break; 303413449892Sdrh } 303513449892Sdrh /* Otherwise, fall thru into the TK_COLUMN case */ 303613449892Sdrh } 3037967e8b73Sdrh case TK_COLUMN: { 3038b2b9d3d7Sdrh int iTab = pExpr->iTable; 3039b2b9d3d7Sdrh if( iTab<0 ){ 3040b2b9d3d7Sdrh if( pParse->ckBase>0 ){ 3041b2b9d3d7Sdrh /* Generating CHECK constraints or inserting into partial index */ 3042aa9b8963Sdrh inReg = pExpr->iColumn + pParse->ckBase; 3043b2b9d3d7Sdrh break; 3044c4a3c779Sdrh }else{ 30451f9ca2c8Sdrh /* Coding an expression that is part of an index where column names 30461f9ca2c8Sdrh ** in the index refer to the table to which the index belongs */ 30471f9ca2c8Sdrh iTab = pParse->iSelfTab; 30482282792aSdrh } 3049b2b9d3d7Sdrh } 3050b2b9d3d7Sdrh inReg = sqlite3ExprCodeGetColumn(pParse, pExpr->pTab, 3051b2b9d3d7Sdrh pExpr->iColumn, iTab, target, 3052b2b9d3d7Sdrh pExpr->op2); 3053cce7d176Sdrh break; 3054cce7d176Sdrh } 3055cce7d176Sdrh case TK_INTEGER: { 305613573c71Sdrh codeInteger(pParse, pExpr, 0, target); 3057fec19aadSdrh break; 305851e9a445Sdrh } 305913573c71Sdrh #ifndef SQLITE_OMIT_FLOATING_POINT 3060598f1340Sdrh case TK_FLOAT: { 306133e619fcSdrh assert( !ExprHasProperty(pExpr, EP_IntValue) ); 306233e619fcSdrh codeReal(v, pExpr->u.zToken, 0, target); 3063598f1340Sdrh break; 3064598f1340Sdrh } 306513573c71Sdrh #endif 3066fec19aadSdrh case TK_STRING: { 306733e619fcSdrh assert( !ExprHasProperty(pExpr, EP_IntValue) ); 3068076e85f5Sdrh sqlite3VdbeLoadString(v, target, pExpr->u.zToken); 3069cce7d176Sdrh break; 3070cce7d176Sdrh } 3071f0863fe5Sdrh case TK_NULL: { 30729de221dfSdrh sqlite3VdbeAddOp2(v, OP_Null, 0, target); 3073f0863fe5Sdrh break; 3074f0863fe5Sdrh } 30755338a5f7Sdanielk1977 #ifndef SQLITE_OMIT_BLOB_LITERAL 3076c572ef7fSdanielk1977 case TK_BLOB: { 30776c8c6cecSdrh int n; 30786c8c6cecSdrh const char *z; 3079ca48c90fSdrh char *zBlob; 308033e619fcSdrh assert( !ExprHasProperty(pExpr, EP_IntValue) ); 308133e619fcSdrh assert( pExpr->u.zToken[0]=='x' || pExpr->u.zToken[0]=='X' ); 308233e619fcSdrh assert( pExpr->u.zToken[1]=='\'' ); 308333e619fcSdrh z = &pExpr->u.zToken[2]; 3084b7916a78Sdrh n = sqlite3Strlen30(z) - 1; 3085b7916a78Sdrh assert( z[n]=='\'' ); 3086ca48c90fSdrh zBlob = sqlite3HexToBlob(sqlite3VdbeDb(v), z, n); 3087ca48c90fSdrh sqlite3VdbeAddOp4(v, OP_Blob, n/2, target, 0, zBlob, P4_DYNAMIC); 3088c572ef7fSdanielk1977 break; 3089c572ef7fSdanielk1977 } 30905338a5f7Sdanielk1977 #endif 309150457896Sdrh case TK_VARIABLE: { 309233e619fcSdrh assert( !ExprHasProperty(pExpr, EP_IntValue) ); 309333e619fcSdrh assert( pExpr->u.zToken!=0 ); 309433e619fcSdrh assert( pExpr->u.zToken[0]!=0 ); 3095eaf52d88Sdrh sqlite3VdbeAddOp2(v, OP_Variable, pExpr->iColumn, target); 309633e619fcSdrh if( pExpr->u.zToken[1]!=0 ){ 309704e9eeadSdrh assert( pExpr->u.zToken[0]=='?' 309804e9eeadSdrh || strcmp(pExpr->u.zToken, pParse->azVar[pExpr->iColumn-1])==0 ); 309904e9eeadSdrh sqlite3VdbeChangeP4(v, -1, pParse->azVar[pExpr->iColumn-1], P4_STATIC); 3100895d7472Sdrh } 310150457896Sdrh break; 310250457896Sdrh } 31034e0cff60Sdrh case TK_REGISTER: { 31049de221dfSdrh inReg = pExpr->iTable; 31054e0cff60Sdrh break; 31064e0cff60Sdrh } 3107487e262fSdrh #ifndef SQLITE_OMIT_CAST 3108487e262fSdrh case TK_CAST: { 3109487e262fSdrh /* Expressions of the form: CAST(pLeft AS token) */ 31102dcef11bSdrh inReg = sqlite3ExprCodeTarget(pParse, pExpr->pLeft, target); 31111735fa88Sdrh if( inReg!=target ){ 31121735fa88Sdrh sqlite3VdbeAddOp2(v, OP_SCopy, inReg, target); 31131735fa88Sdrh inReg = target; 31141735fa88Sdrh } 31154169e430Sdrh sqlite3VdbeAddOp2(v, OP_Cast, target, 31164169e430Sdrh sqlite3AffinityType(pExpr->u.zToken, 0)); 3117c5499befSdrh testcase( usedAsColumnCache(pParse, inReg, inReg) ); 3118b3843a82Sdrh sqlite3ExprCacheAffinityChange(pParse, inReg, 1); 3119487e262fSdrh break; 3120487e262fSdrh } 3121487e262fSdrh #endif /* SQLITE_OMIT_CAST */ 312271c57db0Sdan case TK_IS: 312371c57db0Sdan case TK_ISNOT: 312471c57db0Sdan op = (op==TK_IS) ? TK_EQ : TK_NE; 312571c57db0Sdan p5 = SQLITE_NULLEQ; 312671c57db0Sdan /* fall-through */ 3127c9b84a1fSdrh case TK_LT: 3128c9b84a1fSdrh case TK_LE: 3129c9b84a1fSdrh case TK_GT: 3130c9b84a1fSdrh case TK_GE: 3131c9b84a1fSdrh case TK_NE: 3132c9b84a1fSdrh case TK_EQ: { 313371c57db0Sdan Expr *pLeft = pExpr->pLeft; 313471c57db0Sdan if( (pLeft->flags & EP_Vector) ){ 313571c57db0Sdan codeVectorCompare(pParse, pExpr, target); 313671c57db0Sdan }else{ 313771c57db0Sdan r1 = sqlite3ExprCodeTemp(pParse, pLeft, ®Free1); 3138b6da74ebSdrh r2 = sqlite3ExprCodeTemp(pParse, pExpr->pRight, ®Free2); 313971c57db0Sdan codeCompare(pParse, pLeft, pExpr->pRight, op, 314071c57db0Sdan r1, r2, inReg, SQLITE_STOREP2 | p5); 31417d176105Sdrh assert(TK_LT==OP_Lt); testcase(op==OP_Lt); VdbeCoverageIf(v,op==OP_Lt); 31427d176105Sdrh assert(TK_LE==OP_Le); testcase(op==OP_Le); VdbeCoverageIf(v,op==OP_Le); 31437d176105Sdrh assert(TK_GT==OP_Gt); testcase(op==OP_Gt); VdbeCoverageIf(v,op==OP_Gt); 31447d176105Sdrh assert(TK_GE==OP_Ge); testcase(op==OP_Ge); VdbeCoverageIf(v,op==OP_Ge); 31457d176105Sdrh assert(TK_EQ==OP_Eq); testcase(op==OP_Eq); VdbeCoverageIf(v,op==OP_Eq); 31467d176105Sdrh assert(TK_NE==OP_Ne); testcase(op==OP_Ne); VdbeCoverageIf(v,op==OP_Ne); 3147c5499befSdrh testcase( regFree1==0 ); 3148c5499befSdrh testcase( regFree2==0 ); 3149c9b84a1fSdrh } 31506a2fe093Sdrh break; 31516a2fe093Sdrh } 3152cce7d176Sdrh case TK_AND: 3153cce7d176Sdrh case TK_OR: 3154cce7d176Sdrh case TK_PLUS: 3155cce7d176Sdrh case TK_STAR: 3156cce7d176Sdrh case TK_MINUS: 3157bf4133cbSdrh case TK_REM: 3158bf4133cbSdrh case TK_BITAND: 3159bf4133cbSdrh case TK_BITOR: 316017c40294Sdrh case TK_SLASH: 3161bf4133cbSdrh case TK_LSHIFT: 3162855eb1cfSdrh case TK_RSHIFT: 31630040077dSdrh case TK_CONCAT: { 31647d176105Sdrh assert( TK_AND==OP_And ); testcase( op==TK_AND ); 31657d176105Sdrh assert( TK_OR==OP_Or ); testcase( op==TK_OR ); 31667d176105Sdrh assert( TK_PLUS==OP_Add ); testcase( op==TK_PLUS ); 31677d176105Sdrh assert( TK_MINUS==OP_Subtract ); testcase( op==TK_MINUS ); 31687d176105Sdrh assert( TK_REM==OP_Remainder ); testcase( op==TK_REM ); 31697d176105Sdrh assert( TK_BITAND==OP_BitAnd ); testcase( op==TK_BITAND ); 31707d176105Sdrh assert( TK_BITOR==OP_BitOr ); testcase( op==TK_BITOR ); 31717d176105Sdrh assert( TK_SLASH==OP_Divide ); testcase( op==TK_SLASH ); 31727d176105Sdrh assert( TK_LSHIFT==OP_ShiftLeft ); testcase( op==TK_LSHIFT ); 31737d176105Sdrh assert( TK_RSHIFT==OP_ShiftRight ); testcase( op==TK_RSHIFT ); 31747d176105Sdrh assert( TK_CONCAT==OP_Concat ); testcase( op==TK_CONCAT ); 31752dcef11bSdrh r1 = sqlite3ExprCodeTemp(pParse, pExpr->pLeft, ®Free1); 31762dcef11bSdrh r2 = sqlite3ExprCodeTemp(pParse, pExpr->pRight, ®Free2); 31775b6afba9Sdrh sqlite3VdbeAddOp3(v, op, r2, r1, target); 3178c5499befSdrh testcase( regFree1==0 ); 3179c5499befSdrh testcase( regFree2==0 ); 31800040077dSdrh break; 31810040077dSdrh } 3182cce7d176Sdrh case TK_UMINUS: { 3183fec19aadSdrh Expr *pLeft = pExpr->pLeft; 3184fec19aadSdrh assert( pLeft ); 318513573c71Sdrh if( pLeft->op==TK_INTEGER ){ 318613573c71Sdrh codeInteger(pParse, pLeft, 1, target); 318713573c71Sdrh #ifndef SQLITE_OMIT_FLOATING_POINT 318813573c71Sdrh }else if( pLeft->op==TK_FLOAT ){ 318933e619fcSdrh assert( !ExprHasProperty(pExpr, EP_IntValue) ); 319033e619fcSdrh codeReal(v, pLeft->u.zToken, 1, target); 319113573c71Sdrh #endif 31923c84ddffSdrh }else{ 319310d1edf0Sdrh tempX.op = TK_INTEGER; 319410d1edf0Sdrh tempX.flags = EP_IntValue|EP_TokenOnly; 319510d1edf0Sdrh tempX.u.iValue = 0; 319610d1edf0Sdrh r1 = sqlite3ExprCodeTemp(pParse, &tempX, ®Free1); 3197e55cbd72Sdrh r2 = sqlite3ExprCodeTemp(pParse, pExpr->pLeft, ®Free2); 31982dcef11bSdrh sqlite3VdbeAddOp3(v, OP_Subtract, r2, r1, target); 3199c5499befSdrh testcase( regFree2==0 ); 32003c84ddffSdrh } 32019de221dfSdrh inReg = target; 32026e142f54Sdrh break; 32036e142f54Sdrh } 3204bf4133cbSdrh case TK_BITNOT: 32056e142f54Sdrh case TK_NOT: { 32067d176105Sdrh assert( TK_BITNOT==OP_BitNot ); testcase( op==TK_BITNOT ); 32077d176105Sdrh assert( TK_NOT==OP_Not ); testcase( op==TK_NOT ); 3208e99fa2afSdrh r1 = sqlite3ExprCodeTemp(pParse, pExpr->pLeft, ®Free1); 3209e99fa2afSdrh testcase( regFree1==0 ); 3210e99fa2afSdrh inReg = target; 3211e99fa2afSdrh sqlite3VdbeAddOp2(v, op, r1, inReg); 3212cce7d176Sdrh break; 3213cce7d176Sdrh } 3214cce7d176Sdrh case TK_ISNULL: 3215cce7d176Sdrh case TK_NOTNULL: { 32166a288a33Sdrh int addr; 32177d176105Sdrh assert( TK_ISNULL==OP_IsNull ); testcase( op==TK_ISNULL ); 32187d176105Sdrh assert( TK_NOTNULL==OP_NotNull ); testcase( op==TK_NOTNULL ); 32199de221dfSdrh sqlite3VdbeAddOp2(v, OP_Integer, 1, target); 32202dcef11bSdrh r1 = sqlite3ExprCodeTemp(pParse, pExpr->pLeft, ®Free1); 3221c5499befSdrh testcase( regFree1==0 ); 32222dcef11bSdrh addr = sqlite3VdbeAddOp1(v, op, r1); 32237d176105Sdrh VdbeCoverageIf(v, op==TK_ISNULL); 32247d176105Sdrh VdbeCoverageIf(v, op==TK_NOTNULL); 3225a976979bSdrh sqlite3VdbeAddOp2(v, OP_Integer, 0, target); 32266a288a33Sdrh sqlite3VdbeJumpHere(v, addr); 3227a37cdde0Sdanielk1977 break; 3228f2bc013cSdrh } 32292282792aSdrh case TK_AGG_FUNCTION: { 323013449892Sdrh AggInfo *pInfo = pExpr->pAggInfo; 32317e56e711Sdrh if( pInfo==0 ){ 323233e619fcSdrh assert( !ExprHasProperty(pExpr, EP_IntValue) ); 323333e619fcSdrh sqlite3ErrorMsg(pParse, "misuse of aggregate: %s()", pExpr->u.zToken); 32347e56e711Sdrh }else{ 32359de221dfSdrh inReg = pInfo->aFunc[pExpr->iAgg].iMem; 32367e56e711Sdrh } 32372282792aSdrh break; 32382282792aSdrh } 3239cce7d176Sdrh case TK_FUNCTION: { 324012ffee8cSdrh ExprList *pFarg; /* List of function arguments */ 324112ffee8cSdrh int nFarg; /* Number of function arguments */ 324212ffee8cSdrh FuncDef *pDef; /* The function definition object */ 324312ffee8cSdrh const char *zId; /* The function name */ 3244693e6719Sdrh u32 constMask = 0; /* Mask of function arguments that are constant */ 324512ffee8cSdrh int i; /* Loop counter */ 324612ffee8cSdrh u8 enc = ENC(db); /* The text encoding used by this database */ 324712ffee8cSdrh CollSeq *pColl = 0; /* A collating sequence */ 324817435752Sdrh 32496ab3a2ecSdanielk1977 assert( !ExprHasProperty(pExpr, EP_xIsSelect) ); 3250c5cd1249Sdrh if( ExprHasProperty(pExpr, EP_TokenOnly) ){ 325112ffee8cSdrh pFarg = 0; 325212ffee8cSdrh }else{ 325312ffee8cSdrh pFarg = pExpr->x.pList; 325412ffee8cSdrh } 325512ffee8cSdrh nFarg = pFarg ? pFarg->nExpr : 0; 325633e619fcSdrh assert( !ExprHasProperty(pExpr, EP_IntValue) ); 325733e619fcSdrh zId = pExpr->u.zToken; 325880738d9cSdrh pDef = sqlite3FindFunction(db, zId, nFarg, enc, 0); 32592d80151fSdrh if( pDef==0 || pDef->xFinalize!=0 ){ 326080738d9cSdrh sqlite3ErrorMsg(pParse, "unknown function: %s()", zId); 3261feb306f5Sdrh break; 3262feb306f5Sdrh } 3263ae6bb957Sdrh 3264ae6bb957Sdrh /* Attempt a direct implementation of the built-in COALESCE() and 326560ec914cSpeter.d.reid ** IFNULL() functions. This avoids unnecessary evaluation of 3266ae6bb957Sdrh ** arguments past the first non-NULL argument. 3267ae6bb957Sdrh */ 3268d36e1041Sdrh if( pDef->funcFlags & SQLITE_FUNC_COALESCE ){ 3269ae6bb957Sdrh int endCoalesce = sqlite3VdbeMakeLabel(v); 3270ae6bb957Sdrh assert( nFarg>=2 ); 3271ae6bb957Sdrh sqlite3ExprCode(pParse, pFarg->a[0].pExpr, target); 3272ae6bb957Sdrh for(i=1; i<nFarg; i++){ 3273ae6bb957Sdrh sqlite3VdbeAddOp2(v, OP_NotNull, target, endCoalesce); 3274688852abSdrh VdbeCoverage(v); 3275f49f3523Sdrh sqlite3ExprCacheRemove(pParse, target, 1); 3276ae6bb957Sdrh sqlite3ExprCachePush(pParse); 3277ae6bb957Sdrh sqlite3ExprCode(pParse, pFarg->a[i].pExpr, target); 3278d2490904Sdrh sqlite3ExprCachePop(pParse); 3279ae6bb957Sdrh } 3280ae6bb957Sdrh sqlite3VdbeResolveLabel(v, endCoalesce); 3281ae6bb957Sdrh break; 3282ae6bb957Sdrh } 3283ae6bb957Sdrh 3284cca9f3d2Sdrh /* The UNLIKELY() function is a no-op. The result is the value 3285cca9f3d2Sdrh ** of the first argument. 3286cca9f3d2Sdrh */ 3287cca9f3d2Sdrh if( pDef->funcFlags & SQLITE_FUNC_UNLIKELY ){ 3288cca9f3d2Sdrh assert( nFarg>=1 ); 32895f02ab09Sdrh inReg = sqlite3ExprCodeTarget(pParse, pFarg->a[0].pExpr, target); 3290cca9f3d2Sdrh break; 3291cca9f3d2Sdrh } 3292ae6bb957Sdrh 3293d1a01edaSdrh for(i=0; i<nFarg; i++){ 3294d1a01edaSdrh if( i<32 && sqlite3ExprIsConstant(pFarg->a[i].pExpr) ){ 3295693e6719Sdrh testcase( i==31 ); 3296693e6719Sdrh constMask |= MASKBIT32(i); 3297d1a01edaSdrh } 3298d1a01edaSdrh if( (pDef->funcFlags & SQLITE_FUNC_NEEDCOLL)!=0 && !pColl ){ 3299d1a01edaSdrh pColl = sqlite3ExprCollSeq(pParse, pFarg->a[i].pExpr); 3300d1a01edaSdrh } 3301d1a01edaSdrh } 330212ffee8cSdrh if( pFarg ){ 3303d1a01edaSdrh if( constMask ){ 3304d1a01edaSdrh r1 = pParse->nMem+1; 3305d1a01edaSdrh pParse->nMem += nFarg; 3306d1a01edaSdrh }else{ 330712ffee8cSdrh r1 = sqlite3GetTempRange(pParse, nFarg); 3308d1a01edaSdrh } 3309a748fdccSdrh 3310a748fdccSdrh /* For length() and typeof() functions with a column argument, 3311a748fdccSdrh ** set the P5 parameter to the OP_Column opcode to OPFLAG_LENGTHARG 3312a748fdccSdrh ** or OPFLAG_TYPEOFARG respectively, to avoid unnecessary data 3313a748fdccSdrh ** loading. 3314a748fdccSdrh */ 3315d36e1041Sdrh if( (pDef->funcFlags & (SQLITE_FUNC_LENGTH|SQLITE_FUNC_TYPEOF))!=0 ){ 33164e245a4cSdrh u8 exprOp; 3317a748fdccSdrh assert( nFarg==1 ); 3318a748fdccSdrh assert( pFarg->a[0].pExpr!=0 ); 33194e245a4cSdrh exprOp = pFarg->a[0].pExpr->op; 33204e245a4cSdrh if( exprOp==TK_COLUMN || exprOp==TK_AGG_COLUMN ){ 3321a748fdccSdrh assert( SQLITE_FUNC_LENGTH==OPFLAG_LENGTHARG ); 3322a748fdccSdrh assert( SQLITE_FUNC_TYPEOF==OPFLAG_TYPEOFARG ); 3323b1fba286Sdrh testcase( pDef->funcFlags & OPFLAG_LENGTHARG ); 3324b1fba286Sdrh pFarg->a[0].pExpr->op2 = 3325b1fba286Sdrh pDef->funcFlags & (OPFLAG_LENGTHARG|OPFLAG_TYPEOFARG); 3326a748fdccSdrh } 3327a748fdccSdrh } 3328a748fdccSdrh 3329d7d385ddSdrh sqlite3ExprCachePush(pParse); /* Ticket 2ea2425d34be */ 33305579d59fSdrh sqlite3ExprCodeExprList(pParse, pFarg, r1, 0, 3331d1a01edaSdrh SQLITE_ECEL_DUP|SQLITE_ECEL_FACTOR); 3332d2490904Sdrh sqlite3ExprCachePop(pParse); /* Ticket 2ea2425d34be */ 3333892d3179Sdrh }else{ 333412ffee8cSdrh r1 = 0; 3335892d3179Sdrh } 3336b7f6f68fSdrh #ifndef SQLITE_OMIT_VIRTUALTABLE 3337a43fa227Sdrh /* Possibly overload the function if the first argument is 3338a43fa227Sdrh ** a virtual table column. 3339a43fa227Sdrh ** 3340a43fa227Sdrh ** For infix functions (LIKE, GLOB, REGEXP, and MATCH) use the 3341a43fa227Sdrh ** second argument, not the first, as the argument to test to 3342a43fa227Sdrh ** see if it is a column in a virtual table. This is done because 3343a43fa227Sdrh ** the left operand of infix functions (the operand we want to 3344a43fa227Sdrh ** control overloading) ends up as the second argument to the 3345a43fa227Sdrh ** function. The expression "A glob B" is equivalent to 3346a43fa227Sdrh ** "glob(B,A). We want to use the A in "A glob B" to test 3347a43fa227Sdrh ** for function overloading. But we use the B term in "glob(B,A)". 3348a43fa227Sdrh */ 334912ffee8cSdrh if( nFarg>=2 && (pExpr->flags & EP_InfixFunc) ){ 335012ffee8cSdrh pDef = sqlite3VtabOverloadFunction(db, pDef, nFarg, pFarg->a[1].pExpr); 335112ffee8cSdrh }else if( nFarg>0 ){ 335212ffee8cSdrh pDef = sqlite3VtabOverloadFunction(db, pDef, nFarg, pFarg->a[0].pExpr); 3353b7f6f68fSdrh } 3354b7f6f68fSdrh #endif 3355d36e1041Sdrh if( pDef->funcFlags & SQLITE_FUNC_NEEDCOLL ){ 33568b213899Sdrh if( !pColl ) pColl = db->pDfltColl; 335766a5167bSdrh sqlite3VdbeAddOp4(v, OP_CollSeq, 0, 0, 0, (char *)pColl, P4_COLLSEQ); 3358682f68b0Sdanielk1977 } 33599c7c913cSdrh sqlite3VdbeAddOp4(v, OP_Function0, constMask, r1, target, 336066a5167bSdrh (char*)pDef, P4_FUNCDEF); 336112ffee8cSdrh sqlite3VdbeChangeP5(v, (u8)nFarg); 3362d1a01edaSdrh if( nFarg && constMask==0 ){ 336312ffee8cSdrh sqlite3ReleaseTempRange(pParse, r1, nFarg); 33642dcef11bSdrh } 33656ec2733bSdrh break; 33666ec2733bSdrh } 3367fe2093d7Sdrh #ifndef SQLITE_OMIT_SUBQUERY 3368fe2093d7Sdrh case TK_EXISTS: 336919a775c2Sdrh case TK_SELECT: { 3370*8da209b1Sdan int nCol; 3371c5499befSdrh testcase( op==TK_EXISTS ); 3372c5499befSdrh testcase( op==TK_SELECT ); 3373*8da209b1Sdan if( op==TK_SELECT && (nCol = pExpr->x.pSelect->pEList->nExpr)!=1 ){ 3374*8da209b1Sdan sqlite3SubselectError(pParse, nCol, 1); 3375*8da209b1Sdan }else{ 33761450bc6eSdrh inReg = sqlite3CodeSubselect(pParse, pExpr, 0, 0); 3377*8da209b1Sdan } 337819a775c2Sdrh break; 337919a775c2Sdrh } 3380fef5208cSdrh case TK_IN: { 3381e3365e6cSdrh int destIfFalse = sqlite3VdbeMakeLabel(v); 3382e3365e6cSdrh int destIfNull = sqlite3VdbeMakeLabel(v); 3383e3365e6cSdrh sqlite3VdbeAddOp2(v, OP_Null, 0, target); 3384e3365e6cSdrh sqlite3ExprCodeIN(pParse, pExpr, destIfFalse, destIfNull); 338566ba23ceSdrh sqlite3VdbeAddOp2(v, OP_Integer, 1, target); 3386e3365e6cSdrh sqlite3VdbeResolveLabel(v, destIfFalse); 3387e3365e6cSdrh sqlite3VdbeAddOp2(v, OP_AddImm, target, 0); 3388e3365e6cSdrh sqlite3VdbeResolveLabel(v, destIfNull); 3389fef5208cSdrh break; 3390fef5208cSdrh } 3391e3365e6cSdrh #endif /* SQLITE_OMIT_SUBQUERY */ 3392e3365e6cSdrh 3393e3365e6cSdrh 33942dcef11bSdrh /* 33952dcef11bSdrh ** x BETWEEN y AND z 33962dcef11bSdrh ** 33972dcef11bSdrh ** This is equivalent to 33982dcef11bSdrh ** 33992dcef11bSdrh ** x>=y AND x<=z 34002dcef11bSdrh ** 34012dcef11bSdrh ** X is stored in pExpr->pLeft. 34022dcef11bSdrh ** Y is stored in pExpr->pList->a[0].pExpr. 34032dcef11bSdrh ** Z is stored in pExpr->pList->a[1].pExpr. 34042dcef11bSdrh */ 3405fef5208cSdrh case TK_BETWEEN: { 340671c57db0Sdan exprCodeBetween(pParse, pExpr, target, 0, 0); 340771c57db0Sdan #if 0 3408be5c89acSdrh Expr *pLeft = pExpr->pLeft; 34096ab3a2ecSdanielk1977 struct ExprList_item *pLItem = pExpr->x.pList->a; 3410be5c89acSdrh Expr *pRight = pLItem->pExpr; 341135573356Sdrh 3412b6da74ebSdrh r1 = sqlite3ExprCodeTemp(pParse, pLeft, ®Free1); 3413b6da74ebSdrh r2 = sqlite3ExprCodeTemp(pParse, pRight, ®Free2); 3414c5499befSdrh testcase( regFree1==0 ); 3415c5499befSdrh testcase( regFree2==0 ); 34162dcef11bSdrh r3 = sqlite3GetTempReg(pParse); 3417678ccce8Sdrh r4 = sqlite3GetTempReg(pParse); 341835573356Sdrh codeCompare(pParse, pLeft, pRight, OP_Ge, 34197d176105Sdrh r1, r2, r3, SQLITE_STOREP2); VdbeCoverage(v); 3420be5c89acSdrh pLItem++; 3421be5c89acSdrh pRight = pLItem->pExpr; 34222dcef11bSdrh sqlite3ReleaseTempReg(pParse, regFree2); 34232dcef11bSdrh r2 = sqlite3ExprCodeTemp(pParse, pRight, ®Free2); 3424c5499befSdrh testcase( regFree2==0 ); 3425678ccce8Sdrh codeCompare(pParse, pLeft, pRight, OP_Le, r1, r2, r4, SQLITE_STOREP2); 3426688852abSdrh VdbeCoverage(v); 3427678ccce8Sdrh sqlite3VdbeAddOp3(v, OP_And, r3, r4, target); 34282dcef11bSdrh sqlite3ReleaseTempReg(pParse, r3); 3429678ccce8Sdrh sqlite3ReleaseTempReg(pParse, r4); 343071c57db0Sdan #endif 3431fef5208cSdrh break; 3432fef5208cSdrh } 343394fa9c41Sdrh case TK_SPAN: 3434ae80ddeaSdrh case TK_COLLATE: 34354f07e5fbSdrh case TK_UPLUS: { 34362dcef11bSdrh inReg = sqlite3ExprCodeTarget(pParse, pExpr->pLeft, target); 3437a2e00042Sdrh break; 3438a2e00042Sdrh } 34392dcef11bSdrh 3440165921a7Sdan case TK_TRIGGER: { 344165a7cd16Sdan /* If the opcode is TK_TRIGGER, then the expression is a reference 344265a7cd16Sdan ** to a column in the new.* or old.* pseudo-tables available to 344365a7cd16Sdan ** trigger programs. In this case Expr.iTable is set to 1 for the 344465a7cd16Sdan ** new.* pseudo-table, or 0 for the old.* pseudo-table. Expr.iColumn 344565a7cd16Sdan ** is set to the column of the pseudo-table to read, or to -1 to 344665a7cd16Sdan ** read the rowid field. 344765a7cd16Sdan ** 344865a7cd16Sdan ** The expression is implemented using an OP_Param opcode. The p1 344965a7cd16Sdan ** parameter is set to 0 for an old.rowid reference, or to (i+1) 345065a7cd16Sdan ** to reference another column of the old.* pseudo-table, where 345165a7cd16Sdan ** i is the index of the column. For a new.rowid reference, p1 is 345265a7cd16Sdan ** set to (n+1), where n is the number of columns in each pseudo-table. 345365a7cd16Sdan ** For a reference to any other column in the new.* pseudo-table, p1 345465a7cd16Sdan ** is set to (n+2+i), where n and i are as defined previously. For 345565a7cd16Sdan ** example, if the table on which triggers are being fired is 345665a7cd16Sdan ** declared as: 345765a7cd16Sdan ** 345865a7cd16Sdan ** CREATE TABLE t1(a, b); 345965a7cd16Sdan ** 346065a7cd16Sdan ** Then p1 is interpreted as follows: 346165a7cd16Sdan ** 346265a7cd16Sdan ** p1==0 -> old.rowid p1==3 -> new.rowid 346365a7cd16Sdan ** p1==1 -> old.a p1==4 -> new.a 346465a7cd16Sdan ** p1==2 -> old.b p1==5 -> new.b 346565a7cd16Sdan */ 34662832ad42Sdan Table *pTab = pExpr->pTab; 346765a7cd16Sdan int p1 = pExpr->iTable * (pTab->nCol+1) + 1 + pExpr->iColumn; 346865a7cd16Sdan 346965a7cd16Sdan assert( pExpr->iTable==0 || pExpr->iTable==1 ); 347065a7cd16Sdan assert( pExpr->iColumn>=-1 && pExpr->iColumn<pTab->nCol ); 347165a7cd16Sdan assert( pTab->iPKey<0 || pExpr->iColumn!=pTab->iPKey ); 347265a7cd16Sdan assert( p1>=0 && p1<(pTab->nCol*2+2) ); 347365a7cd16Sdan 347465a7cd16Sdan sqlite3VdbeAddOp2(v, OP_Param, p1, target); 347576d462eeSdan VdbeComment((v, "%s.%s -> $%d", 3476165921a7Sdan (pExpr->iTable ? "new" : "old"), 347776d462eeSdan (pExpr->iColumn<0 ? "rowid" : pExpr->pTab->aCol[pExpr->iColumn].zName), 347876d462eeSdan target 3479165921a7Sdan )); 348065a7cd16Sdan 348144dbca83Sdrh #ifndef SQLITE_OMIT_FLOATING_POINT 348265a7cd16Sdan /* If the column has REAL affinity, it may currently be stored as an 3483113762a2Sdrh ** integer. Use OP_RealAffinity to make sure it is really real. 3484113762a2Sdrh ** 3485113762a2Sdrh ** EVIDENCE-OF: R-60985-57662 SQLite will convert the value back to 3486113762a2Sdrh ** floating point when extracting it from the record. */ 34872832ad42Sdan if( pExpr->iColumn>=0 34882832ad42Sdan && pTab->aCol[pExpr->iColumn].affinity==SQLITE_AFF_REAL 34892832ad42Sdan ){ 34902832ad42Sdan sqlite3VdbeAddOp1(v, OP_RealAffinity, target); 34912832ad42Sdan } 349244dbca83Sdrh #endif 3493165921a7Sdan break; 3494165921a7Sdan } 3495165921a7Sdan 349671c57db0Sdan case TK_VECTOR: { 3497d49fd4e8Sdan sqlite3ErrorMsg(pParse, "invalid use of row value"); 349871c57db0Sdan break; 349971c57db0Sdan } 350071c57db0Sdan 350171c57db0Sdan case TK_SELECT_COLUMN: { 350271c57db0Sdan Expr *pLeft = pExpr->pLeft; 350371c57db0Sdan assert( pLeft ); 350471c57db0Sdan assert( pLeft->op==TK_SELECT || pLeft->op==TK_REGISTER ); 350571c57db0Sdan if( pLeft->op==TK_SELECT ){ 350671c57db0Sdan pLeft->iTable = sqlite3CodeSubselect(pParse, pLeft, 0, 0); 350771c57db0Sdan pLeft->op = TK_REGISTER; 350871c57db0Sdan } 350971c57db0Sdan inReg = pLeft->iTable + pExpr->iColumn; 351071c57db0Sdan break; 351171c57db0Sdan } 3512165921a7Sdan 35132dcef11bSdrh /* 35142dcef11bSdrh ** Form A: 35152dcef11bSdrh ** CASE x WHEN e1 THEN r1 WHEN e2 THEN r2 ... WHEN eN THEN rN ELSE y END 35162dcef11bSdrh ** 35172dcef11bSdrh ** Form B: 35182dcef11bSdrh ** CASE WHEN e1 THEN r1 WHEN e2 THEN r2 ... WHEN eN THEN rN ELSE y END 35192dcef11bSdrh ** 35202dcef11bSdrh ** Form A is can be transformed into the equivalent form B as follows: 35212dcef11bSdrh ** CASE WHEN x=e1 THEN r1 WHEN x=e2 THEN r2 ... 35222dcef11bSdrh ** WHEN x=eN THEN rN ELSE y END 35232dcef11bSdrh ** 35242dcef11bSdrh ** X (if it exists) is in pExpr->pLeft. 3525c5cd1249Sdrh ** Y is in the last element of pExpr->x.pList if pExpr->x.pList->nExpr is 3526c5cd1249Sdrh ** odd. The Y is also optional. If the number of elements in x.pList 3527c5cd1249Sdrh ** is even, then Y is omitted and the "otherwise" result is NULL. 35282dcef11bSdrh ** Ei is in pExpr->pList->a[i*2] and Ri is pExpr->pList->a[i*2+1]. 35292dcef11bSdrh ** 35302dcef11bSdrh ** The result of the expression is the Ri for the first matching Ei, 35312dcef11bSdrh ** or if there is no matching Ei, the ELSE term Y, or if there is 35322dcef11bSdrh ** no ELSE term, NULL. 35332dcef11bSdrh */ 353433cd4909Sdrh default: assert( op==TK_CASE ); { 35352dcef11bSdrh int endLabel; /* GOTO label for end of CASE stmt */ 35362dcef11bSdrh int nextCase; /* GOTO label for next WHEN clause */ 35372dcef11bSdrh int nExpr; /* 2x number of WHEN terms */ 35382dcef11bSdrh int i; /* Loop counter */ 35392dcef11bSdrh ExprList *pEList; /* List of WHEN terms */ 35402dcef11bSdrh struct ExprList_item *aListelem; /* Array of WHEN terms */ 35412dcef11bSdrh Expr opCompare; /* The X==Ei expression */ 35422dcef11bSdrh Expr *pX; /* The X expression */ 35431bd10f8aSdrh Expr *pTest = 0; /* X==Ei (form A) or just Ei (form B) */ 3544ceea3321Sdrh VVA_ONLY( int iCacheLevel = pParse->iCacheLevel; ) 354517a7f8ddSdrh 35466ab3a2ecSdanielk1977 assert( !ExprHasProperty(pExpr, EP_xIsSelect) && pExpr->x.pList ); 35476ab3a2ecSdanielk1977 assert(pExpr->x.pList->nExpr > 0); 35486ab3a2ecSdanielk1977 pEList = pExpr->x.pList; 3549be5c89acSdrh aListelem = pEList->a; 3550be5c89acSdrh nExpr = pEList->nExpr; 35512dcef11bSdrh endLabel = sqlite3VdbeMakeLabel(v); 35522dcef11bSdrh if( (pX = pExpr->pLeft)!=0 ){ 355310d1edf0Sdrh tempX = *pX; 355433cd4909Sdrh testcase( pX->op==TK_COLUMN ); 355510d1edf0Sdrh exprToRegister(&tempX, sqlite3ExprCodeTemp(pParse, pX, ®Free1)); 3556c5499befSdrh testcase( regFree1==0 ); 35572dcef11bSdrh opCompare.op = TK_EQ; 355810d1edf0Sdrh opCompare.pLeft = &tempX; 35592dcef11bSdrh pTest = &opCompare; 35608b1db07fSdrh /* Ticket b351d95f9cd5ef17e9d9dbae18f5ca8611190001: 35618b1db07fSdrh ** The value in regFree1 might get SCopy-ed into the file result. 35628b1db07fSdrh ** So make sure that the regFree1 register is not reused for other 35638b1db07fSdrh ** purposes and possibly overwritten. */ 35648b1db07fSdrh regFree1 = 0; 3565cce7d176Sdrh } 3566c5cd1249Sdrh for(i=0; i<nExpr-1; i=i+2){ 3567ceea3321Sdrh sqlite3ExprCachePush(pParse); 35682dcef11bSdrh if( pX ){ 35691bd10f8aSdrh assert( pTest!=0 ); 35702dcef11bSdrh opCompare.pRight = aListelem[i].pExpr; 3571f5905aa7Sdrh }else{ 35722dcef11bSdrh pTest = aListelem[i].pExpr; 357317a7f8ddSdrh } 35742dcef11bSdrh nextCase = sqlite3VdbeMakeLabel(v); 357533cd4909Sdrh testcase( pTest->op==TK_COLUMN ); 35762dcef11bSdrh sqlite3ExprIfFalse(pParse, pTest, nextCase, SQLITE_JUMPIFNULL); 3577c5499befSdrh testcase( aListelem[i+1].pExpr->op==TK_COLUMN ); 35789de221dfSdrh sqlite3ExprCode(pParse, aListelem[i+1].pExpr, target); 3579076e85f5Sdrh sqlite3VdbeGoto(v, endLabel); 3580d2490904Sdrh sqlite3ExprCachePop(pParse); 35812dcef11bSdrh sqlite3VdbeResolveLabel(v, nextCase); 3582f570f011Sdrh } 3583c5cd1249Sdrh if( (nExpr&1)!=0 ){ 3584ceea3321Sdrh sqlite3ExprCachePush(pParse); 3585c5cd1249Sdrh sqlite3ExprCode(pParse, pEList->a[nExpr-1].pExpr, target); 3586d2490904Sdrh sqlite3ExprCachePop(pParse); 358717a7f8ddSdrh }else{ 35889de221dfSdrh sqlite3VdbeAddOp2(v, OP_Null, 0, target); 358917a7f8ddSdrh } 3590c1f4a19bSdanielk1977 assert( db->mallocFailed || pParse->nErr>0 3591c1f4a19bSdanielk1977 || pParse->iCacheLevel==iCacheLevel ); 35922dcef11bSdrh sqlite3VdbeResolveLabel(v, endLabel); 35936f34903eSdanielk1977 break; 35946f34903eSdanielk1977 } 35955338a5f7Sdanielk1977 #ifndef SQLITE_OMIT_TRIGGER 35966f34903eSdanielk1977 case TK_RAISE: { 3597165921a7Sdan assert( pExpr->affinity==OE_Rollback 3598165921a7Sdan || pExpr->affinity==OE_Abort 3599165921a7Sdan || pExpr->affinity==OE_Fail 3600165921a7Sdan || pExpr->affinity==OE_Ignore 3601165921a7Sdan ); 3602e0af83acSdan if( !pParse->pTriggerTab ){ 3603e0af83acSdan sqlite3ErrorMsg(pParse, 3604e0af83acSdan "RAISE() may only be used within a trigger-program"); 3605e0af83acSdan return 0; 3606e0af83acSdan } 3607e0af83acSdan if( pExpr->affinity==OE_Abort ){ 3608e0af83acSdan sqlite3MayAbort(pParse); 3609e0af83acSdan } 361033e619fcSdrh assert( !ExprHasProperty(pExpr, EP_IntValue) ); 3611e0af83acSdan if( pExpr->affinity==OE_Ignore ){ 3612e0af83acSdan sqlite3VdbeAddOp4( 3613e0af83acSdan v, OP_Halt, SQLITE_OK, OE_Ignore, 0, pExpr->u.zToken,0); 3614688852abSdrh VdbeCoverage(v); 3615e0af83acSdan }else{ 3616433dccfbSdrh sqlite3HaltConstraint(pParse, SQLITE_CONSTRAINT_TRIGGER, 3617f9c8ce3cSdrh pExpr->affinity, pExpr->u.zToken, 0, 0); 3618e0af83acSdan } 3619e0af83acSdan 3620ffe07b2dSdrh break; 362117a7f8ddSdrh } 36225338a5f7Sdanielk1977 #endif 3623ffe07b2dSdrh } 36242dcef11bSdrh sqlite3ReleaseTempReg(pParse, regFree1); 36252dcef11bSdrh sqlite3ReleaseTempReg(pParse, regFree2); 36262dcef11bSdrh return inReg; 36275b6afba9Sdrh } 36282dcef11bSdrh 36292dcef11bSdrh /* 3630d1a01edaSdrh ** Factor out the code of the given expression to initialization time. 3631d1a01edaSdrh */ 3632d673cddaSdrh void sqlite3ExprCodeAtInit( 3633d673cddaSdrh Parse *pParse, /* Parsing context */ 3634d673cddaSdrh Expr *pExpr, /* The expression to code when the VDBE initializes */ 3635d673cddaSdrh int regDest, /* Store the value in this register */ 3636d673cddaSdrh u8 reusable /* True if this expression is reusable */ 3637d673cddaSdrh ){ 3638d1a01edaSdrh ExprList *p; 3639d9f158e7Sdrh assert( ConstFactorOk(pParse) ); 3640d1a01edaSdrh p = pParse->pConstExpr; 3641d1a01edaSdrh pExpr = sqlite3ExprDup(pParse->db, pExpr, 0); 3642d1a01edaSdrh p = sqlite3ExprListAppend(pParse, p, pExpr); 3643d673cddaSdrh if( p ){ 3644d673cddaSdrh struct ExprList_item *pItem = &p->a[p->nExpr-1]; 3645d673cddaSdrh pItem->u.iConstExprReg = regDest; 3646d673cddaSdrh pItem->reusable = reusable; 3647d673cddaSdrh } 3648d1a01edaSdrh pParse->pConstExpr = p; 3649d1a01edaSdrh } 3650d1a01edaSdrh 3651d1a01edaSdrh /* 36522dcef11bSdrh ** Generate code to evaluate an expression and store the results 36532dcef11bSdrh ** into a register. Return the register number where the results 36542dcef11bSdrh ** are stored. 36552dcef11bSdrh ** 36562dcef11bSdrh ** If the register is a temporary register that can be deallocated, 3657678ccce8Sdrh ** then write its number into *pReg. If the result register is not 36582dcef11bSdrh ** a temporary, then set *pReg to zero. 3659f30a969bSdrh ** 3660f30a969bSdrh ** If pExpr is a constant, then this routine might generate this 3661f30a969bSdrh ** code to fill the register in the initialization section of the 3662f30a969bSdrh ** VDBE program, in order to factor it out of the evaluation loop. 36632dcef11bSdrh */ 36642dcef11bSdrh int sqlite3ExprCodeTemp(Parse *pParse, Expr *pExpr, int *pReg){ 3665f30a969bSdrh int r2; 3666f30a969bSdrh pExpr = sqlite3ExprSkipCollate(pExpr); 3667d9f158e7Sdrh if( ConstFactorOk(pParse) 3668f30a969bSdrh && pExpr->op!=TK_REGISTER 3669f30a969bSdrh && sqlite3ExprIsConstantNotJoin(pExpr) 3670f30a969bSdrh ){ 3671f30a969bSdrh ExprList *p = pParse->pConstExpr; 3672f30a969bSdrh int i; 3673f30a969bSdrh *pReg = 0; 3674f30a969bSdrh if( p ){ 3675d673cddaSdrh struct ExprList_item *pItem; 3676d673cddaSdrh for(pItem=p->a, i=p->nExpr; i>0; pItem++, i--){ 3677d673cddaSdrh if( pItem->reusable && sqlite3ExprCompare(pItem->pExpr,pExpr,-1)==0 ){ 3678d673cddaSdrh return pItem->u.iConstExprReg; 3679f30a969bSdrh } 3680f30a969bSdrh } 3681f30a969bSdrh } 3682f30a969bSdrh r2 = ++pParse->nMem; 3683d673cddaSdrh sqlite3ExprCodeAtInit(pParse, pExpr, r2, 1); 3684f30a969bSdrh }else{ 36852dcef11bSdrh int r1 = sqlite3GetTempReg(pParse); 3686f30a969bSdrh r2 = sqlite3ExprCodeTarget(pParse, pExpr, r1); 36872dcef11bSdrh if( r2==r1 ){ 36882dcef11bSdrh *pReg = r1; 36892dcef11bSdrh }else{ 36902dcef11bSdrh sqlite3ReleaseTempReg(pParse, r1); 36912dcef11bSdrh *pReg = 0; 36922dcef11bSdrh } 3693f30a969bSdrh } 36942dcef11bSdrh return r2; 36952dcef11bSdrh } 36962dcef11bSdrh 36972dcef11bSdrh /* 36982dcef11bSdrh ** Generate code that will evaluate expression pExpr and store the 36992dcef11bSdrh ** results in register target. The results are guaranteed to appear 37002dcef11bSdrh ** in register target. 37012dcef11bSdrh */ 370205a86c5cSdrh void sqlite3ExprCode(Parse *pParse, Expr *pExpr, int target){ 37039cbf3425Sdrh int inReg; 37049cbf3425Sdrh 37059cbf3425Sdrh assert( target>0 && target<=pParse->nMem ); 3706ebc16717Sdrh if( pExpr && pExpr->op==TK_REGISTER ){ 3707ebc16717Sdrh sqlite3VdbeAddOp2(pParse->pVdbe, OP_Copy, pExpr->iTable, target); 3708ebc16717Sdrh }else{ 37099cbf3425Sdrh inReg = sqlite3ExprCodeTarget(pParse, pExpr, target); 37101c75c9d7Sdrh assert( pParse->pVdbe!=0 || pParse->db->mallocFailed ); 37110e359b30Sdrh if( inReg!=target && pParse->pVdbe ){ 37129cbf3425Sdrh sqlite3VdbeAddOp2(pParse->pVdbe, OP_SCopy, inReg, target); 371317a7f8ddSdrh } 3714ebc16717Sdrh } 3715cce7d176Sdrh } 3716cce7d176Sdrh 3717cce7d176Sdrh /* 37181c75c9d7Sdrh ** Make a transient copy of expression pExpr and then code it using 37191c75c9d7Sdrh ** sqlite3ExprCode(). This routine works just like sqlite3ExprCode() 37201c75c9d7Sdrh ** except that the input expression is guaranteed to be unchanged. 37211c75c9d7Sdrh */ 37221c75c9d7Sdrh void sqlite3ExprCodeCopy(Parse *pParse, Expr *pExpr, int target){ 37231c75c9d7Sdrh sqlite3 *db = pParse->db; 37241c75c9d7Sdrh pExpr = sqlite3ExprDup(db, pExpr, 0); 37251c75c9d7Sdrh if( !db->mallocFailed ) sqlite3ExprCode(pParse, pExpr, target); 37261c75c9d7Sdrh sqlite3ExprDelete(db, pExpr); 37271c75c9d7Sdrh } 37281c75c9d7Sdrh 37291c75c9d7Sdrh /* 373005a86c5cSdrh ** Generate code that will evaluate expression pExpr and store the 373105a86c5cSdrh ** results in register target. The results are guaranteed to appear 373205a86c5cSdrh ** in register target. If the expression is constant, then this routine 373305a86c5cSdrh ** might choose to code the expression at initialization time. 373405a86c5cSdrh */ 373505a86c5cSdrh void sqlite3ExprCodeFactorable(Parse *pParse, Expr *pExpr, int target){ 373605a86c5cSdrh if( pParse->okConstFactor && sqlite3ExprIsConstant(pExpr) ){ 373705a86c5cSdrh sqlite3ExprCodeAtInit(pParse, pExpr, target, 0); 373805a86c5cSdrh }else{ 373905a86c5cSdrh sqlite3ExprCode(pParse, pExpr, target); 374005a86c5cSdrh } 3741cce7d176Sdrh } 3742cce7d176Sdrh 3743cce7d176Sdrh /* 374460ec914cSpeter.d.reid ** Generate code that evaluates the given expression and puts the result 3745de4fcfddSdrh ** in register target. 374625303780Sdrh ** 37472dcef11bSdrh ** Also make a copy of the expression results into another "cache" register 37482dcef11bSdrh ** and modify the expression so that the next time it is evaluated, 37492dcef11bSdrh ** the result is a copy of the cache register. 37502dcef11bSdrh ** 37512dcef11bSdrh ** This routine is used for expressions that are used multiple 37522dcef11bSdrh ** times. They are evaluated once and the results of the expression 37532dcef11bSdrh ** are reused. 375425303780Sdrh */ 375505a86c5cSdrh void sqlite3ExprCodeAndCache(Parse *pParse, Expr *pExpr, int target){ 375625303780Sdrh Vdbe *v = pParse->pVdbe; 375725303780Sdrh int iMem; 375805a86c5cSdrh 375905a86c5cSdrh assert( target>0 ); 376005a86c5cSdrh assert( pExpr->op!=TK_REGISTER ); 376105a86c5cSdrh sqlite3ExprCode(pParse, pExpr, target); 37622dcef11bSdrh iMem = ++pParse->nMem; 376305a86c5cSdrh sqlite3VdbeAddOp2(v, OP_Copy, target, iMem); 3764a4c3c87eSdrh exprToRegister(pExpr, iMem); 376525303780Sdrh } 37667e02e5e6Sdrh 3767678ccce8Sdrh /* 3768268380caSdrh ** Generate code that pushes the value of every element of the given 37699cbf3425Sdrh ** expression list into a sequence of registers beginning at target. 3770268380caSdrh ** 3771892d3179Sdrh ** Return the number of elements evaluated. 3772d1a01edaSdrh ** 3773d1a01edaSdrh ** The SQLITE_ECEL_DUP flag prevents the arguments from being 3774d1a01edaSdrh ** filled using OP_SCopy. OP_Copy must be used instead. 3775d1a01edaSdrh ** 3776d1a01edaSdrh ** The SQLITE_ECEL_FACTOR argument allows constant arguments to be 3777d1a01edaSdrh ** factored out into initialization code. 3778b0df9634Sdrh ** 3779b0df9634Sdrh ** The SQLITE_ECEL_REF flag means that expressions in the list with 3780b0df9634Sdrh ** ExprList.a[].u.x.iOrderByCol>0 have already been evaluated and stored 3781b0df9634Sdrh ** in registers at srcReg, and so the value can be copied from there. 3782268380caSdrh */ 37834adee20fSdanielk1977 int sqlite3ExprCodeExprList( 3784268380caSdrh Parse *pParse, /* Parsing context */ 3785389a1adbSdrh ExprList *pList, /* The expression list to be coded */ 3786191b54cbSdrh int target, /* Where to write results */ 37875579d59fSdrh int srcReg, /* Source registers if SQLITE_ECEL_REF */ 3788d1a01edaSdrh u8 flags /* SQLITE_ECEL_* flags */ 3789268380caSdrh ){ 3790268380caSdrh struct ExprList_item *pItem; 37915579d59fSdrh int i, j, n; 3792d1a01edaSdrh u8 copyOp = (flags & SQLITE_ECEL_DUP) ? OP_Copy : OP_SCopy; 37935579d59fSdrh Vdbe *v = pParse->pVdbe; 37949d8b3072Sdrh assert( pList!=0 ); 37959cbf3425Sdrh assert( target>0 ); 3796d81a142bSdrh assert( pParse->pVdbe!=0 ); /* Never gets this far otherwise */ 3797268380caSdrh n = pList->nExpr; 3798d9f158e7Sdrh if( !ConstFactorOk(pParse) ) flags &= ~SQLITE_ECEL_FACTOR; 3799191b54cbSdrh for(pItem=pList->a, i=0; i<n; i++, pItem++){ 38007445ffe2Sdrh Expr *pExpr = pItem->pExpr; 38015579d59fSdrh if( (flags & SQLITE_ECEL_REF)!=0 && (j = pList->a[i].u.x.iOrderByCol)>0 ){ 38025579d59fSdrh sqlite3VdbeAddOp2(v, copyOp, j+srcReg-1, target+i); 38035579d59fSdrh }else if( (flags & SQLITE_ECEL_FACTOR)!=0 && sqlite3ExprIsConstant(pExpr) ){ 3804d673cddaSdrh sqlite3ExprCodeAtInit(pParse, pExpr, target+i, 0); 3805d1a01edaSdrh }else{ 38067445ffe2Sdrh int inReg = sqlite3ExprCodeTarget(pParse, pExpr, target+i); 3807746fd9ccSdrh if( inReg!=target+i ){ 38084eded604Sdrh VdbeOp *pOp; 38094eded604Sdrh if( copyOp==OP_Copy 38104eded604Sdrh && (pOp=sqlite3VdbeGetOp(v, -1))->opcode==OP_Copy 38114eded604Sdrh && pOp->p1+pOp->p3+1==inReg 38124eded604Sdrh && pOp->p2+pOp->p3+1==target+i 38134eded604Sdrh ){ 38144eded604Sdrh pOp->p3++; 38154eded604Sdrh }else{ 38164eded604Sdrh sqlite3VdbeAddOp2(v, copyOp, inReg, target+i); 38174eded604Sdrh } 3818d1a01edaSdrh } 3819d176611bSdrh } 3820268380caSdrh } 3821f9b596ebSdrh return n; 3822268380caSdrh } 3823268380caSdrh 3824268380caSdrh /* 382536c563a2Sdrh ** Generate code for a BETWEEN operator. 382636c563a2Sdrh ** 382736c563a2Sdrh ** x BETWEEN y AND z 382836c563a2Sdrh ** 382936c563a2Sdrh ** The above is equivalent to 383036c563a2Sdrh ** 383136c563a2Sdrh ** x>=y AND x<=z 383236c563a2Sdrh ** 383336c563a2Sdrh ** Code it as such, taking care to do the common subexpression 383460ec914cSpeter.d.reid ** elimination of x. 383536c563a2Sdrh */ 383636c563a2Sdrh static void exprCodeBetween( 383736c563a2Sdrh Parse *pParse, /* Parsing and code generating context */ 383836c563a2Sdrh Expr *pExpr, /* The BETWEEN expression */ 383936c563a2Sdrh int dest, /* Jump here if the jump is taken */ 384071c57db0Sdan void (*xJumpIf)(Parse*,Expr*,int,int), 384136c563a2Sdrh int jumpIfNull /* Take the jump if the BETWEEN is NULL */ 384236c563a2Sdrh ){ 384336c563a2Sdrh Expr exprAnd; /* The AND operator in x>=y AND x<=z */ 384436c563a2Sdrh Expr compLeft; /* The x>=y term */ 384536c563a2Sdrh Expr compRight; /* The x<=z term */ 384636c563a2Sdrh Expr exprX; /* The x subexpression */ 384736c563a2Sdrh int regFree1 = 0; /* Temporary use register */ 384836c563a2Sdrh 384971c57db0Sdan memset(&compLeft, 0, sizeof(Expr)); 385071c57db0Sdan memset(&compRight, 0, sizeof(Expr)); 385171c57db0Sdan memset(&exprAnd, 0, sizeof(Expr)); 385271c57db0Sdan 385336c563a2Sdrh assert( !ExprHasProperty(pExpr, EP_xIsSelect) ); 385436c563a2Sdrh exprX = *pExpr->pLeft; 385536c563a2Sdrh exprAnd.op = TK_AND; 385636c563a2Sdrh exprAnd.pLeft = &compLeft; 385736c563a2Sdrh exprAnd.pRight = &compRight; 385836c563a2Sdrh compLeft.op = TK_GE; 385936c563a2Sdrh compLeft.pLeft = &exprX; 386036c563a2Sdrh compLeft.pRight = pExpr->x.pList->a[0].pExpr; 386136c563a2Sdrh compRight.op = TK_LE; 386236c563a2Sdrh compRight.pLeft = &exprX; 386336c563a2Sdrh compRight.pRight = pExpr->x.pList->a[1].pExpr; 386471c57db0Sdan if( (exprX.flags & EP_Vector)==0 ){ 3865a4c3c87eSdrh exprToRegister(&exprX, sqlite3ExprCodeTemp(pParse, &exprX, ®Free1)); 386671c57db0Sdan } 386771c57db0Sdan if( xJumpIf ){ 386871c57db0Sdan xJumpIf(pParse, &exprAnd, dest, jumpIfNull); 386936c563a2Sdrh }else{ 387071c57db0Sdan exprX.flags |= EP_FromJoin; 387171c57db0Sdan sqlite3ExprCodeTarget(pParse, &exprAnd, dest); 387236c563a2Sdrh } 387336c563a2Sdrh sqlite3ReleaseTempReg(pParse, regFree1); 387436c563a2Sdrh 387536c563a2Sdrh /* Ensure adequate test coverage */ 387636c563a2Sdrh testcase( jumpIfTrue==0 && jumpIfNull==0 && regFree1==0 ); 387736c563a2Sdrh testcase( jumpIfTrue==0 && jumpIfNull==0 && regFree1!=0 ); 387836c563a2Sdrh testcase( jumpIfTrue==0 && jumpIfNull!=0 && regFree1==0 ); 387936c563a2Sdrh testcase( jumpIfTrue==0 && jumpIfNull!=0 && regFree1!=0 ); 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 } 388536c563a2Sdrh 388636c563a2Sdrh /* 3887cce7d176Sdrh ** Generate code for a boolean expression such that a jump is made 3888cce7d176Sdrh ** to the label "dest" if the expression is true but execution 3889cce7d176Sdrh ** continues straight thru if the expression is false. 3890f5905aa7Sdrh ** 3891f5905aa7Sdrh ** If the expression evaluates to NULL (neither true nor false), then 389235573356Sdrh ** take the jump if the jumpIfNull flag is SQLITE_JUMPIFNULL. 3893f2bc013cSdrh ** 3894f2bc013cSdrh ** This code depends on the fact that certain token values (ex: TK_EQ) 3895f2bc013cSdrh ** are the same as opcode values (ex: OP_Eq) that implement the corresponding 3896f2bc013cSdrh ** operation. Special comments in vdbe.c and the mkopcodeh.awk script in 3897f2bc013cSdrh ** the make process cause these values to align. Assert()s in the code 3898f2bc013cSdrh ** below verify that the numbers are aligned correctly. 3899cce7d176Sdrh */ 39004adee20fSdanielk1977 void sqlite3ExprIfTrue(Parse *pParse, Expr *pExpr, int dest, int jumpIfNull){ 3901cce7d176Sdrh Vdbe *v = pParse->pVdbe; 3902cce7d176Sdrh int op = 0; 39032dcef11bSdrh int regFree1 = 0; 39042dcef11bSdrh int regFree2 = 0; 39052dcef11bSdrh int r1, r2; 39062dcef11bSdrh 390735573356Sdrh assert( jumpIfNull==SQLITE_JUMPIFNULL || jumpIfNull==0 ); 390848864df9Smistachkin if( NEVER(v==0) ) return; /* Existence of VDBE checked by caller */ 390933cd4909Sdrh if( NEVER(pExpr==0) ) return; /* No way this can happen */ 3910f2bc013cSdrh op = pExpr->op; 391171c57db0Sdan switch( op | (pExpr->pLeft ? (pExpr->pLeft->flags & EP_Vector) : 0)){ 3912cce7d176Sdrh case TK_AND: { 39134adee20fSdanielk1977 int d2 = sqlite3VdbeMakeLabel(v); 3914c5499befSdrh testcase( jumpIfNull==0 ); 391535573356Sdrh sqlite3ExprIfFalse(pParse, pExpr->pLeft, d2,jumpIfNull^SQLITE_JUMPIFNULL); 391654e2adb5Sdrh sqlite3ExprCachePush(pParse); 39174adee20fSdanielk1977 sqlite3ExprIfTrue(pParse, pExpr->pRight, dest, jumpIfNull); 39184adee20fSdanielk1977 sqlite3VdbeResolveLabel(v, d2); 3919d2490904Sdrh sqlite3ExprCachePop(pParse); 3920cce7d176Sdrh break; 3921cce7d176Sdrh } 3922cce7d176Sdrh case TK_OR: { 3923c5499befSdrh testcase( jumpIfNull==0 ); 39244adee20fSdanielk1977 sqlite3ExprIfTrue(pParse, pExpr->pLeft, dest, jumpIfNull); 392554e2adb5Sdrh sqlite3ExprCachePush(pParse); 39264adee20fSdanielk1977 sqlite3ExprIfTrue(pParse, pExpr->pRight, dest, jumpIfNull); 3927d2490904Sdrh sqlite3ExprCachePop(pParse); 3928cce7d176Sdrh break; 3929cce7d176Sdrh } 3930cce7d176Sdrh case TK_NOT: { 3931c5499befSdrh testcase( jumpIfNull==0 ); 39324adee20fSdanielk1977 sqlite3ExprIfFalse(pParse, pExpr->pLeft, dest, jumpIfNull); 3933cce7d176Sdrh break; 3934cce7d176Sdrh } 3935de845c2fSdrh case TK_IS: 3936de845c2fSdrh case TK_ISNOT: 3937de845c2fSdrh testcase( op==TK_IS ); 3938de845c2fSdrh testcase( op==TK_ISNOT ); 3939de845c2fSdrh op = (op==TK_IS) ? TK_EQ : TK_NE; 3940de845c2fSdrh jumpIfNull = SQLITE_NULLEQ; 3941de845c2fSdrh /* Fall thru */ 3942cce7d176Sdrh case TK_LT: 3943cce7d176Sdrh case TK_LE: 3944cce7d176Sdrh case TK_GT: 3945cce7d176Sdrh case TK_GE: 3946cce7d176Sdrh case TK_NE: 39470ac65892Sdrh case TK_EQ: { 3948c5499befSdrh testcase( jumpIfNull==0 ); 3949b6da74ebSdrh r1 = sqlite3ExprCodeTemp(pParse, pExpr->pLeft, ®Free1); 3950b6da74ebSdrh r2 = sqlite3ExprCodeTemp(pParse, pExpr->pRight, ®Free2); 395135573356Sdrh codeCompare(pParse, pExpr->pLeft, pExpr->pRight, op, 39522dcef11bSdrh r1, r2, dest, jumpIfNull); 39537d176105Sdrh assert(TK_LT==OP_Lt); testcase(op==OP_Lt); VdbeCoverageIf(v,op==OP_Lt); 39547d176105Sdrh assert(TK_LE==OP_Le); testcase(op==OP_Le); VdbeCoverageIf(v,op==OP_Le); 39557d176105Sdrh assert(TK_GT==OP_Gt); testcase(op==OP_Gt); VdbeCoverageIf(v,op==OP_Gt); 39567d176105Sdrh assert(TK_GE==OP_Ge); testcase(op==OP_Ge); VdbeCoverageIf(v,op==OP_Ge); 3957de845c2fSdrh assert(TK_EQ==OP_Eq); testcase(op==OP_Eq); 3958de845c2fSdrh VdbeCoverageIf(v, op==OP_Eq && jumpIfNull==SQLITE_NULLEQ); 3959de845c2fSdrh VdbeCoverageIf(v, op==OP_Eq && jumpIfNull!=SQLITE_NULLEQ); 3960de845c2fSdrh assert(TK_NE==OP_Ne); testcase(op==OP_Ne); 3961de845c2fSdrh VdbeCoverageIf(v, op==OP_Ne && jumpIfNull==SQLITE_NULLEQ); 3962de845c2fSdrh VdbeCoverageIf(v, op==OP_Ne && jumpIfNull!=SQLITE_NULLEQ); 39636a2fe093Sdrh testcase( regFree1==0 ); 39646a2fe093Sdrh testcase( regFree2==0 ); 39656a2fe093Sdrh break; 39666a2fe093Sdrh } 3967cce7d176Sdrh case TK_ISNULL: 3968cce7d176Sdrh case TK_NOTNULL: { 39697d176105Sdrh assert( TK_ISNULL==OP_IsNull ); testcase( op==TK_ISNULL ); 39707d176105Sdrh assert( TK_NOTNULL==OP_NotNull ); testcase( op==TK_NOTNULL ); 39712dcef11bSdrh r1 = sqlite3ExprCodeTemp(pParse, pExpr->pLeft, ®Free1); 39722dcef11bSdrh sqlite3VdbeAddOp2(v, op, r1, dest); 39737d176105Sdrh VdbeCoverageIf(v, op==TK_ISNULL); 39747d176105Sdrh VdbeCoverageIf(v, op==TK_NOTNULL); 3975c5499befSdrh testcase( regFree1==0 ); 3976cce7d176Sdrh break; 3977cce7d176Sdrh } 3978fef5208cSdrh case TK_BETWEEN: { 39795c03f30aSdrh testcase( jumpIfNull==0 ); 398071c57db0Sdan exprCodeBetween(pParse, pExpr, dest, sqlite3ExprIfTrue, jumpIfNull); 3981fef5208cSdrh break; 3982fef5208cSdrh } 3983bb201344Sshaneh #ifndef SQLITE_OMIT_SUBQUERY 3984e3365e6cSdrh case TK_IN: { 3985e3365e6cSdrh int destIfFalse = sqlite3VdbeMakeLabel(v); 3986e3365e6cSdrh int destIfNull = jumpIfNull ? dest : destIfFalse; 3987e3365e6cSdrh sqlite3ExprCodeIN(pParse, pExpr, destIfFalse, destIfNull); 3988076e85f5Sdrh sqlite3VdbeGoto(v, dest); 3989e3365e6cSdrh sqlite3VdbeResolveLabel(v, destIfFalse); 3990e3365e6cSdrh break; 3991e3365e6cSdrh } 3992bb201344Sshaneh #endif 3993cce7d176Sdrh default: { 3994991a1985Sdrh if( exprAlwaysTrue(pExpr) ){ 3995076e85f5Sdrh sqlite3VdbeGoto(v, dest); 3996991a1985Sdrh }else if( exprAlwaysFalse(pExpr) ){ 3997991a1985Sdrh /* No-op */ 3998991a1985Sdrh }else{ 39992dcef11bSdrh r1 = sqlite3ExprCodeTemp(pParse, pExpr, ®Free1); 40002dcef11bSdrh sqlite3VdbeAddOp3(v, OP_If, r1, dest, jumpIfNull!=0); 4001688852abSdrh VdbeCoverage(v); 4002c5499befSdrh testcase( regFree1==0 ); 4003c5499befSdrh testcase( jumpIfNull==0 ); 4004991a1985Sdrh } 4005cce7d176Sdrh break; 4006cce7d176Sdrh } 4007cce7d176Sdrh } 40082dcef11bSdrh sqlite3ReleaseTempReg(pParse, regFree1); 40092dcef11bSdrh sqlite3ReleaseTempReg(pParse, regFree2); 4010cce7d176Sdrh } 4011cce7d176Sdrh 4012cce7d176Sdrh /* 401366b89c8fSdrh ** Generate code for a boolean expression such that a jump is made 4014cce7d176Sdrh ** to the label "dest" if the expression is false but execution 4015cce7d176Sdrh ** continues straight thru if the expression is true. 4016f5905aa7Sdrh ** 4017f5905aa7Sdrh ** If the expression evaluates to NULL (neither true nor false) then 401835573356Sdrh ** jump if jumpIfNull is SQLITE_JUMPIFNULL or fall through if jumpIfNull 401935573356Sdrh ** is 0. 4020cce7d176Sdrh */ 40214adee20fSdanielk1977 void sqlite3ExprIfFalse(Parse *pParse, Expr *pExpr, int dest, int jumpIfNull){ 4022cce7d176Sdrh Vdbe *v = pParse->pVdbe; 4023cce7d176Sdrh int op = 0; 40242dcef11bSdrh int regFree1 = 0; 40252dcef11bSdrh int regFree2 = 0; 40262dcef11bSdrh int r1, r2; 40272dcef11bSdrh 402835573356Sdrh assert( jumpIfNull==SQLITE_JUMPIFNULL || jumpIfNull==0 ); 402948864df9Smistachkin if( NEVER(v==0) ) return; /* Existence of VDBE checked by caller */ 403033cd4909Sdrh if( pExpr==0 ) return; 4031f2bc013cSdrh 4032f2bc013cSdrh /* The value of pExpr->op and op are related as follows: 4033f2bc013cSdrh ** 4034f2bc013cSdrh ** pExpr->op op 4035f2bc013cSdrh ** --------- ---------- 4036f2bc013cSdrh ** TK_ISNULL OP_NotNull 4037f2bc013cSdrh ** TK_NOTNULL OP_IsNull 4038f2bc013cSdrh ** TK_NE OP_Eq 4039f2bc013cSdrh ** TK_EQ OP_Ne 4040f2bc013cSdrh ** TK_GT OP_Le 4041f2bc013cSdrh ** TK_LE OP_Gt 4042f2bc013cSdrh ** TK_GE OP_Lt 4043f2bc013cSdrh ** TK_LT OP_Ge 4044f2bc013cSdrh ** 4045f2bc013cSdrh ** For other values of pExpr->op, op is undefined and unused. 4046f2bc013cSdrh ** The value of TK_ and OP_ constants are arranged such that we 4047f2bc013cSdrh ** can compute the mapping above using the following expression. 4048f2bc013cSdrh ** Assert()s verify that the computation is correct. 4049f2bc013cSdrh */ 4050f2bc013cSdrh op = ((pExpr->op+(TK_ISNULL&1))^1)-(TK_ISNULL&1); 4051f2bc013cSdrh 4052f2bc013cSdrh /* Verify correct alignment of TK_ and OP_ constants 4053f2bc013cSdrh */ 4054f2bc013cSdrh assert( pExpr->op!=TK_ISNULL || op==OP_NotNull ); 4055f2bc013cSdrh assert( pExpr->op!=TK_NOTNULL || op==OP_IsNull ); 4056f2bc013cSdrh assert( pExpr->op!=TK_NE || op==OP_Eq ); 4057f2bc013cSdrh assert( pExpr->op!=TK_EQ || op==OP_Ne ); 4058f2bc013cSdrh assert( pExpr->op!=TK_LT || op==OP_Ge ); 4059f2bc013cSdrh assert( pExpr->op!=TK_LE || op==OP_Gt ); 4060f2bc013cSdrh assert( pExpr->op!=TK_GT || op==OP_Le ); 4061f2bc013cSdrh assert( pExpr->op!=TK_GE || op==OP_Lt ); 4062f2bc013cSdrh 4063ba00e30aSdan switch( pExpr->op ){ 4064cce7d176Sdrh case TK_AND: { 4065c5499befSdrh testcase( jumpIfNull==0 ); 40664adee20fSdanielk1977 sqlite3ExprIfFalse(pParse, pExpr->pLeft, dest, jumpIfNull); 406754e2adb5Sdrh sqlite3ExprCachePush(pParse); 40684adee20fSdanielk1977 sqlite3ExprIfFalse(pParse, pExpr->pRight, dest, jumpIfNull); 4069d2490904Sdrh sqlite3ExprCachePop(pParse); 4070cce7d176Sdrh break; 4071cce7d176Sdrh } 4072cce7d176Sdrh case TK_OR: { 40734adee20fSdanielk1977 int d2 = sqlite3VdbeMakeLabel(v); 4074c5499befSdrh testcase( jumpIfNull==0 ); 407535573356Sdrh sqlite3ExprIfTrue(pParse, pExpr->pLeft, d2, jumpIfNull^SQLITE_JUMPIFNULL); 407654e2adb5Sdrh sqlite3ExprCachePush(pParse); 40774adee20fSdanielk1977 sqlite3ExprIfFalse(pParse, pExpr->pRight, dest, jumpIfNull); 40784adee20fSdanielk1977 sqlite3VdbeResolveLabel(v, d2); 4079d2490904Sdrh sqlite3ExprCachePop(pParse); 4080cce7d176Sdrh break; 4081cce7d176Sdrh } 4082cce7d176Sdrh case TK_NOT: { 40835c03f30aSdrh testcase( jumpIfNull==0 ); 40844adee20fSdanielk1977 sqlite3ExprIfTrue(pParse, pExpr->pLeft, dest, jumpIfNull); 4085cce7d176Sdrh break; 4086cce7d176Sdrh } 4087de845c2fSdrh case TK_IS: 4088de845c2fSdrh case TK_ISNOT: 4089de845c2fSdrh testcase( pExpr->op==TK_IS ); 4090de845c2fSdrh testcase( pExpr->op==TK_ISNOT ); 4091de845c2fSdrh op = (pExpr->op==TK_IS) ? TK_NE : TK_EQ; 4092de845c2fSdrh jumpIfNull = SQLITE_NULLEQ; 4093de845c2fSdrh /* Fall thru */ 4094cce7d176Sdrh case TK_LT: 4095cce7d176Sdrh case TK_LE: 4096cce7d176Sdrh case TK_GT: 4097cce7d176Sdrh case TK_GE: 4098cce7d176Sdrh case TK_NE: 4099cce7d176Sdrh case TK_EQ: { 4100ba00e30aSdan if( pExpr->pLeft->flags & EP_Vector ) goto default_expr; 4101ba00e30aSdan 4102c5499befSdrh testcase( jumpIfNull==0 ); 4103b6da74ebSdrh r1 = sqlite3ExprCodeTemp(pParse, pExpr->pLeft, ®Free1); 4104b6da74ebSdrh r2 = sqlite3ExprCodeTemp(pParse, pExpr->pRight, ®Free2); 410535573356Sdrh codeCompare(pParse, pExpr->pLeft, pExpr->pRight, op, 41062dcef11bSdrh r1, r2, dest, jumpIfNull); 41077d176105Sdrh assert(TK_LT==OP_Lt); testcase(op==OP_Lt); VdbeCoverageIf(v,op==OP_Lt); 41087d176105Sdrh assert(TK_LE==OP_Le); testcase(op==OP_Le); VdbeCoverageIf(v,op==OP_Le); 41097d176105Sdrh assert(TK_GT==OP_Gt); testcase(op==OP_Gt); VdbeCoverageIf(v,op==OP_Gt); 41107d176105Sdrh assert(TK_GE==OP_Ge); testcase(op==OP_Ge); VdbeCoverageIf(v,op==OP_Ge); 4111de845c2fSdrh assert(TK_EQ==OP_Eq); testcase(op==OP_Eq); 4112de845c2fSdrh VdbeCoverageIf(v, op==OP_Eq && jumpIfNull!=SQLITE_NULLEQ); 4113de845c2fSdrh VdbeCoverageIf(v, op==OP_Eq && jumpIfNull==SQLITE_NULLEQ); 4114de845c2fSdrh assert(TK_NE==OP_Ne); testcase(op==OP_Ne); 4115de845c2fSdrh VdbeCoverageIf(v, op==OP_Ne && jumpIfNull!=SQLITE_NULLEQ); 4116de845c2fSdrh VdbeCoverageIf(v, op==OP_Ne && jumpIfNull==SQLITE_NULLEQ); 41176a2fe093Sdrh testcase( regFree1==0 ); 41186a2fe093Sdrh testcase( regFree2==0 ); 41196a2fe093Sdrh break; 41206a2fe093Sdrh } 4121cce7d176Sdrh case TK_ISNULL: 4122cce7d176Sdrh case TK_NOTNULL: { 41232dcef11bSdrh r1 = sqlite3ExprCodeTemp(pParse, pExpr->pLeft, ®Free1); 41242dcef11bSdrh sqlite3VdbeAddOp2(v, op, r1, dest); 41257d176105Sdrh testcase( op==TK_ISNULL ); VdbeCoverageIf(v, op==TK_ISNULL); 41267d176105Sdrh testcase( op==TK_NOTNULL ); VdbeCoverageIf(v, op==TK_NOTNULL); 4127c5499befSdrh testcase( regFree1==0 ); 4128cce7d176Sdrh break; 4129cce7d176Sdrh } 4130fef5208cSdrh case TK_BETWEEN: { 41315c03f30aSdrh testcase( jumpIfNull==0 ); 413271c57db0Sdan exprCodeBetween(pParse, pExpr, dest, sqlite3ExprIfFalse, jumpIfNull); 4133fef5208cSdrh break; 4134fef5208cSdrh } 4135bb201344Sshaneh #ifndef SQLITE_OMIT_SUBQUERY 4136e3365e6cSdrh case TK_IN: { 4137e3365e6cSdrh if( jumpIfNull ){ 4138e3365e6cSdrh sqlite3ExprCodeIN(pParse, pExpr, dest, dest); 4139e3365e6cSdrh }else{ 4140e3365e6cSdrh int destIfNull = sqlite3VdbeMakeLabel(v); 4141e3365e6cSdrh sqlite3ExprCodeIN(pParse, pExpr, dest, destIfNull); 4142e3365e6cSdrh sqlite3VdbeResolveLabel(v, destIfNull); 4143e3365e6cSdrh } 4144e3365e6cSdrh break; 4145e3365e6cSdrh } 4146bb201344Sshaneh #endif 4147cce7d176Sdrh default: { 4148ba00e30aSdan default_expr: 4149991a1985Sdrh if( exprAlwaysFalse(pExpr) ){ 4150076e85f5Sdrh sqlite3VdbeGoto(v, dest); 4151991a1985Sdrh }else if( exprAlwaysTrue(pExpr) ){ 4152991a1985Sdrh /* no-op */ 4153991a1985Sdrh }else{ 41542dcef11bSdrh r1 = sqlite3ExprCodeTemp(pParse, pExpr, ®Free1); 41552dcef11bSdrh sqlite3VdbeAddOp3(v, OP_IfNot, r1, dest, jumpIfNull!=0); 4156688852abSdrh VdbeCoverage(v); 4157c5499befSdrh testcase( regFree1==0 ); 4158c5499befSdrh testcase( jumpIfNull==0 ); 4159991a1985Sdrh } 4160cce7d176Sdrh break; 4161cce7d176Sdrh } 4162cce7d176Sdrh } 41632dcef11bSdrh sqlite3ReleaseTempReg(pParse, regFree1); 41642dcef11bSdrh sqlite3ReleaseTempReg(pParse, regFree2); 4165cce7d176Sdrh } 41662282792aSdrh 41672282792aSdrh /* 416872bc8208Sdrh ** Like sqlite3ExprIfFalse() except that a copy is made of pExpr before 416972bc8208Sdrh ** code generation, and that copy is deleted after code generation. This 417072bc8208Sdrh ** ensures that the original pExpr is unchanged. 417172bc8208Sdrh */ 417272bc8208Sdrh void sqlite3ExprIfFalseDup(Parse *pParse, Expr *pExpr, int dest,int jumpIfNull){ 417372bc8208Sdrh sqlite3 *db = pParse->db; 417472bc8208Sdrh Expr *pCopy = sqlite3ExprDup(db, pExpr, 0); 417572bc8208Sdrh if( db->mallocFailed==0 ){ 417672bc8208Sdrh sqlite3ExprIfFalse(pParse, pCopy, dest, jumpIfNull); 417772bc8208Sdrh } 417872bc8208Sdrh sqlite3ExprDelete(db, pCopy); 417972bc8208Sdrh } 418072bc8208Sdrh 418172bc8208Sdrh 418272bc8208Sdrh /* 41831d9da70aSdrh ** Do a deep comparison of two expression trees. Return 0 if the two 41841d9da70aSdrh ** expressions are completely identical. Return 1 if they differ only 41851d9da70aSdrh ** by a COLLATE operator at the top level. Return 2 if there are differences 41861d9da70aSdrh ** other than the top-level COLLATE operator. 4187d40aab0eSdrh ** 4188619a1305Sdrh ** If any subelement of pB has Expr.iTable==(-1) then it is allowed 4189619a1305Sdrh ** to compare equal to an equivalent element in pA with Expr.iTable==iTab. 4190619a1305Sdrh ** 419166518ca7Sdrh ** The pA side might be using TK_REGISTER. If that is the case and pB is 419266518ca7Sdrh ** not using TK_REGISTER but is otherwise equivalent, then still return 0. 419366518ca7Sdrh ** 41941d9da70aSdrh ** Sometimes this routine will return 2 even if the two expressions 4195d40aab0eSdrh ** really are equivalent. If we cannot prove that the expressions are 41961d9da70aSdrh ** identical, we return 2 just to be safe. So if this routine 41971d9da70aSdrh ** returns 2, then you do not really know for certain if the two 41981d9da70aSdrh ** expressions are the same. But if you get a 0 or 1 return, then you 4199d40aab0eSdrh ** can be sure the expressions are the same. In the places where 42001d9da70aSdrh ** this routine is used, it does not hurt to get an extra 2 - that 4201d40aab0eSdrh ** just might result in some slightly slower code. But returning 42021d9da70aSdrh ** an incorrect 0 or 1 could lead to a malfunction. 42032282792aSdrh */ 4204619a1305Sdrh int sqlite3ExprCompare(Expr *pA, Expr *pB, int iTab){ 420510d1edf0Sdrh u32 combinedFlags; 42064b202ae2Sdanielk1977 if( pA==0 || pB==0 ){ 42071d9da70aSdrh return pB==pA ? 0 : 2; 42082282792aSdrh } 420910d1edf0Sdrh combinedFlags = pA->flags | pB->flags; 421010d1edf0Sdrh if( combinedFlags & EP_IntValue ){ 421110d1edf0Sdrh if( (pA->flags&pB->flags&EP_IntValue)!=0 && pA->u.iValue==pB->u.iValue ){ 421210d1edf0Sdrh return 0; 421310d1edf0Sdrh } 42141d9da70aSdrh return 2; 42156ab3a2ecSdanielk1977 } 4216c2acc4e4Sdrh if( pA->op!=pB->op ){ 4217619a1305Sdrh if( pA->op==TK_COLLATE && sqlite3ExprCompare(pA->pLeft, pB, iTab)<2 ){ 4218ae80ddeaSdrh return 1; 4219ae80ddeaSdrh } 4220619a1305Sdrh if( pB->op==TK_COLLATE && sqlite3ExprCompare(pA, pB->pLeft, iTab)<2 ){ 4221ae80ddeaSdrh return 1; 4222ae80ddeaSdrh } 4223ae80ddeaSdrh return 2; 4224ae80ddeaSdrh } 42252edc5fd7Sdrh if( pA->op!=TK_COLUMN && pA->op!=TK_AGG_COLUMN && pA->u.zToken ){ 4226390b88a4Sdrh if( pA->op==TK_FUNCTION ){ 4227390b88a4Sdrh if( sqlite3StrICmp(pA->u.zToken,pB->u.zToken)!=0 ) return 2; 4228390b88a4Sdrh }else if( strcmp(pA->u.zToken,pB->u.zToken)!=0 ){ 422910d1edf0Sdrh return pA->op==TK_COLLATE ? 1 : 2; 423010d1edf0Sdrh } 423110d1edf0Sdrh } 423210d1edf0Sdrh if( (pA->flags & EP_Distinct)!=(pB->flags & EP_Distinct) ) return 2; 423385f8aa79Sdrh if( ALWAYS((combinedFlags & EP_TokenOnly)==0) ){ 423410d1edf0Sdrh if( combinedFlags & EP_xIsSelect ) return 2; 4235619a1305Sdrh if( sqlite3ExprCompare(pA->pLeft, pB->pLeft, iTab) ) return 2; 4236619a1305Sdrh if( sqlite3ExprCompare(pA->pRight, pB->pRight, iTab) ) return 2; 4237619a1305Sdrh if( sqlite3ExprListCompare(pA->x.pList, pB->x.pList, iTab) ) return 2; 42387693c42fSdrh if( ALWAYS((combinedFlags & EP_Reduced)==0) && pA->op!=TK_STRING ){ 4239619a1305Sdrh if( pA->iColumn!=pB->iColumn ) return 2; 424066518ca7Sdrh if( pA->iTable!=pB->iTable 424185f8aa79Sdrh && (pA->iTable!=iTab || NEVER(pB->iTable>=0)) ) return 2; 42421d9da70aSdrh } 42431d9da70aSdrh } 42442646da7eSdrh return 0; 42452646da7eSdrh } 42462282792aSdrh 42478c6f666bSdrh /* 42488c6f666bSdrh ** Compare two ExprList objects. Return 0 if they are identical and 42498c6f666bSdrh ** non-zero if they differ in any way. 42508c6f666bSdrh ** 4251619a1305Sdrh ** If any subelement of pB has Expr.iTable==(-1) then it is allowed 4252619a1305Sdrh ** to compare equal to an equivalent element in pA with Expr.iTable==iTab. 4253619a1305Sdrh ** 42548c6f666bSdrh ** This routine might return non-zero for equivalent ExprLists. The 42558c6f666bSdrh ** only consequence will be disabled optimizations. But this routine 42568c6f666bSdrh ** must never return 0 if the two ExprList objects are different, or 42578c6f666bSdrh ** a malfunction will result. 42588c6f666bSdrh ** 42598c6f666bSdrh ** Two NULL pointers are considered to be the same. But a NULL pointer 42608c6f666bSdrh ** always differs from a non-NULL pointer. 42618c6f666bSdrh */ 4262619a1305Sdrh int sqlite3ExprListCompare(ExprList *pA, ExprList *pB, int iTab){ 42638c6f666bSdrh int i; 42648c6f666bSdrh if( pA==0 && pB==0 ) return 0; 42658c6f666bSdrh if( pA==0 || pB==0 ) return 1; 42668c6f666bSdrh if( pA->nExpr!=pB->nExpr ) return 1; 42678c6f666bSdrh for(i=0; i<pA->nExpr; i++){ 42688c6f666bSdrh Expr *pExprA = pA->a[i].pExpr; 42698c6f666bSdrh Expr *pExprB = pB->a[i].pExpr; 42708c6f666bSdrh if( pA->a[i].sortOrder!=pB->a[i].sortOrder ) return 1; 4271619a1305Sdrh if( sqlite3ExprCompare(pExprA, pExprB, iTab) ) return 1; 42728c6f666bSdrh } 42738c6f666bSdrh return 0; 42748c6f666bSdrh } 427513449892Sdrh 42762282792aSdrh /* 42774bd5f73fSdrh ** Return true if we can prove the pE2 will always be true if pE1 is 42784bd5f73fSdrh ** true. Return false if we cannot complete the proof or if pE2 might 42794bd5f73fSdrh ** be false. Examples: 42804bd5f73fSdrh ** 4281619a1305Sdrh ** pE1: x==5 pE2: x==5 Result: true 42824bd5f73fSdrh ** pE1: x>0 pE2: x==5 Result: false 4283619a1305Sdrh ** pE1: x=21 pE2: x=21 OR y=43 Result: true 42844bd5f73fSdrh ** pE1: x!=123 pE2: x IS NOT NULL Result: true 4285619a1305Sdrh ** pE1: x!=?1 pE2: x IS NOT NULL Result: true 4286619a1305Sdrh ** pE1: x IS NULL pE2: x IS NOT NULL Result: false 4287619a1305Sdrh ** pE1: x IS ?2 pE2: x IS NOT NULL Reuslt: false 42884bd5f73fSdrh ** 42894bd5f73fSdrh ** When comparing TK_COLUMN nodes between pE1 and pE2, if pE2 has 42904bd5f73fSdrh ** Expr.iTable<0 then assume a table number given by iTab. 42914bd5f73fSdrh ** 42924bd5f73fSdrh ** When in doubt, return false. Returning true might give a performance 42934bd5f73fSdrh ** improvement. Returning false might cause a performance reduction, but 42944bd5f73fSdrh ** it will always give the correct answer and is hence always safe. 42954bd5f73fSdrh */ 42964bd5f73fSdrh int sqlite3ExprImpliesExpr(Expr *pE1, Expr *pE2, int iTab){ 4297619a1305Sdrh if( sqlite3ExprCompare(pE1, pE2, iTab)==0 ){ 4298619a1305Sdrh return 1; 4299619a1305Sdrh } 4300619a1305Sdrh if( pE2->op==TK_OR 4301619a1305Sdrh && (sqlite3ExprImpliesExpr(pE1, pE2->pLeft, iTab) 4302619a1305Sdrh || sqlite3ExprImpliesExpr(pE1, pE2->pRight, iTab) ) 4303619a1305Sdrh ){ 4304619a1305Sdrh return 1; 4305619a1305Sdrh } 4306619a1305Sdrh if( pE2->op==TK_NOTNULL 4307619a1305Sdrh && sqlite3ExprCompare(pE1->pLeft, pE2->pLeft, iTab)==0 4308619a1305Sdrh && (pE1->op!=TK_ISNULL && pE1->op!=TK_IS) 4309619a1305Sdrh ){ 4310619a1305Sdrh return 1; 4311619a1305Sdrh } 4312619a1305Sdrh return 0; 43134bd5f73fSdrh } 43144bd5f73fSdrh 43154bd5f73fSdrh /* 4316030796dfSdrh ** An instance of the following structure is used by the tree walker 4317030796dfSdrh ** to count references to table columns in the arguments of an 4318ed551b95Sdrh ** aggregate function, in order to implement the 4319ed551b95Sdrh ** sqlite3FunctionThisSrc() routine. 4320374fdce4Sdrh */ 4321030796dfSdrh struct SrcCount { 4322030796dfSdrh SrcList *pSrc; /* One particular FROM clause in a nested query */ 4323030796dfSdrh int nThis; /* Number of references to columns in pSrcList */ 4324030796dfSdrh int nOther; /* Number of references to columns in other FROM clauses */ 4325030796dfSdrh }; 4326030796dfSdrh 4327030796dfSdrh /* 4328030796dfSdrh ** Count the number of references to columns. 4329030796dfSdrh */ 4330030796dfSdrh static int exprSrcCount(Walker *pWalker, Expr *pExpr){ 4331fb0a6081Sdrh /* The NEVER() on the second term is because sqlite3FunctionUsesThisSrc() 4332fb0a6081Sdrh ** is always called before sqlite3ExprAnalyzeAggregates() and so the 4333fb0a6081Sdrh ** TK_COLUMNs have not yet been converted into TK_AGG_COLUMN. If 4334fb0a6081Sdrh ** sqlite3FunctionUsesThisSrc() is used differently in the future, the 4335fb0a6081Sdrh ** NEVER() will need to be removed. */ 4336fb0a6081Sdrh if( pExpr->op==TK_COLUMN || NEVER(pExpr->op==TK_AGG_COLUMN) ){ 4337374fdce4Sdrh int i; 4338030796dfSdrh struct SrcCount *p = pWalker->u.pSrcCount; 4339030796dfSdrh SrcList *pSrc = p->pSrc; 4340655814d2Sdrh int nSrc = pSrc ? pSrc->nSrc : 0; 4341655814d2Sdrh for(i=0; i<nSrc; i++){ 4342030796dfSdrh if( pExpr->iTable==pSrc->a[i].iCursor ) break; 4343374fdce4Sdrh } 4344655814d2Sdrh if( i<nSrc ){ 4345030796dfSdrh p->nThis++; 4346374fdce4Sdrh }else{ 4347030796dfSdrh p->nOther++; 4348374fdce4Sdrh } 4349374fdce4Sdrh } 4350030796dfSdrh return WRC_Continue; 4351030796dfSdrh } 4352374fdce4Sdrh 4353374fdce4Sdrh /* 4354030796dfSdrh ** Determine if any of the arguments to the pExpr Function reference 4355030796dfSdrh ** pSrcList. Return true if they do. Also return true if the function 4356030796dfSdrh ** has no arguments or has only constant arguments. Return false if pExpr 4357030796dfSdrh ** references columns but not columns of tables found in pSrcList. 4358374fdce4Sdrh */ 4359030796dfSdrh int sqlite3FunctionUsesThisSrc(Expr *pExpr, SrcList *pSrcList){ 4360374fdce4Sdrh Walker w; 4361030796dfSdrh struct SrcCount cnt; 4362374fdce4Sdrh assert( pExpr->op==TK_AGG_FUNCTION ); 4363374fdce4Sdrh memset(&w, 0, sizeof(w)); 4364030796dfSdrh w.xExprCallback = exprSrcCount; 4365030796dfSdrh w.u.pSrcCount = &cnt; 4366030796dfSdrh cnt.pSrc = pSrcList; 4367030796dfSdrh cnt.nThis = 0; 4368030796dfSdrh cnt.nOther = 0; 4369030796dfSdrh sqlite3WalkExprList(&w, pExpr->x.pList); 4370030796dfSdrh return cnt.nThis>0 || cnt.nOther==0; 4371374fdce4Sdrh } 4372374fdce4Sdrh 4373374fdce4Sdrh /* 437413449892Sdrh ** Add a new element to the pAggInfo->aCol[] array. Return the index of 437513449892Sdrh ** the new element. Return a negative number if malloc fails. 43762282792aSdrh */ 437717435752Sdrh static int addAggInfoColumn(sqlite3 *db, AggInfo *pInfo){ 437813449892Sdrh int i; 4379cf643729Sdrh pInfo->aCol = sqlite3ArrayAllocate( 438017435752Sdrh db, 4381cf643729Sdrh pInfo->aCol, 4382cf643729Sdrh sizeof(pInfo->aCol[0]), 4383cf643729Sdrh &pInfo->nColumn, 4384cf643729Sdrh &i 4385cf643729Sdrh ); 438613449892Sdrh return i; 43872282792aSdrh } 438813449892Sdrh 438913449892Sdrh /* 439013449892Sdrh ** Add a new element to the pAggInfo->aFunc[] array. Return the index of 439113449892Sdrh ** the new element. Return a negative number if malloc fails. 439213449892Sdrh */ 439317435752Sdrh static int addAggInfoFunc(sqlite3 *db, AggInfo *pInfo){ 439413449892Sdrh int i; 4395cf643729Sdrh pInfo->aFunc = sqlite3ArrayAllocate( 439617435752Sdrh db, 4397cf643729Sdrh pInfo->aFunc, 4398cf643729Sdrh sizeof(pInfo->aFunc[0]), 4399cf643729Sdrh &pInfo->nFunc, 4400cf643729Sdrh &i 4401cf643729Sdrh ); 440213449892Sdrh return i; 44032282792aSdrh } 44042282792aSdrh 44052282792aSdrh /* 44067d10d5a6Sdrh ** This is the xExprCallback for a tree walker. It is used to 44077d10d5a6Sdrh ** implement sqlite3ExprAnalyzeAggregates(). See sqlite3ExprAnalyzeAggregates 4408626a879aSdrh ** for additional information. 44092282792aSdrh */ 44107d10d5a6Sdrh static int analyzeAggregate(Walker *pWalker, Expr *pExpr){ 44112282792aSdrh int i; 44127d10d5a6Sdrh NameContext *pNC = pWalker->u.pNC; 4413a58fdfb1Sdanielk1977 Parse *pParse = pNC->pParse; 4414a58fdfb1Sdanielk1977 SrcList *pSrcList = pNC->pSrcList; 441513449892Sdrh AggInfo *pAggInfo = pNC->pAggInfo; 441613449892Sdrh 44172282792aSdrh switch( pExpr->op ){ 441889c69d00Sdrh case TK_AGG_COLUMN: 4419967e8b73Sdrh case TK_COLUMN: { 44208b213899Sdrh testcase( pExpr->op==TK_AGG_COLUMN ); 44218b213899Sdrh testcase( pExpr->op==TK_COLUMN ); 442213449892Sdrh /* Check to see if the column is in one of the tables in the FROM 442313449892Sdrh ** clause of the aggregate query */ 442420bc393cSdrh if( ALWAYS(pSrcList!=0) ){ 442513449892Sdrh struct SrcList_item *pItem = pSrcList->a; 442613449892Sdrh for(i=0; i<pSrcList->nSrc; i++, pItem++){ 442713449892Sdrh struct AggInfo_col *pCol; 4428c5cd1249Sdrh assert( !ExprHasProperty(pExpr, EP_TokenOnly|EP_Reduced) ); 442913449892Sdrh if( pExpr->iTable==pItem->iCursor ){ 443013449892Sdrh /* If we reach this point, it means that pExpr refers to a table 443113449892Sdrh ** that is in the FROM clause of the aggregate query. 443213449892Sdrh ** 443313449892Sdrh ** Make an entry for the column in pAggInfo->aCol[] if there 443413449892Sdrh ** is not an entry there already. 443513449892Sdrh */ 44367f906d63Sdrh int k; 443713449892Sdrh pCol = pAggInfo->aCol; 44387f906d63Sdrh for(k=0; k<pAggInfo->nColumn; k++, pCol++){ 443913449892Sdrh if( pCol->iTable==pExpr->iTable && 444013449892Sdrh pCol->iColumn==pExpr->iColumn ){ 44412282792aSdrh break; 44422282792aSdrh } 44432282792aSdrh } 44441e536953Sdanielk1977 if( (k>=pAggInfo->nColumn) 44451e536953Sdanielk1977 && (k = addAggInfoColumn(pParse->db, pAggInfo))>=0 44461e536953Sdanielk1977 ){ 44477f906d63Sdrh pCol = &pAggInfo->aCol[k]; 44480817d0dfSdanielk1977 pCol->pTab = pExpr->pTab; 444913449892Sdrh pCol->iTable = pExpr->iTable; 445013449892Sdrh pCol->iColumn = pExpr->iColumn; 44510a07c107Sdrh pCol->iMem = ++pParse->nMem; 445213449892Sdrh pCol->iSorterColumn = -1; 44535774b806Sdrh pCol->pExpr = pExpr; 445413449892Sdrh if( pAggInfo->pGroupBy ){ 445513449892Sdrh int j, n; 445613449892Sdrh ExprList *pGB = pAggInfo->pGroupBy; 445713449892Sdrh struct ExprList_item *pTerm = pGB->a; 445813449892Sdrh n = pGB->nExpr; 445913449892Sdrh for(j=0; j<n; j++, pTerm++){ 446013449892Sdrh Expr *pE = pTerm->pExpr; 446113449892Sdrh if( pE->op==TK_COLUMN && pE->iTable==pExpr->iTable && 446213449892Sdrh pE->iColumn==pExpr->iColumn ){ 446313449892Sdrh pCol->iSorterColumn = j; 446413449892Sdrh break; 44652282792aSdrh } 446613449892Sdrh } 446713449892Sdrh } 446813449892Sdrh if( pCol->iSorterColumn<0 ){ 446913449892Sdrh pCol->iSorterColumn = pAggInfo->nSortingColumn++; 447013449892Sdrh } 447113449892Sdrh } 447213449892Sdrh /* There is now an entry for pExpr in pAggInfo->aCol[] (either 447313449892Sdrh ** because it was there before or because we just created it). 447413449892Sdrh ** Convert the pExpr to be a TK_AGG_COLUMN referring to that 447513449892Sdrh ** pAggInfo->aCol[] entry. 447613449892Sdrh */ 4477ebb6a65dSdrh ExprSetVVAProperty(pExpr, EP_NoReduce); 447813449892Sdrh pExpr->pAggInfo = pAggInfo; 447913449892Sdrh pExpr->op = TK_AGG_COLUMN; 4480cf697396Sshane pExpr->iAgg = (i16)k; 448113449892Sdrh break; 448213449892Sdrh } /* endif pExpr->iTable==pItem->iCursor */ 448313449892Sdrh } /* end loop over pSrcList */ 4484a58fdfb1Sdanielk1977 } 44857d10d5a6Sdrh return WRC_Prune; 44862282792aSdrh } 44872282792aSdrh case TK_AGG_FUNCTION: { 44883a8c4be7Sdrh if( (pNC->ncFlags & NC_InAggFunc)==0 4489ed551b95Sdrh && pWalker->walkerDepth==pExpr->op2 44903a8c4be7Sdrh ){ 449113449892Sdrh /* Check to see if pExpr is a duplicate of another aggregate 449213449892Sdrh ** function that is already in the pAggInfo structure 449313449892Sdrh */ 449413449892Sdrh struct AggInfo_func *pItem = pAggInfo->aFunc; 449513449892Sdrh for(i=0; i<pAggInfo->nFunc; i++, pItem++){ 4496619a1305Sdrh if( sqlite3ExprCompare(pItem->pExpr, pExpr, -1)==0 ){ 44972282792aSdrh break; 44982282792aSdrh } 44992282792aSdrh } 450013449892Sdrh if( i>=pAggInfo->nFunc ){ 450113449892Sdrh /* pExpr is original. Make a new entry in pAggInfo->aFunc[] 450213449892Sdrh */ 450314db2665Sdanielk1977 u8 enc = ENC(pParse->db); 45041e536953Sdanielk1977 i = addAggInfoFunc(pParse->db, pAggInfo); 450513449892Sdrh if( i>=0 ){ 45066ab3a2ecSdanielk1977 assert( !ExprHasProperty(pExpr, EP_xIsSelect) ); 450713449892Sdrh pItem = &pAggInfo->aFunc[i]; 450813449892Sdrh pItem->pExpr = pExpr; 45090a07c107Sdrh pItem->iMem = ++pParse->nMem; 451033e619fcSdrh assert( !ExprHasProperty(pExpr, EP_IntValue) ); 451113449892Sdrh pItem->pFunc = sqlite3FindFunction(pParse->db, 451280738d9cSdrh pExpr->u.zToken, 45136ab3a2ecSdanielk1977 pExpr->x.pList ? pExpr->x.pList->nExpr : 0, enc, 0); 4514fd357974Sdrh if( pExpr->flags & EP_Distinct ){ 4515fd357974Sdrh pItem->iDistinct = pParse->nTab++; 4516fd357974Sdrh }else{ 4517fd357974Sdrh pItem->iDistinct = -1; 4518fd357974Sdrh } 45192282792aSdrh } 452013449892Sdrh } 452113449892Sdrh /* Make pExpr point to the appropriate pAggInfo->aFunc[] entry 452213449892Sdrh */ 4523c5cd1249Sdrh assert( !ExprHasProperty(pExpr, EP_TokenOnly|EP_Reduced) ); 4524ebb6a65dSdrh ExprSetVVAProperty(pExpr, EP_NoReduce); 4525cf697396Sshane pExpr->iAgg = (i16)i; 452613449892Sdrh pExpr->pAggInfo = pAggInfo; 45273a8c4be7Sdrh return WRC_Prune; 45286e83a57fSdrh }else{ 45296e83a57fSdrh return WRC_Continue; 45306e83a57fSdrh } 45312282792aSdrh } 4532a58fdfb1Sdanielk1977 } 45337d10d5a6Sdrh return WRC_Continue; 45347d10d5a6Sdrh } 45357d10d5a6Sdrh static int analyzeAggregatesInSelect(Walker *pWalker, Select *pSelect){ 4536d5a336efSdrh UNUSED_PARAMETER(pWalker); 4537d5a336efSdrh UNUSED_PARAMETER(pSelect); 45387d10d5a6Sdrh return WRC_Continue; 4539a58fdfb1Sdanielk1977 } 4540626a879aSdrh 4541626a879aSdrh /* 4542e8abb4caSdrh ** Analyze the pExpr expression looking for aggregate functions and 4543e8abb4caSdrh ** for variables that need to be added to AggInfo object that pNC->pAggInfo 4544e8abb4caSdrh ** points to. Additional entries are made on the AggInfo object as 4545e8abb4caSdrh ** necessary. 4546626a879aSdrh ** 4547626a879aSdrh ** This routine should only be called after the expression has been 45487d10d5a6Sdrh ** analyzed by sqlite3ResolveExprNames(). 4549626a879aSdrh */ 4550d2b3e23bSdrh void sqlite3ExprAnalyzeAggregates(NameContext *pNC, Expr *pExpr){ 45517d10d5a6Sdrh Walker w; 4552374fdce4Sdrh memset(&w, 0, sizeof(w)); 45537d10d5a6Sdrh w.xExprCallback = analyzeAggregate; 45547d10d5a6Sdrh w.xSelectCallback = analyzeAggregatesInSelect; 45557d10d5a6Sdrh w.u.pNC = pNC; 455620bc393cSdrh assert( pNC->pSrcList!=0 ); 45577d10d5a6Sdrh sqlite3WalkExpr(&w, pExpr); 45582282792aSdrh } 45595d9a4af9Sdrh 45605d9a4af9Sdrh /* 45615d9a4af9Sdrh ** Call sqlite3ExprAnalyzeAggregates() for every expression in an 45625d9a4af9Sdrh ** expression list. Return the number of errors. 45635d9a4af9Sdrh ** 45645d9a4af9Sdrh ** If an error is found, the analysis is cut short. 45655d9a4af9Sdrh */ 4566d2b3e23bSdrh void sqlite3ExprAnalyzeAggList(NameContext *pNC, ExprList *pList){ 45675d9a4af9Sdrh struct ExprList_item *pItem; 45685d9a4af9Sdrh int i; 45695d9a4af9Sdrh if( pList ){ 4570d2b3e23bSdrh for(pItem=pList->a, i=0; i<pList->nExpr; i++, pItem++){ 4571d2b3e23bSdrh sqlite3ExprAnalyzeAggregates(pNC, pItem->pExpr); 45725d9a4af9Sdrh } 45735d9a4af9Sdrh } 45745d9a4af9Sdrh } 4575892d3179Sdrh 4576892d3179Sdrh /* 4577ceea3321Sdrh ** Allocate a single new register for use to hold some intermediate result. 4578892d3179Sdrh */ 4579892d3179Sdrh int sqlite3GetTempReg(Parse *pParse){ 4580e55cbd72Sdrh if( pParse->nTempReg==0 ){ 4581892d3179Sdrh return ++pParse->nMem; 4582892d3179Sdrh } 45832f425f6bSdanielk1977 return pParse->aTempReg[--pParse->nTempReg]; 4584892d3179Sdrh } 4585ceea3321Sdrh 4586ceea3321Sdrh /* 4587ceea3321Sdrh ** Deallocate a register, making available for reuse for some other 4588ceea3321Sdrh ** purpose. 4589ceea3321Sdrh ** 4590ceea3321Sdrh ** If a register is currently being used by the column cache, then 459160ec914cSpeter.d.reid ** the deallocation is deferred until the column cache line that uses 4592ceea3321Sdrh ** the register becomes stale. 4593ceea3321Sdrh */ 4594892d3179Sdrh void sqlite3ReleaseTempReg(Parse *pParse, int iReg){ 45952dcef11bSdrh if( iReg && pParse->nTempReg<ArraySize(pParse->aTempReg) ){ 4596ceea3321Sdrh int i; 4597ceea3321Sdrh struct yColCache *p; 4598ceea3321Sdrh for(i=0, p=pParse->aColCache; i<SQLITE_N_COLCACHE; i++, p++){ 4599ceea3321Sdrh if( p->iReg==iReg ){ 4600ceea3321Sdrh p->tempReg = 1; 4601ceea3321Sdrh return; 4602ceea3321Sdrh } 4603ceea3321Sdrh } 4604892d3179Sdrh pParse->aTempReg[pParse->nTempReg++] = iReg; 4605892d3179Sdrh } 4606892d3179Sdrh } 4607892d3179Sdrh 4608892d3179Sdrh /* 4609892d3179Sdrh ** Allocate or deallocate a block of nReg consecutive registers 4610892d3179Sdrh */ 4611892d3179Sdrh int sqlite3GetTempRange(Parse *pParse, int nReg){ 4612e55cbd72Sdrh int i, n; 4613892d3179Sdrh i = pParse->iRangeReg; 4614e55cbd72Sdrh n = pParse->nRangeReg; 4615f49f3523Sdrh if( nReg<=n ){ 4616f49f3523Sdrh assert( !usedAsColumnCache(pParse, i, i+n-1) ); 4617892d3179Sdrh pParse->iRangeReg += nReg; 4618892d3179Sdrh pParse->nRangeReg -= nReg; 4619892d3179Sdrh }else{ 4620892d3179Sdrh i = pParse->nMem+1; 4621892d3179Sdrh pParse->nMem += nReg; 4622892d3179Sdrh } 4623892d3179Sdrh return i; 4624892d3179Sdrh } 4625892d3179Sdrh void sqlite3ReleaseTempRange(Parse *pParse, int iReg, int nReg){ 4626f49f3523Sdrh sqlite3ExprCacheRemove(pParse, iReg, nReg); 4627892d3179Sdrh if( nReg>pParse->nRangeReg ){ 4628892d3179Sdrh pParse->nRangeReg = nReg; 4629892d3179Sdrh pParse->iRangeReg = iReg; 4630892d3179Sdrh } 4631892d3179Sdrh } 4632cdc69557Sdrh 4633cdc69557Sdrh /* 4634cdc69557Sdrh ** Mark all temporary registers as being unavailable for reuse. 4635cdc69557Sdrh */ 4636cdc69557Sdrh void sqlite3ClearTempRegCache(Parse *pParse){ 4637cdc69557Sdrh pParse->nTempReg = 0; 4638cdc69557Sdrh pParse->nRangeReg = 0; 4639cdc69557Sdrh } 4640bb9b5f26Sdrh 4641bb9b5f26Sdrh /* 4642bb9b5f26Sdrh ** Validate that no temporary register falls within the range of 4643bb9b5f26Sdrh ** iFirst..iLast, inclusive. This routine is only call from within assert() 4644bb9b5f26Sdrh ** statements. 4645bb9b5f26Sdrh */ 4646bb9b5f26Sdrh #ifdef SQLITE_DEBUG 4647bb9b5f26Sdrh int sqlite3NoTempsInRange(Parse *pParse, int iFirst, int iLast){ 4648bb9b5f26Sdrh int i; 4649bb9b5f26Sdrh if( pParse->nRangeReg>0 4650bb9b5f26Sdrh && pParse->iRangeReg+pParse->nRangeReg<iLast 4651bb9b5f26Sdrh && pParse->iRangeReg>=iFirst 4652bb9b5f26Sdrh ){ 4653bb9b5f26Sdrh return 0; 4654bb9b5f26Sdrh } 4655bb9b5f26Sdrh for(i=0; i<pParse->nTempReg; i++){ 4656bb9b5f26Sdrh if( pParse->aTempReg[i]>=iFirst && pParse->aTempReg[i]<=iLast ){ 4657bb9b5f26Sdrh return 0; 4658bb9b5f26Sdrh } 4659bb9b5f26Sdrh } 4660bb9b5f26Sdrh return 1; 4661bb9b5f26Sdrh } 4662bb9b5f26Sdrh #endif /* SQLITE_DEBUG */ 4663