drh | cce7d17 | 2000-05-31 15:34:51 +0000 | [diff] [blame] | 1 | /* |
drh | b19a2bc | 2001-09-16 00:13:26 +0000 | [diff] [blame] | 2 | ** 2001 September 15 |
drh | cce7d17 | 2000-05-31 15:34:51 +0000 | [diff] [blame] | 3 | ** |
drh | b19a2bc | 2001-09-16 00:13:26 +0000 | [diff] [blame] | 4 | ** The author disclaims copyright to this source code. In place of |
| 5 | ** a legal notice, here is a blessing: |
drh | cce7d17 | 2000-05-31 15:34:51 +0000 | [diff] [blame] | 6 | ** |
drh | b19a2bc | 2001-09-16 00:13:26 +0000 | [diff] [blame] | 7 | ** May you do good and not evil. |
| 8 | ** May you find forgiveness for yourself and forgive others. |
| 9 | ** May you share freely, never taking more than you give. |
drh | cce7d17 | 2000-05-31 15:34:51 +0000 | [diff] [blame] | 10 | ** |
| 11 | ************************************************************************* |
drh | 1ccde15 | 2000-06-17 13:12:39 +0000 | [diff] [blame] | 12 | ** This file contains routines used for analyzing expressions and |
drh | b19a2bc | 2001-09-16 00:13:26 +0000 | [diff] [blame] | 13 | ** for generating VDBE code that evaluates expressions in SQLite. |
drh | cce7d17 | 2000-05-31 15:34:51 +0000 | [diff] [blame] | 14 | ** |
drh | 66ba23c | 2008-06-27 00:47:28 +0000 | [diff] [blame^] | 15 | ** $Id: expr.c,v 1.380 2008/06/27 00:47:29 drh Exp $ |
drh | cce7d17 | 2000-05-31 15:34:51 +0000 | [diff] [blame] | 16 | */ |
| 17 | #include "sqliteInt.h" |
drh | 04738cb | 2002-06-02 18:19:00 +0000 | [diff] [blame] | 18 | #include <ctype.h> |
drh | a2e0004 | 2002-01-22 03:13:42 +0000 | [diff] [blame] | 19 | |
danielk1977 | e014a83 | 2004-05-17 10:48:57 +0000 | [diff] [blame] | 20 | /* |
| 21 | ** Return the 'affinity' of the expression pExpr if any. |
| 22 | ** |
| 23 | ** If pExpr is a column, a reference to a column via an 'AS' alias, |
| 24 | ** or a sub-select with a column as the return value, then the |
| 25 | ** affinity of that column is returned. Otherwise, 0x00 is returned, |
| 26 | ** indicating no affinity for the expression. |
| 27 | ** |
| 28 | ** i.e. the WHERE clause expresssions in the following statements all |
| 29 | ** have an affinity: |
| 30 | ** |
| 31 | ** CREATE TABLE t1(a); |
| 32 | ** SELECT * FROM t1 WHERE a; |
| 33 | ** SELECT a AS b FROM t1 WHERE b; |
| 34 | ** SELECT * FROM t1 WHERE (select a from t1); |
| 35 | */ |
danielk1977 | bf3b721 | 2004-05-18 10:06:24 +0000 | [diff] [blame] | 36 | char sqlite3ExprAffinity(Expr *pExpr){ |
drh | 487e262 | 2005-06-25 18:42:14 +0000 | [diff] [blame] | 37 | int op = pExpr->op; |
drh | 487e262 | 2005-06-25 18:42:14 +0000 | [diff] [blame] | 38 | if( op==TK_SELECT ){ |
danielk1977 | bf3b721 | 2004-05-18 10:06:24 +0000 | [diff] [blame] | 39 | return sqlite3ExprAffinity(pExpr->pSelect->pEList->a[0].pExpr); |
danielk1977 | a37cdde | 2004-05-16 11:15:36 +0000 | [diff] [blame] | 40 | } |
drh | 487e262 | 2005-06-25 18:42:14 +0000 | [diff] [blame] | 41 | #ifndef SQLITE_OMIT_CAST |
| 42 | if( op==TK_CAST ){ |
drh | 8a51256 | 2005-11-14 22:29:05 +0000 | [diff] [blame] | 43 | return sqlite3AffinityType(&pExpr->token); |
drh | 487e262 | 2005-06-25 18:42:14 +0000 | [diff] [blame] | 44 | } |
| 45 | #endif |
danielk1977 | a37cdde | 2004-05-16 11:15:36 +0000 | [diff] [blame] | 46 | return pExpr->affinity; |
| 47 | } |
| 48 | |
drh | 53db145 | 2004-05-20 13:54:53 +0000 | [diff] [blame] | 49 | /* |
drh | 8b4c40d | 2007-02-01 23:02:45 +0000 | [diff] [blame] | 50 | ** Set the collating sequence for expression pExpr to be the collating |
| 51 | ** sequence named by pToken. Return a pointer to the revised expression. |
drh | a34001c | 2007-02-02 12:44:37 +0000 | [diff] [blame] | 52 | ** The collating sequence is marked as "explicit" using the EP_ExpCollate |
| 53 | ** flag. An explicit collating sequence will override implicit |
| 54 | ** collating sequences. |
drh | 8b4c40d | 2007-02-01 23:02:45 +0000 | [diff] [blame] | 55 | */ |
| 56 | Expr *sqlite3ExprSetColl(Parse *pParse, Expr *pExpr, Token *pName){ |
danielk1977 | 3900250 | 2007-11-12 09:50:26 +0000 | [diff] [blame] | 57 | char *zColl = 0; /* Dequoted name of collation sequence */ |
drh | 8b4c40d | 2007-02-01 23:02:45 +0000 | [diff] [blame] | 58 | CollSeq *pColl; |
danielk1977 | 3900250 | 2007-11-12 09:50:26 +0000 | [diff] [blame] | 59 | zColl = sqlite3NameFromToken(pParse->db, pName); |
| 60 | if( pExpr && zColl ){ |
| 61 | pColl = sqlite3LocateCollSeq(pParse, zColl, -1); |
| 62 | if( pColl ){ |
| 63 | pExpr->pColl = pColl; |
| 64 | pExpr->flags |= EP_ExpCollate; |
| 65 | } |
drh | 8b4c40d | 2007-02-01 23:02:45 +0000 | [diff] [blame] | 66 | } |
danielk1977 | 3900250 | 2007-11-12 09:50:26 +0000 | [diff] [blame] | 67 | sqlite3_free(zColl); |
drh | 8b4c40d | 2007-02-01 23:02:45 +0000 | [diff] [blame] | 68 | return pExpr; |
| 69 | } |
| 70 | |
| 71 | /* |
danielk1977 | 0202b29 | 2004-06-09 09:55:16 +0000 | [diff] [blame] | 72 | ** Return the default collation sequence for the expression pExpr. If |
| 73 | ** there is no default collation type, return 0. |
| 74 | */ |
danielk1977 | 7cedc8d | 2004-06-10 10:50:08 +0000 | [diff] [blame] | 75 | CollSeq *sqlite3ExprCollSeq(Parse *pParse, Expr *pExpr){ |
| 76 | CollSeq *pColl = 0; |
danielk1977 | 0202b29 | 2004-06-09 09:55:16 +0000 | [diff] [blame] | 77 | if( pExpr ){ |
drh | 7e09fe0 | 2007-06-20 16:13:23 +0000 | [diff] [blame] | 78 | int op; |
danielk1977 | 7cedc8d | 2004-06-10 10:50:08 +0000 | [diff] [blame] | 79 | pColl = pExpr->pColl; |
drh | 7e09fe0 | 2007-06-20 16:13:23 +0000 | [diff] [blame] | 80 | op = pExpr->op; |
| 81 | if( (op==TK_CAST || op==TK_UPLUS) && !pColl ){ |
danielk1977 | 7cedc8d | 2004-06-10 10:50:08 +0000 | [diff] [blame] | 82 | return sqlite3ExprCollSeq(pParse, pExpr->pLeft); |
danielk1977 | 0202b29 | 2004-06-09 09:55:16 +0000 | [diff] [blame] | 83 | } |
| 84 | } |
danielk1977 | 7cedc8d | 2004-06-10 10:50:08 +0000 | [diff] [blame] | 85 | if( sqlite3CheckCollSeq(pParse, pColl) ){ |
| 86 | pColl = 0; |
| 87 | } |
| 88 | return pColl; |
danielk1977 | 0202b29 | 2004-06-09 09:55:16 +0000 | [diff] [blame] | 89 | } |
| 90 | |
| 91 | /* |
drh | 626a879 | 2005-01-17 22:08:19 +0000 | [diff] [blame] | 92 | ** pExpr is an operand of a comparison operator. aff2 is the |
| 93 | ** type affinity of the other operand. This routine returns the |
drh | 53db145 | 2004-05-20 13:54:53 +0000 | [diff] [blame] | 94 | ** type affinity that should be used for the comparison operator. |
| 95 | */ |
danielk1977 | e014a83 | 2004-05-17 10:48:57 +0000 | [diff] [blame] | 96 | char sqlite3CompareAffinity(Expr *pExpr, char aff2){ |
danielk1977 | bf3b721 | 2004-05-18 10:06:24 +0000 | [diff] [blame] | 97 | char aff1 = sqlite3ExprAffinity(pExpr); |
danielk1977 | e014a83 | 2004-05-17 10:48:57 +0000 | [diff] [blame] | 98 | if( aff1 && aff2 ){ |
drh | 8df447f | 2005-11-01 15:48:24 +0000 | [diff] [blame] | 99 | /* Both sides of the comparison are columns. If one has numeric |
| 100 | ** affinity, use that. Otherwise use no affinity. |
danielk1977 | e014a83 | 2004-05-17 10:48:57 +0000 | [diff] [blame] | 101 | */ |
drh | 8a51256 | 2005-11-14 22:29:05 +0000 | [diff] [blame] | 102 | if( sqlite3IsNumericAffinity(aff1) || sqlite3IsNumericAffinity(aff2) ){ |
danielk1977 | e014a83 | 2004-05-17 10:48:57 +0000 | [diff] [blame] | 103 | return SQLITE_AFF_NUMERIC; |
| 104 | }else{ |
| 105 | return SQLITE_AFF_NONE; |
| 106 | } |
| 107 | }else if( !aff1 && !aff2 ){ |
drh | 5f6a87b | 2004-07-19 00:39:45 +0000 | [diff] [blame] | 108 | /* Neither side of the comparison is a column. Compare the |
| 109 | ** results directly. |
danielk1977 | e014a83 | 2004-05-17 10:48:57 +0000 | [diff] [blame] | 110 | */ |
drh | 5f6a87b | 2004-07-19 00:39:45 +0000 | [diff] [blame] | 111 | return SQLITE_AFF_NONE; |
danielk1977 | e014a83 | 2004-05-17 10:48:57 +0000 | [diff] [blame] | 112 | }else{ |
| 113 | /* One side is a column, the other is not. Use the columns affinity. */ |
drh | fe05af8 | 2005-07-21 03:14:59 +0000 | [diff] [blame] | 114 | assert( aff1==0 || aff2==0 ); |
danielk1977 | e014a83 | 2004-05-17 10:48:57 +0000 | [diff] [blame] | 115 | return (aff1 + aff2); |
| 116 | } |
| 117 | } |
| 118 | |
drh | 53db145 | 2004-05-20 13:54:53 +0000 | [diff] [blame] | 119 | /* |
| 120 | ** pExpr is a comparison operator. Return the type affinity that should |
| 121 | ** be applied to both operands prior to doing the comparison. |
| 122 | */ |
danielk1977 | e014a83 | 2004-05-17 10:48:57 +0000 | [diff] [blame] | 123 | static char comparisonAffinity(Expr *pExpr){ |
| 124 | char aff; |
| 125 | assert( pExpr->op==TK_EQ || pExpr->op==TK_IN || pExpr->op==TK_LT || |
| 126 | pExpr->op==TK_GT || pExpr->op==TK_GE || pExpr->op==TK_LE || |
| 127 | pExpr->op==TK_NE ); |
| 128 | assert( pExpr->pLeft ); |
danielk1977 | bf3b721 | 2004-05-18 10:06:24 +0000 | [diff] [blame] | 129 | aff = sqlite3ExprAffinity(pExpr->pLeft); |
danielk1977 | e014a83 | 2004-05-17 10:48:57 +0000 | [diff] [blame] | 130 | if( pExpr->pRight ){ |
| 131 | aff = sqlite3CompareAffinity(pExpr->pRight, aff); |
| 132 | } |
| 133 | else if( pExpr->pSelect ){ |
| 134 | aff = sqlite3CompareAffinity(pExpr->pSelect->pEList->a[0].pExpr, aff); |
| 135 | } |
| 136 | else if( !aff ){ |
drh | de087bd | 2007-02-23 03:00:44 +0000 | [diff] [blame] | 137 | aff = SQLITE_AFF_NONE; |
danielk1977 | e014a83 | 2004-05-17 10:48:57 +0000 | [diff] [blame] | 138 | } |
| 139 | return aff; |
| 140 | } |
| 141 | |
| 142 | /* |
| 143 | ** pExpr is a comparison expression, eg. '=', '<', IN(...) etc. |
| 144 | ** idx_affinity is the affinity of an indexed column. Return true |
| 145 | ** if the index with affinity idx_affinity may be used to implement |
| 146 | ** the comparison in pExpr. |
| 147 | */ |
| 148 | int sqlite3IndexAffinityOk(Expr *pExpr, char idx_affinity){ |
| 149 | char aff = comparisonAffinity(pExpr); |
drh | 8a51256 | 2005-11-14 22:29:05 +0000 | [diff] [blame] | 150 | switch( aff ){ |
| 151 | case SQLITE_AFF_NONE: |
| 152 | return 1; |
| 153 | case SQLITE_AFF_TEXT: |
| 154 | return idx_affinity==SQLITE_AFF_TEXT; |
| 155 | default: |
| 156 | return sqlite3IsNumericAffinity(idx_affinity); |
| 157 | } |
danielk1977 | e014a83 | 2004-05-17 10:48:57 +0000 | [diff] [blame] | 158 | } |
| 159 | |
danielk1977 | a37cdde | 2004-05-16 11:15:36 +0000 | [diff] [blame] | 160 | /* |
drh | 3557335 | 2008-01-08 23:54:25 +0000 | [diff] [blame] | 161 | ** Return the P5 value that should be used for a binary comparison |
danielk1977 | a37cdde | 2004-05-16 11:15:36 +0000 | [diff] [blame] | 162 | ** opcode (OP_Eq, OP_Ge etc.) used to compare pExpr1 and pExpr2. |
danielk1977 | a37cdde | 2004-05-16 11:15:36 +0000 | [diff] [blame] | 163 | */ |
drh | 3557335 | 2008-01-08 23:54:25 +0000 | [diff] [blame] | 164 | static u8 binaryCompareP5(Expr *pExpr1, Expr *pExpr2, int jumpIfNull){ |
| 165 | u8 aff = (char)sqlite3ExprAffinity(pExpr2); |
| 166 | aff = sqlite3CompareAffinity(pExpr1, aff) | jumpIfNull; |
| 167 | return aff; |
danielk1977 | a37cdde | 2004-05-16 11:15:36 +0000 | [diff] [blame] | 168 | } |
| 169 | |
drh | a2e0004 | 2002-01-22 03:13:42 +0000 | [diff] [blame] | 170 | /* |
danielk1977 | 0202b29 | 2004-06-09 09:55:16 +0000 | [diff] [blame] | 171 | ** Return a pointer to the collation sequence that should be used by |
| 172 | ** a binary comparison operator comparing pLeft and pRight. |
| 173 | ** |
| 174 | ** If the left hand expression has a collating sequence type, then it is |
| 175 | ** used. Otherwise the collation sequence for the right hand expression |
| 176 | ** is used, or the default (BINARY) if neither expression has a collating |
| 177 | ** type. |
danielk1977 | bcbb04e | 2007-05-29 12:11:29 +0000 | [diff] [blame] | 178 | ** |
| 179 | ** Argument pRight (but not pLeft) may be a null pointer. In this case, |
| 180 | ** it is not considered. |
danielk1977 | 0202b29 | 2004-06-09 09:55:16 +0000 | [diff] [blame] | 181 | */ |
drh | 0a0e131 | 2007-08-07 17:04:59 +0000 | [diff] [blame] | 182 | CollSeq *sqlite3BinaryCompareCollSeq( |
danielk1977 | bcbb04e | 2007-05-29 12:11:29 +0000 | [diff] [blame] | 183 | Parse *pParse, |
| 184 | Expr *pLeft, |
| 185 | Expr *pRight |
| 186 | ){ |
drh | ec41dda | 2007-02-07 13:09:45 +0000 | [diff] [blame] | 187 | CollSeq *pColl; |
| 188 | assert( pLeft ); |
drh | ec41dda | 2007-02-07 13:09:45 +0000 | [diff] [blame] | 189 | if( pLeft->flags & EP_ExpCollate ){ |
| 190 | assert( pLeft->pColl ); |
| 191 | pColl = pLeft->pColl; |
danielk1977 | bcbb04e | 2007-05-29 12:11:29 +0000 | [diff] [blame] | 192 | }else if( pRight && pRight->flags & EP_ExpCollate ){ |
drh | ec41dda | 2007-02-07 13:09:45 +0000 | [diff] [blame] | 193 | assert( pRight->pColl ); |
| 194 | pColl = pRight->pColl; |
| 195 | }else{ |
| 196 | pColl = sqlite3ExprCollSeq(pParse, pLeft); |
| 197 | if( !pColl ){ |
| 198 | pColl = sqlite3ExprCollSeq(pParse, pRight); |
| 199 | } |
danielk1977 | 0202b29 | 2004-06-09 09:55:16 +0000 | [diff] [blame] | 200 | } |
| 201 | return pColl; |
| 202 | } |
| 203 | |
| 204 | /* |
drh | da250ea | 2008-04-01 05:07:14 +0000 | [diff] [blame] | 205 | ** Generate the operands for a comparison operation. Before |
| 206 | ** generating the code for each operand, set the EP_AnyAff |
| 207 | ** flag on the expression so that it will be able to used a |
| 208 | ** cached column value that has previously undergone an |
| 209 | ** affinity change. |
| 210 | */ |
| 211 | static void codeCompareOperands( |
| 212 | Parse *pParse, /* Parsing and code generating context */ |
| 213 | Expr *pLeft, /* The left operand */ |
| 214 | int *pRegLeft, /* Register where left operand is stored */ |
| 215 | int *pFreeLeft, /* Free this register when done */ |
| 216 | Expr *pRight, /* The right operand */ |
| 217 | int *pRegRight, /* Register where right operand is stored */ |
| 218 | int *pFreeRight /* Write temp register for right operand there */ |
| 219 | ){ |
| 220 | while( pLeft->op==TK_UPLUS ) pLeft = pLeft->pLeft; |
| 221 | pLeft->flags |= EP_AnyAff; |
| 222 | *pRegLeft = sqlite3ExprCodeTemp(pParse, pLeft, pFreeLeft); |
| 223 | while( pRight->op==TK_UPLUS ) pRight = pRight->pLeft; |
| 224 | pRight->flags |= EP_AnyAff; |
| 225 | *pRegRight = sqlite3ExprCodeTemp(pParse, pRight, pFreeRight); |
| 226 | } |
| 227 | |
| 228 | /* |
drh | be5c89a | 2004-07-26 00:31:09 +0000 | [diff] [blame] | 229 | ** Generate code for a comparison operator. |
| 230 | */ |
| 231 | static int codeCompare( |
| 232 | Parse *pParse, /* The parsing (and code generating) context */ |
| 233 | Expr *pLeft, /* The left operand */ |
| 234 | Expr *pRight, /* The right operand */ |
| 235 | int opcode, /* The comparison opcode */ |
drh | 3557335 | 2008-01-08 23:54:25 +0000 | [diff] [blame] | 236 | int in1, int in2, /* Register holding operands */ |
drh | be5c89a | 2004-07-26 00:31:09 +0000 | [diff] [blame] | 237 | int dest, /* Jump here if true. */ |
| 238 | int jumpIfNull /* If true, jump if either operand is NULL */ |
| 239 | ){ |
drh | 3557335 | 2008-01-08 23:54:25 +0000 | [diff] [blame] | 240 | int p5; |
| 241 | int addr; |
| 242 | CollSeq *p4; |
| 243 | |
| 244 | p4 = sqlite3BinaryCompareCollSeq(pParse, pLeft, pRight); |
| 245 | p5 = binaryCompareP5(pLeft, pRight, jumpIfNull); |
| 246 | addr = sqlite3VdbeAddOp4(pParse->pVdbe, opcode, in2, dest, in1, |
| 247 | (void*)p4, P4_COLLSEQ); |
| 248 | sqlite3VdbeChangeP5(pParse->pVdbe, p5); |
drh | 2f7794c | 2008-04-01 03:27:39 +0000 | [diff] [blame] | 249 | if( p5 & SQLITE_AFF_MASK ){ |
drh | da250ea | 2008-04-01 05:07:14 +0000 | [diff] [blame] | 250 | sqlite3ExprCacheAffinityChange(pParse, in1, 1); |
| 251 | sqlite3ExprCacheAffinityChange(pParse, in2, 1); |
drh | 2f7794c | 2008-04-01 03:27:39 +0000 | [diff] [blame] | 252 | } |
drh | 3557335 | 2008-01-08 23:54:25 +0000 | [diff] [blame] | 253 | return addr; |
drh | be5c89a | 2004-07-26 00:31:09 +0000 | [diff] [blame] | 254 | } |
| 255 | |
danielk1977 | 4b5255a | 2008-06-05 16:47:39 +0000 | [diff] [blame] | 256 | #if SQLITE_MAX_EXPR_DEPTH>0 |
| 257 | /* |
| 258 | ** Check that argument nHeight is less than or equal to the maximum |
| 259 | ** expression depth allowed. If it is not, leave an error message in |
| 260 | ** pParse. |
| 261 | */ |
| 262 | static int checkExprHeight(Parse *pParse, int nHeight){ |
| 263 | int rc = SQLITE_OK; |
| 264 | int mxHeight = pParse->db->aLimit[SQLITE_LIMIT_EXPR_DEPTH]; |
| 265 | if( nHeight>mxHeight ){ |
| 266 | sqlite3ErrorMsg(pParse, |
| 267 | "Expression tree is too large (maximum depth %d)", mxHeight |
| 268 | ); |
| 269 | rc = SQLITE_ERROR; |
| 270 | } |
| 271 | return rc; |
| 272 | } |
| 273 | |
| 274 | /* The following three functions, heightOfExpr(), heightOfExprList() |
| 275 | ** and heightOfSelect(), are used to determine the maximum height |
| 276 | ** of any expression tree referenced by the structure passed as the |
| 277 | ** first argument. |
| 278 | ** |
| 279 | ** If this maximum height is greater than the current value pointed |
| 280 | ** to by pnHeight, the second parameter, then set *pnHeight to that |
| 281 | ** value. |
| 282 | */ |
| 283 | static void heightOfExpr(Expr *p, int *pnHeight){ |
| 284 | if( p ){ |
| 285 | if( p->nHeight>*pnHeight ){ |
| 286 | *pnHeight = p->nHeight; |
| 287 | } |
| 288 | } |
| 289 | } |
| 290 | static void heightOfExprList(ExprList *p, int *pnHeight){ |
| 291 | if( p ){ |
| 292 | int i; |
| 293 | for(i=0; i<p->nExpr; i++){ |
| 294 | heightOfExpr(p->a[i].pExpr, pnHeight); |
| 295 | } |
| 296 | } |
| 297 | } |
| 298 | static void heightOfSelect(Select *p, int *pnHeight){ |
| 299 | if( p ){ |
| 300 | heightOfExpr(p->pWhere, pnHeight); |
| 301 | heightOfExpr(p->pHaving, pnHeight); |
| 302 | heightOfExpr(p->pLimit, pnHeight); |
| 303 | heightOfExpr(p->pOffset, pnHeight); |
| 304 | heightOfExprList(p->pEList, pnHeight); |
| 305 | heightOfExprList(p->pGroupBy, pnHeight); |
| 306 | heightOfExprList(p->pOrderBy, pnHeight); |
| 307 | heightOfSelect(p->pPrior, pnHeight); |
| 308 | } |
| 309 | } |
| 310 | |
| 311 | /* |
| 312 | ** Set the Expr.nHeight variable in the structure passed as an |
| 313 | ** argument. An expression with no children, Expr.pList or |
| 314 | ** Expr.pSelect member has a height of 1. Any other expression |
| 315 | ** has a height equal to the maximum height of any other |
| 316 | ** referenced Expr plus one. |
| 317 | */ |
| 318 | static void exprSetHeight(Expr *p){ |
| 319 | int nHeight = 0; |
| 320 | heightOfExpr(p->pLeft, &nHeight); |
| 321 | heightOfExpr(p->pRight, &nHeight); |
| 322 | heightOfExprList(p->pList, &nHeight); |
| 323 | heightOfSelect(p->pSelect, &nHeight); |
| 324 | p->nHeight = nHeight + 1; |
| 325 | } |
| 326 | |
| 327 | /* |
| 328 | ** Set the Expr.nHeight variable using the exprSetHeight() function. If |
| 329 | ** the height is greater than the maximum allowed expression depth, |
| 330 | ** leave an error in pParse. |
| 331 | */ |
| 332 | void sqlite3ExprSetHeight(Parse *pParse, Expr *p){ |
| 333 | exprSetHeight(p); |
| 334 | checkExprHeight(pParse, p->nHeight); |
| 335 | } |
| 336 | |
| 337 | /* |
| 338 | ** Return the maximum height of any expression tree referenced |
| 339 | ** by the select statement passed as an argument. |
| 340 | */ |
| 341 | int sqlite3SelectExprHeight(Select *p){ |
| 342 | int nHeight = 0; |
| 343 | heightOfSelect(p, &nHeight); |
| 344 | return nHeight; |
| 345 | } |
| 346 | #else |
| 347 | #define checkExprHeight(x,y) |
| 348 | #define exprSetHeight(y) |
| 349 | #endif /* SQLITE_MAX_EXPR_DEPTH>0 */ |
| 350 | |
drh | be5c89a | 2004-07-26 00:31:09 +0000 | [diff] [blame] | 351 | /* |
drh | a76b5df | 2002-02-23 02:32:10 +0000 | [diff] [blame] | 352 | ** Construct a new expression node and return a pointer to it. Memory |
drh | 1743575 | 2007-08-16 04:30:38 +0000 | [diff] [blame] | 353 | ** for this node is obtained from sqlite3_malloc(). The calling function |
drh | a76b5df | 2002-02-23 02:32:10 +0000 | [diff] [blame] | 354 | ** is responsible for making sure the node eventually gets freed. |
| 355 | */ |
drh | 1743575 | 2007-08-16 04:30:38 +0000 | [diff] [blame] | 356 | Expr *sqlite3Expr( |
danielk1977 | a1644fd | 2007-08-29 12:31:25 +0000 | [diff] [blame] | 357 | sqlite3 *db, /* Handle for sqlite3DbMallocZero() (may be null) */ |
drh | 1743575 | 2007-08-16 04:30:38 +0000 | [diff] [blame] | 358 | int op, /* Expression opcode */ |
| 359 | Expr *pLeft, /* Left operand */ |
| 360 | Expr *pRight, /* Right operand */ |
| 361 | const Token *pToken /* Argument token */ |
| 362 | ){ |
drh | a76b5df | 2002-02-23 02:32:10 +0000 | [diff] [blame] | 363 | Expr *pNew; |
drh | 26e4a8b | 2008-05-01 17:16:52 +0000 | [diff] [blame] | 364 | pNew = sqlite3DbMallocZero(db, sizeof(Expr)); |
drh | a76b5df | 2002-02-23 02:32:10 +0000 | [diff] [blame] | 365 | if( pNew==0 ){ |
danielk1977 | d5d5652 | 2005-03-16 12:15:20 +0000 | [diff] [blame] | 366 | /* When malloc fails, delete pLeft and pRight. Expressions passed to |
| 367 | ** this function must always be allocated with sqlite3Expr() for this |
| 368 | ** reason. |
| 369 | */ |
| 370 | sqlite3ExprDelete(pLeft); |
| 371 | sqlite3ExprDelete(pRight); |
drh | a76b5df | 2002-02-23 02:32:10 +0000 | [diff] [blame] | 372 | return 0; |
| 373 | } |
| 374 | pNew->op = op; |
| 375 | pNew->pLeft = pLeft; |
| 376 | pNew->pRight = pRight; |
danielk1977 | a58fdfb | 2005-02-08 07:50:40 +0000 | [diff] [blame] | 377 | pNew->iAgg = -1; |
drh | a76b5df | 2002-02-23 02:32:10 +0000 | [diff] [blame] | 378 | if( pToken ){ |
drh | 4b59ab5 | 2002-08-24 18:24:51 +0000 | [diff] [blame] | 379 | assert( pToken->dyn==0 ); |
drh | 145716b | 2004-09-24 12:24:06 +0000 | [diff] [blame] | 380 | pNew->span = pNew->token = *pToken; |
drh | a34001c | 2007-02-02 12:44:37 +0000 | [diff] [blame] | 381 | }else if( pLeft ){ |
| 382 | if( pRight ){ |
| 383 | sqlite3ExprSpan(pNew, &pLeft->span, &pRight->span); |
drh | 5ffb3ac | 2007-04-18 17:07:57 +0000 | [diff] [blame] | 384 | if( pRight->flags & EP_ExpCollate ){ |
drh | a34001c | 2007-02-02 12:44:37 +0000 | [diff] [blame] | 385 | pNew->flags |= EP_ExpCollate; |
| 386 | pNew->pColl = pRight->pColl; |
| 387 | } |
| 388 | } |
drh | 5ffb3ac | 2007-04-18 17:07:57 +0000 | [diff] [blame] | 389 | if( pLeft->flags & EP_ExpCollate ){ |
drh | a34001c | 2007-02-02 12:44:37 +0000 | [diff] [blame] | 390 | pNew->flags |= EP_ExpCollate; |
| 391 | pNew->pColl = pLeft->pColl; |
| 392 | } |
drh | a76b5df | 2002-02-23 02:32:10 +0000 | [diff] [blame] | 393 | } |
danielk1977 | fc97606 | 2007-05-10 10:46:56 +0000 | [diff] [blame] | 394 | |
danielk1977 | 4b5255a | 2008-06-05 16:47:39 +0000 | [diff] [blame] | 395 | exprSetHeight(pNew); |
drh | a76b5df | 2002-02-23 02:32:10 +0000 | [diff] [blame] | 396 | return pNew; |
| 397 | } |
| 398 | |
| 399 | /* |
drh | 1743575 | 2007-08-16 04:30:38 +0000 | [diff] [blame] | 400 | ** Works like sqlite3Expr() except that it takes an extra Parse* |
| 401 | ** argument and notifies the associated connection object if malloc fails. |
drh | 206f3d9 | 2006-07-11 13:15:08 +0000 | [diff] [blame] | 402 | */ |
drh | 1743575 | 2007-08-16 04:30:38 +0000 | [diff] [blame] | 403 | Expr *sqlite3PExpr( |
| 404 | Parse *pParse, /* Parsing context */ |
| 405 | int op, /* Expression opcode */ |
| 406 | Expr *pLeft, /* Left operand */ |
| 407 | Expr *pRight, /* Right operand */ |
| 408 | const Token *pToken /* Argument token */ |
| 409 | ){ |
danielk1977 | 4b5255a | 2008-06-05 16:47:39 +0000 | [diff] [blame] | 410 | Expr *p = sqlite3Expr(pParse->db, op, pLeft, pRight, pToken); |
| 411 | if( p ){ |
| 412 | checkExprHeight(pParse, p->nHeight); |
| 413 | } |
| 414 | return p; |
drh | 206f3d9 | 2006-07-11 13:15:08 +0000 | [diff] [blame] | 415 | } |
| 416 | |
| 417 | /* |
drh | 4e0cff6 | 2004-11-05 05:10:28 +0000 | [diff] [blame] | 418 | ** When doing a nested parse, you can include terms in an expression |
drh | b765411 | 2008-01-12 12:48:07 +0000 | [diff] [blame] | 419 | ** that look like this: #1 #2 ... These terms refer to registers |
| 420 | ** in the virtual machine. #N is the N-th register. |
drh | 4e0cff6 | 2004-11-05 05:10:28 +0000 | [diff] [blame] | 421 | ** |
| 422 | ** This routine is called by the parser to deal with on of those terms. |
| 423 | ** It immediately generates code to store the value in a memory location. |
| 424 | ** The returns an expression that will code to extract the value from |
| 425 | ** that memory location as needed. |
| 426 | */ |
| 427 | Expr *sqlite3RegisterExpr(Parse *pParse, Token *pToken){ |
| 428 | Vdbe *v = pParse->pVdbe; |
| 429 | Expr *p; |
drh | 4e0cff6 | 2004-11-05 05:10:28 +0000 | [diff] [blame] | 430 | if( pParse->nested==0 ){ |
| 431 | sqlite3ErrorMsg(pParse, "near \"%T\": syntax error", pToken); |
danielk1977 | a1644fd | 2007-08-29 12:31:25 +0000 | [diff] [blame] | 432 | return sqlite3PExpr(pParse, TK_NULL, 0, 0, 0); |
drh | 4e0cff6 | 2004-11-05 05:10:28 +0000 | [diff] [blame] | 433 | } |
drh | bb7ac00 | 2005-08-12 22:58:53 +0000 | [diff] [blame] | 434 | if( v==0 ) return 0; |
danielk1977 | a1644fd | 2007-08-29 12:31:25 +0000 | [diff] [blame] | 435 | p = sqlite3PExpr(pParse, TK_REGISTER, 0, 0, pToken); |
drh | 73c42a1 | 2004-11-20 18:13:10 +0000 | [diff] [blame] | 436 | if( p==0 ){ |
| 437 | return 0; /* Malloc failed */ |
| 438 | } |
drh | b765411 | 2008-01-12 12:48:07 +0000 | [diff] [blame] | 439 | p->iTable = atoi((char*)&pToken->z[1]); |
drh | 4e0cff6 | 2004-11-05 05:10:28 +0000 | [diff] [blame] | 440 | return p; |
| 441 | } |
| 442 | |
| 443 | /* |
drh | 91bb0ee | 2004-09-01 03:06:34 +0000 | [diff] [blame] | 444 | ** Join two expressions using an AND operator. If either expression is |
| 445 | ** NULL, then just return the other expression. |
| 446 | */ |
danielk1977 | 1e53695 | 2007-08-16 10:09:01 +0000 | [diff] [blame] | 447 | Expr *sqlite3ExprAnd(sqlite3 *db, Expr *pLeft, Expr *pRight){ |
drh | 91bb0ee | 2004-09-01 03:06:34 +0000 | [diff] [blame] | 448 | if( pLeft==0 ){ |
| 449 | return pRight; |
| 450 | }else if( pRight==0 ){ |
| 451 | return pLeft; |
| 452 | }else{ |
danielk1977 | 880c15b | 2007-09-01 18:24:55 +0000 | [diff] [blame] | 453 | return sqlite3Expr(db, TK_AND, pLeft, pRight, 0); |
drh | 91bb0ee | 2004-09-01 03:06:34 +0000 | [diff] [blame] | 454 | } |
| 455 | } |
| 456 | |
| 457 | /* |
drh | 6977fea | 2002-10-22 23:38:04 +0000 | [diff] [blame] | 458 | ** Set the Expr.span field of the given expression to span all |
drh | a76b5df | 2002-02-23 02:32:10 +0000 | [diff] [blame] | 459 | ** text between the two given tokens. |
| 460 | */ |
danielk1977 | 4adee20 | 2004-05-08 08:23:19 +0000 | [diff] [blame] | 461 | void sqlite3ExprSpan(Expr *pExpr, Token *pLeft, Token *pRight){ |
drh | 4efc475 | 2004-01-16 15:55:37 +0000 | [diff] [blame] | 462 | assert( pRight!=0 ); |
| 463 | assert( pLeft!=0 ); |
drh | f3a65f7 | 2007-08-22 20:18:21 +0000 | [diff] [blame] | 464 | if( pExpr && pRight->z && pLeft->z ){ |
drh | ad6d946 | 2004-09-19 02:15:24 +0000 | [diff] [blame] | 465 | assert( pLeft->dyn==0 || pLeft->z[pLeft->n]==0 ); |
drh | 145716b | 2004-09-24 12:24:06 +0000 | [diff] [blame] | 466 | if( pLeft->dyn==0 && pRight->dyn==0 ){ |
drh | 6977fea | 2002-10-22 23:38:04 +0000 | [diff] [blame] | 467 | pExpr->span.z = pLeft->z; |
drh | 97903fe | 2005-05-24 20:19:57 +0000 | [diff] [blame] | 468 | pExpr->span.n = pRight->n + (pRight->z - pLeft->z); |
drh | 4b59ab5 | 2002-08-24 18:24:51 +0000 | [diff] [blame] | 469 | }else{ |
drh | 6977fea | 2002-10-22 23:38:04 +0000 | [diff] [blame] | 470 | pExpr->span.z = 0; |
drh | 4b59ab5 | 2002-08-24 18:24:51 +0000 | [diff] [blame] | 471 | } |
drh | a76b5df | 2002-02-23 02:32:10 +0000 | [diff] [blame] | 472 | } |
| 473 | } |
| 474 | |
| 475 | /* |
| 476 | ** Construct a new expression node for a function with multiple |
| 477 | ** arguments. |
| 478 | */ |
drh | 1743575 | 2007-08-16 04:30:38 +0000 | [diff] [blame] | 479 | Expr *sqlite3ExprFunction(Parse *pParse, ExprList *pList, Token *pToken){ |
drh | a76b5df | 2002-02-23 02:32:10 +0000 | [diff] [blame] | 480 | Expr *pNew; |
danielk1977 | 4b202ae | 2006-01-23 05:50:58 +0000 | [diff] [blame] | 481 | assert( pToken ); |
drh | 1743575 | 2007-08-16 04:30:38 +0000 | [diff] [blame] | 482 | pNew = sqlite3DbMallocZero(pParse->db, sizeof(Expr) ); |
drh | a76b5df | 2002-02-23 02:32:10 +0000 | [diff] [blame] | 483 | if( pNew==0 ){ |
danielk1977 | d5d5652 | 2005-03-16 12:15:20 +0000 | [diff] [blame] | 484 | sqlite3ExprListDelete(pList); /* Avoid leaking memory when malloc fails */ |
drh | a76b5df | 2002-02-23 02:32:10 +0000 | [diff] [blame] | 485 | return 0; |
| 486 | } |
| 487 | pNew->op = TK_FUNCTION; |
| 488 | pNew->pList = pList; |
danielk1977 | 4b202ae | 2006-01-23 05:50:58 +0000 | [diff] [blame] | 489 | assert( pToken->dyn==0 ); |
| 490 | pNew->token = *pToken; |
drh | 6977fea | 2002-10-22 23:38:04 +0000 | [diff] [blame] | 491 | pNew->span = pNew->token; |
danielk1977 | fc97606 | 2007-05-10 10:46:56 +0000 | [diff] [blame] | 492 | |
danielk1977 | 4b5255a | 2008-06-05 16:47:39 +0000 | [diff] [blame] | 493 | sqlite3ExprSetHeight(pParse, pNew); |
drh | a76b5df | 2002-02-23 02:32:10 +0000 | [diff] [blame] | 494 | return pNew; |
| 495 | } |
| 496 | |
| 497 | /* |
drh | fa6bc00 | 2004-09-07 16:19:52 +0000 | [diff] [blame] | 498 | ** Assign a variable number to an expression that encodes a wildcard |
| 499 | ** in the original SQL statement. |
| 500 | ** |
| 501 | ** Wildcards consisting of a single "?" are assigned the next sequential |
| 502 | ** variable number. |
| 503 | ** |
| 504 | ** Wildcards of the form "?nnn" are assigned the number "nnn". We make |
| 505 | ** sure "nnn" is not too be to avoid a denial of service attack when |
| 506 | ** the SQL statement comes from an external source. |
| 507 | ** |
| 508 | ** Wildcards of the form ":aaa" or "$aaa" are assigned the same number |
| 509 | ** as the previous instance of the same wildcard. Or if this is the first |
| 510 | ** instance of the wildcard, the next sequenial variable number is |
| 511 | ** assigned. |
| 512 | */ |
| 513 | void sqlite3ExprAssignVarNumber(Parse *pParse, Expr *pExpr){ |
| 514 | Token *pToken; |
drh | 1743575 | 2007-08-16 04:30:38 +0000 | [diff] [blame] | 515 | sqlite3 *db = pParse->db; |
| 516 | |
drh | fa6bc00 | 2004-09-07 16:19:52 +0000 | [diff] [blame] | 517 | if( pExpr==0 ) return; |
| 518 | pToken = &pExpr->token; |
| 519 | assert( pToken->n>=1 ); |
| 520 | assert( pToken->z!=0 ); |
| 521 | assert( pToken->z[0]!=0 ); |
| 522 | if( pToken->n==1 ){ |
| 523 | /* Wildcard of the form "?". Assign the next variable number */ |
| 524 | pExpr->iTable = ++pParse->nVar; |
| 525 | }else if( pToken->z[0]=='?' ){ |
| 526 | /* Wildcard of the form "?nnn". Convert "nnn" to an integer and |
| 527 | ** use it as the variable number */ |
| 528 | int i; |
drh | 2646da7 | 2005-12-09 20:02:05 +0000 | [diff] [blame] | 529 | pExpr->iTable = i = atoi((char*)&pToken->z[1]); |
drh | c5499be | 2008-04-01 15:06:33 +0000 | [diff] [blame] | 530 | testcase( i==0 ); |
| 531 | testcase( i==1 ); |
| 532 | testcase( i==db->aLimit[SQLITE_LIMIT_VARIABLE_NUMBER]-1 ); |
| 533 | testcase( i==db->aLimit[SQLITE_LIMIT_VARIABLE_NUMBER] ); |
drh | bb4957f | 2008-03-20 14:03:29 +0000 | [diff] [blame] | 534 | if( i<1 || i>db->aLimit[SQLITE_LIMIT_VARIABLE_NUMBER] ){ |
drh | fa6bc00 | 2004-09-07 16:19:52 +0000 | [diff] [blame] | 535 | sqlite3ErrorMsg(pParse, "variable number must be between ?1 and ?%d", |
drh | bb4957f | 2008-03-20 14:03:29 +0000 | [diff] [blame] | 536 | db->aLimit[SQLITE_LIMIT_VARIABLE_NUMBER]); |
drh | fa6bc00 | 2004-09-07 16:19:52 +0000 | [diff] [blame] | 537 | } |
| 538 | if( i>pParse->nVar ){ |
| 539 | pParse->nVar = i; |
| 540 | } |
| 541 | }else{ |
| 542 | /* Wildcards of the form ":aaa" or "$aaa". Reuse the same variable |
| 543 | ** number as the prior appearance of the same name, or if the name |
| 544 | ** has never appeared before, reuse the same variable number |
| 545 | */ |
| 546 | int i, n; |
| 547 | n = pToken->n; |
| 548 | for(i=0; i<pParse->nVarExpr; i++){ |
| 549 | Expr *pE; |
| 550 | if( (pE = pParse->apVarExpr[i])!=0 |
| 551 | && pE->token.n==n |
| 552 | && memcmp(pE->token.z, pToken->z, n)==0 ){ |
| 553 | pExpr->iTable = pE->iTable; |
| 554 | break; |
| 555 | } |
| 556 | } |
| 557 | if( i>=pParse->nVarExpr ){ |
| 558 | pExpr->iTable = ++pParse->nVar; |
| 559 | if( pParse->nVarExpr>=pParse->nVarExprAlloc-1 ){ |
| 560 | pParse->nVarExprAlloc += pParse->nVarExprAlloc + 10; |
drh | 1743575 | 2007-08-16 04:30:38 +0000 | [diff] [blame] | 561 | pParse->apVarExpr = |
| 562 | sqlite3DbReallocOrFree( |
| 563 | db, |
| 564 | pParse->apVarExpr, |
| 565 | pParse->nVarExprAlloc*sizeof(pParse->apVarExpr[0]) |
| 566 | ); |
drh | fa6bc00 | 2004-09-07 16:19:52 +0000 | [diff] [blame] | 567 | } |
drh | 1743575 | 2007-08-16 04:30:38 +0000 | [diff] [blame] | 568 | if( !db->mallocFailed ){ |
drh | fa6bc00 | 2004-09-07 16:19:52 +0000 | [diff] [blame] | 569 | assert( pParse->apVarExpr!=0 ); |
| 570 | pParse->apVarExpr[pParse->nVarExpr++] = pExpr; |
| 571 | } |
| 572 | } |
| 573 | } |
drh | bb4957f | 2008-03-20 14:03:29 +0000 | [diff] [blame] | 574 | if( !pParse->nErr && pParse->nVar>db->aLimit[SQLITE_LIMIT_VARIABLE_NUMBER] ){ |
danielk1977 | 832b266 | 2007-05-09 11:37:22 +0000 | [diff] [blame] | 575 | sqlite3ErrorMsg(pParse, "too many SQL variables"); |
| 576 | } |
drh | fa6bc00 | 2004-09-07 16:19:52 +0000 | [diff] [blame] | 577 | } |
| 578 | |
| 579 | /* |
drh | a2e0004 | 2002-01-22 03:13:42 +0000 | [diff] [blame] | 580 | ** Recursively delete an expression tree. |
| 581 | */ |
danielk1977 | 4adee20 | 2004-05-08 08:23:19 +0000 | [diff] [blame] | 582 | void sqlite3ExprDelete(Expr *p){ |
drh | a2e0004 | 2002-01-22 03:13:42 +0000 | [diff] [blame] | 583 | if( p==0 ) return; |
drh | 1743575 | 2007-08-16 04:30:38 +0000 | [diff] [blame] | 584 | if( p->span.dyn ) sqlite3_free((char*)p->span.z); |
| 585 | if( p->token.dyn ) sqlite3_free((char*)p->token.z); |
danielk1977 | 4adee20 | 2004-05-08 08:23:19 +0000 | [diff] [blame] | 586 | sqlite3ExprDelete(p->pLeft); |
| 587 | sqlite3ExprDelete(p->pRight); |
| 588 | sqlite3ExprListDelete(p->pList); |
| 589 | sqlite3SelectDelete(p->pSelect); |
drh | 1743575 | 2007-08-16 04:30:38 +0000 | [diff] [blame] | 590 | sqlite3_free(p); |
drh | a2e0004 | 2002-01-22 03:13:42 +0000 | [diff] [blame] | 591 | } |
| 592 | |
drh | d2687b7 | 2005-08-12 22:56:09 +0000 | [diff] [blame] | 593 | /* |
| 594 | ** The Expr.token field might be a string literal that is quoted. |
| 595 | ** If so, remove the quotation marks. |
| 596 | */ |
drh | 1743575 | 2007-08-16 04:30:38 +0000 | [diff] [blame] | 597 | void sqlite3DequoteExpr(sqlite3 *db, Expr *p){ |
drh | d2687b7 | 2005-08-12 22:56:09 +0000 | [diff] [blame] | 598 | if( ExprHasAnyProperty(p, EP_Dequoted) ){ |
| 599 | return; |
| 600 | } |
| 601 | ExprSetProperty(p, EP_Dequoted); |
| 602 | if( p->token.dyn==0 ){ |
drh | 1743575 | 2007-08-16 04:30:38 +0000 | [diff] [blame] | 603 | sqlite3TokenCopy(db, &p->token, &p->token); |
drh | d2687b7 | 2005-08-12 22:56:09 +0000 | [diff] [blame] | 604 | } |
| 605 | sqlite3Dequote((char*)p->token.z); |
| 606 | } |
| 607 | |
drh | a76b5df | 2002-02-23 02:32:10 +0000 | [diff] [blame] | 608 | |
| 609 | /* |
drh | ff78bd2 | 2002-02-27 01:47:11 +0000 | [diff] [blame] | 610 | ** The following group of routines make deep copies of expressions, |
| 611 | ** expression lists, ID lists, and select statements. The copies can |
| 612 | ** be deleted (by being passed to their respective ...Delete() routines) |
| 613 | ** without effecting the originals. |
| 614 | ** |
danielk1977 | 4adee20 | 2004-05-08 08:23:19 +0000 | [diff] [blame] | 615 | ** The expression list, ID, and source lists return by sqlite3ExprListDup(), |
| 616 | ** sqlite3IdListDup(), and sqlite3SrcListDup() can not be further expanded |
drh | ad3cab5 | 2002-05-24 02:04:32 +0000 | [diff] [blame] | 617 | ** by subsequent calls to sqlite*ListAppend() routines. |
drh | ff78bd2 | 2002-02-27 01:47:11 +0000 | [diff] [blame] | 618 | ** |
drh | ad3cab5 | 2002-05-24 02:04:32 +0000 | [diff] [blame] | 619 | ** Any tables that the SrcList might point to are not duplicated. |
drh | ff78bd2 | 2002-02-27 01:47:11 +0000 | [diff] [blame] | 620 | */ |
danielk1977 | 1e53695 | 2007-08-16 10:09:01 +0000 | [diff] [blame] | 621 | Expr *sqlite3ExprDup(sqlite3 *db, Expr *p){ |
drh | ff78bd2 | 2002-02-27 01:47:11 +0000 | [diff] [blame] | 622 | Expr *pNew; |
| 623 | if( p==0 ) return 0; |
drh | 1743575 | 2007-08-16 04:30:38 +0000 | [diff] [blame] | 624 | pNew = sqlite3DbMallocRaw(db, sizeof(*p) ); |
drh | ff78bd2 | 2002-02-27 01:47:11 +0000 | [diff] [blame] | 625 | if( pNew==0 ) return 0; |
drh | 3b167c7 | 2002-06-28 12:18:47 +0000 | [diff] [blame] | 626 | memcpy(pNew, p, sizeof(*pNew)); |
drh | 6977fea | 2002-10-22 23:38:04 +0000 | [diff] [blame] | 627 | if( p->token.z!=0 ){ |
drh | 1743575 | 2007-08-16 04:30:38 +0000 | [diff] [blame] | 628 | pNew->token.z = (u8*)sqlite3DbStrNDup(db, (char*)p->token.z, p->token.n); |
drh | 4b59ab5 | 2002-08-24 18:24:51 +0000 | [diff] [blame] | 629 | pNew->token.dyn = 1; |
| 630 | }else{ |
drh | 4efc475 | 2004-01-16 15:55:37 +0000 | [diff] [blame] | 631 | assert( pNew->token.z==0 ); |
drh | 4b59ab5 | 2002-08-24 18:24:51 +0000 | [diff] [blame] | 632 | } |
drh | 6977fea | 2002-10-22 23:38:04 +0000 | [diff] [blame] | 633 | pNew->span.z = 0; |
drh | 1743575 | 2007-08-16 04:30:38 +0000 | [diff] [blame] | 634 | pNew->pLeft = sqlite3ExprDup(db, p->pLeft); |
| 635 | pNew->pRight = sqlite3ExprDup(db, p->pRight); |
| 636 | pNew->pList = sqlite3ExprListDup(db, p->pList); |
| 637 | pNew->pSelect = sqlite3SelectDup(db, p->pSelect); |
drh | ff78bd2 | 2002-02-27 01:47:11 +0000 | [diff] [blame] | 638 | return pNew; |
| 639 | } |
drh | 1743575 | 2007-08-16 04:30:38 +0000 | [diff] [blame] | 640 | void sqlite3TokenCopy(sqlite3 *db, Token *pTo, Token *pFrom){ |
| 641 | if( pTo->dyn ) sqlite3_free((char*)pTo->z); |
drh | 4b59ab5 | 2002-08-24 18:24:51 +0000 | [diff] [blame] | 642 | if( pFrom->z ){ |
| 643 | pTo->n = pFrom->n; |
drh | 1743575 | 2007-08-16 04:30:38 +0000 | [diff] [blame] | 644 | pTo->z = (u8*)sqlite3DbStrNDup(db, (char*)pFrom->z, pFrom->n); |
drh | 4b59ab5 | 2002-08-24 18:24:51 +0000 | [diff] [blame] | 645 | pTo->dyn = 1; |
| 646 | }else{ |
drh | 4b59ab5 | 2002-08-24 18:24:51 +0000 | [diff] [blame] | 647 | pTo->z = 0; |
drh | 4b59ab5 | 2002-08-24 18:24:51 +0000 | [diff] [blame] | 648 | } |
| 649 | } |
drh | 1743575 | 2007-08-16 04:30:38 +0000 | [diff] [blame] | 650 | ExprList *sqlite3ExprListDup(sqlite3 *db, ExprList *p){ |
drh | ff78bd2 | 2002-02-27 01:47:11 +0000 | [diff] [blame] | 651 | ExprList *pNew; |
drh | 145716b | 2004-09-24 12:24:06 +0000 | [diff] [blame] | 652 | struct ExprList_item *pItem, *pOldItem; |
drh | ff78bd2 | 2002-02-27 01:47:11 +0000 | [diff] [blame] | 653 | int i; |
| 654 | if( p==0 ) return 0; |
drh | 1743575 | 2007-08-16 04:30:38 +0000 | [diff] [blame] | 655 | pNew = sqlite3DbMallocRaw(db, sizeof(*pNew) ); |
drh | ff78bd2 | 2002-02-27 01:47:11 +0000 | [diff] [blame] | 656 | if( pNew==0 ) return 0; |
danielk1977 | 31dad9d | 2007-08-16 11:36:15 +0000 | [diff] [blame] | 657 | pNew->iECursor = 0; |
drh | 4305d10 | 2003-07-30 12:34:12 +0000 | [diff] [blame] | 658 | pNew->nExpr = pNew->nAlloc = p->nExpr; |
drh | 1743575 | 2007-08-16 04:30:38 +0000 | [diff] [blame] | 659 | pNew->a = pItem = sqlite3DbMallocRaw(db, p->nExpr*sizeof(p->a[0]) ); |
danielk1977 | e004840 | 2004-06-15 16:51:01 +0000 | [diff] [blame] | 660 | if( pItem==0 ){ |
drh | 1743575 | 2007-08-16 04:30:38 +0000 | [diff] [blame] | 661 | sqlite3_free(pNew); |
danielk1977 | e004840 | 2004-06-15 16:51:01 +0000 | [diff] [blame] | 662 | return 0; |
| 663 | } |
drh | 145716b | 2004-09-24 12:24:06 +0000 | [diff] [blame] | 664 | pOldItem = p->a; |
| 665 | for(i=0; i<p->nExpr; i++, pItem++, pOldItem++){ |
drh | 4b59ab5 | 2002-08-24 18:24:51 +0000 | [diff] [blame] | 666 | Expr *pNewExpr, *pOldExpr; |
drh | 1743575 | 2007-08-16 04:30:38 +0000 | [diff] [blame] | 667 | pItem->pExpr = pNewExpr = sqlite3ExprDup(db, pOldExpr = pOldItem->pExpr); |
drh | 6977fea | 2002-10-22 23:38:04 +0000 | [diff] [blame] | 668 | if( pOldExpr->span.z!=0 && pNewExpr ){ |
| 669 | /* Always make a copy of the span for top-level expressions in the |
drh | 4b59ab5 | 2002-08-24 18:24:51 +0000 | [diff] [blame] | 670 | ** expression list. The logic in SELECT processing that determines |
| 671 | ** the names of columns in the result set needs this information */ |
drh | 1743575 | 2007-08-16 04:30:38 +0000 | [diff] [blame] | 672 | sqlite3TokenCopy(db, &pNewExpr->span, &pOldExpr->span); |
drh | 4b59ab5 | 2002-08-24 18:24:51 +0000 | [diff] [blame] | 673 | } |
drh | 1f3e905 | 2002-10-31 00:09:39 +0000 | [diff] [blame] | 674 | assert( pNewExpr==0 || pNewExpr->span.z!=0 |
drh | 6f7adc8 | 2006-01-11 21:41:20 +0000 | [diff] [blame] | 675 | || pOldExpr->span.z==0 |
drh | 1743575 | 2007-08-16 04:30:38 +0000 | [diff] [blame] | 676 | || db->mallocFailed ); |
| 677 | pItem->zName = sqlite3DbStrDup(db, pOldItem->zName); |
drh | 145716b | 2004-09-24 12:24:06 +0000 | [diff] [blame] | 678 | pItem->sortOrder = pOldItem->sortOrder; |
| 679 | pItem->isAgg = pOldItem->isAgg; |
drh | 3e7bc9c | 2004-02-21 19:17:17 +0000 | [diff] [blame] | 680 | pItem->done = 0; |
drh | ff78bd2 | 2002-02-27 01:47:11 +0000 | [diff] [blame] | 681 | } |
| 682 | return pNew; |
| 683 | } |
danielk1977 | 93758c8 | 2005-01-21 08:13:14 +0000 | [diff] [blame] | 684 | |
| 685 | /* |
| 686 | ** If cursors, triggers, views and subqueries are all omitted from |
| 687 | ** the build, then none of the following routines, except for |
| 688 | ** sqlite3SelectDup(), can be called. sqlite3SelectDup() is sometimes |
| 689 | ** called with a NULL argument. |
| 690 | */ |
danielk1977 | 6a67fe8 | 2005-02-04 04:07:16 +0000 | [diff] [blame] | 691 | #if !defined(SQLITE_OMIT_VIEW) || !defined(SQLITE_OMIT_TRIGGER) \ |
| 692 | || !defined(SQLITE_OMIT_SUBQUERY) |
drh | 1743575 | 2007-08-16 04:30:38 +0000 | [diff] [blame] | 693 | SrcList *sqlite3SrcListDup(sqlite3 *db, SrcList *p){ |
drh | ad3cab5 | 2002-05-24 02:04:32 +0000 | [diff] [blame] | 694 | SrcList *pNew; |
| 695 | int i; |
drh | 113088e | 2003-03-20 01:16:58 +0000 | [diff] [blame] | 696 | int nByte; |
drh | ad3cab5 | 2002-05-24 02:04:32 +0000 | [diff] [blame] | 697 | if( p==0 ) return 0; |
drh | 113088e | 2003-03-20 01:16:58 +0000 | [diff] [blame] | 698 | nByte = sizeof(*p) + (p->nSrc>0 ? sizeof(p->a[0]) * (p->nSrc-1) : 0); |
drh | 1743575 | 2007-08-16 04:30:38 +0000 | [diff] [blame] | 699 | pNew = sqlite3DbMallocRaw(db, nByte ); |
drh | ad3cab5 | 2002-05-24 02:04:32 +0000 | [diff] [blame] | 700 | if( pNew==0 ) return 0; |
drh | 4305d10 | 2003-07-30 12:34:12 +0000 | [diff] [blame] | 701 | pNew->nSrc = pNew->nAlloc = p->nSrc; |
drh | ad3cab5 | 2002-05-24 02:04:32 +0000 | [diff] [blame] | 702 | for(i=0; i<p->nSrc; i++){ |
drh | 4efc475 | 2004-01-16 15:55:37 +0000 | [diff] [blame] | 703 | struct SrcList_item *pNewItem = &pNew->a[i]; |
| 704 | struct SrcList_item *pOldItem = &p->a[i]; |
drh | ed8a3bb | 2005-06-06 21:19:56 +0000 | [diff] [blame] | 705 | Table *pTab; |
drh | 1743575 | 2007-08-16 04:30:38 +0000 | [diff] [blame] | 706 | pNewItem->zDatabase = sqlite3DbStrDup(db, pOldItem->zDatabase); |
| 707 | pNewItem->zName = sqlite3DbStrDup(db, pOldItem->zName); |
| 708 | pNewItem->zAlias = sqlite3DbStrDup(db, pOldItem->zAlias); |
drh | 4efc475 | 2004-01-16 15:55:37 +0000 | [diff] [blame] | 709 | pNewItem->jointype = pOldItem->jointype; |
| 710 | pNewItem->iCursor = pOldItem->iCursor; |
danielk1977 | 1787cca | 2006-02-10 07:07:14 +0000 | [diff] [blame] | 711 | pNewItem->isPopulated = pOldItem->isPopulated; |
drh | ed8a3bb | 2005-06-06 21:19:56 +0000 | [diff] [blame] | 712 | pTab = pNewItem->pTab = pOldItem->pTab; |
| 713 | if( pTab ){ |
| 714 | pTab->nRef++; |
danielk1977 | a1cb183 | 2005-02-12 08:59:55 +0000 | [diff] [blame] | 715 | } |
drh | 1743575 | 2007-08-16 04:30:38 +0000 | [diff] [blame] | 716 | pNewItem->pSelect = sqlite3SelectDup(db, pOldItem->pSelect); |
| 717 | pNewItem->pOn = sqlite3ExprDup(db, pOldItem->pOn); |
| 718 | pNewItem->pUsing = sqlite3IdListDup(db, pOldItem->pUsing); |
danielk1977 | 6c18b6e | 2005-01-30 09:17:58 +0000 | [diff] [blame] | 719 | pNewItem->colUsed = pOldItem->colUsed; |
drh | ad3cab5 | 2002-05-24 02:04:32 +0000 | [diff] [blame] | 720 | } |
| 721 | return pNew; |
| 722 | } |
drh | 1743575 | 2007-08-16 04:30:38 +0000 | [diff] [blame] | 723 | IdList *sqlite3IdListDup(sqlite3 *db, IdList *p){ |
drh | ff78bd2 | 2002-02-27 01:47:11 +0000 | [diff] [blame] | 724 | IdList *pNew; |
| 725 | int i; |
| 726 | if( p==0 ) return 0; |
drh | 1743575 | 2007-08-16 04:30:38 +0000 | [diff] [blame] | 727 | pNew = sqlite3DbMallocRaw(db, sizeof(*pNew) ); |
drh | ff78bd2 | 2002-02-27 01:47:11 +0000 | [diff] [blame] | 728 | if( pNew==0 ) return 0; |
drh | 4305d10 | 2003-07-30 12:34:12 +0000 | [diff] [blame] | 729 | pNew->nId = pNew->nAlloc = p->nId; |
drh | 1743575 | 2007-08-16 04:30:38 +0000 | [diff] [blame] | 730 | pNew->a = sqlite3DbMallocRaw(db, p->nId*sizeof(p->a[0]) ); |
danielk1977 | d5d5652 | 2005-03-16 12:15:20 +0000 | [diff] [blame] | 731 | if( pNew->a==0 ){ |
drh | 1743575 | 2007-08-16 04:30:38 +0000 | [diff] [blame] | 732 | sqlite3_free(pNew); |
danielk1977 | d5d5652 | 2005-03-16 12:15:20 +0000 | [diff] [blame] | 733 | return 0; |
| 734 | } |
drh | ff78bd2 | 2002-02-27 01:47:11 +0000 | [diff] [blame] | 735 | for(i=0; i<p->nId; i++){ |
drh | 4efc475 | 2004-01-16 15:55:37 +0000 | [diff] [blame] | 736 | struct IdList_item *pNewItem = &pNew->a[i]; |
| 737 | struct IdList_item *pOldItem = &p->a[i]; |
drh | 1743575 | 2007-08-16 04:30:38 +0000 | [diff] [blame] | 738 | pNewItem->zName = sqlite3DbStrDup(db, pOldItem->zName); |
drh | 4efc475 | 2004-01-16 15:55:37 +0000 | [diff] [blame] | 739 | pNewItem->idx = pOldItem->idx; |
drh | ff78bd2 | 2002-02-27 01:47:11 +0000 | [diff] [blame] | 740 | } |
| 741 | return pNew; |
| 742 | } |
drh | 1743575 | 2007-08-16 04:30:38 +0000 | [diff] [blame] | 743 | Select *sqlite3SelectDup(sqlite3 *db, Select *p){ |
drh | ff78bd2 | 2002-02-27 01:47:11 +0000 | [diff] [blame] | 744 | Select *pNew; |
| 745 | if( p==0 ) return 0; |
drh | 1743575 | 2007-08-16 04:30:38 +0000 | [diff] [blame] | 746 | pNew = sqlite3DbMallocRaw(db, sizeof(*p) ); |
drh | ff78bd2 | 2002-02-27 01:47:11 +0000 | [diff] [blame] | 747 | if( pNew==0 ) return 0; |
| 748 | pNew->isDistinct = p->isDistinct; |
drh | 1743575 | 2007-08-16 04:30:38 +0000 | [diff] [blame] | 749 | pNew->pEList = sqlite3ExprListDup(db, p->pEList); |
| 750 | pNew->pSrc = sqlite3SrcListDup(db, p->pSrc); |
| 751 | pNew->pWhere = sqlite3ExprDup(db, p->pWhere); |
| 752 | pNew->pGroupBy = sqlite3ExprListDup(db, p->pGroupBy); |
| 753 | pNew->pHaving = sqlite3ExprDup(db, p->pHaving); |
| 754 | pNew->pOrderBy = sqlite3ExprListDup(db, p->pOrderBy); |
drh | ff78bd2 | 2002-02-27 01:47:11 +0000 | [diff] [blame] | 755 | pNew->op = p->op; |
drh | 1743575 | 2007-08-16 04:30:38 +0000 | [diff] [blame] | 756 | pNew->pPrior = sqlite3SelectDup(db, p->pPrior); |
| 757 | pNew->pLimit = sqlite3ExprDup(db, p->pLimit); |
| 758 | pNew->pOffset = sqlite3ExprDup(db, p->pOffset); |
drh | 92b01d5 | 2008-06-24 00:32:35 +0000 | [diff] [blame] | 759 | pNew->iLimit = 0; |
| 760 | pNew->iOffset = 0; |
danielk1977 | a1cb183 | 2005-02-12 08:59:55 +0000 | [diff] [blame] | 761 | pNew->isResolved = p->isResolved; |
| 762 | pNew->isAgg = p->isAgg; |
drh | b9bb7c1 | 2006-06-11 23:41:55 +0000 | [diff] [blame] | 763 | pNew->usesEphm = 0; |
drh | 8e647b8 | 2005-09-23 21:11:53 +0000 | [diff] [blame] | 764 | pNew->disallowOrderBy = 0; |
drh | 0342b1f | 2005-09-01 03:07:44 +0000 | [diff] [blame] | 765 | pNew->pRightmost = 0; |
drh | b9bb7c1 | 2006-06-11 23:41:55 +0000 | [diff] [blame] | 766 | pNew->addrOpenEphm[0] = -1; |
| 767 | pNew->addrOpenEphm[1] = -1; |
| 768 | pNew->addrOpenEphm[2] = -1; |
drh | ff78bd2 | 2002-02-27 01:47:11 +0000 | [diff] [blame] | 769 | return pNew; |
| 770 | } |
danielk1977 | 93758c8 | 2005-01-21 08:13:14 +0000 | [diff] [blame] | 771 | #else |
drh | 1743575 | 2007-08-16 04:30:38 +0000 | [diff] [blame] | 772 | Select *sqlite3SelectDup(sqlite3 *db, Select *p){ |
danielk1977 | 93758c8 | 2005-01-21 08:13:14 +0000 | [diff] [blame] | 773 | assert( p==0 ); |
| 774 | return 0; |
| 775 | } |
| 776 | #endif |
drh | ff78bd2 | 2002-02-27 01:47:11 +0000 | [diff] [blame] | 777 | |
| 778 | |
| 779 | /* |
drh | a76b5df | 2002-02-23 02:32:10 +0000 | [diff] [blame] | 780 | ** Add a new element to the end of an expression list. If pList is |
| 781 | ** initially NULL, then create a new expression list. |
| 782 | */ |
drh | 1743575 | 2007-08-16 04:30:38 +0000 | [diff] [blame] | 783 | ExprList *sqlite3ExprListAppend( |
| 784 | Parse *pParse, /* Parsing context */ |
| 785 | ExprList *pList, /* List to which to append. Might be NULL */ |
| 786 | Expr *pExpr, /* Expression to be appended */ |
| 787 | Token *pName /* AS keyword for the expression */ |
| 788 | ){ |
| 789 | sqlite3 *db = pParse->db; |
drh | a76b5df | 2002-02-23 02:32:10 +0000 | [diff] [blame] | 790 | if( pList==0 ){ |
drh | 1743575 | 2007-08-16 04:30:38 +0000 | [diff] [blame] | 791 | pList = sqlite3DbMallocZero(db, sizeof(ExprList) ); |
drh | a76b5df | 2002-02-23 02:32:10 +0000 | [diff] [blame] | 792 | if( pList==0 ){ |
danielk1977 | d5d5652 | 2005-03-16 12:15:20 +0000 | [diff] [blame] | 793 | goto no_mem; |
drh | a76b5df | 2002-02-23 02:32:10 +0000 | [diff] [blame] | 794 | } |
drh | 4efc475 | 2004-01-16 15:55:37 +0000 | [diff] [blame] | 795 | assert( pList->nAlloc==0 ); |
drh | a76b5df | 2002-02-23 02:32:10 +0000 | [diff] [blame] | 796 | } |
drh | 4305d10 | 2003-07-30 12:34:12 +0000 | [diff] [blame] | 797 | if( pList->nAlloc<=pList->nExpr ){ |
danielk1977 | d5d5652 | 2005-03-16 12:15:20 +0000 | [diff] [blame] | 798 | struct ExprList_item *a; |
| 799 | int n = pList->nAlloc*2 + 4; |
danielk1977 | 26783a5 | 2007-08-29 14:06:22 +0000 | [diff] [blame] | 800 | a = sqlite3DbRealloc(db, pList->a, n*sizeof(pList->a[0])); |
danielk1977 | d5d5652 | 2005-03-16 12:15:20 +0000 | [diff] [blame] | 801 | if( a==0 ){ |
| 802 | goto no_mem; |
drh | a76b5df | 2002-02-23 02:32:10 +0000 | [diff] [blame] | 803 | } |
danielk1977 | d5d5652 | 2005-03-16 12:15:20 +0000 | [diff] [blame] | 804 | pList->a = a; |
| 805 | pList->nAlloc = n; |
drh | a76b5df | 2002-02-23 02:32:10 +0000 | [diff] [blame] | 806 | } |
drh | 4efc475 | 2004-01-16 15:55:37 +0000 | [diff] [blame] | 807 | assert( pList->a!=0 ); |
| 808 | if( pExpr || pName ){ |
| 809 | struct ExprList_item *pItem = &pList->a[pList->nExpr++]; |
| 810 | memset(pItem, 0, sizeof(*pItem)); |
drh | 1743575 | 2007-08-16 04:30:38 +0000 | [diff] [blame] | 811 | pItem->zName = sqlite3NameFromToken(db, pName); |
danielk1977 | e94ddc9 | 2005-03-21 03:53:38 +0000 | [diff] [blame] | 812 | pItem->pExpr = pExpr; |
drh | a76b5df | 2002-02-23 02:32:10 +0000 | [diff] [blame] | 813 | } |
| 814 | return pList; |
danielk1977 | d5d5652 | 2005-03-16 12:15:20 +0000 | [diff] [blame] | 815 | |
| 816 | no_mem: |
| 817 | /* Avoid leaking memory if malloc has failed. */ |
| 818 | sqlite3ExprDelete(pExpr); |
| 819 | sqlite3ExprListDelete(pList); |
| 820 | return 0; |
drh | a76b5df | 2002-02-23 02:32:10 +0000 | [diff] [blame] | 821 | } |
| 822 | |
| 823 | /* |
danielk1977 | 7a15a4b | 2007-05-08 17:54:43 +0000 | [diff] [blame] | 824 | ** If the expression list pEList contains more than iLimit elements, |
| 825 | ** leave an error message in pParse. |
| 826 | */ |
| 827 | void sqlite3ExprListCheckLength( |
| 828 | Parse *pParse, |
| 829 | ExprList *pEList, |
danielk1977 | 7a15a4b | 2007-05-08 17:54:43 +0000 | [diff] [blame] | 830 | const char *zObject |
| 831 | ){ |
drh | b1a6c3c | 2008-03-20 16:30:17 +0000 | [diff] [blame] | 832 | int mx = pParse->db->aLimit[SQLITE_LIMIT_COLUMN]; |
drh | c5499be | 2008-04-01 15:06:33 +0000 | [diff] [blame] | 833 | testcase( pEList && pEList->nExpr==mx ); |
| 834 | testcase( pEList && pEList->nExpr==mx+1 ); |
drh | b1a6c3c | 2008-03-20 16:30:17 +0000 | [diff] [blame] | 835 | if( pEList && pEList->nExpr>mx ){ |
danielk1977 | 7a15a4b | 2007-05-08 17:54:43 +0000 | [diff] [blame] | 836 | sqlite3ErrorMsg(pParse, "too many columns in %s", zObject); |
| 837 | } |
| 838 | } |
| 839 | |
| 840 | /* |
drh | a76b5df | 2002-02-23 02:32:10 +0000 | [diff] [blame] | 841 | ** Delete an entire expression list. |
| 842 | */ |
danielk1977 | 4adee20 | 2004-05-08 08:23:19 +0000 | [diff] [blame] | 843 | void sqlite3ExprListDelete(ExprList *pList){ |
drh | a76b5df | 2002-02-23 02:32:10 +0000 | [diff] [blame] | 844 | int i; |
drh | be5c89a | 2004-07-26 00:31:09 +0000 | [diff] [blame] | 845 | struct ExprList_item *pItem; |
drh | a76b5df | 2002-02-23 02:32:10 +0000 | [diff] [blame] | 846 | if( pList==0 ) return; |
drh | 1bdd9b5 | 2004-04-23 17:04:44 +0000 | [diff] [blame] | 847 | assert( pList->a!=0 || (pList->nExpr==0 && pList->nAlloc==0) ); |
| 848 | assert( pList->nExpr<=pList->nAlloc ); |
drh | be5c89a | 2004-07-26 00:31:09 +0000 | [diff] [blame] | 849 | for(pItem=pList->a, i=0; i<pList->nExpr; i++, pItem++){ |
| 850 | sqlite3ExprDelete(pItem->pExpr); |
drh | 1743575 | 2007-08-16 04:30:38 +0000 | [diff] [blame] | 851 | sqlite3_free(pItem->zName); |
drh | a76b5df | 2002-02-23 02:32:10 +0000 | [diff] [blame] | 852 | } |
drh | 1743575 | 2007-08-16 04:30:38 +0000 | [diff] [blame] | 853 | sqlite3_free(pList->a); |
| 854 | sqlite3_free(pList); |
drh | a76b5df | 2002-02-23 02:32:10 +0000 | [diff] [blame] | 855 | } |
| 856 | |
| 857 | /* |
drh | 678ccce | 2008-03-31 18:19:54 +0000 | [diff] [blame] | 858 | ** Walk an expression tree. Call xFunc for each node visited. xFunc |
| 859 | ** is called on the node before xFunc is called on the nodes children. |
drh | 73b211a | 2005-01-18 04:00:42 +0000 | [diff] [blame] | 860 | ** |
drh | 626a879 | 2005-01-17 22:08:19 +0000 | [diff] [blame] | 861 | ** The return value from xFunc determines whether the tree walk continues. |
| 862 | ** 0 means continue walking the tree. 1 means do not walk children |
| 863 | ** of the current node but continue with siblings. 2 means abandon |
| 864 | ** the tree walk completely. |
| 865 | ** |
| 866 | ** The return value from this routine is 1 to abandon the tree walk |
| 867 | ** and 0 to continue. |
drh | 87abf5c | 2005-08-25 12:45:04 +0000 | [diff] [blame] | 868 | ** |
| 869 | ** NOTICE: This routine does *not* descend into subqueries. |
drh | 626a879 | 2005-01-17 22:08:19 +0000 | [diff] [blame] | 870 | */ |
danielk1977 | a58fdfb | 2005-02-08 07:50:40 +0000 | [diff] [blame] | 871 | static int walkExprList(ExprList *, int (*)(void *, Expr*), void *); |
drh | 626a879 | 2005-01-17 22:08:19 +0000 | [diff] [blame] | 872 | static int walkExprTree(Expr *pExpr, int (*xFunc)(void*,Expr*), void *pArg){ |
drh | 626a879 | 2005-01-17 22:08:19 +0000 | [diff] [blame] | 873 | int rc; |
| 874 | if( pExpr==0 ) return 0; |
| 875 | rc = (*xFunc)(pArg, pExpr); |
| 876 | if( rc==0 ){ |
| 877 | if( walkExprTree(pExpr->pLeft, xFunc, pArg) ) return 1; |
| 878 | if( walkExprTree(pExpr->pRight, xFunc, pArg) ) return 1; |
danielk1977 | a58fdfb | 2005-02-08 07:50:40 +0000 | [diff] [blame] | 879 | if( walkExprList(pExpr->pList, xFunc, pArg) ) return 1; |
drh | 626a879 | 2005-01-17 22:08:19 +0000 | [diff] [blame] | 880 | } |
| 881 | return rc>1; |
| 882 | } |
| 883 | |
| 884 | /* |
danielk1977 | a58fdfb | 2005-02-08 07:50:40 +0000 | [diff] [blame] | 885 | ** Call walkExprTree() for every expression in list p. |
| 886 | */ |
| 887 | static int walkExprList(ExprList *p, int (*xFunc)(void *, Expr*), void *pArg){ |
| 888 | int i; |
| 889 | struct ExprList_item *pItem; |
| 890 | if( !p ) return 0; |
| 891 | for(i=p->nExpr, pItem=p->a; i>0; i--, pItem++){ |
| 892 | if( walkExprTree(pItem->pExpr, xFunc, pArg) ) return 1; |
| 893 | } |
| 894 | return 0; |
| 895 | } |
| 896 | |
| 897 | /* |
| 898 | ** Call walkExprTree() for every expression in Select p, not including |
| 899 | ** expressions that are part of sub-selects in any FROM clause or the LIMIT |
| 900 | ** or OFFSET expressions.. |
| 901 | */ |
| 902 | static int walkSelectExpr(Select *p, int (*xFunc)(void *, Expr*), void *pArg){ |
| 903 | walkExprList(p->pEList, xFunc, pArg); |
| 904 | walkExprTree(p->pWhere, xFunc, pArg); |
| 905 | walkExprList(p->pGroupBy, xFunc, pArg); |
| 906 | walkExprTree(p->pHaving, xFunc, pArg); |
| 907 | walkExprList(p->pOrderBy, xFunc, pArg); |
danielk1977 | 15d7982 | 2007-05-15 07:00:34 +0000 | [diff] [blame] | 908 | if( p->pPrior ){ |
| 909 | walkSelectExpr(p->pPrior, xFunc, pArg); |
| 910 | } |
danielk1977 | a58fdfb | 2005-02-08 07:50:40 +0000 | [diff] [blame] | 911 | return 0; |
| 912 | } |
| 913 | |
| 914 | |
| 915 | /* |
drh | 626a879 | 2005-01-17 22:08:19 +0000 | [diff] [blame] | 916 | ** This routine is designed as an xFunc for walkExprTree(). |
| 917 | ** |
| 918 | ** pArg is really a pointer to an integer. If we can tell by looking |
drh | 73b211a | 2005-01-18 04:00:42 +0000 | [diff] [blame] | 919 | ** at pExpr that the expression that contains pExpr is not a constant |
| 920 | ** expression, then set *pArg to 0 and return 2 to abandon the tree walk. |
| 921 | ** If pExpr does does not disqualify the expression from being a constant |
| 922 | ** then do nothing. |
| 923 | ** |
| 924 | ** After walking the whole tree, if no nodes are found that disqualify |
| 925 | ** the expression as constant, then we assume the whole expression |
| 926 | ** is constant. See sqlite3ExprIsConstant() for additional information. |
drh | 626a879 | 2005-01-17 22:08:19 +0000 | [diff] [blame] | 927 | */ |
| 928 | static int exprNodeIsConstant(void *pArg, Expr *pExpr){ |
drh | 0a16837 | 2007-06-08 00:20:47 +0000 | [diff] [blame] | 929 | int *pN = (int*)pArg; |
| 930 | |
| 931 | /* If *pArg is 3 then any term of the expression that comes from |
| 932 | ** the ON or USING clauses of a join disqualifies the expression |
| 933 | ** from being considered constant. */ |
| 934 | if( (*pN)==3 && ExprHasAnyProperty(pExpr, EP_FromJoin) ){ |
| 935 | *pN = 0; |
| 936 | return 2; |
| 937 | } |
| 938 | |
drh | 626a879 | 2005-01-17 22:08:19 +0000 | [diff] [blame] | 939 | switch( pExpr->op ){ |
drh | eb55bd2 | 2005-06-30 17:04:21 +0000 | [diff] [blame] | 940 | /* Consider functions to be constant if all their arguments are constant |
| 941 | ** and *pArg==2 */ |
| 942 | case TK_FUNCTION: |
drh | 0a16837 | 2007-06-08 00:20:47 +0000 | [diff] [blame] | 943 | if( (*pN)==2 ) return 0; |
drh | eb55bd2 | 2005-06-30 17:04:21 +0000 | [diff] [blame] | 944 | /* Fall through */ |
drh | 626a879 | 2005-01-17 22:08:19 +0000 | [diff] [blame] | 945 | case TK_ID: |
| 946 | case TK_COLUMN: |
| 947 | case TK_DOT: |
| 948 | case TK_AGG_FUNCTION: |
drh | 1344989 | 2005-09-07 21:22:45 +0000 | [diff] [blame] | 949 | case TK_AGG_COLUMN: |
drh | fe2093d | 2005-01-20 22:48:47 +0000 | [diff] [blame] | 950 | #ifndef SQLITE_OMIT_SUBQUERY |
| 951 | case TK_SELECT: |
| 952 | case TK_EXISTS: |
drh | c5499be | 2008-04-01 15:06:33 +0000 | [diff] [blame] | 953 | testcase( pExpr->op==TK_SELECT ); |
| 954 | testcase( pExpr->op==TK_EXISTS ); |
drh | fe2093d | 2005-01-20 22:48:47 +0000 | [diff] [blame] | 955 | #endif |
drh | c5499be | 2008-04-01 15:06:33 +0000 | [diff] [blame] | 956 | testcase( pExpr->op==TK_ID ); |
| 957 | testcase( pExpr->op==TK_COLUMN ); |
| 958 | testcase( pExpr->op==TK_DOT ); |
| 959 | testcase( pExpr->op==TK_AGG_FUNCTION ); |
| 960 | testcase( pExpr->op==TK_AGG_COLUMN ); |
drh | 0a16837 | 2007-06-08 00:20:47 +0000 | [diff] [blame] | 961 | *pN = 0; |
drh | 626a879 | 2005-01-17 22:08:19 +0000 | [diff] [blame] | 962 | return 2; |
drh | 87abf5c | 2005-08-25 12:45:04 +0000 | [diff] [blame] | 963 | case TK_IN: |
| 964 | if( pExpr->pSelect ){ |
drh | 0a16837 | 2007-06-08 00:20:47 +0000 | [diff] [blame] | 965 | *pN = 0; |
drh | 87abf5c | 2005-08-25 12:45:04 +0000 | [diff] [blame] | 966 | return 2; |
| 967 | } |
drh | 626a879 | 2005-01-17 22:08:19 +0000 | [diff] [blame] | 968 | default: |
| 969 | return 0; |
| 970 | } |
| 971 | } |
| 972 | |
| 973 | /* |
drh | fef5208 | 2000-06-06 01:50:43 +0000 | [diff] [blame] | 974 | ** Walk an expression tree. Return 1 if the expression is constant |
drh | eb55bd2 | 2005-06-30 17:04:21 +0000 | [diff] [blame] | 975 | ** and 0 if it involves variables or function calls. |
drh | 2398937 | 2002-05-21 13:43:04 +0000 | [diff] [blame] | 976 | ** |
| 977 | ** For the purposes of this function, a double-quoted string (ex: "abc") |
| 978 | ** is considered a variable but a single-quoted string (ex: 'abc') is |
| 979 | ** a constant. |
drh | fef5208 | 2000-06-06 01:50:43 +0000 | [diff] [blame] | 980 | */ |
danielk1977 | 4adee20 | 2004-05-08 08:23:19 +0000 | [diff] [blame] | 981 | int sqlite3ExprIsConstant(Expr *p){ |
drh | 626a879 | 2005-01-17 22:08:19 +0000 | [diff] [blame] | 982 | int isConst = 1; |
| 983 | walkExprTree(p, exprNodeIsConstant, &isConst); |
| 984 | return isConst; |
drh | fef5208 | 2000-06-06 01:50:43 +0000 | [diff] [blame] | 985 | } |
| 986 | |
| 987 | /* |
drh | eb55bd2 | 2005-06-30 17:04:21 +0000 | [diff] [blame] | 988 | ** Walk an expression tree. Return 1 if the expression is constant |
drh | 0a16837 | 2007-06-08 00:20:47 +0000 | [diff] [blame] | 989 | ** that does no originate from the ON or USING clauses of a join. |
| 990 | ** Return 0 if it involves variables or function calls or terms from |
| 991 | ** an ON or USING clause. |
| 992 | */ |
| 993 | int sqlite3ExprIsConstantNotJoin(Expr *p){ |
| 994 | int isConst = 3; |
| 995 | walkExprTree(p, exprNodeIsConstant, &isConst); |
| 996 | return isConst!=0; |
| 997 | } |
| 998 | |
| 999 | /* |
| 1000 | ** Walk an expression tree. Return 1 if the expression is constant |
drh | eb55bd2 | 2005-06-30 17:04:21 +0000 | [diff] [blame] | 1001 | ** or a function call with constant arguments. Return and 0 if there |
| 1002 | ** are any variables. |
| 1003 | ** |
| 1004 | ** For the purposes of this function, a double-quoted string (ex: "abc") |
| 1005 | ** is considered a variable but a single-quoted string (ex: 'abc') is |
| 1006 | ** a constant. |
| 1007 | */ |
| 1008 | int sqlite3ExprIsConstantOrFunction(Expr *p){ |
| 1009 | int isConst = 2; |
| 1010 | walkExprTree(p, exprNodeIsConstant, &isConst); |
| 1011 | return isConst!=0; |
| 1012 | } |
| 1013 | |
| 1014 | /* |
drh | 73b211a | 2005-01-18 04:00:42 +0000 | [diff] [blame] | 1015 | ** If the expression p codes a constant integer that is small enough |
drh | 202b2df | 2004-01-06 01:13:46 +0000 | [diff] [blame] | 1016 | ** to fit in a 32-bit integer, return 1 and put the value of the integer |
| 1017 | ** in *pValue. If the expression is not an integer or if it is too big |
| 1018 | ** to fit in a signed 32-bit integer, return 0 and leave *pValue unchanged. |
drh | e4de1fe | 2002-06-02 16:09:01 +0000 | [diff] [blame] | 1019 | */ |
danielk1977 | 4adee20 | 2004-05-08 08:23:19 +0000 | [diff] [blame] | 1020 | int sqlite3ExprIsInteger(Expr *p, int *pValue){ |
drh | 92b01d5 | 2008-06-24 00:32:35 +0000 | [diff] [blame] | 1021 | int rc = 0; |
| 1022 | if( p->flags & EP_IntValue ){ |
| 1023 | *pValue = p->iTable; |
| 1024 | return 1; |
| 1025 | } |
drh | e4de1fe | 2002-06-02 16:09:01 +0000 | [diff] [blame] | 1026 | switch( p->op ){ |
| 1027 | case TK_INTEGER: { |
drh | 92b01d5 | 2008-06-24 00:32:35 +0000 | [diff] [blame] | 1028 | rc = sqlite3GetInt32((char*)p->token.z, pValue); |
drh | 202b2df | 2004-01-06 01:13:46 +0000 | [diff] [blame] | 1029 | break; |
drh | e4de1fe | 2002-06-02 16:09:01 +0000 | [diff] [blame] | 1030 | } |
drh | 4b59ab5 | 2002-08-24 18:24:51 +0000 | [diff] [blame] | 1031 | case TK_UPLUS: { |
drh | 92b01d5 | 2008-06-24 00:32:35 +0000 | [diff] [blame] | 1032 | rc = sqlite3ExprIsInteger(p->pLeft, pValue); |
drh | f6e369a | 2008-06-24 12:46:30 +0000 | [diff] [blame] | 1033 | break; |
drh | 4b59ab5 | 2002-08-24 18:24:51 +0000 | [diff] [blame] | 1034 | } |
drh | e4de1fe | 2002-06-02 16:09:01 +0000 | [diff] [blame] | 1035 | case TK_UMINUS: { |
| 1036 | int v; |
danielk1977 | 4adee20 | 2004-05-08 08:23:19 +0000 | [diff] [blame] | 1037 | if( sqlite3ExprIsInteger(p->pLeft, &v) ){ |
drh | e4de1fe | 2002-06-02 16:09:01 +0000 | [diff] [blame] | 1038 | *pValue = -v; |
drh | 92b01d5 | 2008-06-24 00:32:35 +0000 | [diff] [blame] | 1039 | rc = 1; |
drh | e4de1fe | 2002-06-02 16:09:01 +0000 | [diff] [blame] | 1040 | } |
| 1041 | break; |
| 1042 | } |
| 1043 | default: break; |
| 1044 | } |
drh | 92b01d5 | 2008-06-24 00:32:35 +0000 | [diff] [blame] | 1045 | if( rc ){ |
| 1046 | p->op = TK_INTEGER; |
| 1047 | p->flags |= EP_IntValue; |
| 1048 | p->iTable = *pValue; |
| 1049 | } |
| 1050 | return rc; |
drh | e4de1fe | 2002-06-02 16:09:01 +0000 | [diff] [blame] | 1051 | } |
| 1052 | |
| 1053 | /* |
drh | c4a3c77 | 2001-04-04 11:48:57 +0000 | [diff] [blame] | 1054 | ** Return TRUE if the given string is a row-id column name. |
| 1055 | */ |
danielk1977 | 4adee20 | 2004-05-08 08:23:19 +0000 | [diff] [blame] | 1056 | int sqlite3IsRowid(const char *z){ |
| 1057 | if( sqlite3StrICmp(z, "_ROWID_")==0 ) return 1; |
| 1058 | if( sqlite3StrICmp(z, "ROWID")==0 ) return 1; |
| 1059 | if( sqlite3StrICmp(z, "OID")==0 ) return 1; |
drh | c4a3c77 | 2001-04-04 11:48:57 +0000 | [diff] [blame] | 1060 | return 0; |
| 1061 | } |
| 1062 | |
| 1063 | /* |
drh | 8141f61 | 2004-01-25 22:44:58 +0000 | [diff] [blame] | 1064 | ** Given the name of a column of the form X.Y.Z or Y.Z or just Z, look up |
| 1065 | ** that name in the set of source tables in pSrcList and make the pExpr |
| 1066 | ** expression node refer back to that source column. The following changes |
| 1067 | ** are made to pExpr: |
| 1068 | ** |
| 1069 | ** pExpr->iDb Set the index in db->aDb[] of the database holding |
| 1070 | ** the table. |
| 1071 | ** pExpr->iTable Set to the cursor number for the table obtained |
| 1072 | ** from pSrcList. |
| 1073 | ** pExpr->iColumn Set to the column number within the table. |
drh | 8141f61 | 2004-01-25 22:44:58 +0000 | [diff] [blame] | 1074 | ** pExpr->op Set to TK_COLUMN. |
| 1075 | ** pExpr->pLeft Any expression this points to is deleted |
| 1076 | ** pExpr->pRight Any expression this points to is deleted. |
| 1077 | ** |
| 1078 | ** The pDbToken is the name of the database (the "X"). This value may be |
| 1079 | ** NULL meaning that name is of the form Y.Z or Z. Any available database |
| 1080 | ** can be used. The pTableToken is the name of the table (the "Y"). This |
| 1081 | ** value can be NULL if pDbToken is also NULL. If pTableToken is NULL it |
| 1082 | ** means that the form of the name is Z and that columns from any table |
| 1083 | ** can be used. |
| 1084 | ** |
| 1085 | ** If the name cannot be resolved unambiguously, leave an error message |
| 1086 | ** in pParse and return non-zero. Return zero on success. |
| 1087 | */ |
| 1088 | static int lookupName( |
drh | ffe07b2 | 2005-11-03 00:41:17 +0000 | [diff] [blame] | 1089 | Parse *pParse, /* The parsing context */ |
drh | 8141f61 | 2004-01-25 22:44:58 +0000 | [diff] [blame] | 1090 | Token *pDbToken, /* Name of the database containing table, or NULL */ |
| 1091 | Token *pTableToken, /* Name of table containing column, or NULL */ |
| 1092 | Token *pColumnToken, /* Name of the column. */ |
drh | 626a879 | 2005-01-17 22:08:19 +0000 | [diff] [blame] | 1093 | NameContext *pNC, /* The name context used to resolve the name */ |
drh | 8141f61 | 2004-01-25 22:44:58 +0000 | [diff] [blame] | 1094 | Expr *pExpr /* Make this EXPR node point to the selected column */ |
| 1095 | ){ |
| 1096 | char *zDb = 0; /* Name of the database. The "X" in X.Y.Z */ |
| 1097 | char *zTab = 0; /* Name of the table. The "Y" in X.Y.Z or Y.Z */ |
| 1098 | char *zCol = 0; /* Name of the column. The "Z" */ |
| 1099 | int i, j; /* Loop counters */ |
| 1100 | int cnt = 0; /* Number of matching column names */ |
| 1101 | int cntTab = 0; /* Number of matching table names */ |
drh | 626a879 | 2005-01-17 22:08:19 +0000 | [diff] [blame] | 1102 | sqlite3 *db = pParse->db; /* The database */ |
drh | 5166986 | 2004-12-18 18:40:26 +0000 | [diff] [blame] | 1103 | struct SrcList_item *pItem; /* Use for looping over pSrcList items */ |
| 1104 | struct SrcList_item *pMatch = 0; /* The matching pSrcList item */ |
drh | 73b211a | 2005-01-18 04:00:42 +0000 | [diff] [blame] | 1105 | NameContext *pTopNC = pNC; /* First namecontext in the list */ |
drh | 728b577 | 2007-09-18 15:55:07 +0000 | [diff] [blame] | 1106 | Schema *pSchema = 0; /* Schema of the expression */ |
drh | 8141f61 | 2004-01-25 22:44:58 +0000 | [diff] [blame] | 1107 | |
| 1108 | assert( pColumnToken && pColumnToken->z ); /* The Z in X.Y.Z cannot be NULL */ |
drh | 1743575 | 2007-08-16 04:30:38 +0000 | [diff] [blame] | 1109 | zDb = sqlite3NameFromToken(db, pDbToken); |
| 1110 | zTab = sqlite3NameFromToken(db, pTableToken); |
| 1111 | zCol = sqlite3NameFromToken(db, pColumnToken); |
| 1112 | if( db->mallocFailed ){ |
danielk1977 | d5d5652 | 2005-03-16 12:15:20 +0000 | [diff] [blame] | 1113 | goto lookupname_end; |
drh | 8141f61 | 2004-01-25 22:44:58 +0000 | [diff] [blame] | 1114 | } |
drh | 8141f61 | 2004-01-25 22:44:58 +0000 | [diff] [blame] | 1115 | |
| 1116 | pExpr->iTable = -1; |
drh | 626a879 | 2005-01-17 22:08:19 +0000 | [diff] [blame] | 1117 | while( pNC && cnt==0 ){ |
drh | ffe07b2 | 2005-11-03 00:41:17 +0000 | [diff] [blame] | 1118 | ExprList *pEList; |
drh | 626a879 | 2005-01-17 22:08:19 +0000 | [diff] [blame] | 1119 | SrcList *pSrcList = pNC->pSrcList; |
drh | 8141f61 | 2004-01-25 22:44:58 +0000 | [diff] [blame] | 1120 | |
danielk1977 | b3bce66 | 2005-01-29 08:32:43 +0000 | [diff] [blame] | 1121 | if( pSrcList ){ |
| 1122 | for(i=0, pItem=pSrcList->a; i<pSrcList->nSrc; i++, pItem++){ |
drh | 43617e9 | 2006-03-06 20:55:46 +0000 | [diff] [blame] | 1123 | Table *pTab; |
| 1124 | int iDb; |
danielk1977 | b3bce66 | 2005-01-29 08:32:43 +0000 | [diff] [blame] | 1125 | Column *pCol; |
| 1126 | |
drh | 43617e9 | 2006-03-06 20:55:46 +0000 | [diff] [blame] | 1127 | pTab = pItem->pTab; |
| 1128 | assert( pTab!=0 ); |
| 1129 | iDb = sqlite3SchemaToIndex(db, pTab->pSchema); |
danielk1977 | b3bce66 | 2005-01-29 08:32:43 +0000 | [diff] [blame] | 1130 | assert( pTab->nCol>0 ); |
| 1131 | if( zTab ){ |
| 1132 | if( pItem->zAlias ){ |
| 1133 | char *zTabName = pItem->zAlias; |
| 1134 | if( sqlite3StrICmp(zTabName, zTab)!=0 ) continue; |
| 1135 | }else{ |
| 1136 | char *zTabName = pTab->zName; |
| 1137 | if( zTabName==0 || sqlite3StrICmp(zTabName, zTab)!=0 ) continue; |
danielk1977 | da18423 | 2006-01-05 11:34:32 +0000 | [diff] [blame] | 1138 | if( zDb!=0 && sqlite3StrICmp(db->aDb[iDb].zName, zDb)!=0 ){ |
danielk1977 | b3bce66 | 2005-01-29 08:32:43 +0000 | [diff] [blame] | 1139 | continue; |
| 1140 | } |
drh | 626a879 | 2005-01-17 22:08:19 +0000 | [diff] [blame] | 1141 | } |
drh | 8141f61 | 2004-01-25 22:44:58 +0000 | [diff] [blame] | 1142 | } |
danielk1977 | b3bce66 | 2005-01-29 08:32:43 +0000 | [diff] [blame] | 1143 | if( 0==(cntTab++) ){ |
drh | 626a879 | 2005-01-17 22:08:19 +0000 | [diff] [blame] | 1144 | pExpr->iTable = pItem->iCursor; |
drh | 728b577 | 2007-09-18 15:55:07 +0000 | [diff] [blame] | 1145 | pSchema = pTab->pSchema; |
danielk1977 | b3bce66 | 2005-01-29 08:32:43 +0000 | [diff] [blame] | 1146 | pMatch = pItem; |
| 1147 | } |
| 1148 | for(j=0, pCol=pTab->aCol; j<pTab->nCol; j++, pCol++){ |
| 1149 | if( sqlite3StrICmp(pCol->zName, zCol)==0 ){ |
danielk1977 | b3bf556 | 2006-01-10 17:58:23 +0000 | [diff] [blame] | 1150 | const char *zColl = pTab->aCol[j].zColl; |
drh | 873fac0 | 2005-06-06 17:11:46 +0000 | [diff] [blame] | 1151 | IdList *pUsing; |
danielk1977 | b3bce66 | 2005-01-29 08:32:43 +0000 | [diff] [blame] | 1152 | cnt++; |
| 1153 | pExpr->iTable = pItem->iCursor; |
| 1154 | pMatch = pItem; |
drh | 728b577 | 2007-09-18 15:55:07 +0000 | [diff] [blame] | 1155 | pSchema = pTab->pSchema; |
danielk1977 | b3bce66 | 2005-01-29 08:32:43 +0000 | [diff] [blame] | 1156 | /* Substitute the rowid (column -1) for the INTEGER PRIMARY KEY */ |
| 1157 | pExpr->iColumn = j==pTab->iPKey ? -1 : j; |
| 1158 | pExpr->affinity = pTab->aCol[j].affinity; |
drh | 8b4c40d | 2007-02-01 23:02:45 +0000 | [diff] [blame] | 1159 | if( (pExpr->flags & EP_ExpCollate)==0 ){ |
| 1160 | pExpr->pColl = sqlite3FindCollSeq(db, ENC(db), zColl,-1, 0); |
| 1161 | } |
drh | 61dfc31 | 2006-12-16 16:25:15 +0000 | [diff] [blame] | 1162 | if( i<pSrcList->nSrc-1 ){ |
| 1163 | if( pItem[1].jointype & JT_NATURAL ){ |
| 1164 | /* If this match occurred in the left table of a natural join, |
| 1165 | ** then skip the right table to avoid a duplicate match */ |
| 1166 | pItem++; |
| 1167 | i++; |
| 1168 | }else if( (pUsing = pItem[1].pUsing)!=0 ){ |
| 1169 | /* If this match occurs on a column that is in the USING clause |
| 1170 | ** of a join, skip the search of the right table of the join |
| 1171 | ** to avoid a duplicate match there. */ |
| 1172 | int k; |
| 1173 | for(k=0; k<pUsing->nId; k++){ |
| 1174 | if( sqlite3StrICmp(pUsing->a[k].zName, zCol)==0 ){ |
| 1175 | pItem++; |
| 1176 | i++; |
| 1177 | break; |
| 1178 | } |
drh | 873fac0 | 2005-06-06 17:11:46 +0000 | [diff] [blame] | 1179 | } |
| 1180 | } |
| 1181 | } |
danielk1977 | b3bce66 | 2005-01-29 08:32:43 +0000 | [diff] [blame] | 1182 | break; |
| 1183 | } |
drh | 8141f61 | 2004-01-25 22:44:58 +0000 | [diff] [blame] | 1184 | } |
| 1185 | } |
| 1186 | } |
drh | 626a879 | 2005-01-17 22:08:19 +0000 | [diff] [blame] | 1187 | |
| 1188 | #ifndef SQLITE_OMIT_TRIGGER |
| 1189 | /* If we have not already resolved the name, then maybe |
| 1190 | ** it is a new.* or old.* trigger argument reference |
| 1191 | */ |
| 1192 | if( zDb==0 && zTab!=0 && cnt==0 && pParse->trigStack!=0 ){ |
| 1193 | TriggerStack *pTriggerStack = pParse->trigStack; |
| 1194 | Table *pTab = 0; |
danielk1977 | 8f2c54e | 2008-01-01 19:02:09 +0000 | [diff] [blame] | 1195 | u32 *piColMask; |
drh | 626a879 | 2005-01-17 22:08:19 +0000 | [diff] [blame] | 1196 | if( pTriggerStack->newIdx != -1 && sqlite3StrICmp("new", zTab) == 0 ){ |
| 1197 | pExpr->iTable = pTriggerStack->newIdx; |
| 1198 | assert( pTriggerStack->pTab ); |
| 1199 | pTab = pTriggerStack->pTab; |
danielk1977 | 8f2c54e | 2008-01-01 19:02:09 +0000 | [diff] [blame] | 1200 | piColMask = &(pTriggerStack->newColMask); |
drh | 626a879 | 2005-01-17 22:08:19 +0000 | [diff] [blame] | 1201 | }else if( pTriggerStack->oldIdx != -1 && sqlite3StrICmp("old", zTab)==0 ){ |
| 1202 | pExpr->iTable = pTriggerStack->oldIdx; |
| 1203 | assert( pTriggerStack->pTab ); |
| 1204 | pTab = pTriggerStack->pTab; |
danielk1977 | 8f2c54e | 2008-01-01 19:02:09 +0000 | [diff] [blame] | 1205 | piColMask = &(pTriggerStack->oldColMask); |
drh | 626a879 | 2005-01-17 22:08:19 +0000 | [diff] [blame] | 1206 | } |
| 1207 | |
| 1208 | if( pTab ){ |
danielk1977 | f011300 | 2006-01-24 12:09:17 +0000 | [diff] [blame] | 1209 | int iCol; |
drh | 626a879 | 2005-01-17 22:08:19 +0000 | [diff] [blame] | 1210 | Column *pCol = pTab->aCol; |
| 1211 | |
drh | 728b577 | 2007-09-18 15:55:07 +0000 | [diff] [blame] | 1212 | pSchema = pTab->pSchema; |
drh | 626a879 | 2005-01-17 22:08:19 +0000 | [diff] [blame] | 1213 | cntTab++; |
danielk1977 | f011300 | 2006-01-24 12:09:17 +0000 | [diff] [blame] | 1214 | for(iCol=0; iCol < pTab->nCol; iCol++, pCol++) { |
drh | 626a879 | 2005-01-17 22:08:19 +0000 | [diff] [blame] | 1215 | if( sqlite3StrICmp(pCol->zName, zCol)==0 ){ |
danielk1977 | f011300 | 2006-01-24 12:09:17 +0000 | [diff] [blame] | 1216 | const char *zColl = pTab->aCol[iCol].zColl; |
drh | 626a879 | 2005-01-17 22:08:19 +0000 | [diff] [blame] | 1217 | cnt++; |
danielk1977 | f011300 | 2006-01-24 12:09:17 +0000 | [diff] [blame] | 1218 | pExpr->iColumn = iCol==pTab->iPKey ? -1 : iCol; |
| 1219 | pExpr->affinity = pTab->aCol[iCol].affinity; |
drh | 8b4c40d | 2007-02-01 23:02:45 +0000 | [diff] [blame] | 1220 | if( (pExpr->flags & EP_ExpCollate)==0 ){ |
| 1221 | pExpr->pColl = sqlite3FindCollSeq(db, ENC(db), zColl,-1, 0); |
| 1222 | } |
danielk1977 | aee18ef | 2005-03-09 12:26:50 +0000 | [diff] [blame] | 1223 | pExpr->pTab = pTab; |
danielk1977 | 8f2c54e | 2008-01-01 19:02:09 +0000 | [diff] [blame] | 1224 | if( iCol>=0 ){ |
drh | c5499be | 2008-04-01 15:06:33 +0000 | [diff] [blame] | 1225 | testcase( iCol==31 ); |
| 1226 | testcase( iCol==32 ); |
danielk1977 | 8f2c54e | 2008-01-01 19:02:09 +0000 | [diff] [blame] | 1227 | *piColMask |= ((u32)1<<iCol) | (iCol>=32?0xffffffff:0); |
| 1228 | } |
drh | 626a879 | 2005-01-17 22:08:19 +0000 | [diff] [blame] | 1229 | break; |
| 1230 | } |
| 1231 | } |
| 1232 | } |
| 1233 | } |
drh | b7f9164 | 2004-10-31 02:22:47 +0000 | [diff] [blame] | 1234 | #endif /* !defined(SQLITE_OMIT_TRIGGER) */ |
drh | 8141f61 | 2004-01-25 22:44:58 +0000 | [diff] [blame] | 1235 | |
drh | 626a879 | 2005-01-17 22:08:19 +0000 | [diff] [blame] | 1236 | /* |
| 1237 | ** Perhaps the name is a reference to the ROWID |
| 1238 | */ |
| 1239 | if( cnt==0 && cntTab==1 && sqlite3IsRowid(zCol) ){ |
| 1240 | cnt = 1; |
| 1241 | pExpr->iColumn = -1; |
drh | 8a51256 | 2005-11-14 22:29:05 +0000 | [diff] [blame] | 1242 | pExpr->affinity = SQLITE_AFF_INTEGER; |
drh | 626a879 | 2005-01-17 22:08:19 +0000 | [diff] [blame] | 1243 | } |
drh | 8141f61 | 2004-01-25 22:44:58 +0000 | [diff] [blame] | 1244 | |
drh | 626a879 | 2005-01-17 22:08:19 +0000 | [diff] [blame] | 1245 | /* |
| 1246 | ** If the input is of the form Z (not Y.Z or X.Y.Z) then the name Z |
| 1247 | ** might refer to an result-set alias. This happens, for example, when |
| 1248 | ** we are resolving names in the WHERE clause of the following command: |
| 1249 | ** |
| 1250 | ** SELECT a+b AS x FROM table WHERE x<10; |
| 1251 | ** |
| 1252 | ** In cases like this, replace pExpr with a copy of the expression that |
| 1253 | ** forms the result set entry ("a+b" in the example) and return immediately. |
| 1254 | ** Note that the expression in the result set should have already been |
| 1255 | ** resolved by the time the WHERE clause is resolved. |
| 1256 | */ |
drh | ffe07b2 | 2005-11-03 00:41:17 +0000 | [diff] [blame] | 1257 | if( cnt==0 && (pEList = pNC->pEList)!=0 && zTab==0 ){ |
drh | 626a879 | 2005-01-17 22:08:19 +0000 | [diff] [blame] | 1258 | for(j=0; j<pEList->nExpr; j++){ |
| 1259 | char *zAs = pEList->a[j].zName; |
| 1260 | if( zAs!=0 && sqlite3StrICmp(zAs, zCol)==0 ){ |
drh | 36379e9 | 2007-07-23 22:51:15 +0000 | [diff] [blame] | 1261 | Expr *pDup, *pOrig; |
drh | 626a879 | 2005-01-17 22:08:19 +0000 | [diff] [blame] | 1262 | assert( pExpr->pLeft==0 && pExpr->pRight==0 ); |
drh | 4f07e5f | 2007-05-14 11:34:46 +0000 | [diff] [blame] | 1263 | assert( pExpr->pList==0 ); |
| 1264 | assert( pExpr->pSelect==0 ); |
drh | 36379e9 | 2007-07-23 22:51:15 +0000 | [diff] [blame] | 1265 | pOrig = pEList->a[j].pExpr; |
| 1266 | if( !pNC->allowAgg && ExprHasProperty(pOrig, EP_Agg) ){ |
| 1267 | sqlite3ErrorMsg(pParse, "misuse of aliased aggregate %s", zAs); |
drh | 1743575 | 2007-08-16 04:30:38 +0000 | [diff] [blame] | 1268 | sqlite3_free(zCol); |
drh | 36379e9 | 2007-07-23 22:51:15 +0000 | [diff] [blame] | 1269 | return 2; |
| 1270 | } |
danielk1977 | 1e53695 | 2007-08-16 10:09:01 +0000 | [diff] [blame] | 1271 | pDup = sqlite3ExprDup(db, pOrig); |
drh | 4f07e5f | 2007-05-14 11:34:46 +0000 | [diff] [blame] | 1272 | if( pExpr->flags & EP_ExpCollate ){ |
| 1273 | pDup->pColl = pExpr->pColl; |
| 1274 | pDup->flags |= EP_ExpCollate; |
| 1275 | } |
drh | 1743575 | 2007-08-16 04:30:38 +0000 | [diff] [blame] | 1276 | if( pExpr->span.dyn ) sqlite3_free((char*)pExpr->span.z); |
| 1277 | if( pExpr->token.dyn ) sqlite3_free((char*)pExpr->token.z); |
drh | 4f07e5f | 2007-05-14 11:34:46 +0000 | [diff] [blame] | 1278 | memcpy(pExpr, pDup, sizeof(*pExpr)); |
drh | 1743575 | 2007-08-16 04:30:38 +0000 | [diff] [blame] | 1279 | sqlite3_free(pDup); |
drh | 15ccce1 | 2005-05-23 15:06:39 +0000 | [diff] [blame] | 1280 | cnt = 1; |
danielk1977 | c9cf6e3 | 2007-06-25 16:29:33 +0000 | [diff] [blame] | 1281 | pMatch = 0; |
drh | 626a879 | 2005-01-17 22:08:19 +0000 | [diff] [blame] | 1282 | assert( zTab==0 && zDb==0 ); |
drh | 15ccce1 | 2005-05-23 15:06:39 +0000 | [diff] [blame] | 1283 | goto lookupname_end_2; |
drh | 626a879 | 2005-01-17 22:08:19 +0000 | [diff] [blame] | 1284 | } |
| 1285 | } |
| 1286 | } |
| 1287 | |
| 1288 | /* Advance to the next name context. The loop will exit when either |
| 1289 | ** we have a match (cnt>0) or when we run out of name contexts. |
| 1290 | */ |
| 1291 | if( cnt==0 ){ |
| 1292 | pNC = pNC->pNext; |
| 1293 | } |
drh | 8141f61 | 2004-01-25 22:44:58 +0000 | [diff] [blame] | 1294 | } |
| 1295 | |
| 1296 | /* |
| 1297 | ** If X and Y are NULL (in other words if only the column name Z is |
| 1298 | ** supplied) and the value of Z is enclosed in double-quotes, then |
| 1299 | ** Z is a string literal if it doesn't match any column names. In that |
| 1300 | ** case, we need to return right away and not make any changes to |
| 1301 | ** pExpr. |
drh | 15ccce1 | 2005-05-23 15:06:39 +0000 | [diff] [blame] | 1302 | ** |
| 1303 | ** Because no reference was made to outer contexts, the pNC->nRef |
| 1304 | ** fields are not changed in any context. |
drh | 8141f61 | 2004-01-25 22:44:58 +0000 | [diff] [blame] | 1305 | */ |
| 1306 | if( cnt==0 && zTab==0 && pColumnToken->z[0]=='"' ){ |
drh | 1743575 | 2007-08-16 04:30:38 +0000 | [diff] [blame] | 1307 | sqlite3_free(zCol); |
drh | 8141f61 | 2004-01-25 22:44:58 +0000 | [diff] [blame] | 1308 | return 0; |
| 1309 | } |
| 1310 | |
| 1311 | /* |
| 1312 | ** cnt==0 means there was not match. cnt>1 means there were two or |
| 1313 | ** more matches. Either way, we have an error. |
| 1314 | */ |
| 1315 | if( cnt!=1 ){ |
drh | de4fcfd | 2008-01-19 23:50:26 +0000 | [diff] [blame] | 1316 | const char *zErr; |
| 1317 | zErr = cnt==0 ? "no such column" : "ambiguous column name"; |
drh | 8141f61 | 2004-01-25 22:44:58 +0000 | [diff] [blame] | 1318 | if( zDb ){ |
drh | de4fcfd | 2008-01-19 23:50:26 +0000 | [diff] [blame] | 1319 | sqlite3ErrorMsg(pParse, "%s: %s.%s.%s", zErr, zDb, zTab, zCol); |
drh | 8141f61 | 2004-01-25 22:44:58 +0000 | [diff] [blame] | 1320 | }else if( zTab ){ |
drh | de4fcfd | 2008-01-19 23:50:26 +0000 | [diff] [blame] | 1321 | sqlite3ErrorMsg(pParse, "%s: %s.%s", zErr, zTab, zCol); |
drh | 8141f61 | 2004-01-25 22:44:58 +0000 | [diff] [blame] | 1322 | }else{ |
drh | de4fcfd | 2008-01-19 23:50:26 +0000 | [diff] [blame] | 1323 | sqlite3ErrorMsg(pParse, "%s: %s", zErr, zCol); |
drh | 8141f61 | 2004-01-25 22:44:58 +0000 | [diff] [blame] | 1324 | } |
drh | de4fcfd | 2008-01-19 23:50:26 +0000 | [diff] [blame] | 1325 | pTopNC->nErr++; |
drh | 8141f61 | 2004-01-25 22:44:58 +0000 | [diff] [blame] | 1326 | } |
| 1327 | |
drh | 5166986 | 2004-12-18 18:40:26 +0000 | [diff] [blame] | 1328 | /* If a column from a table in pSrcList is referenced, then record |
| 1329 | ** this fact in the pSrcList.a[].colUsed bitmask. Column 0 causes |
| 1330 | ** bit 0 to be set. Column 1 sets bit 1. And so forth. If the |
| 1331 | ** column number is greater than the number of bits in the bitmask |
| 1332 | ** then set the high-order bit of the bitmask. |
| 1333 | */ |
| 1334 | if( pExpr->iColumn>=0 && pMatch!=0 ){ |
| 1335 | int n = pExpr->iColumn; |
drh | c5499be | 2008-04-01 15:06:33 +0000 | [diff] [blame] | 1336 | testcase( n==sizeof(Bitmask)*8-1 ); |
drh | 5166986 | 2004-12-18 18:40:26 +0000 | [diff] [blame] | 1337 | if( n>=sizeof(Bitmask)*8 ){ |
| 1338 | n = sizeof(Bitmask)*8-1; |
| 1339 | } |
| 1340 | assert( pMatch->iCursor==pExpr->iTable ); |
drh | ca83ac5 | 2007-02-01 01:40:44 +0000 | [diff] [blame] | 1341 | pMatch->colUsed |= ((Bitmask)1)<<n; |
drh | 5166986 | 2004-12-18 18:40:26 +0000 | [diff] [blame] | 1342 | } |
| 1343 | |
danielk1977 | d5d5652 | 2005-03-16 12:15:20 +0000 | [diff] [blame] | 1344 | lookupname_end: |
drh | 8141f61 | 2004-01-25 22:44:58 +0000 | [diff] [blame] | 1345 | /* Clean up and return |
| 1346 | */ |
drh | 1743575 | 2007-08-16 04:30:38 +0000 | [diff] [blame] | 1347 | sqlite3_free(zDb); |
| 1348 | sqlite3_free(zTab); |
danielk1977 | 4adee20 | 2004-05-08 08:23:19 +0000 | [diff] [blame] | 1349 | sqlite3ExprDelete(pExpr->pLeft); |
drh | 8141f61 | 2004-01-25 22:44:58 +0000 | [diff] [blame] | 1350 | pExpr->pLeft = 0; |
danielk1977 | 4adee20 | 2004-05-08 08:23:19 +0000 | [diff] [blame] | 1351 | sqlite3ExprDelete(pExpr->pRight); |
drh | 8141f61 | 2004-01-25 22:44:58 +0000 | [diff] [blame] | 1352 | pExpr->pRight = 0; |
| 1353 | pExpr->op = TK_COLUMN; |
drh | 15ccce1 | 2005-05-23 15:06:39 +0000 | [diff] [blame] | 1354 | lookupname_end_2: |
drh | 1743575 | 2007-08-16 04:30:38 +0000 | [diff] [blame] | 1355 | sqlite3_free(zCol); |
drh | 626a879 | 2005-01-17 22:08:19 +0000 | [diff] [blame] | 1356 | if( cnt==1 ){ |
danielk1977 | b3bce66 | 2005-01-29 08:32:43 +0000 | [diff] [blame] | 1357 | assert( pNC!=0 ); |
drh | 728b577 | 2007-09-18 15:55:07 +0000 | [diff] [blame] | 1358 | sqlite3AuthRead(pParse, pExpr, pSchema, pNC->pSrcList); |
danielk1977 | aee18ef | 2005-03-09 12:26:50 +0000 | [diff] [blame] | 1359 | if( pMatch && !pMatch->pSelect ){ |
| 1360 | pExpr->pTab = pMatch->pTab; |
| 1361 | } |
drh | 15ccce1 | 2005-05-23 15:06:39 +0000 | [diff] [blame] | 1362 | /* Increment the nRef value on all name contexts from TopNC up to |
| 1363 | ** the point where the name matched. */ |
| 1364 | for(;;){ |
| 1365 | assert( pTopNC!=0 ); |
| 1366 | pTopNC->nRef++; |
| 1367 | if( pTopNC==pNC ) break; |
| 1368 | pTopNC = pTopNC->pNext; |
| 1369 | } |
| 1370 | return 0; |
| 1371 | } else { |
| 1372 | return 1; |
drh | 626a879 | 2005-01-17 22:08:19 +0000 | [diff] [blame] | 1373 | } |
drh | 8141f61 | 2004-01-25 22:44:58 +0000 | [diff] [blame] | 1374 | } |
| 1375 | |
| 1376 | /* |
drh | 626a879 | 2005-01-17 22:08:19 +0000 | [diff] [blame] | 1377 | ** This routine is designed as an xFunc for walkExprTree(). |
| 1378 | ** |
drh | 73b211a | 2005-01-18 04:00:42 +0000 | [diff] [blame] | 1379 | ** Resolve symbolic names into TK_COLUMN operators for the current |
drh | 626a879 | 2005-01-17 22:08:19 +0000 | [diff] [blame] | 1380 | ** node in the expression tree. Return 0 to continue the search down |
drh | 73b211a | 2005-01-18 04:00:42 +0000 | [diff] [blame] | 1381 | ** the tree or 2 to abort the tree walk. |
| 1382 | ** |
| 1383 | ** This routine also does error checking and name resolution for |
| 1384 | ** function names. The operator for aggregate functions is changed |
| 1385 | ** to TK_AGG_FUNCTION. |
drh | 626a879 | 2005-01-17 22:08:19 +0000 | [diff] [blame] | 1386 | */ |
| 1387 | static int nameResolverStep(void *pArg, Expr *pExpr){ |
| 1388 | NameContext *pNC = (NameContext*)pArg; |
drh | 626a879 | 2005-01-17 22:08:19 +0000 | [diff] [blame] | 1389 | Parse *pParse; |
drh | 626a879 | 2005-01-17 22:08:19 +0000 | [diff] [blame] | 1390 | |
danielk1977 | b3bce66 | 2005-01-29 08:32:43 +0000 | [diff] [blame] | 1391 | if( pExpr==0 ) return 1; |
drh | 626a879 | 2005-01-17 22:08:19 +0000 | [diff] [blame] | 1392 | assert( pNC!=0 ); |
drh | 626a879 | 2005-01-17 22:08:19 +0000 | [diff] [blame] | 1393 | pParse = pNC->pParse; |
danielk1977 | b3bce66 | 2005-01-29 08:32:43 +0000 | [diff] [blame] | 1394 | |
drh | 626a879 | 2005-01-17 22:08:19 +0000 | [diff] [blame] | 1395 | if( ExprHasAnyProperty(pExpr, EP_Resolved) ) return 1; |
| 1396 | ExprSetProperty(pExpr, EP_Resolved); |
| 1397 | #ifndef NDEBUG |
danielk1977 | f011300 | 2006-01-24 12:09:17 +0000 | [diff] [blame] | 1398 | if( pNC->pSrcList && pNC->pSrcList->nAlloc>0 ){ |
| 1399 | SrcList *pSrcList = pNC->pSrcList; |
danielk1977 | 940fac9 | 2005-01-23 22:41:37 +0000 | [diff] [blame] | 1400 | int i; |
danielk1977 | f011300 | 2006-01-24 12:09:17 +0000 | [diff] [blame] | 1401 | for(i=0; i<pNC->pSrcList->nSrc; i++){ |
drh | 626a879 | 2005-01-17 22:08:19 +0000 | [diff] [blame] | 1402 | assert( pSrcList->a[i].iCursor>=0 && pSrcList->a[i].iCursor<pParse->nTab); |
| 1403 | } |
| 1404 | } |
| 1405 | #endif |
| 1406 | switch( pExpr->op ){ |
| 1407 | /* Double-quoted strings (ex: "abc") are used as identifiers if |
| 1408 | ** possible. Otherwise they remain as strings. Single-quoted |
| 1409 | ** strings (ex: 'abc') are always string literals. |
| 1410 | */ |
| 1411 | case TK_STRING: { |
| 1412 | if( pExpr->token.z[0]=='\'' ) break; |
| 1413 | /* Fall thru into the TK_ID case if this is a double-quoted string */ |
| 1414 | } |
| 1415 | /* A lone identifier is the name of a column. |
| 1416 | */ |
| 1417 | case TK_ID: { |
drh | 626a879 | 2005-01-17 22:08:19 +0000 | [diff] [blame] | 1418 | lookupName(pParse, 0, 0, &pExpr->token, pNC, pExpr); |
| 1419 | return 1; |
| 1420 | } |
| 1421 | |
| 1422 | /* A table name and column name: ID.ID |
| 1423 | ** Or a database, table and column: ID.ID.ID |
| 1424 | */ |
| 1425 | case TK_DOT: { |
| 1426 | Token *pColumn; |
| 1427 | Token *pTable; |
| 1428 | Token *pDb; |
| 1429 | Expr *pRight; |
| 1430 | |
danielk1977 | b3bce66 | 2005-01-29 08:32:43 +0000 | [diff] [blame] | 1431 | /* if( pSrcList==0 ) break; */ |
drh | 626a879 | 2005-01-17 22:08:19 +0000 | [diff] [blame] | 1432 | pRight = pExpr->pRight; |
| 1433 | if( pRight->op==TK_ID ){ |
| 1434 | pDb = 0; |
| 1435 | pTable = &pExpr->pLeft->token; |
| 1436 | pColumn = &pRight->token; |
| 1437 | }else{ |
| 1438 | assert( pRight->op==TK_DOT ); |
| 1439 | pDb = &pExpr->pLeft->token; |
| 1440 | pTable = &pRight->pLeft->token; |
| 1441 | pColumn = &pRight->pRight->token; |
| 1442 | } |
| 1443 | lookupName(pParse, pDb, pTable, pColumn, pNC, pExpr); |
| 1444 | return 1; |
| 1445 | } |
| 1446 | |
| 1447 | /* Resolve function names |
| 1448 | */ |
drh | b71090f | 2005-05-23 17:26:51 +0000 | [diff] [blame] | 1449 | case TK_CONST_FUNC: |
drh | 626a879 | 2005-01-17 22:08:19 +0000 | [diff] [blame] | 1450 | case TK_FUNCTION: { |
| 1451 | ExprList *pList = pExpr->pList; /* The argument list */ |
| 1452 | int n = pList ? pList->nExpr : 0; /* Number of arguments */ |
| 1453 | int no_such_func = 0; /* True if no such function exists */ |
| 1454 | int wrong_num_args = 0; /* True if wrong number of arguments */ |
| 1455 | int is_agg = 0; /* True if is an aggregate function */ |
| 1456 | int i; |
drh | 5169bbc | 2006-08-24 14:59:45 +0000 | [diff] [blame] | 1457 | int auth; /* Authorization to use the function */ |
drh | 626a879 | 2005-01-17 22:08:19 +0000 | [diff] [blame] | 1458 | int nId; /* Number of characters in function name */ |
| 1459 | const char *zId; /* The function name. */ |
drh | 73b211a | 2005-01-18 04:00:42 +0000 | [diff] [blame] | 1460 | FuncDef *pDef; /* Information about the function */ |
danielk1977 | 14db266 | 2006-01-09 16:12:04 +0000 | [diff] [blame] | 1461 | int enc = ENC(pParse->db); /* The database encoding */ |
drh | 626a879 | 2005-01-17 22:08:19 +0000 | [diff] [blame] | 1462 | |
drh | 2646da7 | 2005-12-09 20:02:05 +0000 | [diff] [blame] | 1463 | zId = (char*)pExpr->token.z; |
drh | b71090f | 2005-05-23 17:26:51 +0000 | [diff] [blame] | 1464 | nId = pExpr->token.n; |
drh | 626a879 | 2005-01-17 22:08:19 +0000 | [diff] [blame] | 1465 | pDef = sqlite3FindFunction(pParse->db, zId, nId, n, enc, 0); |
| 1466 | if( pDef==0 ){ |
| 1467 | pDef = sqlite3FindFunction(pParse->db, zId, nId, -1, enc, 0); |
| 1468 | if( pDef==0 ){ |
| 1469 | no_such_func = 1; |
| 1470 | }else{ |
| 1471 | wrong_num_args = 1; |
| 1472 | } |
| 1473 | }else{ |
| 1474 | is_agg = pDef->xFunc==0; |
| 1475 | } |
drh | 2fca7fe | 2006-11-23 11:59:13 +0000 | [diff] [blame] | 1476 | #ifndef SQLITE_OMIT_AUTHORIZATION |
drh | 5169bbc | 2006-08-24 14:59:45 +0000 | [diff] [blame] | 1477 | if( pDef ){ |
| 1478 | auth = sqlite3AuthCheck(pParse, SQLITE_FUNCTION, 0, pDef->zName, 0); |
| 1479 | if( auth!=SQLITE_OK ){ |
| 1480 | if( auth==SQLITE_DENY ){ |
| 1481 | sqlite3ErrorMsg(pParse, "not authorized to use function: %s", |
| 1482 | pDef->zName); |
| 1483 | pNC->nErr++; |
| 1484 | } |
| 1485 | pExpr->op = TK_NULL; |
| 1486 | return 1; |
| 1487 | } |
| 1488 | } |
drh | b8b1421 | 2006-08-24 15:18:25 +0000 | [diff] [blame] | 1489 | #endif |
drh | 626a879 | 2005-01-17 22:08:19 +0000 | [diff] [blame] | 1490 | if( is_agg && !pNC->allowAgg ){ |
| 1491 | sqlite3ErrorMsg(pParse, "misuse of aggregate function %.*s()", nId,zId); |
| 1492 | pNC->nErr++; |
| 1493 | is_agg = 0; |
| 1494 | }else if( no_such_func ){ |
| 1495 | sqlite3ErrorMsg(pParse, "no such function: %.*s", nId, zId); |
| 1496 | pNC->nErr++; |
| 1497 | }else if( wrong_num_args ){ |
| 1498 | sqlite3ErrorMsg(pParse,"wrong number of arguments to function %.*s()", |
| 1499 | nId, zId); |
| 1500 | pNC->nErr++; |
| 1501 | } |
| 1502 | if( is_agg ){ |
| 1503 | pExpr->op = TK_AGG_FUNCTION; |
| 1504 | pNC->hasAgg = 1; |
| 1505 | } |
drh | 73b211a | 2005-01-18 04:00:42 +0000 | [diff] [blame] | 1506 | if( is_agg ) pNC->allowAgg = 0; |
drh | 626a879 | 2005-01-17 22:08:19 +0000 | [diff] [blame] | 1507 | for(i=0; pNC->nErr==0 && i<n; i++){ |
drh | 73b211a | 2005-01-18 04:00:42 +0000 | [diff] [blame] | 1508 | walkExprTree(pList->a[i].pExpr, nameResolverStep, pNC); |
drh | 626a879 | 2005-01-17 22:08:19 +0000 | [diff] [blame] | 1509 | } |
drh | 73b211a | 2005-01-18 04:00:42 +0000 | [diff] [blame] | 1510 | if( is_agg ) pNC->allowAgg = 1; |
drh | 626a879 | 2005-01-17 22:08:19 +0000 | [diff] [blame] | 1511 | /* FIX ME: Compute pExpr->affinity based on the expected return |
| 1512 | ** type of the function |
| 1513 | */ |
| 1514 | return is_agg; |
| 1515 | } |
danielk1977 | b3bce66 | 2005-01-29 08:32:43 +0000 | [diff] [blame] | 1516 | #ifndef SQLITE_OMIT_SUBQUERY |
| 1517 | case TK_SELECT: |
| 1518 | case TK_EXISTS: |
| 1519 | #endif |
| 1520 | case TK_IN: { |
| 1521 | if( pExpr->pSelect ){ |
drh | 8a9f38f | 2005-11-05 15:07:55 +0000 | [diff] [blame] | 1522 | int nRef = pNC->nRef; |
drh | 06f6541 | 2005-11-03 02:03:13 +0000 | [diff] [blame] | 1523 | #ifndef SQLITE_OMIT_CHECK |
| 1524 | if( pNC->isCheck ){ |
| 1525 | sqlite3ErrorMsg(pParse,"subqueries prohibited in CHECK constraints"); |
| 1526 | } |
| 1527 | #endif |
danielk1977 | b3bce66 | 2005-01-29 08:32:43 +0000 | [diff] [blame] | 1528 | sqlite3SelectResolve(pParse, pExpr->pSelect, pNC); |
| 1529 | assert( pNC->nRef>=nRef ); |
| 1530 | if( nRef!=pNC->nRef ){ |
| 1531 | ExprSetProperty(pExpr, EP_VarSelect); |
| 1532 | } |
| 1533 | } |
drh | 4284fb0 | 2005-11-03 12:33:28 +0000 | [diff] [blame] | 1534 | break; |
danielk1977 | b3bce66 | 2005-01-29 08:32:43 +0000 | [diff] [blame] | 1535 | } |
drh | 4284fb0 | 2005-11-03 12:33:28 +0000 | [diff] [blame] | 1536 | #ifndef SQLITE_OMIT_CHECK |
| 1537 | case TK_VARIABLE: { |
| 1538 | if( pNC->isCheck ){ |
| 1539 | sqlite3ErrorMsg(pParse,"parameters prohibited in CHECK constraints"); |
| 1540 | } |
| 1541 | break; |
| 1542 | } |
| 1543 | #endif |
drh | 626a879 | 2005-01-17 22:08:19 +0000 | [diff] [blame] | 1544 | } |
| 1545 | return 0; |
| 1546 | } |
| 1547 | |
| 1548 | /* |
drh | cce7d17 | 2000-05-31 15:34:51 +0000 | [diff] [blame] | 1549 | ** This routine walks an expression tree and resolves references to |
drh | 967e8b7 | 2000-06-21 13:59:10 +0000 | [diff] [blame] | 1550 | ** table columns. Nodes of the form ID.ID or ID resolve into an |
drh | aacc543 | 2002-01-06 17:07:40 +0000 | [diff] [blame] | 1551 | ** index to the table in the table list and a column offset. The |
| 1552 | ** Expr.opcode for such nodes is changed to TK_COLUMN. The Expr.iTable |
| 1553 | ** value is changed to the index of the referenced table in pTabList |
drh | 832508b | 2002-03-02 17:04:07 +0000 | [diff] [blame] | 1554 | ** plus the "base" value. The base value will ultimately become the |
drh | aacc543 | 2002-01-06 17:07:40 +0000 | [diff] [blame] | 1555 | ** VDBE cursor number for a cursor that is pointing into the referenced |
| 1556 | ** table. The Expr.iColumn value is changed to the index of the column |
| 1557 | ** of the referenced table. The Expr.iColumn value for the special |
| 1558 | ** ROWID column is -1. Any INTEGER PRIMARY KEY column is tried as an |
| 1559 | ** alias for ROWID. |
drh | 19a775c | 2000-06-05 18:54:46 +0000 | [diff] [blame] | 1560 | ** |
drh | 626a879 | 2005-01-17 22:08:19 +0000 | [diff] [blame] | 1561 | ** Also resolve function names and check the functions for proper |
| 1562 | ** usage. Make sure all function names are recognized and all functions |
| 1563 | ** have the correct number of arguments. Leave an error message |
| 1564 | ** in pParse->zErrMsg if anything is amiss. Return the number of errors. |
| 1565 | ** |
drh | 73b211a | 2005-01-18 04:00:42 +0000 | [diff] [blame] | 1566 | ** If the expression contains aggregate functions then set the EP_Agg |
| 1567 | ** property on the expression. |
drh | 626a879 | 2005-01-17 22:08:19 +0000 | [diff] [blame] | 1568 | */ |
danielk1977 | fc97606 | 2007-05-10 10:46:56 +0000 | [diff] [blame] | 1569 | int sqlite3ExprResolveNames( |
danielk1977 | b3bce66 | 2005-01-29 08:32:43 +0000 | [diff] [blame] | 1570 | NameContext *pNC, /* Namespace to resolve expressions in. */ |
| 1571 | Expr *pExpr /* The expression to be analyzed. */ |
drh | 626a879 | 2005-01-17 22:08:19 +0000 | [diff] [blame] | 1572 | ){ |
drh | 1344989 | 2005-09-07 21:22:45 +0000 | [diff] [blame] | 1573 | int savedHasAgg; |
drh | bb4957f | 2008-03-20 14:03:29 +0000 | [diff] [blame] | 1574 | |
drh | 73b211a | 2005-01-18 04:00:42 +0000 | [diff] [blame] | 1575 | if( pExpr==0 ) return 0; |
drh | bb4957f | 2008-03-20 14:03:29 +0000 | [diff] [blame] | 1576 | #if SQLITE_MAX_EXPR_DEPTH>0 |
| 1577 | { |
danielk1977 | 4b5255a | 2008-06-05 16:47:39 +0000 | [diff] [blame] | 1578 | if( checkExprHeight(pNC->pParse, pExpr->nHeight + pNC->pParse->nHeight) ){ |
drh | bb4957f | 2008-03-20 14:03:29 +0000 | [diff] [blame] | 1579 | return 1; |
| 1580 | } |
| 1581 | pNC->pParse->nHeight += pExpr->nHeight; |
danielk1977 | fc97606 | 2007-05-10 10:46:56 +0000 | [diff] [blame] | 1582 | } |
danielk1977 | fc97606 | 2007-05-10 10:46:56 +0000 | [diff] [blame] | 1583 | #endif |
drh | 1344989 | 2005-09-07 21:22:45 +0000 | [diff] [blame] | 1584 | savedHasAgg = pNC->hasAgg; |
| 1585 | pNC->hasAgg = 0; |
danielk1977 | b3bce66 | 2005-01-29 08:32:43 +0000 | [diff] [blame] | 1586 | walkExprTree(pExpr, nameResolverStep, pNC); |
drh | bb4957f | 2008-03-20 14:03:29 +0000 | [diff] [blame] | 1587 | #if SQLITE_MAX_EXPR_DEPTH>0 |
danielk1977 | fc97606 | 2007-05-10 10:46:56 +0000 | [diff] [blame] | 1588 | pNC->pParse->nHeight -= pExpr->nHeight; |
| 1589 | #endif |
danielk1977 | b3bce66 | 2005-01-29 08:32:43 +0000 | [diff] [blame] | 1590 | if( pNC->nErr>0 ){ |
drh | 73b211a | 2005-01-18 04:00:42 +0000 | [diff] [blame] | 1591 | ExprSetProperty(pExpr, EP_Error); |
drh | 73b211a | 2005-01-18 04:00:42 +0000 | [diff] [blame] | 1592 | } |
drh | 1344989 | 2005-09-07 21:22:45 +0000 | [diff] [blame] | 1593 | if( pNC->hasAgg ){ |
| 1594 | ExprSetProperty(pExpr, EP_Agg); |
| 1595 | }else if( savedHasAgg ){ |
| 1596 | pNC->hasAgg = 1; |
| 1597 | } |
drh | 73b211a | 2005-01-18 04:00:42 +0000 | [diff] [blame] | 1598 | return ExprHasProperty(pExpr, EP_Error); |
drh | 626a879 | 2005-01-17 22:08:19 +0000 | [diff] [blame] | 1599 | } |
| 1600 | |
drh | 1398ad3 | 2005-01-19 23:24:50 +0000 | [diff] [blame] | 1601 | /* |
| 1602 | ** A pointer instance of this structure is used to pass information |
| 1603 | ** through walkExprTree into codeSubqueryStep(). |
| 1604 | */ |
| 1605 | typedef struct QueryCoder QueryCoder; |
| 1606 | struct QueryCoder { |
| 1607 | Parse *pParse; /* The parsing context */ |
| 1608 | NameContext *pNC; /* Namespace of first enclosing query */ |
| 1609 | }; |
| 1610 | |
danielk1977 | 9a96b66 | 2007-11-29 17:05:18 +0000 | [diff] [blame] | 1611 | #ifdef SQLITE_TEST |
| 1612 | int sqlite3_enable_in_opt = 1; |
| 1613 | #else |
| 1614 | #define sqlite3_enable_in_opt 1 |
| 1615 | #endif |
| 1616 | |
| 1617 | /* |
drh | b287f4b | 2008-04-25 00:08:38 +0000 | [diff] [blame] | 1618 | ** Return true if the IN operator optimization is enabled and |
| 1619 | ** the SELECT statement p exists and is of the |
| 1620 | ** simple form: |
| 1621 | ** |
| 1622 | ** SELECT <column> FROM <table> |
| 1623 | ** |
| 1624 | ** If this is the case, it may be possible to use an existing table |
| 1625 | ** or index instead of generating an epheremal table. |
| 1626 | */ |
| 1627 | #ifndef SQLITE_OMIT_SUBQUERY |
| 1628 | static int isCandidateForInOpt(Select *p){ |
| 1629 | SrcList *pSrc; |
| 1630 | ExprList *pEList; |
| 1631 | Table *pTab; |
| 1632 | if( !sqlite3_enable_in_opt ) return 0; /* IN optimization must be enabled */ |
| 1633 | if( p==0 ) return 0; /* right-hand side of IN is SELECT */ |
| 1634 | if( p->pPrior ) return 0; /* Not a compound SELECT */ |
| 1635 | if( p->isDistinct ) return 0; /* No DISTINCT keyword */ |
| 1636 | if( p->isAgg ) return 0; /* Contains no aggregate functions */ |
| 1637 | if( p->pGroupBy ) return 0; /* Has no GROUP BY clause */ |
| 1638 | if( p->pLimit ) return 0; /* Has no LIMIT clause */ |
| 1639 | if( p->pOffset ) return 0; |
| 1640 | if( p->pWhere ) return 0; /* Has no WHERE clause */ |
| 1641 | pSrc = p->pSrc; |
| 1642 | if( pSrc==0 ) return 0; /* A single table in the FROM clause */ |
| 1643 | if( pSrc->nSrc!=1 ) return 0; |
| 1644 | if( pSrc->a[0].pSelect ) return 0; /* FROM clause is not a subquery */ |
| 1645 | pTab = pSrc->a[0].pTab; |
| 1646 | if( pTab==0 ) return 0; |
| 1647 | if( pTab->pSelect ) return 0; /* FROM clause is not a view */ |
| 1648 | if( IsVirtual(pTab) ) return 0; /* FROM clause not a virtual table */ |
| 1649 | pEList = p->pEList; |
| 1650 | if( pEList->nExpr!=1 ) return 0; /* One column in the result set */ |
| 1651 | if( pEList->a[0].pExpr->op!=TK_COLUMN ) return 0; /* Result is a column */ |
| 1652 | return 1; |
| 1653 | } |
| 1654 | #endif /* SQLITE_OMIT_SUBQUERY */ |
| 1655 | |
| 1656 | /* |
danielk1977 | 9a96b66 | 2007-11-29 17:05:18 +0000 | [diff] [blame] | 1657 | ** This function is used by the implementation of the IN (...) operator. |
| 1658 | ** It's job is to find or create a b-tree structure that may be used |
| 1659 | ** either to test for membership of the (...) set or to iterate through |
drh | 85b623f | 2007-12-13 21:54:09 +0000 | [diff] [blame] | 1660 | ** its members, skipping duplicates. |
danielk1977 | 9a96b66 | 2007-11-29 17:05:18 +0000 | [diff] [blame] | 1661 | ** |
| 1662 | ** The cursor opened on the structure (database table, database index |
| 1663 | ** or ephermal table) is stored in pX->iTable before this function returns. |
| 1664 | ** The returned value indicates the structure type, as follows: |
| 1665 | ** |
| 1666 | ** IN_INDEX_ROWID - The cursor was opened on a database table. |
drh | 2d401ab | 2008-01-10 23:50:11 +0000 | [diff] [blame] | 1667 | ** IN_INDEX_INDEX - The cursor was opened on a database index. |
danielk1977 | 9a96b66 | 2007-11-29 17:05:18 +0000 | [diff] [blame] | 1668 | ** IN_INDEX_EPH - The cursor was opened on a specially created and |
| 1669 | ** populated epheremal table. |
| 1670 | ** |
| 1671 | ** An existing structure may only be used if the SELECT is of the simple |
| 1672 | ** form: |
| 1673 | ** |
| 1674 | ** SELECT <column> FROM <table> |
| 1675 | ** |
danielk1977 | 0cdc022 | 2008-06-26 18:04:03 +0000 | [diff] [blame] | 1676 | ** If prNotFound parameter is 0, then the structure will be used to iterate |
danielk1977 | 9a96b66 | 2007-11-29 17:05:18 +0000 | [diff] [blame] | 1677 | ** through the set members, skipping any duplicates. In this case an |
| 1678 | ** epheremal table must be used unless the selected <column> is guaranteed |
| 1679 | ** to be unique - either because it is an INTEGER PRIMARY KEY or it |
| 1680 | ** is unique by virtue of a constraint or implicit index. |
danielk1977 | 0cdc022 | 2008-06-26 18:04:03 +0000 | [diff] [blame] | 1681 | ** |
| 1682 | ** If the prNotFound parameter is not 0, then the structure will be used |
| 1683 | ** for fast set membership tests. In this case an epheremal table must |
| 1684 | ** be used unless <column> is an INTEGER PRIMARY KEY or an index can |
| 1685 | ** be found with <column> as its left-most column. |
| 1686 | ** |
| 1687 | ** When the structure is being used for set membership tests, the user |
| 1688 | ** needs to know whether or not the structure contains an SQL NULL |
| 1689 | ** value in order to correctly evaluate expressions like "X IN (Y, Z)". |
| 1690 | ** If there is a chance that the structure may contain a NULL value at |
| 1691 | ** runtime, then a register is allocated and the register number written |
| 1692 | ** to *prNotFound. If there is no chance that the structure contains a |
| 1693 | ** NULL value, then *prNotFound is left unchanged. |
| 1694 | ** |
| 1695 | ** If a register is allocated and its location stored in *prNotFound, then |
| 1696 | ** its initial value is NULL. If the structure does not remain constant |
| 1697 | ** for the duration of the query (i.e. the set is a correlated sub-select), |
| 1698 | ** the value of the allocated register is reset to NULL each time the |
| 1699 | ** structure is repopulated. This allows the caller to use vdbe code |
| 1700 | ** equivalent to the following: |
| 1701 | ** |
| 1702 | ** if( register==NULL ){ |
| 1703 | ** has_null = <test if data structure contains null> |
| 1704 | ** register = 1 |
| 1705 | ** } |
| 1706 | ** |
| 1707 | ** in order to avoid running the <test if data structure contains null> |
| 1708 | ** test more often than is necessary. |
danielk1977 | 9a96b66 | 2007-11-29 17:05:18 +0000 | [diff] [blame] | 1709 | */ |
danielk1977 | 284f4ac | 2007-12-10 05:03:46 +0000 | [diff] [blame] | 1710 | #ifndef SQLITE_OMIT_SUBQUERY |
danielk1977 | 0cdc022 | 2008-06-26 18:04:03 +0000 | [diff] [blame] | 1711 | int sqlite3FindInIndex(Parse *pParse, Expr *pX, int *prNotFound){ |
danielk1977 | 9a96b66 | 2007-11-29 17:05:18 +0000 | [diff] [blame] | 1712 | Select *p; |
| 1713 | int eType = 0; |
| 1714 | int iTab = pParse->nTab++; |
danielk1977 | 0cdc022 | 2008-06-26 18:04:03 +0000 | [diff] [blame] | 1715 | int mustBeUnique = !prNotFound; |
danielk1977 | 9a96b66 | 2007-11-29 17:05:18 +0000 | [diff] [blame] | 1716 | |
| 1717 | /* The follwing if(...) expression is true if the SELECT is of the |
| 1718 | ** simple form: |
| 1719 | ** |
| 1720 | ** SELECT <column> FROM <table> |
| 1721 | ** |
| 1722 | ** If this is the case, it may be possible to use an existing table |
| 1723 | ** or index instead of generating an epheremal table. |
| 1724 | */ |
drh | b287f4b | 2008-04-25 00:08:38 +0000 | [diff] [blame] | 1725 | p = pX->pSelect; |
| 1726 | if( isCandidateForInOpt(p) ){ |
danielk1977 | 9a96b66 | 2007-11-29 17:05:18 +0000 | [diff] [blame] | 1727 | sqlite3 *db = pParse->db; |
| 1728 | Index *pIdx; |
| 1729 | Expr *pExpr = p->pEList->a[0].pExpr; |
| 1730 | int iCol = pExpr->iColumn; |
| 1731 | Vdbe *v = sqlite3GetVdbe(pParse); |
| 1732 | |
| 1733 | /* This function is only called from two places. In both cases the vdbe |
| 1734 | ** has already been allocated. So assume sqlite3GetVdbe() is always |
| 1735 | ** successful here. |
| 1736 | */ |
| 1737 | assert(v); |
| 1738 | if( iCol<0 ){ |
drh | 0a07c10 | 2008-01-03 18:03:08 +0000 | [diff] [blame] | 1739 | int iMem = ++pParse->nMem; |
danielk1977 | 9a96b66 | 2007-11-29 17:05:18 +0000 | [diff] [blame] | 1740 | int iAddr; |
| 1741 | Table *pTab = p->pSrc->a[0].pTab; |
| 1742 | int iDb = sqlite3SchemaToIndex(db, pTab->pSchema); |
| 1743 | sqlite3VdbeUsesBtree(v, iDb); |
| 1744 | |
drh | 892d317 | 2008-01-10 03:46:36 +0000 | [diff] [blame] | 1745 | iAddr = sqlite3VdbeAddOp1(v, OP_If, iMem); |
drh | 4c58312 | 2008-01-04 22:01:03 +0000 | [diff] [blame] | 1746 | sqlite3VdbeAddOp2(v, OP_Integer, 1, iMem); |
danielk1977 | 9a96b66 | 2007-11-29 17:05:18 +0000 | [diff] [blame] | 1747 | |
| 1748 | sqlite3OpenTable(pParse, iTab, iDb, pTab, OP_OpenRead); |
| 1749 | eType = IN_INDEX_ROWID; |
| 1750 | |
| 1751 | sqlite3VdbeJumpHere(v, iAddr); |
| 1752 | }else{ |
| 1753 | /* The collation sequence used by the comparison. If an index is to |
| 1754 | ** be used in place of a temp-table, it must be ordered according |
| 1755 | ** to this collation sequence. |
| 1756 | */ |
| 1757 | CollSeq *pReq = sqlite3BinaryCompareCollSeq(pParse, pX->pLeft, pExpr); |
| 1758 | |
| 1759 | /* Check that the affinity that will be used to perform the |
| 1760 | ** comparison is the same as the affinity of the column. If |
| 1761 | ** it is not, it is not possible to use any index. |
| 1762 | */ |
| 1763 | Table *pTab = p->pSrc->a[0].pTab; |
| 1764 | char aff = comparisonAffinity(pX); |
| 1765 | int affinity_ok = (pTab->aCol[iCol].affinity==aff||aff==SQLITE_AFF_NONE); |
| 1766 | |
| 1767 | for(pIdx=pTab->pIndex; pIdx && eType==0 && affinity_ok; pIdx=pIdx->pNext){ |
| 1768 | if( (pIdx->aiColumn[0]==iCol) |
| 1769 | && (pReq==sqlite3FindCollSeq(db, ENC(db), pIdx->azColl[0], -1, 0)) |
| 1770 | && (!mustBeUnique || (pIdx->nColumn==1 && pIdx->onError!=OE_None)) |
| 1771 | ){ |
| 1772 | int iDb; |
drh | 0a07c10 | 2008-01-03 18:03:08 +0000 | [diff] [blame] | 1773 | int iMem = ++pParse->nMem; |
danielk1977 | 9a96b66 | 2007-11-29 17:05:18 +0000 | [diff] [blame] | 1774 | int iAddr; |
| 1775 | char *pKey; |
| 1776 | |
| 1777 | pKey = (char *)sqlite3IndexKeyinfo(pParse, pIdx); |
| 1778 | iDb = sqlite3SchemaToIndex(db, pIdx->pSchema); |
| 1779 | sqlite3VdbeUsesBtree(v, iDb); |
| 1780 | |
drh | 892d317 | 2008-01-10 03:46:36 +0000 | [diff] [blame] | 1781 | iAddr = sqlite3VdbeAddOp1(v, OP_If, iMem); |
drh | 4c58312 | 2008-01-04 22:01:03 +0000 | [diff] [blame] | 1782 | sqlite3VdbeAddOp2(v, OP_Integer, 1, iMem); |
danielk1977 | 9a96b66 | 2007-11-29 17:05:18 +0000 | [diff] [blame] | 1783 | |
danielk1977 | cd3e8f7 | 2008-03-25 09:47:35 +0000 | [diff] [blame] | 1784 | sqlite3VdbeAddOp2(v, OP_SetNumColumns, 0, pIdx->nColumn); |
danielk1977 | 207872a | 2008-01-03 07:54:23 +0000 | [diff] [blame] | 1785 | sqlite3VdbeAddOp4(v, OP_OpenRead, iTab, pIdx->tnum, iDb, |
drh | 66a5167 | 2008-01-03 00:01:23 +0000 | [diff] [blame] | 1786 | pKey,P4_KEYINFO_HANDOFF); |
danielk1977 | 207872a | 2008-01-03 07:54:23 +0000 | [diff] [blame] | 1787 | VdbeComment((v, "%s", pIdx->zName)); |
danielk1977 | 9a96b66 | 2007-11-29 17:05:18 +0000 | [diff] [blame] | 1788 | eType = IN_INDEX_INDEX; |
danielk1977 | 9a96b66 | 2007-11-29 17:05:18 +0000 | [diff] [blame] | 1789 | |
| 1790 | sqlite3VdbeJumpHere(v, iAddr); |
danielk1977 | 0cdc022 | 2008-06-26 18:04:03 +0000 | [diff] [blame] | 1791 | if( prNotFound && !pTab->aCol[iCol].notNull ){ |
| 1792 | *prNotFound = ++pParse->nMem; |
| 1793 | } |
danielk1977 | 9a96b66 | 2007-11-29 17:05:18 +0000 | [diff] [blame] | 1794 | } |
| 1795 | } |
| 1796 | } |
| 1797 | } |
| 1798 | |
| 1799 | if( eType==0 ){ |
danielk1977 | 0cdc022 | 2008-06-26 18:04:03 +0000 | [diff] [blame] | 1800 | int rMayHaveNull = 0; |
| 1801 | if( prNotFound ){ |
| 1802 | *prNotFound = rMayHaveNull = ++pParse->nMem; |
| 1803 | } |
| 1804 | sqlite3CodeSubselect(pParse, pX, rMayHaveNull); |
danielk1977 | 9a96b66 | 2007-11-29 17:05:18 +0000 | [diff] [blame] | 1805 | eType = IN_INDEX_EPH; |
| 1806 | }else{ |
| 1807 | pX->iTable = iTab; |
| 1808 | } |
| 1809 | return eType; |
| 1810 | } |
danielk1977 | 284f4ac | 2007-12-10 05:03:46 +0000 | [diff] [blame] | 1811 | #endif |
drh | 626a879 | 2005-01-17 22:08:19 +0000 | [diff] [blame] | 1812 | |
| 1813 | /* |
drh | 9cbe635 | 2005-11-29 03:13:21 +0000 | [diff] [blame] | 1814 | ** Generate code for scalar subqueries used as an expression |
| 1815 | ** and IN operators. Examples: |
drh | 626a879 | 2005-01-17 22:08:19 +0000 | [diff] [blame] | 1816 | ** |
drh | 9cbe635 | 2005-11-29 03:13:21 +0000 | [diff] [blame] | 1817 | ** (SELECT a FROM b) -- subquery |
| 1818 | ** EXISTS (SELECT a FROM b) -- EXISTS subquery |
| 1819 | ** x IN (4,5,11) -- IN operator with list on right-hand side |
| 1820 | ** x IN (SELECT a FROM b) -- IN operator with subquery on the right |
drh | fef5208 | 2000-06-06 01:50:43 +0000 | [diff] [blame] | 1821 | ** |
drh | 9cbe635 | 2005-11-29 03:13:21 +0000 | [diff] [blame] | 1822 | ** The pExpr parameter describes the expression that contains the IN |
| 1823 | ** operator or subquery. |
drh | cce7d17 | 2000-05-31 15:34:51 +0000 | [diff] [blame] | 1824 | */ |
drh | 51522cd | 2005-01-20 13:36:19 +0000 | [diff] [blame] | 1825 | #ifndef SQLITE_OMIT_SUBQUERY |
danielk1977 | 0cdc022 | 2008-06-26 18:04:03 +0000 | [diff] [blame] | 1826 | void sqlite3CodeSubselect(Parse *pParse, Expr *pExpr, int rMayHaveNull){ |
drh | 57dbd7b | 2005-07-08 18:25:26 +0000 | [diff] [blame] | 1827 | int testAddr = 0; /* One-time test address */ |
danielk1977 | b3bce66 | 2005-01-29 08:32:43 +0000 | [diff] [blame] | 1828 | Vdbe *v = sqlite3GetVdbe(pParse); |
| 1829 | if( v==0 ) return; |
| 1830 | |
danielk1977 | fc97606 | 2007-05-10 10:46:56 +0000 | [diff] [blame] | 1831 | |
drh | 57dbd7b | 2005-07-08 18:25:26 +0000 | [diff] [blame] | 1832 | /* This code must be run in its entirety every time it is encountered |
| 1833 | ** if any of the following is true: |
| 1834 | ** |
| 1835 | ** * The right-hand side is a correlated subquery |
| 1836 | ** * The right-hand side is an expression list containing variables |
| 1837 | ** * We are inside a trigger |
| 1838 | ** |
| 1839 | ** If all of the above are false, then we can run this code just once |
| 1840 | ** save the results, and reuse the same result on subsequent invocations. |
danielk1977 | b3bce66 | 2005-01-29 08:32:43 +0000 | [diff] [blame] | 1841 | */ |
| 1842 | if( !ExprHasAnyProperty(pExpr, EP_VarSelect) && !pParse->trigStack ){ |
drh | 0a07c10 | 2008-01-03 18:03:08 +0000 | [diff] [blame] | 1843 | int mem = ++pParse->nMem; |
drh | 892d317 | 2008-01-10 03:46:36 +0000 | [diff] [blame] | 1844 | sqlite3VdbeAddOp1(v, OP_If, mem); |
| 1845 | testAddr = sqlite3VdbeAddOp2(v, OP_Integer, 1, mem); |
drh | 1743575 | 2007-08-16 04:30:38 +0000 | [diff] [blame] | 1846 | assert( testAddr>0 || pParse->db->mallocFailed ); |
danielk1977 | b3bce66 | 2005-01-29 08:32:43 +0000 | [diff] [blame] | 1847 | } |
| 1848 | |
drh | cce7d17 | 2000-05-31 15:34:51 +0000 | [diff] [blame] | 1849 | switch( pExpr->op ){ |
drh | fef5208 | 2000-06-06 01:50:43 +0000 | [diff] [blame] | 1850 | case TK_IN: { |
danielk1977 | e014a83 | 2004-05-17 10:48:57 +0000 | [diff] [blame] | 1851 | char affinity; |
drh | d3d39e9 | 2004-05-20 22:16:29 +0000 | [diff] [blame] | 1852 | KeyInfo keyInfo; |
drh | b9bb7c1 | 2006-06-11 23:41:55 +0000 | [diff] [blame] | 1853 | int addr; /* Address of OP_OpenEphemeral instruction */ |
drh | d3d39e9 | 2004-05-20 22:16:29 +0000 | [diff] [blame] | 1854 | |
danielk1977 | 0cdc022 | 2008-06-26 18:04:03 +0000 | [diff] [blame] | 1855 | if( rMayHaveNull ){ |
| 1856 | sqlite3VdbeAddOp2(v, OP_Null, 0, rMayHaveNull); |
| 1857 | } |
| 1858 | |
danielk1977 | bf3b721 | 2004-05-18 10:06:24 +0000 | [diff] [blame] | 1859 | affinity = sqlite3ExprAffinity(pExpr->pLeft); |
danielk1977 | e014a83 | 2004-05-17 10:48:57 +0000 | [diff] [blame] | 1860 | |
| 1861 | /* Whether this is an 'x IN(SELECT...)' or an 'x IN(<exprlist>)' |
drh | 57dbd7b | 2005-07-08 18:25:26 +0000 | [diff] [blame] | 1862 | ** expression it is handled the same way. A virtual table is |
danielk1977 | e014a83 | 2004-05-17 10:48:57 +0000 | [diff] [blame] | 1863 | ** filled with single-field index keys representing the results |
| 1864 | ** from the SELECT or the <exprlist>. |
| 1865 | ** |
| 1866 | ** If the 'x' expression is a column value, or the SELECT... |
| 1867 | ** statement returns a column value, then the affinity of that |
| 1868 | ** column is used to build the index keys. If both 'x' and the |
| 1869 | ** SELECT... statement are columns, then numeric affinity is used |
| 1870 | ** if either column has NUMERIC or INTEGER affinity. If neither |
| 1871 | ** 'x' nor the SELECT... statement are columns, then numeric affinity |
| 1872 | ** is used. |
| 1873 | */ |
| 1874 | pExpr->iTable = pParse->nTab++; |
danielk1977 | cd3e8f7 | 2008-03-25 09:47:35 +0000 | [diff] [blame] | 1875 | addr = sqlite3VdbeAddOp2(v, OP_OpenEphemeral, pExpr->iTable, 1); |
drh | d3d39e9 | 2004-05-20 22:16:29 +0000 | [diff] [blame] | 1876 | memset(&keyInfo, 0, sizeof(keyInfo)); |
| 1877 | keyInfo.nField = 1; |
danielk1977 | e014a83 | 2004-05-17 10:48:57 +0000 | [diff] [blame] | 1878 | |
drh | fef5208 | 2000-06-06 01:50:43 +0000 | [diff] [blame] | 1879 | if( pExpr->pSelect ){ |
| 1880 | /* Case 1: expr IN (SELECT ...) |
| 1881 | ** |
danielk1977 | e014a83 | 2004-05-17 10:48:57 +0000 | [diff] [blame] | 1882 | ** Generate code to write the results of the select into the temporary |
| 1883 | ** table allocated and opened above. |
drh | fef5208 | 2000-06-06 01:50:43 +0000 | [diff] [blame] | 1884 | */ |
drh | 1013c93 | 2008-01-06 00:25:21 +0000 | [diff] [blame] | 1885 | SelectDest dest; |
drh | be5c89a | 2004-07-26 00:31:09 +0000 | [diff] [blame] | 1886 | ExprList *pEList; |
drh | 1013c93 | 2008-01-06 00:25:21 +0000 | [diff] [blame] | 1887 | |
| 1888 | sqlite3SelectDestInit(&dest, SRT_Set, pExpr->iTable); |
| 1889 | dest.affinity = (int)affinity; |
danielk1977 | e014a83 | 2004-05-17 10:48:57 +0000 | [diff] [blame] | 1890 | assert( (pExpr->iTable&0x0000FFFF)==pExpr->iTable ); |
danielk1977 | 6c8c8ce | 2008-01-02 16:27:09 +0000 | [diff] [blame] | 1891 | if( sqlite3Select(pParse, pExpr->pSelect, &dest, 0, 0, 0, 0) ){ |
drh | 94ccde5 | 2007-04-13 16:06:32 +0000 | [diff] [blame] | 1892 | return; |
| 1893 | } |
drh | be5c89a | 2004-07-26 00:31:09 +0000 | [diff] [blame] | 1894 | pEList = pExpr->pSelect->pEList; |
| 1895 | if( pEList && pEList->nExpr>0 ){ |
danielk1977 | bcbb04e | 2007-05-29 12:11:29 +0000 | [diff] [blame] | 1896 | keyInfo.aColl[0] = sqlite3BinaryCompareCollSeq(pParse, pExpr->pLeft, |
drh | be5c89a | 2004-07-26 00:31:09 +0000 | [diff] [blame] | 1897 | pEList->a[0].pExpr); |
danielk1977 | 0202b29 | 2004-06-09 09:55:16 +0000 | [diff] [blame] | 1898 | } |
drh | fef5208 | 2000-06-06 01:50:43 +0000 | [diff] [blame] | 1899 | }else if( pExpr->pList ){ |
| 1900 | /* Case 2: expr IN (exprlist) |
| 1901 | ** |
drh | fd131da | 2007-08-07 17:13:03 +0000 | [diff] [blame] | 1902 | ** For each expression, build an index key from the evaluation and |
danielk1977 | e014a83 | 2004-05-17 10:48:57 +0000 | [diff] [blame] | 1903 | ** store it in the temporary table. If <expr> is a column, then use |
| 1904 | ** that columns affinity when building index keys. If <expr> is not |
| 1905 | ** a column, use numeric affinity. |
drh | fef5208 | 2000-06-06 01:50:43 +0000 | [diff] [blame] | 1906 | */ |
danielk1977 | e014a83 | 2004-05-17 10:48:57 +0000 | [diff] [blame] | 1907 | int i; |
drh | 57dbd7b | 2005-07-08 18:25:26 +0000 | [diff] [blame] | 1908 | ExprList *pList = pExpr->pList; |
| 1909 | struct ExprList_item *pItem; |
drh | ecc3180 | 2008-06-26 20:06:06 +0000 | [diff] [blame] | 1910 | int r1, r2, r3; |
drh | 57dbd7b | 2005-07-08 18:25:26 +0000 | [diff] [blame] | 1911 | |
danielk1977 | e014a83 | 2004-05-17 10:48:57 +0000 | [diff] [blame] | 1912 | if( !affinity ){ |
drh | 8159a35 | 2006-05-23 23:22:29 +0000 | [diff] [blame] | 1913 | affinity = SQLITE_AFF_NONE; |
danielk1977 | e014a83 | 2004-05-17 10:48:57 +0000 | [diff] [blame] | 1914 | } |
danielk1977 | 0202b29 | 2004-06-09 09:55:16 +0000 | [diff] [blame] | 1915 | keyInfo.aColl[0] = pExpr->pLeft->pColl; |
danielk1977 | e014a83 | 2004-05-17 10:48:57 +0000 | [diff] [blame] | 1916 | |
| 1917 | /* Loop through each expression in <exprlist>. */ |
drh | 2d401ab | 2008-01-10 23:50:11 +0000 | [diff] [blame] | 1918 | r1 = sqlite3GetTempReg(pParse); |
| 1919 | r2 = sqlite3GetTempReg(pParse); |
drh | 57dbd7b | 2005-07-08 18:25:26 +0000 | [diff] [blame] | 1920 | for(i=pList->nExpr, pItem=pList->a; i>0; i--, pItem++){ |
| 1921 | Expr *pE2 = pItem->pExpr; |
danielk1977 | e014a83 | 2004-05-17 10:48:57 +0000 | [diff] [blame] | 1922 | |
drh | 57dbd7b | 2005-07-08 18:25:26 +0000 | [diff] [blame] | 1923 | /* If the expression is not constant then we will need to |
| 1924 | ** disable the test that was generated above that makes sure |
| 1925 | ** this code only executes once. Because for a non-constant |
| 1926 | ** expression we need to rerun this code each time. |
| 1927 | */ |
drh | 892d317 | 2008-01-10 03:46:36 +0000 | [diff] [blame] | 1928 | if( testAddr && !sqlite3ExprIsConstant(pE2) ){ |
| 1929 | sqlite3VdbeChangeToNoop(v, testAddr-1, 2); |
drh | 57dbd7b | 2005-07-08 18:25:26 +0000 | [diff] [blame] | 1930 | testAddr = 0; |
drh | 4794b98 | 2000-06-06 13:54:14 +0000 | [diff] [blame] | 1931 | } |
danielk1977 | e014a83 | 2004-05-17 10:48:57 +0000 | [diff] [blame] | 1932 | |
| 1933 | /* Evaluate the expression and insert it into the temp table */ |
drh | e55cbd7 | 2008-03-31 23:48:03 +0000 | [diff] [blame] | 1934 | pParse->disableColCache++; |
drh | ecc3180 | 2008-06-26 20:06:06 +0000 | [diff] [blame] | 1935 | r3 = sqlite3ExprCodeTarget(pParse, pE2, r1); |
drh | c5499be | 2008-04-01 15:06:33 +0000 | [diff] [blame] | 1936 | assert( pParse->disableColCache>0 ); |
drh | e55cbd7 | 2008-03-31 23:48:03 +0000 | [diff] [blame] | 1937 | pParse->disableColCache--; |
drh | ecc3180 | 2008-06-26 20:06:06 +0000 | [diff] [blame] | 1938 | sqlite3VdbeAddOp4(v, OP_MakeRecord, r3, 1, r2, &affinity, 1); |
drh | 3c31fc2 | 2008-06-26 21:45:26 +0000 | [diff] [blame] | 1939 | sqlite3ExprCacheAffinityChange(pParse, r3, 1); |
drh | 2d401ab | 2008-01-10 23:50:11 +0000 | [diff] [blame] | 1940 | sqlite3VdbeAddOp2(v, OP_IdxInsert, pExpr->iTable, r2); |
drh | fef5208 | 2000-06-06 01:50:43 +0000 | [diff] [blame] | 1941 | } |
drh | 2d401ab | 2008-01-10 23:50:11 +0000 | [diff] [blame] | 1942 | sqlite3ReleaseTempReg(pParse, r1); |
| 1943 | sqlite3ReleaseTempReg(pParse, r2); |
drh | fef5208 | 2000-06-06 01:50:43 +0000 | [diff] [blame] | 1944 | } |
drh | 66a5167 | 2008-01-03 00:01:23 +0000 | [diff] [blame] | 1945 | sqlite3VdbeChangeP4(v, addr, (void *)&keyInfo, P4_KEYINFO); |
danielk1977 | b3bce66 | 2005-01-29 08:32:43 +0000 | [diff] [blame] | 1946 | break; |
drh | fef5208 | 2000-06-06 01:50:43 +0000 | [diff] [blame] | 1947 | } |
| 1948 | |
drh | 51522cd | 2005-01-20 13:36:19 +0000 | [diff] [blame] | 1949 | case TK_EXISTS: |
drh | 19a775c | 2000-06-05 18:54:46 +0000 | [diff] [blame] | 1950 | case TK_SELECT: { |
drh | fef5208 | 2000-06-06 01:50:43 +0000 | [diff] [blame] | 1951 | /* This has to be a scalar SELECT. Generate code to put the |
| 1952 | ** value of this select in a memory cell and record the number |
drh | 967e8b7 | 2000-06-21 13:59:10 +0000 | [diff] [blame] | 1953 | ** of the memory cell in iColumn. |
drh | fef5208 | 2000-06-06 01:50:43 +0000 | [diff] [blame] | 1954 | */ |
drh | 2646da7 | 2005-12-09 20:02:05 +0000 | [diff] [blame] | 1955 | static const Token one = { (u8*)"1", 0, 1 }; |
drh | 51522cd | 2005-01-20 13:36:19 +0000 | [diff] [blame] | 1956 | Select *pSel; |
danielk1977 | 6c8c8ce | 2008-01-02 16:27:09 +0000 | [diff] [blame] | 1957 | SelectDest dest; |
drh | 1398ad3 | 2005-01-19 23:24:50 +0000 | [diff] [blame] | 1958 | |
drh | 51522cd | 2005-01-20 13:36:19 +0000 | [diff] [blame] | 1959 | pSel = pExpr->pSelect; |
drh | 1013c93 | 2008-01-06 00:25:21 +0000 | [diff] [blame] | 1960 | sqlite3SelectDestInit(&dest, 0, ++pParse->nMem); |
drh | 51522cd | 2005-01-20 13:36:19 +0000 | [diff] [blame] | 1961 | if( pExpr->op==TK_SELECT ){ |
danielk1977 | 6c8c8ce | 2008-01-02 16:27:09 +0000 | [diff] [blame] | 1962 | dest.eDest = SRT_Mem; |
drh | 4c58312 | 2008-01-04 22:01:03 +0000 | [diff] [blame] | 1963 | sqlite3VdbeAddOp2(v, OP_Null, 0, dest.iParm); |
drh | d4e70eb | 2008-01-02 00:34:36 +0000 | [diff] [blame] | 1964 | VdbeComment((v, "Init subquery result")); |
drh | 51522cd | 2005-01-20 13:36:19 +0000 | [diff] [blame] | 1965 | }else{ |
danielk1977 | 6c8c8ce | 2008-01-02 16:27:09 +0000 | [diff] [blame] | 1966 | dest.eDest = SRT_Exists; |
drh | 4c58312 | 2008-01-04 22:01:03 +0000 | [diff] [blame] | 1967 | sqlite3VdbeAddOp2(v, OP_Integer, 0, dest.iParm); |
drh | d4e70eb | 2008-01-02 00:34:36 +0000 | [diff] [blame] | 1968 | VdbeComment((v, "Init EXISTS result")); |
drh | 51522cd | 2005-01-20 13:36:19 +0000 | [diff] [blame] | 1969 | } |
drh | ec7429a | 2005-10-06 16:53:14 +0000 | [diff] [blame] | 1970 | sqlite3ExprDelete(pSel->pLimit); |
danielk1977 | a1644fd | 2007-08-29 12:31:25 +0000 | [diff] [blame] | 1971 | pSel->pLimit = sqlite3PExpr(pParse, TK_INTEGER, 0, 0, &one); |
danielk1977 | 6c8c8ce | 2008-01-02 16:27:09 +0000 | [diff] [blame] | 1972 | if( sqlite3Select(pParse, pSel, &dest, 0, 0, 0, 0) ){ |
drh | 94ccde5 | 2007-04-13 16:06:32 +0000 | [diff] [blame] | 1973 | return; |
| 1974 | } |
danielk1977 | 6c8c8ce | 2008-01-02 16:27:09 +0000 | [diff] [blame] | 1975 | pExpr->iColumn = dest.iParm; |
danielk1977 | b3bce66 | 2005-01-29 08:32:43 +0000 | [diff] [blame] | 1976 | break; |
drh | cce7d17 | 2000-05-31 15:34:51 +0000 | [diff] [blame] | 1977 | } |
| 1978 | } |
danielk1977 | b3bce66 | 2005-01-29 08:32:43 +0000 | [diff] [blame] | 1979 | |
drh | 57dbd7b | 2005-07-08 18:25:26 +0000 | [diff] [blame] | 1980 | if( testAddr ){ |
drh | 892d317 | 2008-01-10 03:46:36 +0000 | [diff] [blame] | 1981 | sqlite3VdbeJumpHere(v, testAddr-1); |
danielk1977 | b3bce66 | 2005-01-29 08:32:43 +0000 | [diff] [blame] | 1982 | } |
danielk1977 | fc97606 | 2007-05-10 10:46:56 +0000 | [diff] [blame] | 1983 | |
danielk1977 | b3bce66 | 2005-01-29 08:32:43 +0000 | [diff] [blame] | 1984 | return; |
drh | cce7d17 | 2000-05-31 15:34:51 +0000 | [diff] [blame] | 1985 | } |
drh | 51522cd | 2005-01-20 13:36:19 +0000 | [diff] [blame] | 1986 | #endif /* SQLITE_OMIT_SUBQUERY */ |
drh | cce7d17 | 2000-05-31 15:34:51 +0000 | [diff] [blame] | 1987 | |
drh | cce7d17 | 2000-05-31 15:34:51 +0000 | [diff] [blame] | 1988 | /* |
drh | 598f134 | 2007-10-23 15:39:45 +0000 | [diff] [blame] | 1989 | ** Duplicate an 8-byte value |
| 1990 | */ |
| 1991 | static char *dup8bytes(Vdbe *v, const char *in){ |
| 1992 | char *out = sqlite3DbMallocRaw(sqlite3VdbeDb(v), 8); |
| 1993 | if( out ){ |
| 1994 | memcpy(out, in, 8); |
| 1995 | } |
| 1996 | return out; |
| 1997 | } |
| 1998 | |
| 1999 | /* |
| 2000 | ** Generate an instruction that will put the floating point |
drh | 9cbf342 | 2008-01-17 16:22:13 +0000 | [diff] [blame] | 2001 | ** value described by z[0..n-1] into register iMem. |
drh | 0cf19ed | 2007-10-23 18:55:48 +0000 | [diff] [blame] | 2002 | ** |
| 2003 | ** The z[] string will probably not be zero-terminated. But the |
| 2004 | ** z[n] character is guaranteed to be something that does not look |
| 2005 | ** like the continuation of the number. |
drh | 598f134 | 2007-10-23 15:39:45 +0000 | [diff] [blame] | 2006 | */ |
drh | 9de221d | 2008-01-05 06:51:30 +0000 | [diff] [blame] | 2007 | static void codeReal(Vdbe *v, const char *z, int n, int negateFlag, int iMem){ |
drh | 598f134 | 2007-10-23 15:39:45 +0000 | [diff] [blame] | 2008 | assert( z || v==0 || sqlite3VdbeDb(v)->mallocFailed ); |
| 2009 | if( z ){ |
| 2010 | double value; |
| 2011 | char *zV; |
drh | 0cf19ed | 2007-10-23 18:55:48 +0000 | [diff] [blame] | 2012 | assert( !isdigit(z[n]) ); |
drh | 598f134 | 2007-10-23 15:39:45 +0000 | [diff] [blame] | 2013 | sqlite3AtoF(z, &value); |
drh | 2eaf93d | 2008-04-29 00:15:20 +0000 | [diff] [blame] | 2014 | if( sqlite3IsNaN(value) ){ |
| 2015 | sqlite3VdbeAddOp2(v, OP_Null, 0, iMem); |
| 2016 | }else{ |
| 2017 | if( negateFlag ) value = -value; |
| 2018 | zV = dup8bytes(v, (char*)&value); |
| 2019 | sqlite3VdbeAddOp4(v, OP_Real, 0, iMem, 0, zV, P4_REAL); |
| 2020 | } |
drh | 598f134 | 2007-10-23 15:39:45 +0000 | [diff] [blame] | 2021 | } |
| 2022 | } |
| 2023 | |
| 2024 | |
| 2025 | /* |
drh | fec19aa | 2004-05-19 20:41:03 +0000 | [diff] [blame] | 2026 | ** Generate an instruction that will put the integer describe by |
drh | 9cbf342 | 2008-01-17 16:22:13 +0000 | [diff] [blame] | 2027 | ** text z[0..n-1] into register iMem. |
drh | 0cf19ed | 2007-10-23 18:55:48 +0000 | [diff] [blame] | 2028 | ** |
| 2029 | ** The z[] string will probably not be zero-terminated. But the |
| 2030 | ** z[n] character is guaranteed to be something that does not look |
| 2031 | ** like the continuation of the number. |
drh | fec19aa | 2004-05-19 20:41:03 +0000 | [diff] [blame] | 2032 | */ |
drh | 92b01d5 | 2008-06-24 00:32:35 +0000 | [diff] [blame] | 2033 | static void codeInteger(Vdbe *v, Expr *pExpr, int negFlag, int iMem){ |
| 2034 | const char *z; |
| 2035 | if( pExpr->flags & EP_IntValue ){ |
| 2036 | int i = pExpr->iTable; |
| 2037 | if( negFlag ) i = -i; |
| 2038 | sqlite3VdbeAddOp2(v, OP_Integer, i, iMem); |
| 2039 | }else if( (z = (char*)pExpr->token.z)!=0 ){ |
danielk1977 | c9cf901 | 2007-05-30 10:36:47 +0000 | [diff] [blame] | 2040 | int i; |
drh | 92b01d5 | 2008-06-24 00:32:35 +0000 | [diff] [blame] | 2041 | int n = pExpr->token.n; |
drh | 0cf19ed | 2007-10-23 18:55:48 +0000 | [diff] [blame] | 2042 | assert( !isdigit(z[n]) ); |
danielk1977 | c9cf901 | 2007-05-30 10:36:47 +0000 | [diff] [blame] | 2043 | if( sqlite3GetInt32(z, &i) ){ |
drh | 9de221d | 2008-01-05 06:51:30 +0000 | [diff] [blame] | 2044 | if( negFlag ) i = -i; |
| 2045 | sqlite3VdbeAddOp2(v, OP_Integer, i, iMem); |
| 2046 | }else if( sqlite3FitsIn64Bits(z, negFlag) ){ |
drh | 598f134 | 2007-10-23 15:39:45 +0000 | [diff] [blame] | 2047 | i64 value; |
| 2048 | char *zV; |
| 2049 | sqlite3Atoi64(z, &value); |
drh | 9de221d | 2008-01-05 06:51:30 +0000 | [diff] [blame] | 2050 | if( negFlag ) value = -value; |
drh | 598f134 | 2007-10-23 15:39:45 +0000 | [diff] [blame] | 2051 | zV = dup8bytes(v, (char*)&value); |
drh | 9de221d | 2008-01-05 06:51:30 +0000 | [diff] [blame] | 2052 | sqlite3VdbeAddOp4(v, OP_Int64, 0, iMem, 0, zV, P4_INT64); |
danielk1977 | c9cf901 | 2007-05-30 10:36:47 +0000 | [diff] [blame] | 2053 | }else{ |
drh | 9de221d | 2008-01-05 06:51:30 +0000 | [diff] [blame] | 2054 | codeReal(v, z, n, negFlag, iMem); |
danielk1977 | c9cf901 | 2007-05-30 10:36:47 +0000 | [diff] [blame] | 2055 | } |
drh | fec19aa | 2004-05-19 20:41:03 +0000 | [diff] [blame] | 2056 | } |
| 2057 | } |
| 2058 | |
drh | 945498f | 2007-02-24 11:52:52 +0000 | [diff] [blame] | 2059 | |
| 2060 | /* |
| 2061 | ** Generate code that will extract the iColumn-th column from |
drh | e55cbd7 | 2008-03-31 23:48:03 +0000 | [diff] [blame] | 2062 | ** table pTab and store the column value in a register. An effort |
| 2063 | ** is made to store the column value in register iReg, but this is |
| 2064 | ** not guaranteed. The location of the column value is returned. |
| 2065 | ** |
| 2066 | ** There must be an open cursor to pTab in iTable when this routine |
| 2067 | ** is called. If iColumn<0 then code is generated that extracts the rowid. |
drh | da250ea | 2008-04-01 05:07:14 +0000 | [diff] [blame] | 2068 | ** |
| 2069 | ** This routine might attempt to reuse the value of the column that |
| 2070 | ** has already been loaded into a register. The value will always |
| 2071 | ** be used if it has not undergone any affinity changes. But if |
| 2072 | ** an affinity change has occurred, then the cached value will only be |
| 2073 | ** used if allowAffChng is true. |
drh | 945498f | 2007-02-24 11:52:52 +0000 | [diff] [blame] | 2074 | */ |
drh | e55cbd7 | 2008-03-31 23:48:03 +0000 | [diff] [blame] | 2075 | int sqlite3ExprCodeGetColumn( |
| 2076 | Parse *pParse, /* Parsing and code generating context */ |
drh | 2133d82 | 2008-01-03 18:44:59 +0000 | [diff] [blame] | 2077 | Table *pTab, /* Description of the table we are reading from */ |
| 2078 | int iColumn, /* Index of the table column */ |
| 2079 | int iTable, /* The cursor pointing to the table */ |
drh | da250ea | 2008-04-01 05:07:14 +0000 | [diff] [blame] | 2080 | int iReg, /* Store results here */ |
| 2081 | int allowAffChng /* True if prior affinity changes are OK */ |
drh | 2133d82 | 2008-01-03 18:44:59 +0000 | [diff] [blame] | 2082 | ){ |
drh | e55cbd7 | 2008-03-31 23:48:03 +0000 | [diff] [blame] | 2083 | Vdbe *v = pParse->pVdbe; |
| 2084 | int i; |
drh | da250ea | 2008-04-01 05:07:14 +0000 | [diff] [blame] | 2085 | struct yColCache *p; |
drh | e55cbd7 | 2008-03-31 23:48:03 +0000 | [diff] [blame] | 2086 | |
drh | da250ea | 2008-04-01 05:07:14 +0000 | [diff] [blame] | 2087 | for(i=0, p=pParse->aColCache; i<pParse->nColCache; i++, p++){ |
| 2088 | if( p->iTable==iTable && p->iColumn==iColumn |
| 2089 | && (!p->affChange || allowAffChng) ){ |
drh | e55cbd7 | 2008-03-31 23:48:03 +0000 | [diff] [blame] | 2090 | #if 0 |
| 2091 | sqlite3VdbeAddOp0(v, OP_Noop); |
drh | da250ea | 2008-04-01 05:07:14 +0000 | [diff] [blame] | 2092 | VdbeComment((v, "OPT: tab%d.col%d -> r%d", iTable, iColumn, p->iReg)); |
drh | e55cbd7 | 2008-03-31 23:48:03 +0000 | [diff] [blame] | 2093 | #endif |
drh | da250ea | 2008-04-01 05:07:14 +0000 | [diff] [blame] | 2094 | return p->iReg; |
drh | e55cbd7 | 2008-03-31 23:48:03 +0000 | [diff] [blame] | 2095 | } |
| 2096 | } |
| 2097 | assert( v!=0 ); |
drh | 945498f | 2007-02-24 11:52:52 +0000 | [diff] [blame] | 2098 | if( iColumn<0 ){ |
| 2099 | int op = (pTab && IsVirtual(pTab)) ? OP_VRowid : OP_Rowid; |
drh | 2133d82 | 2008-01-03 18:44:59 +0000 | [diff] [blame] | 2100 | sqlite3VdbeAddOp2(v, op, iTable, iReg); |
drh | 945498f | 2007-02-24 11:52:52 +0000 | [diff] [blame] | 2101 | }else if( pTab==0 ){ |
drh | 2133d82 | 2008-01-03 18:44:59 +0000 | [diff] [blame] | 2102 | sqlite3VdbeAddOp3(v, OP_Column, iTable, iColumn, iReg); |
drh | 945498f | 2007-02-24 11:52:52 +0000 | [diff] [blame] | 2103 | }else{ |
| 2104 | int op = IsVirtual(pTab) ? OP_VColumn : OP_Column; |
drh | 2133d82 | 2008-01-03 18:44:59 +0000 | [diff] [blame] | 2105 | sqlite3VdbeAddOp3(v, op, iTable, iColumn, iReg); |
drh | 945498f | 2007-02-24 11:52:52 +0000 | [diff] [blame] | 2106 | sqlite3ColumnDefault(v, pTab, iColumn); |
| 2107 | #ifndef SQLITE_OMIT_FLOATING_POINT |
| 2108 | if( pTab->aCol[iColumn].affinity==SQLITE_AFF_REAL ){ |
drh | 2133d82 | 2008-01-03 18:44:59 +0000 | [diff] [blame] | 2109 | sqlite3VdbeAddOp1(v, OP_RealAffinity, iReg); |
drh | 945498f | 2007-02-24 11:52:52 +0000 | [diff] [blame] | 2110 | } |
| 2111 | #endif |
| 2112 | } |
drh | e55cbd7 | 2008-03-31 23:48:03 +0000 | [diff] [blame] | 2113 | if( pParse->disableColCache==0 ){ |
| 2114 | i = pParse->iColCache; |
drh | da250ea | 2008-04-01 05:07:14 +0000 | [diff] [blame] | 2115 | p = &pParse->aColCache[i]; |
| 2116 | p->iTable = iTable; |
| 2117 | p->iColumn = iColumn; |
| 2118 | p->iReg = iReg; |
drh | c5499be | 2008-04-01 15:06:33 +0000 | [diff] [blame] | 2119 | p->affChange = 0; |
drh | e55cbd7 | 2008-03-31 23:48:03 +0000 | [diff] [blame] | 2120 | i++; |
drh | 2f7794c | 2008-04-01 03:27:39 +0000 | [diff] [blame] | 2121 | if( i>=ArraySize(pParse->aColCache) ) i = 0; |
drh | e55cbd7 | 2008-03-31 23:48:03 +0000 | [diff] [blame] | 2122 | if( i>pParse->nColCache ) pParse->nColCache = i; |
drh | 2f7794c | 2008-04-01 03:27:39 +0000 | [diff] [blame] | 2123 | pParse->iColCache = i; |
drh | e55cbd7 | 2008-03-31 23:48:03 +0000 | [diff] [blame] | 2124 | } |
| 2125 | return iReg; |
| 2126 | } |
| 2127 | |
| 2128 | /* |
drh | e55cbd7 | 2008-03-31 23:48:03 +0000 | [diff] [blame] | 2129 | ** Clear all column cache entries associated with the vdbe |
| 2130 | ** cursor with cursor number iTable. |
| 2131 | */ |
| 2132 | void sqlite3ExprClearColumnCache(Parse *pParse, int iTable){ |
| 2133 | if( iTable<0 ){ |
| 2134 | pParse->nColCache = 0; |
| 2135 | pParse->iColCache = 0; |
| 2136 | }else{ |
| 2137 | int i; |
| 2138 | for(i=0; i<pParse->nColCache; i++){ |
| 2139 | if( pParse->aColCache[i].iTable==iTable ){ |
drh | c5499be | 2008-04-01 15:06:33 +0000 | [diff] [blame] | 2140 | testcase( i==pParse->nColCache-1 ); |
drh | e55cbd7 | 2008-03-31 23:48:03 +0000 | [diff] [blame] | 2141 | pParse->aColCache[i] = pParse->aColCache[--pParse->nColCache]; |
drh | da250ea | 2008-04-01 05:07:14 +0000 | [diff] [blame] | 2142 | pParse->iColCache = pParse->nColCache; |
drh | e55cbd7 | 2008-03-31 23:48:03 +0000 | [diff] [blame] | 2143 | } |
| 2144 | } |
drh | e55cbd7 | 2008-03-31 23:48:03 +0000 | [diff] [blame] | 2145 | } |
| 2146 | } |
| 2147 | |
| 2148 | /* |
drh | da250ea | 2008-04-01 05:07:14 +0000 | [diff] [blame] | 2149 | ** Record the fact that an affinity change has occurred on iCount |
| 2150 | ** registers starting with iStart. |
drh | e55cbd7 | 2008-03-31 23:48:03 +0000 | [diff] [blame] | 2151 | */ |
drh | da250ea | 2008-04-01 05:07:14 +0000 | [diff] [blame] | 2152 | void sqlite3ExprCacheAffinityChange(Parse *pParse, int iStart, int iCount){ |
| 2153 | int iEnd = iStart + iCount - 1; |
drh | e55cbd7 | 2008-03-31 23:48:03 +0000 | [diff] [blame] | 2154 | int i; |
| 2155 | for(i=0; i<pParse->nColCache; i++){ |
| 2156 | int r = pParse->aColCache[i].iReg; |
drh | da250ea | 2008-04-01 05:07:14 +0000 | [diff] [blame] | 2157 | if( r>=iStart && r<=iEnd ){ |
| 2158 | pParse->aColCache[i].affChange = 1; |
drh | e55cbd7 | 2008-03-31 23:48:03 +0000 | [diff] [blame] | 2159 | } |
| 2160 | } |
drh | e55cbd7 | 2008-03-31 23:48:03 +0000 | [diff] [blame] | 2161 | } |
| 2162 | |
| 2163 | /* |
drh | b21e7c7 | 2008-06-22 12:37:57 +0000 | [diff] [blame] | 2164 | ** Generate code to move content from registers iFrom...iFrom+nReg-1 |
| 2165 | ** over to iTo..iTo+nReg-1. Keep the column cache up-to-date. |
drh | e55cbd7 | 2008-03-31 23:48:03 +0000 | [diff] [blame] | 2166 | */ |
drh | b21e7c7 | 2008-06-22 12:37:57 +0000 | [diff] [blame] | 2167 | void sqlite3ExprCodeMove(Parse *pParse, int iFrom, int iTo, int nReg){ |
drh | e55cbd7 | 2008-03-31 23:48:03 +0000 | [diff] [blame] | 2168 | int i; |
| 2169 | if( iFrom==iTo ) return; |
drh | b21e7c7 | 2008-06-22 12:37:57 +0000 | [diff] [blame] | 2170 | sqlite3VdbeAddOp3(pParse->pVdbe, OP_Move, iFrom, iTo, nReg); |
drh | e55cbd7 | 2008-03-31 23:48:03 +0000 | [diff] [blame] | 2171 | for(i=0; i<pParse->nColCache; i++){ |
drh | b21e7c7 | 2008-06-22 12:37:57 +0000 | [diff] [blame] | 2172 | int x = pParse->aColCache[i].iReg; |
| 2173 | if( x>=iFrom && x<iFrom+nReg ){ |
| 2174 | pParse->aColCache[i].iReg += iTo-iFrom; |
drh | e55cbd7 | 2008-03-31 23:48:03 +0000 | [diff] [blame] | 2175 | } |
| 2176 | } |
drh | 945498f | 2007-02-24 11:52:52 +0000 | [diff] [blame] | 2177 | } |
| 2178 | |
drh | fec19aa | 2004-05-19 20:41:03 +0000 | [diff] [blame] | 2179 | /* |
drh | 92b01d5 | 2008-06-24 00:32:35 +0000 | [diff] [blame] | 2180 | ** Generate code to copy content from registers iFrom...iFrom+nReg-1 |
| 2181 | ** over to iTo..iTo+nReg-1. |
| 2182 | */ |
| 2183 | void sqlite3ExprCodeCopy(Parse *pParse, int iFrom, int iTo, int nReg){ |
| 2184 | int i; |
| 2185 | if( iFrom==iTo ) return; |
| 2186 | for(i=0; i<nReg; i++){ |
| 2187 | sqlite3VdbeAddOp2(pParse->pVdbe, OP_Copy, iFrom+i, iTo+i); |
| 2188 | } |
| 2189 | } |
| 2190 | |
| 2191 | /* |
drh | 652fbf5 | 2008-04-01 01:42:41 +0000 | [diff] [blame] | 2192 | ** Return true if any register in the range iFrom..iTo (inclusive) |
| 2193 | ** is used as part of the column cache. |
| 2194 | */ |
| 2195 | static int usedAsColumnCache(Parse *pParse, int iFrom, int iTo){ |
| 2196 | int i; |
| 2197 | for(i=0; i<pParse->nColCache; i++){ |
| 2198 | int r = pParse->aColCache[i].iReg; |
| 2199 | if( r>=iFrom && r<=iTo ) return 1; |
| 2200 | } |
| 2201 | return 0; |
| 2202 | } |
| 2203 | |
| 2204 | /* |
| 2205 | ** Theres is a value in register iCurrent. We ultimately want |
| 2206 | ** the value to be in register iTarget. It might be that |
| 2207 | ** iCurrent and iTarget are the same register. |
| 2208 | ** |
| 2209 | ** We are going to modify the value, so we need to make sure it |
| 2210 | ** is not a cached register. If iCurrent is a cached register, |
| 2211 | ** then try to move the value over to iTarget. If iTarget is a |
| 2212 | ** cached register, then clear the corresponding cache line. |
| 2213 | ** |
| 2214 | ** Return the register that the value ends up in. |
| 2215 | */ |
| 2216 | int sqlite3ExprWritableRegister(Parse *pParse, int iCurrent, int iTarget){ |
drh | da250ea | 2008-04-01 05:07:14 +0000 | [diff] [blame] | 2217 | int i; |
drh | 652fbf5 | 2008-04-01 01:42:41 +0000 | [diff] [blame] | 2218 | assert( pParse->pVdbe!=0 ); |
| 2219 | if( !usedAsColumnCache(pParse, iCurrent, iCurrent) ){ |
| 2220 | return iCurrent; |
| 2221 | } |
drh | 2f7794c | 2008-04-01 03:27:39 +0000 | [diff] [blame] | 2222 | if( iCurrent!=iTarget ){ |
| 2223 | sqlite3VdbeAddOp2(pParse->pVdbe, OP_SCopy, iCurrent, iTarget); |
| 2224 | } |
drh | da250ea | 2008-04-01 05:07:14 +0000 | [diff] [blame] | 2225 | for(i=0; i<pParse->nColCache; i++){ |
| 2226 | if( pParse->aColCache[i].iReg==iTarget ){ |
| 2227 | pParse->aColCache[i] = pParse->aColCache[--pParse->nColCache]; |
| 2228 | pParse->iColCache = pParse->nColCache; |
| 2229 | } |
| 2230 | } |
drh | 652fbf5 | 2008-04-01 01:42:41 +0000 | [diff] [blame] | 2231 | return iTarget; |
| 2232 | } |
| 2233 | |
| 2234 | /* |
drh | 191b54c | 2008-04-15 12:14:21 +0000 | [diff] [blame] | 2235 | ** If the last instruction coded is an ephemeral copy of any of |
| 2236 | ** the registers in the nReg registers beginning with iReg, then |
| 2237 | ** convert the last instruction from OP_SCopy to OP_Copy. |
| 2238 | */ |
| 2239 | void sqlite3ExprHardCopy(Parse *pParse, int iReg, int nReg){ |
| 2240 | int addr; |
| 2241 | VdbeOp *pOp; |
| 2242 | Vdbe *v; |
| 2243 | |
| 2244 | v = pParse->pVdbe; |
| 2245 | addr = sqlite3VdbeCurrentAddr(v); |
| 2246 | pOp = sqlite3VdbeGetOp(v, addr-1); |
danielk1977 | d7eb2ed | 2008-04-24 12:36:35 +0000 | [diff] [blame] | 2247 | assert( pOp || pParse->db->mallocFailed ); |
| 2248 | if( pOp && pOp->opcode==OP_SCopy && pOp->p1>=iReg && pOp->p1<iReg+nReg ){ |
drh | 191b54c | 2008-04-15 12:14:21 +0000 | [diff] [blame] | 2249 | pOp->opcode = OP_Copy; |
| 2250 | } |
| 2251 | } |
| 2252 | |
| 2253 | /* |
drh | cce7d17 | 2000-05-31 15:34:51 +0000 | [diff] [blame] | 2254 | ** Generate code into the current Vdbe to evaluate the given |
drh | 2dcef11 | 2008-01-12 19:03:48 +0000 | [diff] [blame] | 2255 | ** expression. Attempt to store the results in register "target". |
| 2256 | ** Return the register where results are stored. |
drh | 389a1ad | 2008-01-03 23:44:53 +0000 | [diff] [blame] | 2257 | ** |
drh | 2dcef11 | 2008-01-12 19:03:48 +0000 | [diff] [blame] | 2258 | ** With this routine, there is no guaranteed that results will |
| 2259 | ** be stored in target. The result might be stored in some other |
| 2260 | ** register if it is convenient to do so. The calling function |
| 2261 | ** must check the return code and move the results to the desired |
| 2262 | ** register. |
drh | cce7d17 | 2000-05-31 15:34:51 +0000 | [diff] [blame] | 2263 | */ |
drh | 678ccce | 2008-03-31 18:19:54 +0000 | [diff] [blame] | 2264 | int sqlite3ExprCodeTarget(Parse *pParse, Expr *pExpr, int target){ |
drh | 2dcef11 | 2008-01-12 19:03:48 +0000 | [diff] [blame] | 2265 | Vdbe *v = pParse->pVdbe; /* The VM under construction */ |
| 2266 | int op; /* The opcode being coded */ |
| 2267 | int inReg = target; /* Results stored in register inReg */ |
| 2268 | int regFree1 = 0; /* If non-zero free this temporary register */ |
| 2269 | int regFree2 = 0; /* If non-zero free this temporary register */ |
drh | 678ccce | 2008-03-31 18:19:54 +0000 | [diff] [blame] | 2270 | int r1, r2, r3, r4; /* Various register numbers */ |
drh | ffe07b2 | 2005-11-03 00:41:17 +0000 | [diff] [blame] | 2271 | |
drh | 389a1ad | 2008-01-03 23:44:53 +0000 | [diff] [blame] | 2272 | assert( v!=0 || pParse->db->mallocFailed ); |
drh | 9cbf342 | 2008-01-17 16:22:13 +0000 | [diff] [blame] | 2273 | assert( target>0 && target<=pParse->nMem ); |
drh | 389a1ad | 2008-01-03 23:44:53 +0000 | [diff] [blame] | 2274 | if( v==0 ) return 0; |
drh | 389a1ad | 2008-01-03 23:44:53 +0000 | [diff] [blame] | 2275 | |
| 2276 | if( pExpr==0 ){ |
| 2277 | op = TK_NULL; |
| 2278 | }else{ |
| 2279 | op = pExpr->op; |
| 2280 | } |
drh | f2bc013 | 2004-10-04 13:19:23 +0000 | [diff] [blame] | 2281 | switch( op ){ |
drh | 1344989 | 2005-09-07 21:22:45 +0000 | [diff] [blame] | 2282 | case TK_AGG_COLUMN: { |
| 2283 | AggInfo *pAggInfo = pExpr->pAggInfo; |
| 2284 | struct AggInfo_col *pCol = &pAggInfo->aCol[pExpr->iAgg]; |
| 2285 | if( !pAggInfo->directMode ){ |
drh | 9de221d | 2008-01-05 06:51:30 +0000 | [diff] [blame] | 2286 | assert( pCol->iMem>0 ); |
| 2287 | inReg = pCol->iMem; |
drh | 1344989 | 2005-09-07 21:22:45 +0000 | [diff] [blame] | 2288 | break; |
| 2289 | }else if( pAggInfo->useSortingIdx ){ |
drh | 389a1ad | 2008-01-03 23:44:53 +0000 | [diff] [blame] | 2290 | sqlite3VdbeAddOp3(v, OP_Column, pAggInfo->sortingIdx, |
| 2291 | pCol->iSorterColumn, target); |
drh | 1344989 | 2005-09-07 21:22:45 +0000 | [diff] [blame] | 2292 | break; |
| 2293 | } |
| 2294 | /* Otherwise, fall thru into the TK_COLUMN case */ |
| 2295 | } |
drh | 967e8b7 | 2000-06-21 13:59:10 +0000 | [diff] [blame] | 2296 | case TK_COLUMN: { |
drh | ffe07b2 | 2005-11-03 00:41:17 +0000 | [diff] [blame] | 2297 | if( pExpr->iTable<0 ){ |
| 2298 | /* This only happens when coding check constraints */ |
drh | aa9b896 | 2008-01-08 02:57:55 +0000 | [diff] [blame] | 2299 | assert( pParse->ckBase>0 ); |
| 2300 | inReg = pExpr->iColumn + pParse->ckBase; |
drh | c4a3c77 | 2001-04-04 11:48:57 +0000 | [diff] [blame] | 2301 | }else{ |
drh | c5499be | 2008-04-01 15:06:33 +0000 | [diff] [blame] | 2302 | testcase( (pExpr->flags & EP_AnyAff)!=0 ); |
drh | e55cbd7 | 2008-03-31 23:48:03 +0000 | [diff] [blame] | 2303 | inReg = sqlite3ExprCodeGetColumn(pParse, pExpr->pTab, |
drh | da250ea | 2008-04-01 05:07:14 +0000 | [diff] [blame] | 2304 | pExpr->iColumn, pExpr->iTable, target, |
| 2305 | pExpr->flags & EP_AnyAff); |
drh | 2282792 | 2000-06-06 17:27:05 +0000 | [diff] [blame] | 2306 | } |
drh | cce7d17 | 2000-05-31 15:34:51 +0000 | [diff] [blame] | 2307 | break; |
| 2308 | } |
| 2309 | case TK_INTEGER: { |
drh | 92b01d5 | 2008-06-24 00:32:35 +0000 | [diff] [blame] | 2310 | codeInteger(v, pExpr, 0, target); |
drh | fec19aa | 2004-05-19 20:41:03 +0000 | [diff] [blame] | 2311 | break; |
| 2312 | } |
drh | 598f134 | 2007-10-23 15:39:45 +0000 | [diff] [blame] | 2313 | case TK_FLOAT: { |
drh | 9de221d | 2008-01-05 06:51:30 +0000 | [diff] [blame] | 2314 | codeReal(v, (char*)pExpr->token.z, pExpr->token.n, 0, target); |
drh | 598f134 | 2007-10-23 15:39:45 +0000 | [diff] [blame] | 2315 | break; |
| 2316 | } |
drh | fec19aa | 2004-05-19 20:41:03 +0000 | [diff] [blame] | 2317 | case TK_STRING: { |
danielk1977 | 1e53695 | 2007-08-16 10:09:01 +0000 | [diff] [blame] | 2318 | sqlite3DequoteExpr(pParse->db, pExpr); |
drh | 9de221d | 2008-01-05 06:51:30 +0000 | [diff] [blame] | 2319 | sqlite3VdbeAddOp4(v,OP_String8, 0, target, 0, |
drh | 66a5167 | 2008-01-03 00:01:23 +0000 | [diff] [blame] | 2320 | (char*)pExpr->token.z, pExpr->token.n); |
drh | cce7d17 | 2000-05-31 15:34:51 +0000 | [diff] [blame] | 2321 | break; |
| 2322 | } |
drh | f0863fe | 2005-06-12 21:35:51 +0000 | [diff] [blame] | 2323 | case TK_NULL: { |
drh | 9de221d | 2008-01-05 06:51:30 +0000 | [diff] [blame] | 2324 | sqlite3VdbeAddOp2(v, OP_Null, 0, target); |
drh | f0863fe | 2005-06-12 21:35:51 +0000 | [diff] [blame] | 2325 | break; |
| 2326 | } |
danielk1977 | 5338a5f | 2005-01-20 13:03:10 +0000 | [diff] [blame] | 2327 | #ifndef SQLITE_OMIT_BLOB_LITERAL |
danielk1977 | c572ef7 | 2004-05-27 09:28:41 +0000 | [diff] [blame] | 2328 | case TK_BLOB: { |
drh | 6c8c6ce | 2005-08-23 11:17:58 +0000 | [diff] [blame] | 2329 | int n; |
| 2330 | const char *z; |
drh | ca48c90 | 2008-01-18 14:08:24 +0000 | [diff] [blame] | 2331 | char *zBlob; |
| 2332 | assert( pExpr->token.n>=3 ); |
| 2333 | assert( pExpr->token.z[0]=='x' || pExpr->token.z[0]=='X' ); |
| 2334 | assert( pExpr->token.z[1]=='\'' ); |
| 2335 | assert( pExpr->token.z[pExpr->token.n-1]=='\'' ); |
drh | 6c8c6ce | 2005-08-23 11:17:58 +0000 | [diff] [blame] | 2336 | n = pExpr->token.n - 3; |
drh | 2646da7 | 2005-12-09 20:02:05 +0000 | [diff] [blame] | 2337 | z = (char*)pExpr->token.z + 2; |
drh | ca48c90 | 2008-01-18 14:08:24 +0000 | [diff] [blame] | 2338 | zBlob = sqlite3HexToBlob(sqlite3VdbeDb(v), z, n); |
| 2339 | sqlite3VdbeAddOp4(v, OP_Blob, n/2, target, 0, zBlob, P4_DYNAMIC); |
danielk1977 | c572ef7 | 2004-05-27 09:28:41 +0000 | [diff] [blame] | 2340 | break; |
| 2341 | } |
danielk1977 | 5338a5f | 2005-01-20 13:03:10 +0000 | [diff] [blame] | 2342 | #endif |
drh | 5045789 | 2003-09-06 01:10:47 +0000 | [diff] [blame] | 2343 | case TK_VARIABLE: { |
drh | 9de221d | 2008-01-05 06:51:30 +0000 | [diff] [blame] | 2344 | sqlite3VdbeAddOp2(v, OP_Variable, pExpr->iTable, target); |
drh | 895d747 | 2004-08-20 16:02:39 +0000 | [diff] [blame] | 2345 | if( pExpr->token.n>1 ){ |
drh | 66a5167 | 2008-01-03 00:01:23 +0000 | [diff] [blame] | 2346 | sqlite3VdbeChangeP4(v, -1, (char*)pExpr->token.z, pExpr->token.n); |
drh | 895d747 | 2004-08-20 16:02:39 +0000 | [diff] [blame] | 2347 | } |
drh | 5045789 | 2003-09-06 01:10:47 +0000 | [diff] [blame] | 2348 | break; |
| 2349 | } |
drh | 4e0cff6 | 2004-11-05 05:10:28 +0000 | [diff] [blame] | 2350 | case TK_REGISTER: { |
drh | 9de221d | 2008-01-05 06:51:30 +0000 | [diff] [blame] | 2351 | inReg = pExpr->iTable; |
drh | 4e0cff6 | 2004-11-05 05:10:28 +0000 | [diff] [blame] | 2352 | break; |
| 2353 | } |
drh | 487e262 | 2005-06-25 18:42:14 +0000 | [diff] [blame] | 2354 | #ifndef SQLITE_OMIT_CAST |
| 2355 | case TK_CAST: { |
| 2356 | /* Expressions of the form: CAST(pLeft AS token) */ |
danielk1977 | f011300 | 2006-01-24 12:09:17 +0000 | [diff] [blame] | 2357 | int aff, to_op; |
drh | 2dcef11 | 2008-01-12 19:03:48 +0000 | [diff] [blame] | 2358 | inReg = sqlite3ExprCodeTarget(pParse, pExpr->pLeft, target); |
drh | 8a51256 | 2005-11-14 22:29:05 +0000 | [diff] [blame] | 2359 | aff = sqlite3AffinityType(&pExpr->token); |
danielk1977 | f011300 | 2006-01-24 12:09:17 +0000 | [diff] [blame] | 2360 | to_op = aff - SQLITE_AFF_TEXT + OP_ToText; |
| 2361 | assert( to_op==OP_ToText || aff!=SQLITE_AFF_TEXT ); |
| 2362 | assert( to_op==OP_ToBlob || aff!=SQLITE_AFF_NONE ); |
| 2363 | assert( to_op==OP_ToNumeric || aff!=SQLITE_AFF_NUMERIC ); |
| 2364 | assert( to_op==OP_ToInt || aff!=SQLITE_AFF_INTEGER ); |
| 2365 | assert( to_op==OP_ToReal || aff!=SQLITE_AFF_REAL ); |
drh | c5499be | 2008-04-01 15:06:33 +0000 | [diff] [blame] | 2366 | testcase( to_op==OP_ToText ); |
| 2367 | testcase( to_op==OP_ToBlob ); |
| 2368 | testcase( to_op==OP_ToNumeric ); |
| 2369 | testcase( to_op==OP_ToInt ); |
| 2370 | testcase( to_op==OP_ToReal ); |
drh | 2dcef11 | 2008-01-12 19:03:48 +0000 | [diff] [blame] | 2371 | sqlite3VdbeAddOp1(v, to_op, inReg); |
drh | c5499be | 2008-04-01 15:06:33 +0000 | [diff] [blame] | 2372 | testcase( usedAsColumnCache(pParse, inReg, inReg) ); |
drh | b3843a8 | 2008-04-01 12:24:11 +0000 | [diff] [blame] | 2373 | sqlite3ExprCacheAffinityChange(pParse, inReg, 1); |
drh | 487e262 | 2005-06-25 18:42:14 +0000 | [diff] [blame] | 2374 | break; |
| 2375 | } |
| 2376 | #endif /* SQLITE_OMIT_CAST */ |
drh | c9b84a1 | 2002-06-20 11:36:48 +0000 | [diff] [blame] | 2377 | case TK_LT: |
| 2378 | case TK_LE: |
| 2379 | case TK_GT: |
| 2380 | case TK_GE: |
| 2381 | case TK_NE: |
| 2382 | case TK_EQ: { |
drh | f2bc013 | 2004-10-04 13:19:23 +0000 | [diff] [blame] | 2383 | assert( TK_LT==OP_Lt ); |
| 2384 | assert( TK_LE==OP_Le ); |
| 2385 | assert( TK_GT==OP_Gt ); |
| 2386 | assert( TK_GE==OP_Ge ); |
| 2387 | assert( TK_EQ==OP_Eq ); |
| 2388 | assert( TK_NE==OP_Ne ); |
drh | c5499be | 2008-04-01 15:06:33 +0000 | [diff] [blame] | 2389 | testcase( op==TK_LT ); |
| 2390 | testcase( op==TK_LE ); |
| 2391 | testcase( op==TK_GT ); |
| 2392 | testcase( op==TK_GE ); |
| 2393 | testcase( op==TK_EQ ); |
| 2394 | testcase( op==TK_NE ); |
drh | da250ea | 2008-04-01 05:07:14 +0000 | [diff] [blame] | 2395 | codeCompareOperands(pParse, pExpr->pLeft, &r1, ®Free1, |
| 2396 | pExpr->pRight, &r2, ®Free2); |
drh | 3557335 | 2008-01-08 23:54:25 +0000 | [diff] [blame] | 2397 | codeCompare(pParse, pExpr->pLeft, pExpr->pRight, op, |
| 2398 | r1, r2, inReg, SQLITE_STOREP2); |
drh | c5499be | 2008-04-01 15:06:33 +0000 | [diff] [blame] | 2399 | testcase( regFree1==0 ); |
| 2400 | testcase( regFree2==0 ); |
danielk1977 | a37cdde | 2004-05-16 11:15:36 +0000 | [diff] [blame] | 2401 | break; |
drh | c9b84a1 | 2002-06-20 11:36:48 +0000 | [diff] [blame] | 2402 | } |
drh | cce7d17 | 2000-05-31 15:34:51 +0000 | [diff] [blame] | 2403 | case TK_AND: |
| 2404 | case TK_OR: |
| 2405 | case TK_PLUS: |
| 2406 | case TK_STAR: |
| 2407 | case TK_MINUS: |
drh | bf4133c | 2001-10-13 02:59:08 +0000 | [diff] [blame] | 2408 | case TK_REM: |
| 2409 | case TK_BITAND: |
| 2410 | case TK_BITOR: |
drh | 17c4029 | 2004-07-21 02:53:29 +0000 | [diff] [blame] | 2411 | case TK_SLASH: |
drh | bf4133c | 2001-10-13 02:59:08 +0000 | [diff] [blame] | 2412 | case TK_LSHIFT: |
drh | 855eb1c | 2004-08-31 13:45:11 +0000 | [diff] [blame] | 2413 | case TK_RSHIFT: |
drh | 0040077 | 2000-06-16 20:51:26 +0000 | [diff] [blame] | 2414 | case TK_CONCAT: { |
drh | f2bc013 | 2004-10-04 13:19:23 +0000 | [diff] [blame] | 2415 | assert( TK_AND==OP_And ); |
| 2416 | assert( TK_OR==OP_Or ); |
| 2417 | assert( TK_PLUS==OP_Add ); |
| 2418 | assert( TK_MINUS==OP_Subtract ); |
| 2419 | assert( TK_REM==OP_Remainder ); |
| 2420 | assert( TK_BITAND==OP_BitAnd ); |
| 2421 | assert( TK_BITOR==OP_BitOr ); |
| 2422 | assert( TK_SLASH==OP_Divide ); |
| 2423 | assert( TK_LSHIFT==OP_ShiftLeft ); |
| 2424 | assert( TK_RSHIFT==OP_ShiftRight ); |
| 2425 | assert( TK_CONCAT==OP_Concat ); |
drh | c5499be | 2008-04-01 15:06:33 +0000 | [diff] [blame] | 2426 | testcase( op==TK_AND ); |
| 2427 | testcase( op==TK_OR ); |
| 2428 | testcase( op==TK_PLUS ); |
| 2429 | testcase( op==TK_MINUS ); |
| 2430 | testcase( op==TK_REM ); |
| 2431 | testcase( op==TK_BITAND ); |
| 2432 | testcase( op==TK_BITOR ); |
| 2433 | testcase( op==TK_SLASH ); |
| 2434 | testcase( op==TK_LSHIFT ); |
| 2435 | testcase( op==TK_RSHIFT ); |
| 2436 | testcase( op==TK_CONCAT ); |
drh | 2dcef11 | 2008-01-12 19:03:48 +0000 | [diff] [blame] | 2437 | r1 = sqlite3ExprCodeTemp(pParse, pExpr->pLeft, ®Free1); |
| 2438 | r2 = sqlite3ExprCodeTemp(pParse, pExpr->pRight, ®Free2); |
drh | 5b6afba | 2008-01-05 16:29:28 +0000 | [diff] [blame] | 2439 | sqlite3VdbeAddOp3(v, op, r2, r1, target); |
drh | c5499be | 2008-04-01 15:06:33 +0000 | [diff] [blame] | 2440 | testcase( regFree1==0 ); |
| 2441 | testcase( regFree2==0 ); |
drh | 0040077 | 2000-06-16 20:51:26 +0000 | [diff] [blame] | 2442 | break; |
| 2443 | } |
drh | cce7d17 | 2000-05-31 15:34:51 +0000 | [diff] [blame] | 2444 | case TK_UMINUS: { |
drh | fec19aa | 2004-05-19 20:41:03 +0000 | [diff] [blame] | 2445 | Expr *pLeft = pExpr->pLeft; |
| 2446 | assert( pLeft ); |
| 2447 | if( pLeft->op==TK_FLOAT || pLeft->op==TK_INTEGER ){ |
drh | fec19aa | 2004-05-19 20:41:03 +0000 | [diff] [blame] | 2448 | if( pLeft->op==TK_FLOAT ){ |
drh | 92b01d5 | 2008-06-24 00:32:35 +0000 | [diff] [blame] | 2449 | codeReal(v, (char*)pLeft->token.z, pLeft->token.n, 1, target); |
drh | e684090 | 2002-03-06 03:08:25 +0000 | [diff] [blame] | 2450 | }else{ |
drh | 92b01d5 | 2008-06-24 00:32:35 +0000 | [diff] [blame] | 2451 | codeInteger(v, pLeft, 1, target); |
drh | e684090 | 2002-03-06 03:08:25 +0000 | [diff] [blame] | 2452 | } |
drh | 3c84ddf | 2008-01-09 02:15:38 +0000 | [diff] [blame] | 2453 | }else{ |
drh | 2dcef11 | 2008-01-12 19:03:48 +0000 | [diff] [blame] | 2454 | regFree1 = r1 = sqlite3GetTempReg(pParse); |
drh | 3c84ddf | 2008-01-09 02:15:38 +0000 | [diff] [blame] | 2455 | sqlite3VdbeAddOp2(v, OP_Integer, 0, r1); |
drh | e55cbd7 | 2008-03-31 23:48:03 +0000 | [diff] [blame] | 2456 | r2 = sqlite3ExprCodeTemp(pParse, pExpr->pLeft, ®Free2); |
drh | 2dcef11 | 2008-01-12 19:03:48 +0000 | [diff] [blame] | 2457 | sqlite3VdbeAddOp3(v, OP_Subtract, r2, r1, target); |
drh | c5499be | 2008-04-01 15:06:33 +0000 | [diff] [blame] | 2458 | testcase( regFree2==0 ); |
drh | 6e142f5 | 2000-06-08 13:36:40 +0000 | [diff] [blame] | 2459 | } |
drh | 3c84ddf | 2008-01-09 02:15:38 +0000 | [diff] [blame] | 2460 | inReg = target; |
| 2461 | break; |
drh | 6e142f5 | 2000-06-08 13:36:40 +0000 | [diff] [blame] | 2462 | } |
drh | bf4133c | 2001-10-13 02:59:08 +0000 | [diff] [blame] | 2463 | case TK_BITNOT: |
drh | 6e142f5 | 2000-06-08 13:36:40 +0000 | [diff] [blame] | 2464 | case TK_NOT: { |
drh | f2bc013 | 2004-10-04 13:19:23 +0000 | [diff] [blame] | 2465 | assert( TK_BITNOT==OP_BitNot ); |
| 2466 | assert( TK_NOT==OP_Not ); |
drh | c5499be | 2008-04-01 15:06:33 +0000 | [diff] [blame] | 2467 | testcase( op==TK_BITNOT ); |
| 2468 | testcase( op==TK_NOT ); |
drh | 2dcef11 | 2008-01-12 19:03:48 +0000 | [diff] [blame] | 2469 | inReg = sqlite3ExprCodeTarget(pParse, pExpr->pLeft, target); |
drh | c5499be | 2008-04-01 15:06:33 +0000 | [diff] [blame] | 2470 | testcase( inReg==target ); |
| 2471 | testcase( usedAsColumnCache(pParse, inReg, inReg) ); |
drh | 652fbf5 | 2008-04-01 01:42:41 +0000 | [diff] [blame] | 2472 | inReg = sqlite3ExprWritableRegister(pParse, inReg, target); |
drh | 2dcef11 | 2008-01-12 19:03:48 +0000 | [diff] [blame] | 2473 | sqlite3VdbeAddOp1(v, op, inReg); |
drh | cce7d17 | 2000-05-31 15:34:51 +0000 | [diff] [blame] | 2474 | break; |
| 2475 | } |
| 2476 | case TK_ISNULL: |
| 2477 | case TK_NOTNULL: { |
drh | 6a288a3 | 2008-01-07 19:20:24 +0000 | [diff] [blame] | 2478 | int addr; |
drh | f2bc013 | 2004-10-04 13:19:23 +0000 | [diff] [blame] | 2479 | assert( TK_ISNULL==OP_IsNull ); |
| 2480 | assert( TK_NOTNULL==OP_NotNull ); |
drh | c5499be | 2008-04-01 15:06:33 +0000 | [diff] [blame] | 2481 | testcase( op==TK_ISNULL ); |
| 2482 | testcase( op==TK_NOTNULL ); |
drh | 9de221d | 2008-01-05 06:51:30 +0000 | [diff] [blame] | 2483 | sqlite3VdbeAddOp2(v, OP_Integer, 1, target); |
drh | 2dcef11 | 2008-01-12 19:03:48 +0000 | [diff] [blame] | 2484 | r1 = sqlite3ExprCodeTemp(pParse, pExpr->pLeft, ®Free1); |
drh | c5499be | 2008-04-01 15:06:33 +0000 | [diff] [blame] | 2485 | testcase( regFree1==0 ); |
drh | 2dcef11 | 2008-01-12 19:03:48 +0000 | [diff] [blame] | 2486 | addr = sqlite3VdbeAddOp1(v, op, r1); |
drh | 9de221d | 2008-01-05 06:51:30 +0000 | [diff] [blame] | 2487 | sqlite3VdbeAddOp2(v, OP_AddImm, target, -1); |
drh | 6a288a3 | 2008-01-07 19:20:24 +0000 | [diff] [blame] | 2488 | sqlite3VdbeJumpHere(v, addr); |
drh | f2bc013 | 2004-10-04 13:19:23 +0000 | [diff] [blame] | 2489 | break; |
drh | cce7d17 | 2000-05-31 15:34:51 +0000 | [diff] [blame] | 2490 | } |
drh | 2282792 | 2000-06-06 17:27:05 +0000 | [diff] [blame] | 2491 | case TK_AGG_FUNCTION: { |
drh | 1344989 | 2005-09-07 21:22:45 +0000 | [diff] [blame] | 2492 | AggInfo *pInfo = pExpr->pAggInfo; |
drh | 7e56e71 | 2005-11-16 12:53:15 +0000 | [diff] [blame] | 2493 | if( pInfo==0 ){ |
| 2494 | sqlite3ErrorMsg(pParse, "misuse of aggregate: %T", |
| 2495 | &pExpr->span); |
| 2496 | }else{ |
drh | 9de221d | 2008-01-05 06:51:30 +0000 | [diff] [blame] | 2497 | inReg = pInfo->aFunc[pExpr->iAgg].iMem; |
drh | 7e56e71 | 2005-11-16 12:53:15 +0000 | [diff] [blame] | 2498 | } |
drh | 2282792 | 2000-06-06 17:27:05 +0000 | [diff] [blame] | 2499 | break; |
| 2500 | } |
drh | b71090f | 2005-05-23 17:26:51 +0000 | [diff] [blame] | 2501 | case TK_CONST_FUNC: |
drh | cce7d17 | 2000-05-31 15:34:51 +0000 | [diff] [blame] | 2502 | case TK_FUNCTION: { |
drh | cce7d17 | 2000-05-31 15:34:51 +0000 | [diff] [blame] | 2503 | ExprList *pList = pExpr->pList; |
drh | 89425d5 | 2002-02-28 03:04:48 +0000 | [diff] [blame] | 2504 | int nExpr = pList ? pList->nExpr : 0; |
drh | 0bce835 | 2002-02-28 00:41:10 +0000 | [diff] [blame] | 2505 | FuncDef *pDef; |
drh | 4b59ab5 | 2002-08-24 18:24:51 +0000 | [diff] [blame] | 2506 | int nId; |
| 2507 | const char *zId; |
drh | 1344989 | 2005-09-07 21:22:45 +0000 | [diff] [blame] | 2508 | int constMask = 0; |
danielk1977 | 682f68b | 2004-06-05 10:22:17 +0000 | [diff] [blame] | 2509 | int i; |
drh | 1743575 | 2007-08-16 04:30:38 +0000 | [diff] [blame] | 2510 | sqlite3 *db = pParse->db; |
| 2511 | u8 enc = ENC(db); |
danielk1977 | dc1bdc4 | 2004-06-11 10:51:27 +0000 | [diff] [blame] | 2512 | CollSeq *pColl = 0; |
drh | 1743575 | 2007-08-16 04:30:38 +0000 | [diff] [blame] | 2513 | |
drh | c5499be | 2008-04-01 15:06:33 +0000 | [diff] [blame] | 2514 | testcase( op==TK_CONST_FUNC ); |
| 2515 | testcase( op==TK_FUNCTION ); |
drh | 2646da7 | 2005-12-09 20:02:05 +0000 | [diff] [blame] | 2516 | zId = (char*)pExpr->token.z; |
drh | b71090f | 2005-05-23 17:26:51 +0000 | [diff] [blame] | 2517 | nId = pExpr->token.n; |
danielk1977 | d812336 | 2004-06-12 09:25:12 +0000 | [diff] [blame] | 2518 | pDef = sqlite3FindFunction(pParse->db, zId, nId, nExpr, enc, 0); |
drh | 0bce835 | 2002-02-28 00:41:10 +0000 | [diff] [blame] | 2519 | assert( pDef!=0 ); |
drh | 892d317 | 2008-01-10 03:46:36 +0000 | [diff] [blame] | 2520 | if( pList ){ |
| 2521 | nExpr = pList->nExpr; |
drh | 2dcef11 | 2008-01-12 19:03:48 +0000 | [diff] [blame] | 2522 | r1 = sqlite3GetTempRange(pParse, nExpr); |
drh | 191b54c | 2008-04-15 12:14:21 +0000 | [diff] [blame] | 2523 | sqlite3ExprCodeExprList(pParse, pList, r1, 1); |
drh | 892d317 | 2008-01-10 03:46:36 +0000 | [diff] [blame] | 2524 | }else{ |
drh | d847eaa | 2008-01-17 17:15:56 +0000 | [diff] [blame] | 2525 | nExpr = r1 = 0; |
drh | 892d317 | 2008-01-10 03:46:36 +0000 | [diff] [blame] | 2526 | } |
drh | b7f6f68 | 2006-07-08 17:06:43 +0000 | [diff] [blame] | 2527 | #ifndef SQLITE_OMIT_VIRTUALTABLE |
drh | a43fa22 | 2006-07-08 18:41:37 +0000 | [diff] [blame] | 2528 | /* Possibly overload the function if the first argument is |
| 2529 | ** a virtual table column. |
| 2530 | ** |
| 2531 | ** For infix functions (LIKE, GLOB, REGEXP, and MATCH) use the |
| 2532 | ** second argument, not the first, as the argument to test to |
| 2533 | ** see if it is a column in a virtual table. This is done because |
| 2534 | ** the left operand of infix functions (the operand we want to |
| 2535 | ** control overloading) ends up as the second argument to the |
| 2536 | ** function. The expression "A glob B" is equivalent to |
| 2537 | ** "glob(B,A). We want to use the A in "A glob B" to test |
| 2538 | ** for function overloading. But we use the B term in "glob(B,A)". |
| 2539 | */ |
drh | 6a03a1c | 2006-07-08 18:34:59 +0000 | [diff] [blame] | 2540 | if( nExpr>=2 && (pExpr->flags & EP_InfixFunc) ){ |
drh | 1743575 | 2007-08-16 04:30:38 +0000 | [diff] [blame] | 2541 | pDef = sqlite3VtabOverloadFunction(db, pDef, nExpr, pList->a[1].pExpr); |
drh | 6a03a1c | 2006-07-08 18:34:59 +0000 | [diff] [blame] | 2542 | }else if( nExpr>0 ){ |
drh | 1743575 | 2007-08-16 04:30:38 +0000 | [diff] [blame] | 2543 | pDef = sqlite3VtabOverloadFunction(db, pDef, nExpr, pList->a[0].pExpr); |
drh | b7f6f68 | 2006-07-08 17:06:43 +0000 | [diff] [blame] | 2544 | } |
| 2545 | #endif |
danielk1977 | 682f68b | 2004-06-05 10:22:17 +0000 | [diff] [blame] | 2546 | for(i=0; i<nExpr && i<32; i++){ |
danielk1977 | d02eb1f | 2004-06-06 09:44:03 +0000 | [diff] [blame] | 2547 | if( sqlite3ExprIsConstant(pList->a[i].pExpr) ){ |
drh | 1344989 | 2005-09-07 21:22:45 +0000 | [diff] [blame] | 2548 | constMask |= (1<<i); |
danielk1977 | d02eb1f | 2004-06-06 09:44:03 +0000 | [diff] [blame] | 2549 | } |
danielk1977 | dc1bdc4 | 2004-06-11 10:51:27 +0000 | [diff] [blame] | 2550 | if( pDef->needCollSeq && !pColl ){ |
| 2551 | pColl = sqlite3ExprCollSeq(pParse, pList->a[i].pExpr); |
| 2552 | } |
| 2553 | } |
| 2554 | if( pDef->needCollSeq ){ |
| 2555 | if( !pColl ) pColl = pParse->db->pDfltColl; |
drh | 66a5167 | 2008-01-03 00:01:23 +0000 | [diff] [blame] | 2556 | sqlite3VdbeAddOp4(v, OP_CollSeq, 0, 0, 0, (char *)pColl, P4_COLLSEQ); |
danielk1977 | 682f68b | 2004-06-05 10:22:17 +0000 | [diff] [blame] | 2557 | } |
drh | 2dcef11 | 2008-01-12 19:03:48 +0000 | [diff] [blame] | 2558 | sqlite3VdbeAddOp4(v, OP_Function, constMask, r1, target, |
drh | 66a5167 | 2008-01-03 00:01:23 +0000 | [diff] [blame] | 2559 | (char*)pDef, P4_FUNCDEF); |
drh | 9875715 | 2008-01-09 23:04:12 +0000 | [diff] [blame] | 2560 | sqlite3VdbeChangeP5(v, nExpr); |
drh | 2dcef11 | 2008-01-12 19:03:48 +0000 | [diff] [blame] | 2561 | if( nExpr ){ |
| 2562 | sqlite3ReleaseTempRange(pParse, r1, nExpr); |
| 2563 | } |
drh | da250ea | 2008-04-01 05:07:14 +0000 | [diff] [blame] | 2564 | sqlite3ExprCacheAffinityChange(pParse, r1, nExpr); |
drh | cce7d17 | 2000-05-31 15:34:51 +0000 | [diff] [blame] | 2565 | break; |
| 2566 | } |
drh | fe2093d | 2005-01-20 22:48:47 +0000 | [diff] [blame] | 2567 | #ifndef SQLITE_OMIT_SUBQUERY |
| 2568 | case TK_EXISTS: |
drh | 19a775c | 2000-06-05 18:54:46 +0000 | [diff] [blame] | 2569 | case TK_SELECT: { |
drh | c5499be | 2008-04-01 15:06:33 +0000 | [diff] [blame] | 2570 | testcase( op==TK_EXISTS ); |
| 2571 | testcase( op==TK_SELECT ); |
drh | 41714d6 | 2006-03-02 04:44:23 +0000 | [diff] [blame] | 2572 | if( pExpr->iColumn==0 ){ |
danielk1977 | 0cdc022 | 2008-06-26 18:04:03 +0000 | [diff] [blame] | 2573 | sqlite3CodeSubselect(pParse, pExpr, 0); |
drh | 41714d6 | 2006-03-02 04:44:23 +0000 | [diff] [blame] | 2574 | } |
drh | 9de221d | 2008-01-05 06:51:30 +0000 | [diff] [blame] | 2575 | inReg = pExpr->iColumn; |
drh | 19a775c | 2000-06-05 18:54:46 +0000 | [diff] [blame] | 2576 | break; |
| 2577 | } |
drh | fef5208 | 2000-06-06 01:50:43 +0000 | [diff] [blame] | 2578 | case TK_IN: { |
danielk1977 | 0cdc022 | 2008-06-26 18:04:03 +0000 | [diff] [blame] | 2579 | int rNotFound = 0; |
| 2580 | int rMayHaveNull = 0; |
drh | 6a288a3 | 2008-01-07 19:20:24 +0000 | [diff] [blame] | 2581 | int j1, j2, j3, j4, j5; |
drh | 94a1121 | 2004-09-25 13:12:14 +0000 | [diff] [blame] | 2582 | char affinity; |
danielk1977 | 9a96b66 | 2007-11-29 17:05:18 +0000 | [diff] [blame] | 2583 | int eType; |
danielk1977 | 9a96b66 | 2007-11-29 17:05:18 +0000 | [diff] [blame] | 2584 | |
drh | 3c31fc2 | 2008-06-26 21:45:26 +0000 | [diff] [blame] | 2585 | VdbeNoopComment((v, "begin IN expr r%d", target)); |
danielk1977 | 0cdc022 | 2008-06-26 18:04:03 +0000 | [diff] [blame] | 2586 | eType = sqlite3FindInIndex(pParse, pExpr, &rMayHaveNull); |
| 2587 | if( rMayHaveNull ){ |
| 2588 | rNotFound = ++pParse->nMem; |
| 2589 | } |
danielk1977 | e014a83 | 2004-05-17 10:48:57 +0000 | [diff] [blame] | 2590 | |
| 2591 | /* Figure out the affinity to use to create a key from the results |
| 2592 | ** of the expression. affinityStr stores a static string suitable for |
drh | 66a5167 | 2008-01-03 00:01:23 +0000 | [diff] [blame] | 2593 | ** P4 of OP_MakeRecord. |
danielk1977 | e014a83 | 2004-05-17 10:48:57 +0000 | [diff] [blame] | 2594 | */ |
drh | 94a1121 | 2004-09-25 13:12:14 +0000 | [diff] [blame] | 2595 | affinity = comparisonAffinity(pExpr); |
danielk1977 | e014a83 | 2004-05-17 10:48:57 +0000 | [diff] [blame] | 2596 | |
danielk1977 | e014a83 | 2004-05-17 10:48:57 +0000 | [diff] [blame] | 2597 | |
| 2598 | /* Code the <expr> from "<expr> IN (...)". The temporary table |
| 2599 | ** pExpr->iTable contains the values that make up the (...) set. |
| 2600 | */ |
drh | 66ba23c | 2008-06-27 00:47:28 +0000 | [diff] [blame^] | 2601 | pParse->disableColCache++; |
| 2602 | sqlite3ExprCode(pParse, pExpr->pLeft, target); |
| 2603 | pParse->disableColCache--; |
| 2604 | j2 = sqlite3VdbeAddOp1(v, OP_IsNull, target); |
danielk1977 | 9a96b66 | 2007-11-29 17:05:18 +0000 | [diff] [blame] | 2605 | if( eType==IN_INDEX_ROWID ){ |
drh | 66ba23c | 2008-06-27 00:47:28 +0000 | [diff] [blame^] | 2606 | j3 = sqlite3VdbeAddOp1(v, OP_MustBeInt, target); |
| 2607 | j4 = sqlite3VdbeAddOp3(v, OP_NotExists, pExpr->iTable, 0, target); |
| 2608 | sqlite3VdbeAddOp2(v, OP_Integer, 1, target); |
drh | 6a288a3 | 2008-01-07 19:20:24 +0000 | [diff] [blame] | 2609 | j5 = sqlite3VdbeAddOp0(v, OP_Goto); |
| 2610 | sqlite3VdbeJumpHere(v, j3); |
| 2611 | sqlite3VdbeJumpHere(v, j4); |
danielk1977 | 0cdc022 | 2008-06-26 18:04:03 +0000 | [diff] [blame] | 2612 | sqlite3VdbeAddOp2(v, OP_Integer, 0, target); |
danielk1977 | 9a96b66 | 2007-11-29 17:05:18 +0000 | [diff] [blame] | 2613 | }else{ |
drh | 2dcef11 | 2008-01-12 19:03:48 +0000 | [diff] [blame] | 2614 | r2 = regFree2 = sqlite3GetTempReg(pParse); |
danielk1977 | 0cdc022 | 2008-06-26 18:04:03 +0000 | [diff] [blame] | 2615 | |
| 2616 | /* Create a record and test for set membership. If the set contains |
| 2617 | ** the value, then jump to the end of the test code. The target |
| 2618 | ** register still contains the true (1) value written to it earlier. |
| 2619 | */ |
drh | 66ba23c | 2008-06-27 00:47:28 +0000 | [diff] [blame^] | 2620 | sqlite3VdbeAddOp4(v, OP_MakeRecord, target, 1, r2, &affinity, 1); |
| 2621 | sqlite3VdbeAddOp2(v, OP_Integer, 1, target); |
drh | 2dcef11 | 2008-01-12 19:03:48 +0000 | [diff] [blame] | 2622 | j5 = sqlite3VdbeAddOp3(v, OP_Found, pExpr->iTable, 0, r2); |
danielk1977 | 0cdc022 | 2008-06-26 18:04:03 +0000 | [diff] [blame] | 2623 | |
| 2624 | /* If the set membership test fails, then the result of the |
| 2625 | ** "x IN (...)" expression must be either 0 or NULL. If the set |
| 2626 | ** contains no NULL values, then the result is 0. If the set |
| 2627 | ** contains one or more NULL values, then the result of the |
| 2628 | ** expression is also NULL. |
| 2629 | */ |
| 2630 | if( rNotFound==0 ){ |
| 2631 | /* This branch runs if it is known at compile time (now) that |
| 2632 | ** the set contains no NULL values. This happens as the result |
| 2633 | ** of a "NOT NULL" constraint in the database schema. No need |
| 2634 | ** to test the data structure at runtime in this case. |
| 2635 | */ |
| 2636 | sqlite3VdbeAddOp2(v, OP_Integer, 0, target); |
| 2637 | }else{ |
| 2638 | /* This block populates the rNotFound register with either NULL |
| 2639 | ** or 0 (an integer value). If the data structure contains one |
| 2640 | ** or more NULLs, then set rNotFound to NULL. Otherwise, set it |
| 2641 | ** to 0. If register rMayHaveNull is already set to some value |
| 2642 | ** other than NULL, then the test has already been run and |
| 2643 | ** rNotFound is already populated. |
| 2644 | */ |
drh | 66ba23c | 2008-06-27 00:47:28 +0000 | [diff] [blame^] | 2645 | static const char nullRecord[] = { 0x02, 0x00 }; |
danielk1977 | 0cdc022 | 2008-06-26 18:04:03 +0000 | [diff] [blame] | 2646 | j3 = sqlite3VdbeAddOp1(v, OP_NotNull, rMayHaveNull); |
| 2647 | sqlite3VdbeAddOp2(v, OP_Null, 0, rNotFound); |
drh | 66ba23c | 2008-06-27 00:47:28 +0000 | [diff] [blame^] | 2648 | sqlite3VdbeAddOp4(v, OP_Blob, 2, rMayHaveNull, 0, |
| 2649 | nullRecord, P4_STATIC); |
| 2650 | j4 = sqlite3VdbeAddOp3(v, OP_Found, pExpr->iTable, 0, rMayHaveNull); |
danielk1977 | 0cdc022 | 2008-06-26 18:04:03 +0000 | [diff] [blame] | 2651 | sqlite3VdbeAddOp2(v, OP_Integer, 0, rNotFound); |
| 2652 | sqlite3VdbeJumpHere(v, j4); |
| 2653 | sqlite3VdbeJumpHere(v, j3); |
| 2654 | |
| 2655 | /* Copy the value of register rNotFound (which is either NULL or 0) |
| 2656 | ** into the target register. This will be the result of the |
| 2657 | ** expression. |
| 2658 | */ |
| 2659 | sqlite3VdbeAddOp2(v, OP_Copy, rNotFound, target); |
| 2660 | } |
danielk1977 | 9a96b66 | 2007-11-29 17:05:18 +0000 | [diff] [blame] | 2661 | } |
drh | 6a288a3 | 2008-01-07 19:20:24 +0000 | [diff] [blame] | 2662 | sqlite3VdbeJumpHere(v, j2); |
| 2663 | sqlite3VdbeJumpHere(v, j5); |
drh | 3c31fc2 | 2008-06-26 21:45:26 +0000 | [diff] [blame] | 2664 | VdbeComment((v, "end IN expr r%d", target)); |
drh | fef5208 | 2000-06-06 01:50:43 +0000 | [diff] [blame] | 2665 | break; |
| 2666 | } |
danielk1977 | 93758c8 | 2005-01-21 08:13:14 +0000 | [diff] [blame] | 2667 | #endif |
drh | 2dcef11 | 2008-01-12 19:03:48 +0000 | [diff] [blame] | 2668 | /* |
| 2669 | ** x BETWEEN y AND z |
| 2670 | ** |
| 2671 | ** This is equivalent to |
| 2672 | ** |
| 2673 | ** x>=y AND x<=z |
| 2674 | ** |
| 2675 | ** X is stored in pExpr->pLeft. |
| 2676 | ** Y is stored in pExpr->pList->a[0].pExpr. |
| 2677 | ** Z is stored in pExpr->pList->a[1].pExpr. |
| 2678 | */ |
drh | fef5208 | 2000-06-06 01:50:43 +0000 | [diff] [blame] | 2679 | case TK_BETWEEN: { |
drh | be5c89a | 2004-07-26 00:31:09 +0000 | [diff] [blame] | 2680 | Expr *pLeft = pExpr->pLeft; |
| 2681 | struct ExprList_item *pLItem = pExpr->pList->a; |
| 2682 | Expr *pRight = pLItem->pExpr; |
drh | 3557335 | 2008-01-08 23:54:25 +0000 | [diff] [blame] | 2683 | |
drh | da250ea | 2008-04-01 05:07:14 +0000 | [diff] [blame] | 2684 | codeCompareOperands(pParse, pLeft, &r1, ®Free1, |
| 2685 | pRight, &r2, ®Free2); |
drh | c5499be | 2008-04-01 15:06:33 +0000 | [diff] [blame] | 2686 | testcase( regFree1==0 ); |
| 2687 | testcase( regFree2==0 ); |
drh | 2dcef11 | 2008-01-12 19:03:48 +0000 | [diff] [blame] | 2688 | r3 = sqlite3GetTempReg(pParse); |
drh | 678ccce | 2008-03-31 18:19:54 +0000 | [diff] [blame] | 2689 | r4 = sqlite3GetTempReg(pParse); |
drh | 3557335 | 2008-01-08 23:54:25 +0000 | [diff] [blame] | 2690 | codeCompare(pParse, pLeft, pRight, OP_Ge, |
| 2691 | r1, r2, r3, SQLITE_STOREP2); |
drh | be5c89a | 2004-07-26 00:31:09 +0000 | [diff] [blame] | 2692 | pLItem++; |
| 2693 | pRight = pLItem->pExpr; |
drh | 2dcef11 | 2008-01-12 19:03:48 +0000 | [diff] [blame] | 2694 | sqlite3ReleaseTempReg(pParse, regFree2); |
| 2695 | r2 = sqlite3ExprCodeTemp(pParse, pRight, ®Free2); |
drh | c5499be | 2008-04-01 15:06:33 +0000 | [diff] [blame] | 2696 | testcase( regFree2==0 ); |
drh | 678ccce | 2008-03-31 18:19:54 +0000 | [diff] [blame] | 2697 | codeCompare(pParse, pLeft, pRight, OP_Le, r1, r2, r4, SQLITE_STOREP2); |
| 2698 | sqlite3VdbeAddOp3(v, OP_And, r3, r4, target); |
drh | 2dcef11 | 2008-01-12 19:03:48 +0000 | [diff] [blame] | 2699 | sqlite3ReleaseTempReg(pParse, r3); |
drh | 678ccce | 2008-03-31 18:19:54 +0000 | [diff] [blame] | 2700 | sqlite3ReleaseTempReg(pParse, r4); |
drh | fef5208 | 2000-06-06 01:50:43 +0000 | [diff] [blame] | 2701 | break; |
| 2702 | } |
drh | 4f07e5f | 2007-05-14 11:34:46 +0000 | [diff] [blame] | 2703 | case TK_UPLUS: { |
drh | 2dcef11 | 2008-01-12 19:03:48 +0000 | [diff] [blame] | 2704 | inReg = sqlite3ExprCodeTarget(pParse, pExpr->pLeft, target); |
drh | a2e0004 | 2002-01-22 03:13:42 +0000 | [diff] [blame] | 2705 | break; |
| 2706 | } |
drh | 2dcef11 | 2008-01-12 19:03:48 +0000 | [diff] [blame] | 2707 | |
| 2708 | /* |
| 2709 | ** Form A: |
| 2710 | ** CASE x WHEN e1 THEN r1 WHEN e2 THEN r2 ... WHEN eN THEN rN ELSE y END |
| 2711 | ** |
| 2712 | ** Form B: |
| 2713 | ** CASE WHEN e1 THEN r1 WHEN e2 THEN r2 ... WHEN eN THEN rN ELSE y END |
| 2714 | ** |
| 2715 | ** Form A is can be transformed into the equivalent form B as follows: |
| 2716 | ** CASE WHEN x=e1 THEN r1 WHEN x=e2 THEN r2 ... |
| 2717 | ** WHEN x=eN THEN rN ELSE y END |
| 2718 | ** |
| 2719 | ** X (if it exists) is in pExpr->pLeft. |
| 2720 | ** Y is in pExpr->pRight. The Y is also optional. If there is no |
| 2721 | ** ELSE clause and no other term matches, then the result of the |
| 2722 | ** exprssion is NULL. |
| 2723 | ** Ei is in pExpr->pList->a[i*2] and Ri is pExpr->pList->a[i*2+1]. |
| 2724 | ** |
| 2725 | ** The result of the expression is the Ri for the first matching Ei, |
| 2726 | ** or if there is no matching Ei, the ELSE term Y, or if there is |
| 2727 | ** no ELSE term, NULL. |
| 2728 | */ |
drh | 17a7f8d | 2002-03-24 13:13:27 +0000 | [diff] [blame] | 2729 | case TK_CASE: { |
drh | 2dcef11 | 2008-01-12 19:03:48 +0000 | [diff] [blame] | 2730 | int endLabel; /* GOTO label for end of CASE stmt */ |
| 2731 | int nextCase; /* GOTO label for next WHEN clause */ |
| 2732 | int nExpr; /* 2x number of WHEN terms */ |
| 2733 | int i; /* Loop counter */ |
| 2734 | ExprList *pEList; /* List of WHEN terms */ |
| 2735 | struct ExprList_item *aListelem; /* Array of WHEN terms */ |
| 2736 | Expr opCompare; /* The X==Ei expression */ |
| 2737 | Expr cacheX; /* Cached expression X */ |
| 2738 | Expr *pX; /* The X expression */ |
| 2739 | Expr *pTest; /* X==Ei (form A) or just Ei (form B) */ |
drh | 17a7f8d | 2002-03-24 13:13:27 +0000 | [diff] [blame] | 2740 | |
| 2741 | assert(pExpr->pList); |
| 2742 | assert((pExpr->pList->nExpr % 2) == 0); |
| 2743 | assert(pExpr->pList->nExpr > 0); |
drh | be5c89a | 2004-07-26 00:31:09 +0000 | [diff] [blame] | 2744 | pEList = pExpr->pList; |
| 2745 | aListelem = pEList->a; |
| 2746 | nExpr = pEList->nExpr; |
drh | 2dcef11 | 2008-01-12 19:03:48 +0000 | [diff] [blame] | 2747 | endLabel = sqlite3VdbeMakeLabel(v); |
| 2748 | if( (pX = pExpr->pLeft)!=0 ){ |
| 2749 | cacheX = *pX; |
drh | c5499be | 2008-04-01 15:06:33 +0000 | [diff] [blame] | 2750 | testcase( pX->op==TK_COLUMN || pX->op==TK_REGISTER ); |
drh | 2dcef11 | 2008-01-12 19:03:48 +0000 | [diff] [blame] | 2751 | cacheX.iTable = sqlite3ExprCodeTemp(pParse, pX, ®Free1); |
drh | c5499be | 2008-04-01 15:06:33 +0000 | [diff] [blame] | 2752 | testcase( regFree1==0 ); |
drh | 2dcef11 | 2008-01-12 19:03:48 +0000 | [diff] [blame] | 2753 | cacheX.op = TK_REGISTER; |
drh | 678ccce | 2008-03-31 18:19:54 +0000 | [diff] [blame] | 2754 | cacheX.iColumn = 0; |
drh | 2dcef11 | 2008-01-12 19:03:48 +0000 | [diff] [blame] | 2755 | opCompare.op = TK_EQ; |
| 2756 | opCompare.pLeft = &cacheX; |
| 2757 | pTest = &opCompare; |
drh | 17a7f8d | 2002-03-24 13:13:27 +0000 | [diff] [blame] | 2758 | } |
drh | c5499be | 2008-04-01 15:06:33 +0000 | [diff] [blame] | 2759 | pParse->disableColCache++; |
drh | f5905aa | 2002-05-26 20:54:33 +0000 | [diff] [blame] | 2760 | for(i=0; i<nExpr; i=i+2){ |
drh | 2dcef11 | 2008-01-12 19:03:48 +0000 | [diff] [blame] | 2761 | if( pX ){ |
| 2762 | opCompare.pRight = aListelem[i].pExpr; |
drh | 17a7f8d | 2002-03-24 13:13:27 +0000 | [diff] [blame] | 2763 | }else{ |
drh | 2dcef11 | 2008-01-12 19:03:48 +0000 | [diff] [blame] | 2764 | pTest = aListelem[i].pExpr; |
drh | 17a7f8d | 2002-03-24 13:13:27 +0000 | [diff] [blame] | 2765 | } |
drh | 2dcef11 | 2008-01-12 19:03:48 +0000 | [diff] [blame] | 2766 | nextCase = sqlite3VdbeMakeLabel(v); |
drh | c5499be | 2008-04-01 15:06:33 +0000 | [diff] [blame] | 2767 | testcase( pTest->op==TK_COLUMN || pTest->op==TK_REGISTER ); |
drh | 2dcef11 | 2008-01-12 19:03:48 +0000 | [diff] [blame] | 2768 | sqlite3ExprIfFalse(pParse, pTest, nextCase, SQLITE_JUMPIFNULL); |
drh | c5499be | 2008-04-01 15:06:33 +0000 | [diff] [blame] | 2769 | testcase( aListelem[i+1].pExpr->op==TK_COLUMN ); |
| 2770 | testcase( aListelem[i+1].pExpr->op==TK_REGISTER ); |
drh | 9de221d | 2008-01-05 06:51:30 +0000 | [diff] [blame] | 2771 | sqlite3ExprCode(pParse, aListelem[i+1].pExpr, target); |
drh | 2dcef11 | 2008-01-12 19:03:48 +0000 | [diff] [blame] | 2772 | sqlite3VdbeAddOp2(v, OP_Goto, 0, endLabel); |
| 2773 | sqlite3VdbeResolveLabel(v, nextCase); |
drh | f570f01 | 2002-05-31 15:51:25 +0000 | [diff] [blame] | 2774 | } |
drh | 17a7f8d | 2002-03-24 13:13:27 +0000 | [diff] [blame] | 2775 | if( pExpr->pRight ){ |
drh | 9de221d | 2008-01-05 06:51:30 +0000 | [diff] [blame] | 2776 | sqlite3ExprCode(pParse, pExpr->pRight, target); |
drh | 17a7f8d | 2002-03-24 13:13:27 +0000 | [diff] [blame] | 2777 | }else{ |
drh | 9de221d | 2008-01-05 06:51:30 +0000 | [diff] [blame] | 2778 | sqlite3VdbeAddOp2(v, OP_Null, 0, target); |
drh | 17a7f8d | 2002-03-24 13:13:27 +0000 | [diff] [blame] | 2779 | } |
drh | 2dcef11 | 2008-01-12 19:03:48 +0000 | [diff] [blame] | 2780 | sqlite3VdbeResolveLabel(v, endLabel); |
drh | c5499be | 2008-04-01 15:06:33 +0000 | [diff] [blame] | 2781 | assert( pParse->disableColCache>0 ); |
| 2782 | pParse->disableColCache--; |
danielk1977 | 6f34903 | 2002-06-11 02:25:40 +0000 | [diff] [blame] | 2783 | break; |
| 2784 | } |
danielk1977 | 5338a5f | 2005-01-20 13:03:10 +0000 | [diff] [blame] | 2785 | #ifndef SQLITE_OMIT_TRIGGER |
danielk1977 | 6f34903 | 2002-06-11 02:25:40 +0000 | [diff] [blame] | 2786 | case TK_RAISE: { |
| 2787 | if( !pParse->trigStack ){ |
danielk1977 | 4adee20 | 2004-05-08 08:23:19 +0000 | [diff] [blame] | 2788 | sqlite3ErrorMsg(pParse, |
drh | da93d23 | 2003-03-31 02:12:46 +0000 | [diff] [blame] | 2789 | "RAISE() may only be used within a trigger-program"); |
drh | 389a1ad | 2008-01-03 23:44:53 +0000 | [diff] [blame] | 2790 | return 0; |
danielk1977 | 6f34903 | 2002-06-11 02:25:40 +0000 | [diff] [blame] | 2791 | } |
drh | ad6d946 | 2004-09-19 02:15:24 +0000 | [diff] [blame] | 2792 | if( pExpr->iColumn!=OE_Ignore ){ |
| 2793 | assert( pExpr->iColumn==OE_Rollback || |
| 2794 | pExpr->iColumn == OE_Abort || |
| 2795 | pExpr->iColumn == OE_Fail ); |
danielk1977 | 1e53695 | 2007-08-16 10:09:01 +0000 | [diff] [blame] | 2796 | sqlite3DequoteExpr(pParse->db, pExpr); |
drh | 66a5167 | 2008-01-03 00:01:23 +0000 | [diff] [blame] | 2797 | sqlite3VdbeAddOp4(v, OP_Halt, SQLITE_CONSTRAINT, pExpr->iColumn, 0, |
drh | 2646da7 | 2005-12-09 20:02:05 +0000 | [diff] [blame] | 2798 | (char*)pExpr->token.z, pExpr->token.n); |
danielk1977 | 6f34903 | 2002-06-11 02:25:40 +0000 | [diff] [blame] | 2799 | } else { |
drh | ad6d946 | 2004-09-19 02:15:24 +0000 | [diff] [blame] | 2800 | assert( pExpr->iColumn == OE_Ignore ); |
drh | 66a5167 | 2008-01-03 00:01:23 +0000 | [diff] [blame] | 2801 | sqlite3VdbeAddOp2(v, OP_ContextPop, 0, 0); |
| 2802 | sqlite3VdbeAddOp2(v, OP_Goto, 0, pParse->trigStack->ignoreJump); |
drh | d4e70eb | 2008-01-02 00:34:36 +0000 | [diff] [blame] | 2803 | VdbeComment((v, "raise(IGNORE)")); |
danielk1977 | 6f34903 | 2002-06-11 02:25:40 +0000 | [diff] [blame] | 2804 | } |
drh | ffe07b2 | 2005-11-03 00:41:17 +0000 | [diff] [blame] | 2805 | break; |
drh | 17a7f8d | 2002-03-24 13:13:27 +0000 | [diff] [blame] | 2806 | } |
danielk1977 | 5338a5f | 2005-01-20 13:03:10 +0000 | [diff] [blame] | 2807 | #endif |
drh | ffe07b2 | 2005-11-03 00:41:17 +0000 | [diff] [blame] | 2808 | } |
drh | 2dcef11 | 2008-01-12 19:03:48 +0000 | [diff] [blame] | 2809 | sqlite3ReleaseTempReg(pParse, regFree1); |
| 2810 | sqlite3ReleaseTempReg(pParse, regFree2); |
| 2811 | return inReg; |
| 2812 | } |
| 2813 | |
| 2814 | /* |
| 2815 | ** Generate code to evaluate an expression and store the results |
| 2816 | ** into a register. Return the register number where the results |
| 2817 | ** are stored. |
| 2818 | ** |
| 2819 | ** If the register is a temporary register that can be deallocated, |
drh | 678ccce | 2008-03-31 18:19:54 +0000 | [diff] [blame] | 2820 | ** then write its number into *pReg. If the result register is not |
drh | 2dcef11 | 2008-01-12 19:03:48 +0000 | [diff] [blame] | 2821 | ** a temporary, then set *pReg to zero. |
| 2822 | */ |
| 2823 | int sqlite3ExprCodeTemp(Parse *pParse, Expr *pExpr, int *pReg){ |
| 2824 | int r1 = sqlite3GetTempReg(pParse); |
| 2825 | int r2 = sqlite3ExprCodeTarget(pParse, pExpr, r1); |
| 2826 | if( r2==r1 ){ |
| 2827 | *pReg = r1; |
| 2828 | }else{ |
| 2829 | sqlite3ReleaseTempReg(pParse, r1); |
| 2830 | *pReg = 0; |
| 2831 | } |
| 2832 | return r2; |
| 2833 | } |
| 2834 | |
| 2835 | /* |
| 2836 | ** Generate code that will evaluate expression pExpr and store the |
| 2837 | ** results in register target. The results are guaranteed to appear |
| 2838 | ** in register target. |
| 2839 | */ |
| 2840 | int sqlite3ExprCode(Parse *pParse, Expr *pExpr, int target){ |
drh | 9cbf342 | 2008-01-17 16:22:13 +0000 | [diff] [blame] | 2841 | int inReg; |
| 2842 | |
| 2843 | assert( target>0 && target<=pParse->nMem ); |
| 2844 | inReg = sqlite3ExprCodeTarget(pParse, pExpr, target); |
drh | 0e359b3 | 2008-01-13 19:02:11 +0000 | [diff] [blame] | 2845 | assert( pParse->pVdbe || pParse->db->mallocFailed ); |
| 2846 | if( inReg!=target && pParse->pVdbe ){ |
drh | 9cbf342 | 2008-01-17 16:22:13 +0000 | [diff] [blame] | 2847 | sqlite3VdbeAddOp2(pParse->pVdbe, OP_SCopy, inReg, target); |
drh | cce7d17 | 2000-05-31 15:34:51 +0000 | [diff] [blame] | 2848 | } |
drh | 389a1ad | 2008-01-03 23:44:53 +0000 | [diff] [blame] | 2849 | return target; |
drh | cce7d17 | 2000-05-31 15:34:51 +0000 | [diff] [blame] | 2850 | } |
| 2851 | |
| 2852 | /* |
drh | 2dcef11 | 2008-01-12 19:03:48 +0000 | [diff] [blame] | 2853 | ** Generate code that evalutes the given expression and puts the result |
drh | de4fcfd | 2008-01-19 23:50:26 +0000 | [diff] [blame] | 2854 | ** in register target. |
drh | 2530378 | 2004-12-07 15:41:48 +0000 | [diff] [blame] | 2855 | ** |
drh | 2dcef11 | 2008-01-12 19:03:48 +0000 | [diff] [blame] | 2856 | ** Also make a copy of the expression results into another "cache" register |
| 2857 | ** and modify the expression so that the next time it is evaluated, |
| 2858 | ** the result is a copy of the cache register. |
| 2859 | ** |
| 2860 | ** This routine is used for expressions that are used multiple |
| 2861 | ** times. They are evaluated once and the results of the expression |
| 2862 | ** are reused. |
drh | 2530378 | 2004-12-07 15:41:48 +0000 | [diff] [blame] | 2863 | */ |
drh | 2dcef11 | 2008-01-12 19:03:48 +0000 | [diff] [blame] | 2864 | int sqlite3ExprCodeAndCache(Parse *pParse, Expr *pExpr, int target){ |
drh | 2530378 | 2004-12-07 15:41:48 +0000 | [diff] [blame] | 2865 | Vdbe *v = pParse->pVdbe; |
drh | 2dcef11 | 2008-01-12 19:03:48 +0000 | [diff] [blame] | 2866 | int inReg; |
| 2867 | inReg = sqlite3ExprCode(pParse, pExpr, target); |
drh | de4fcfd | 2008-01-19 23:50:26 +0000 | [diff] [blame] | 2868 | assert( target>0 ); |
drh | 2dcef11 | 2008-01-12 19:03:48 +0000 | [diff] [blame] | 2869 | if( pExpr->op!=TK_REGISTER ){ |
| 2870 | int iMem; |
drh | de4fcfd | 2008-01-19 23:50:26 +0000 | [diff] [blame] | 2871 | iMem = ++pParse->nMem; |
| 2872 | sqlite3VdbeAddOp2(v, OP_Copy, inReg, iMem); |
drh | 2dcef11 | 2008-01-12 19:03:48 +0000 | [diff] [blame] | 2873 | pExpr->iTable = iMem; |
drh | 678ccce | 2008-03-31 18:19:54 +0000 | [diff] [blame] | 2874 | pExpr->iColumn = pExpr->op; |
drh | 2530378 | 2004-12-07 15:41:48 +0000 | [diff] [blame] | 2875 | pExpr->op = TK_REGISTER; |
| 2876 | } |
drh | 2dcef11 | 2008-01-12 19:03:48 +0000 | [diff] [blame] | 2877 | return inReg; |
drh | 2530378 | 2004-12-07 15:41:48 +0000 | [diff] [blame] | 2878 | } |
drh | 2dcef11 | 2008-01-12 19:03:48 +0000 | [diff] [blame] | 2879 | |
drh | 678ccce | 2008-03-31 18:19:54 +0000 | [diff] [blame] | 2880 | /* |
drh | 47de955 | 2008-04-01 18:04:11 +0000 | [diff] [blame] | 2881 | ** Return TRUE if pExpr is an constant expression that is appropriate |
| 2882 | ** for factoring out of a loop. Appropriate expressions are: |
| 2883 | ** |
| 2884 | ** * Any expression that evaluates to two or more opcodes. |
| 2885 | ** |
| 2886 | ** * Any OP_Integer, OP_Real, OP_String, OP_Blob, OP_Null, |
| 2887 | ** or OP_Variable that does not need to be placed in a |
| 2888 | ** specific register. |
| 2889 | ** |
| 2890 | ** There is no point in factoring out single-instruction constant |
| 2891 | ** expressions that need to be placed in a particular register. |
| 2892 | ** We could factor them out, but then we would end up adding an |
| 2893 | ** OP_SCopy instruction to move the value into the correct register |
| 2894 | ** later. We might as well just use the original instruction and |
| 2895 | ** avoid the OP_SCopy. |
| 2896 | */ |
| 2897 | static int isAppropriateForFactoring(Expr *p){ |
| 2898 | if( !sqlite3ExprIsConstantNotJoin(p) ){ |
| 2899 | return 0; /* Only constant expressions are appropriate for factoring */ |
| 2900 | } |
| 2901 | if( (p->flags & EP_FixedDest)==0 ){ |
| 2902 | return 1; /* Any constant without a fixed destination is appropriate */ |
| 2903 | } |
| 2904 | while( p->op==TK_UPLUS ) p = p->pLeft; |
| 2905 | switch( p->op ){ |
| 2906 | #ifndef SQLITE_OMIT_BLOB_LITERAL |
| 2907 | case TK_BLOB: |
| 2908 | #endif |
| 2909 | case TK_VARIABLE: |
| 2910 | case TK_INTEGER: |
| 2911 | case TK_FLOAT: |
| 2912 | case TK_NULL: |
| 2913 | case TK_STRING: { |
| 2914 | testcase( p->op==TK_BLOB ); |
| 2915 | testcase( p->op==TK_VARIABLE ); |
| 2916 | testcase( p->op==TK_INTEGER ); |
| 2917 | testcase( p->op==TK_FLOAT ); |
| 2918 | testcase( p->op==TK_NULL ); |
| 2919 | testcase( p->op==TK_STRING ); |
| 2920 | /* Single-instruction constants with a fixed destination are |
| 2921 | ** better done in-line. If we factor them, they will just end |
| 2922 | ** up generating an OP_SCopy to move the value to the destination |
| 2923 | ** register. */ |
| 2924 | return 0; |
| 2925 | } |
| 2926 | case TK_UMINUS: { |
| 2927 | if( p->pLeft->op==TK_FLOAT || p->pLeft->op==TK_INTEGER ){ |
| 2928 | return 0; |
| 2929 | } |
| 2930 | break; |
| 2931 | } |
| 2932 | default: { |
| 2933 | break; |
| 2934 | } |
| 2935 | } |
| 2936 | return 1; |
| 2937 | } |
| 2938 | |
| 2939 | /* |
| 2940 | ** If pExpr is a constant expression that is appropriate for |
| 2941 | ** factoring out of a loop, then evaluate the expression |
drh | 678ccce | 2008-03-31 18:19:54 +0000 | [diff] [blame] | 2942 | ** into a register and convert the expression into a TK_REGISTER |
| 2943 | ** expression. |
| 2944 | */ |
| 2945 | static int evalConstExpr(void *pArg, Expr *pExpr){ |
| 2946 | Parse *pParse = (Parse*)pArg; |
drh | 47de955 | 2008-04-01 18:04:11 +0000 | [diff] [blame] | 2947 | switch( pExpr->op ){ |
| 2948 | case TK_REGISTER: { |
| 2949 | return 1; |
| 2950 | } |
| 2951 | case TK_FUNCTION: |
| 2952 | case TK_AGG_FUNCTION: |
| 2953 | case TK_CONST_FUNC: { |
| 2954 | /* The arguments to a function have a fixed destination. |
| 2955 | ** Mark them this way to avoid generated unneeded OP_SCopy |
| 2956 | ** instructions. |
| 2957 | */ |
| 2958 | ExprList *pList = pExpr->pList; |
| 2959 | if( pList ){ |
| 2960 | int i = pList->nExpr; |
| 2961 | struct ExprList_item *pItem = pList->a; |
| 2962 | for(; i>0; i--, pItem++){ |
| 2963 | if( pItem->pExpr ) pItem->pExpr->flags |= EP_FixedDest; |
| 2964 | } |
| 2965 | } |
| 2966 | break; |
| 2967 | } |
drh | 678ccce | 2008-03-31 18:19:54 +0000 | [diff] [blame] | 2968 | } |
drh | 47de955 | 2008-04-01 18:04:11 +0000 | [diff] [blame] | 2969 | if( isAppropriateForFactoring(pExpr) ){ |
drh | 678ccce | 2008-03-31 18:19:54 +0000 | [diff] [blame] | 2970 | int r1 = ++pParse->nMem; |
| 2971 | int r2; |
| 2972 | r2 = sqlite3ExprCodeTarget(pParse, pExpr, r1); |
drh | c5499be | 2008-04-01 15:06:33 +0000 | [diff] [blame] | 2973 | if( r1!=r2 ) sqlite3ReleaseTempReg(pParse, r1); |
drh | 678ccce | 2008-03-31 18:19:54 +0000 | [diff] [blame] | 2974 | pExpr->iColumn = pExpr->op; |
| 2975 | pExpr->op = TK_REGISTER; |
| 2976 | pExpr->iTable = r2; |
| 2977 | return 1; |
| 2978 | } |
| 2979 | return 0; |
| 2980 | } |
| 2981 | |
| 2982 | /* |
| 2983 | ** Preevaluate constant subexpressions within pExpr and store the |
| 2984 | ** results in registers. Modify pExpr so that the constant subexpresions |
| 2985 | ** are TK_REGISTER opcodes that refer to the precomputed values. |
| 2986 | */ |
| 2987 | void sqlite3ExprCodeConstants(Parse *pParse, Expr *pExpr){ |
| 2988 | walkExprTree(pExpr, evalConstExpr, pParse); |
| 2989 | } |
| 2990 | |
drh | 2530378 | 2004-12-07 15:41:48 +0000 | [diff] [blame] | 2991 | |
| 2992 | /* |
drh | 268380c | 2004-02-25 13:47:31 +0000 | [diff] [blame] | 2993 | ** Generate code that pushes the value of every element of the given |
drh | 9cbf342 | 2008-01-17 16:22:13 +0000 | [diff] [blame] | 2994 | ** expression list into a sequence of registers beginning at target. |
drh | 268380c | 2004-02-25 13:47:31 +0000 | [diff] [blame] | 2995 | ** |
drh | 892d317 | 2008-01-10 03:46:36 +0000 | [diff] [blame] | 2996 | ** Return the number of elements evaluated. |
drh | 268380c | 2004-02-25 13:47:31 +0000 | [diff] [blame] | 2997 | */ |
danielk1977 | 4adee20 | 2004-05-08 08:23:19 +0000 | [diff] [blame] | 2998 | int sqlite3ExprCodeExprList( |
drh | 268380c | 2004-02-25 13:47:31 +0000 | [diff] [blame] | 2999 | Parse *pParse, /* Parsing context */ |
drh | 389a1ad | 2008-01-03 23:44:53 +0000 | [diff] [blame] | 3000 | ExprList *pList, /* The expression list to be coded */ |
drh | 191b54c | 2008-04-15 12:14:21 +0000 | [diff] [blame] | 3001 | int target, /* Where to write results */ |
| 3002 | int doHardCopy /* Call sqlite3ExprHardCopy on each element if true */ |
drh | 268380c | 2004-02-25 13:47:31 +0000 | [diff] [blame] | 3003 | ){ |
| 3004 | struct ExprList_item *pItem; |
drh | 9cbf342 | 2008-01-17 16:22:13 +0000 | [diff] [blame] | 3005 | int i, n; |
drh | 892d317 | 2008-01-10 03:46:36 +0000 | [diff] [blame] | 3006 | assert( pList!=0 || pParse->db->mallocFailed ); |
| 3007 | if( pList==0 ){ |
| 3008 | return 0; |
| 3009 | } |
drh | 9cbf342 | 2008-01-17 16:22:13 +0000 | [diff] [blame] | 3010 | assert( target>0 ); |
drh | 268380c | 2004-02-25 13:47:31 +0000 | [diff] [blame] | 3011 | n = pList->nExpr; |
drh | 191b54c | 2008-04-15 12:14:21 +0000 | [diff] [blame] | 3012 | for(pItem=pList->a, i=0; i<n; i++, pItem++){ |
| 3013 | sqlite3ExprCode(pParse, pItem->pExpr, target+i); |
| 3014 | if( doHardCopy ) sqlite3ExprHardCopy(pParse, target, n); |
drh | 268380c | 2004-02-25 13:47:31 +0000 | [diff] [blame] | 3015 | } |
drh | f9b596e | 2004-05-26 16:54:42 +0000 | [diff] [blame] | 3016 | return n; |
drh | 268380c | 2004-02-25 13:47:31 +0000 | [diff] [blame] | 3017 | } |
| 3018 | |
| 3019 | /* |
drh | cce7d17 | 2000-05-31 15:34:51 +0000 | [diff] [blame] | 3020 | ** Generate code for a boolean expression such that a jump is made |
| 3021 | ** to the label "dest" if the expression is true but execution |
| 3022 | ** continues straight thru if the expression is false. |
drh | f5905aa | 2002-05-26 20:54:33 +0000 | [diff] [blame] | 3023 | ** |
| 3024 | ** If the expression evaluates to NULL (neither true nor false), then |
drh | 3557335 | 2008-01-08 23:54:25 +0000 | [diff] [blame] | 3025 | ** take the jump if the jumpIfNull flag is SQLITE_JUMPIFNULL. |
drh | f2bc013 | 2004-10-04 13:19:23 +0000 | [diff] [blame] | 3026 | ** |
| 3027 | ** This code depends on the fact that certain token values (ex: TK_EQ) |
| 3028 | ** are the same as opcode values (ex: OP_Eq) that implement the corresponding |
| 3029 | ** operation. Special comments in vdbe.c and the mkopcodeh.awk script in |
| 3030 | ** the make process cause these values to align. Assert()s in the code |
| 3031 | ** below verify that the numbers are aligned correctly. |
drh | cce7d17 | 2000-05-31 15:34:51 +0000 | [diff] [blame] | 3032 | */ |
danielk1977 | 4adee20 | 2004-05-08 08:23:19 +0000 | [diff] [blame] | 3033 | void sqlite3ExprIfTrue(Parse *pParse, Expr *pExpr, int dest, int jumpIfNull){ |
drh | cce7d17 | 2000-05-31 15:34:51 +0000 | [diff] [blame] | 3034 | Vdbe *v = pParse->pVdbe; |
| 3035 | int op = 0; |
drh | 2dcef11 | 2008-01-12 19:03:48 +0000 | [diff] [blame] | 3036 | int regFree1 = 0; |
| 3037 | int regFree2 = 0; |
| 3038 | int r1, r2; |
| 3039 | |
drh | 3557335 | 2008-01-08 23:54:25 +0000 | [diff] [blame] | 3040 | assert( jumpIfNull==SQLITE_JUMPIFNULL || jumpIfNull==0 ); |
drh | daffd0e | 2001-04-11 14:28:42 +0000 | [diff] [blame] | 3041 | if( v==0 || pExpr==0 ) return; |
drh | f2bc013 | 2004-10-04 13:19:23 +0000 | [diff] [blame] | 3042 | op = pExpr->op; |
| 3043 | switch( op ){ |
drh | cce7d17 | 2000-05-31 15:34:51 +0000 | [diff] [blame] | 3044 | case TK_AND: { |
danielk1977 | 4adee20 | 2004-05-08 08:23:19 +0000 | [diff] [blame] | 3045 | int d2 = sqlite3VdbeMakeLabel(v); |
drh | c5499be | 2008-04-01 15:06:33 +0000 | [diff] [blame] | 3046 | testcase( jumpIfNull==0 ); |
| 3047 | testcase( pParse->disableColCache==0 ); |
drh | 3557335 | 2008-01-08 23:54:25 +0000 | [diff] [blame] | 3048 | sqlite3ExprIfFalse(pParse, pExpr->pLeft, d2,jumpIfNull^SQLITE_JUMPIFNULL); |
drh | e55cbd7 | 2008-03-31 23:48:03 +0000 | [diff] [blame] | 3049 | pParse->disableColCache++; |
danielk1977 | 4adee20 | 2004-05-08 08:23:19 +0000 | [diff] [blame] | 3050 | sqlite3ExprIfTrue(pParse, pExpr->pRight, dest, jumpIfNull); |
drh | c5499be | 2008-04-01 15:06:33 +0000 | [diff] [blame] | 3051 | assert( pParse->disableColCache>0 ); |
drh | e55cbd7 | 2008-03-31 23:48:03 +0000 | [diff] [blame] | 3052 | pParse->disableColCache--; |
danielk1977 | 4adee20 | 2004-05-08 08:23:19 +0000 | [diff] [blame] | 3053 | sqlite3VdbeResolveLabel(v, d2); |
drh | cce7d17 | 2000-05-31 15:34:51 +0000 | [diff] [blame] | 3054 | break; |
| 3055 | } |
| 3056 | case TK_OR: { |
drh | c5499be | 2008-04-01 15:06:33 +0000 | [diff] [blame] | 3057 | testcase( jumpIfNull==0 ); |
| 3058 | testcase( pParse->disableColCache==0 ); |
danielk1977 | 4adee20 | 2004-05-08 08:23:19 +0000 | [diff] [blame] | 3059 | sqlite3ExprIfTrue(pParse, pExpr->pLeft, dest, jumpIfNull); |
drh | e55cbd7 | 2008-03-31 23:48:03 +0000 | [diff] [blame] | 3060 | pParse->disableColCache++; |
danielk1977 | 4adee20 | 2004-05-08 08:23:19 +0000 | [diff] [blame] | 3061 | sqlite3ExprIfTrue(pParse, pExpr->pRight, dest, jumpIfNull); |
drh | c5499be | 2008-04-01 15:06:33 +0000 | [diff] [blame] | 3062 | assert( pParse->disableColCache>0 ); |
drh | e55cbd7 | 2008-03-31 23:48:03 +0000 | [diff] [blame] | 3063 | pParse->disableColCache--; |
drh | cce7d17 | 2000-05-31 15:34:51 +0000 | [diff] [blame] | 3064 | break; |
| 3065 | } |
| 3066 | case TK_NOT: { |
drh | c5499be | 2008-04-01 15:06:33 +0000 | [diff] [blame] | 3067 | testcase( jumpIfNull==0 ); |
danielk1977 | 4adee20 | 2004-05-08 08:23:19 +0000 | [diff] [blame] | 3068 | sqlite3ExprIfFalse(pParse, pExpr->pLeft, dest, jumpIfNull); |
drh | cce7d17 | 2000-05-31 15:34:51 +0000 | [diff] [blame] | 3069 | break; |
| 3070 | } |
| 3071 | case TK_LT: |
| 3072 | case TK_LE: |
| 3073 | case TK_GT: |
| 3074 | case TK_GE: |
| 3075 | case TK_NE: |
drh | 0ac6589 | 2002-04-20 14:24:41 +0000 | [diff] [blame] | 3076 | case TK_EQ: { |
drh | f2bc013 | 2004-10-04 13:19:23 +0000 | [diff] [blame] | 3077 | assert( TK_LT==OP_Lt ); |
| 3078 | assert( TK_LE==OP_Le ); |
| 3079 | assert( TK_GT==OP_Gt ); |
| 3080 | assert( TK_GE==OP_Ge ); |
| 3081 | assert( TK_EQ==OP_Eq ); |
| 3082 | assert( TK_NE==OP_Ne ); |
drh | c5499be | 2008-04-01 15:06:33 +0000 | [diff] [blame] | 3083 | testcase( op==TK_LT ); |
| 3084 | testcase( op==TK_LE ); |
| 3085 | testcase( op==TK_GT ); |
| 3086 | testcase( op==TK_GE ); |
| 3087 | testcase( op==TK_EQ ); |
| 3088 | testcase( op==TK_NE ); |
| 3089 | testcase( jumpIfNull==0 ); |
drh | da250ea | 2008-04-01 05:07:14 +0000 | [diff] [blame] | 3090 | codeCompareOperands(pParse, pExpr->pLeft, &r1, ®Free1, |
| 3091 | pExpr->pRight, &r2, ®Free2); |
drh | 3557335 | 2008-01-08 23:54:25 +0000 | [diff] [blame] | 3092 | codeCompare(pParse, pExpr->pLeft, pExpr->pRight, op, |
drh | 2dcef11 | 2008-01-12 19:03:48 +0000 | [diff] [blame] | 3093 | r1, r2, dest, jumpIfNull); |
drh | c5499be | 2008-04-01 15:06:33 +0000 | [diff] [blame] | 3094 | testcase( regFree1==0 ); |
| 3095 | testcase( regFree2==0 ); |
drh | cce7d17 | 2000-05-31 15:34:51 +0000 | [diff] [blame] | 3096 | break; |
| 3097 | } |
| 3098 | case TK_ISNULL: |
| 3099 | case TK_NOTNULL: { |
drh | f2bc013 | 2004-10-04 13:19:23 +0000 | [diff] [blame] | 3100 | assert( TK_ISNULL==OP_IsNull ); |
| 3101 | assert( TK_NOTNULL==OP_NotNull ); |
drh | c5499be | 2008-04-01 15:06:33 +0000 | [diff] [blame] | 3102 | testcase( op==TK_ISNULL ); |
| 3103 | testcase( op==TK_NOTNULL ); |
drh | 2dcef11 | 2008-01-12 19:03:48 +0000 | [diff] [blame] | 3104 | r1 = sqlite3ExprCodeTemp(pParse, pExpr->pLeft, ®Free1); |
| 3105 | sqlite3VdbeAddOp2(v, op, r1, dest); |
drh | c5499be | 2008-04-01 15:06:33 +0000 | [diff] [blame] | 3106 | testcase( regFree1==0 ); |
drh | cce7d17 | 2000-05-31 15:34:51 +0000 | [diff] [blame] | 3107 | break; |
| 3108 | } |
drh | fef5208 | 2000-06-06 01:50:43 +0000 | [diff] [blame] | 3109 | case TK_BETWEEN: { |
drh | 2dcef11 | 2008-01-12 19:03:48 +0000 | [diff] [blame] | 3110 | /* x BETWEEN y AND z |
danielk1977 | 0202b29 | 2004-06-09 09:55:16 +0000 | [diff] [blame] | 3111 | ** |
drh | 2dcef11 | 2008-01-12 19:03:48 +0000 | [diff] [blame] | 3112 | ** Is equivalent to |
| 3113 | ** |
| 3114 | ** x>=y AND x<=z |
| 3115 | ** |
| 3116 | ** Code it as such, taking care to do the common subexpression |
| 3117 | ** elementation of x. |
danielk1977 | 0202b29 | 2004-06-09 09:55:16 +0000 | [diff] [blame] | 3118 | */ |
drh | 2dcef11 | 2008-01-12 19:03:48 +0000 | [diff] [blame] | 3119 | Expr exprAnd; |
| 3120 | Expr compLeft; |
| 3121 | Expr compRight; |
| 3122 | Expr exprX; |
danielk1977 | 0202b29 | 2004-06-09 09:55:16 +0000 | [diff] [blame] | 3123 | |
drh | 2dcef11 | 2008-01-12 19:03:48 +0000 | [diff] [blame] | 3124 | exprX = *pExpr->pLeft; |
| 3125 | exprAnd.op = TK_AND; |
| 3126 | exprAnd.pLeft = &compLeft; |
| 3127 | exprAnd.pRight = &compRight; |
| 3128 | compLeft.op = TK_GE; |
| 3129 | compLeft.pLeft = &exprX; |
| 3130 | compLeft.pRight = pExpr->pList->a[0].pExpr; |
| 3131 | compRight.op = TK_LE; |
| 3132 | compRight.pLeft = &exprX; |
| 3133 | compRight.pRight = pExpr->pList->a[1].pExpr; |
| 3134 | exprX.iTable = sqlite3ExprCodeTemp(pParse, &exprX, ®Free1); |
drh | c5499be | 2008-04-01 15:06:33 +0000 | [diff] [blame] | 3135 | testcase( regFree1==0 ); |
drh | 2dcef11 | 2008-01-12 19:03:48 +0000 | [diff] [blame] | 3136 | exprX.op = TK_REGISTER; |
drh | c5499be | 2008-04-01 15:06:33 +0000 | [diff] [blame] | 3137 | testcase( jumpIfNull==0 ); |
drh | 2dcef11 | 2008-01-12 19:03:48 +0000 | [diff] [blame] | 3138 | sqlite3ExprIfTrue(pParse, &exprAnd, dest, jumpIfNull); |
drh | fef5208 | 2000-06-06 01:50:43 +0000 | [diff] [blame] | 3139 | break; |
| 3140 | } |
drh | cce7d17 | 2000-05-31 15:34:51 +0000 | [diff] [blame] | 3141 | default: { |
drh | 2dcef11 | 2008-01-12 19:03:48 +0000 | [diff] [blame] | 3142 | r1 = sqlite3ExprCodeTemp(pParse, pExpr, ®Free1); |
| 3143 | sqlite3VdbeAddOp3(v, OP_If, r1, dest, jumpIfNull!=0); |
drh | c5499be | 2008-04-01 15:06:33 +0000 | [diff] [blame] | 3144 | testcase( regFree1==0 ); |
| 3145 | testcase( jumpIfNull==0 ); |
drh | cce7d17 | 2000-05-31 15:34:51 +0000 | [diff] [blame] | 3146 | break; |
| 3147 | } |
| 3148 | } |
drh | 2dcef11 | 2008-01-12 19:03:48 +0000 | [diff] [blame] | 3149 | sqlite3ReleaseTempReg(pParse, regFree1); |
| 3150 | sqlite3ReleaseTempReg(pParse, regFree2); |
drh | cce7d17 | 2000-05-31 15:34:51 +0000 | [diff] [blame] | 3151 | } |
| 3152 | |
| 3153 | /* |
drh | 66b89c8 | 2000-11-28 20:47:17 +0000 | [diff] [blame] | 3154 | ** Generate code for a boolean expression such that a jump is made |
drh | cce7d17 | 2000-05-31 15:34:51 +0000 | [diff] [blame] | 3155 | ** to the label "dest" if the expression is false but execution |
| 3156 | ** continues straight thru if the expression is true. |
drh | f5905aa | 2002-05-26 20:54:33 +0000 | [diff] [blame] | 3157 | ** |
| 3158 | ** If the expression evaluates to NULL (neither true nor false) then |
drh | 3557335 | 2008-01-08 23:54:25 +0000 | [diff] [blame] | 3159 | ** jump if jumpIfNull is SQLITE_JUMPIFNULL or fall through if jumpIfNull |
| 3160 | ** is 0. |
drh | cce7d17 | 2000-05-31 15:34:51 +0000 | [diff] [blame] | 3161 | */ |
danielk1977 | 4adee20 | 2004-05-08 08:23:19 +0000 | [diff] [blame] | 3162 | void sqlite3ExprIfFalse(Parse *pParse, Expr *pExpr, int dest, int jumpIfNull){ |
drh | cce7d17 | 2000-05-31 15:34:51 +0000 | [diff] [blame] | 3163 | Vdbe *v = pParse->pVdbe; |
| 3164 | int op = 0; |
drh | 2dcef11 | 2008-01-12 19:03:48 +0000 | [diff] [blame] | 3165 | int regFree1 = 0; |
| 3166 | int regFree2 = 0; |
| 3167 | int r1, r2; |
| 3168 | |
drh | 3557335 | 2008-01-08 23:54:25 +0000 | [diff] [blame] | 3169 | assert( jumpIfNull==SQLITE_JUMPIFNULL || jumpIfNull==0 ); |
drh | daffd0e | 2001-04-11 14:28:42 +0000 | [diff] [blame] | 3170 | if( v==0 || pExpr==0 ) return; |
drh | f2bc013 | 2004-10-04 13:19:23 +0000 | [diff] [blame] | 3171 | |
| 3172 | /* The value of pExpr->op and op are related as follows: |
| 3173 | ** |
| 3174 | ** pExpr->op op |
| 3175 | ** --------- ---------- |
| 3176 | ** TK_ISNULL OP_NotNull |
| 3177 | ** TK_NOTNULL OP_IsNull |
| 3178 | ** TK_NE OP_Eq |
| 3179 | ** TK_EQ OP_Ne |
| 3180 | ** TK_GT OP_Le |
| 3181 | ** TK_LE OP_Gt |
| 3182 | ** TK_GE OP_Lt |
| 3183 | ** TK_LT OP_Ge |
| 3184 | ** |
| 3185 | ** For other values of pExpr->op, op is undefined and unused. |
| 3186 | ** The value of TK_ and OP_ constants are arranged such that we |
| 3187 | ** can compute the mapping above using the following expression. |
| 3188 | ** Assert()s verify that the computation is correct. |
| 3189 | */ |
| 3190 | op = ((pExpr->op+(TK_ISNULL&1))^1)-(TK_ISNULL&1); |
| 3191 | |
| 3192 | /* Verify correct alignment of TK_ and OP_ constants |
| 3193 | */ |
| 3194 | assert( pExpr->op!=TK_ISNULL || op==OP_NotNull ); |
| 3195 | assert( pExpr->op!=TK_NOTNULL || op==OP_IsNull ); |
| 3196 | assert( pExpr->op!=TK_NE || op==OP_Eq ); |
| 3197 | assert( pExpr->op!=TK_EQ || op==OP_Ne ); |
| 3198 | assert( pExpr->op!=TK_LT || op==OP_Ge ); |
| 3199 | assert( pExpr->op!=TK_LE || op==OP_Gt ); |
| 3200 | assert( pExpr->op!=TK_GT || op==OP_Le ); |
| 3201 | assert( pExpr->op!=TK_GE || op==OP_Lt ); |
| 3202 | |
drh | cce7d17 | 2000-05-31 15:34:51 +0000 | [diff] [blame] | 3203 | switch( pExpr->op ){ |
| 3204 | case TK_AND: { |
drh | c5499be | 2008-04-01 15:06:33 +0000 | [diff] [blame] | 3205 | testcase( jumpIfNull==0 ); |
| 3206 | testcase( pParse->disableColCache==0 ); |
danielk1977 | 4adee20 | 2004-05-08 08:23:19 +0000 | [diff] [blame] | 3207 | sqlite3ExprIfFalse(pParse, pExpr->pLeft, dest, jumpIfNull); |
drh | e55cbd7 | 2008-03-31 23:48:03 +0000 | [diff] [blame] | 3208 | pParse->disableColCache++; |
danielk1977 | 4adee20 | 2004-05-08 08:23:19 +0000 | [diff] [blame] | 3209 | sqlite3ExprIfFalse(pParse, pExpr->pRight, dest, jumpIfNull); |
drh | c5499be | 2008-04-01 15:06:33 +0000 | [diff] [blame] | 3210 | assert( pParse->disableColCache>0 ); |
drh | e55cbd7 | 2008-03-31 23:48:03 +0000 | [diff] [blame] | 3211 | pParse->disableColCache--; |
drh | cce7d17 | 2000-05-31 15:34:51 +0000 | [diff] [blame] | 3212 | break; |
| 3213 | } |
| 3214 | case TK_OR: { |
danielk1977 | 4adee20 | 2004-05-08 08:23:19 +0000 | [diff] [blame] | 3215 | int d2 = sqlite3VdbeMakeLabel(v); |
drh | c5499be | 2008-04-01 15:06:33 +0000 | [diff] [blame] | 3216 | testcase( jumpIfNull==0 ); |
| 3217 | testcase( pParse->disableColCache==0 ); |
drh | 3557335 | 2008-01-08 23:54:25 +0000 | [diff] [blame] | 3218 | sqlite3ExprIfTrue(pParse, pExpr->pLeft, d2, jumpIfNull^SQLITE_JUMPIFNULL); |
drh | e55cbd7 | 2008-03-31 23:48:03 +0000 | [diff] [blame] | 3219 | pParse->disableColCache++; |
danielk1977 | 4adee20 | 2004-05-08 08:23:19 +0000 | [diff] [blame] | 3220 | sqlite3ExprIfFalse(pParse, pExpr->pRight, dest, jumpIfNull); |
drh | c5499be | 2008-04-01 15:06:33 +0000 | [diff] [blame] | 3221 | assert( pParse->disableColCache>0 ); |
drh | e55cbd7 | 2008-03-31 23:48:03 +0000 | [diff] [blame] | 3222 | pParse->disableColCache--; |
danielk1977 | 4adee20 | 2004-05-08 08:23:19 +0000 | [diff] [blame] | 3223 | sqlite3VdbeResolveLabel(v, d2); |
drh | cce7d17 | 2000-05-31 15:34:51 +0000 | [diff] [blame] | 3224 | break; |
| 3225 | } |
| 3226 | case TK_NOT: { |
danielk1977 | 4adee20 | 2004-05-08 08:23:19 +0000 | [diff] [blame] | 3227 | sqlite3ExprIfTrue(pParse, pExpr->pLeft, dest, jumpIfNull); |
drh | cce7d17 | 2000-05-31 15:34:51 +0000 | [diff] [blame] | 3228 | break; |
| 3229 | } |
| 3230 | case TK_LT: |
| 3231 | case TK_LE: |
| 3232 | case TK_GT: |
| 3233 | case TK_GE: |
| 3234 | case TK_NE: |
| 3235 | case TK_EQ: { |
drh | c5499be | 2008-04-01 15:06:33 +0000 | [diff] [blame] | 3236 | testcase( op==TK_LT ); |
| 3237 | testcase( op==TK_LE ); |
| 3238 | testcase( op==TK_GT ); |
| 3239 | testcase( op==TK_GE ); |
| 3240 | testcase( op==TK_EQ ); |
| 3241 | testcase( op==TK_NE ); |
| 3242 | testcase( jumpIfNull==0 ); |
drh | da250ea | 2008-04-01 05:07:14 +0000 | [diff] [blame] | 3243 | codeCompareOperands(pParse, pExpr->pLeft, &r1, ®Free1, |
| 3244 | pExpr->pRight, &r2, ®Free2); |
drh | 3557335 | 2008-01-08 23:54:25 +0000 | [diff] [blame] | 3245 | codeCompare(pParse, pExpr->pLeft, pExpr->pRight, op, |
drh | 2dcef11 | 2008-01-12 19:03:48 +0000 | [diff] [blame] | 3246 | r1, r2, dest, jumpIfNull); |
drh | c5499be | 2008-04-01 15:06:33 +0000 | [diff] [blame] | 3247 | testcase( regFree1==0 ); |
| 3248 | testcase( regFree2==0 ); |
drh | cce7d17 | 2000-05-31 15:34:51 +0000 | [diff] [blame] | 3249 | break; |
| 3250 | } |
drh | cce7d17 | 2000-05-31 15:34:51 +0000 | [diff] [blame] | 3251 | case TK_ISNULL: |
| 3252 | case TK_NOTNULL: { |
drh | c5499be | 2008-04-01 15:06:33 +0000 | [diff] [blame] | 3253 | testcase( op==TK_ISNULL ); |
| 3254 | testcase( op==TK_NOTNULL ); |
drh | 2dcef11 | 2008-01-12 19:03:48 +0000 | [diff] [blame] | 3255 | r1 = sqlite3ExprCodeTemp(pParse, pExpr->pLeft, ®Free1); |
| 3256 | sqlite3VdbeAddOp2(v, op, r1, dest); |
drh | c5499be | 2008-04-01 15:06:33 +0000 | [diff] [blame] | 3257 | testcase( regFree1==0 ); |
drh | cce7d17 | 2000-05-31 15:34:51 +0000 | [diff] [blame] | 3258 | break; |
| 3259 | } |
drh | fef5208 | 2000-06-06 01:50:43 +0000 | [diff] [blame] | 3260 | case TK_BETWEEN: { |
drh | 2dcef11 | 2008-01-12 19:03:48 +0000 | [diff] [blame] | 3261 | /* x BETWEEN y AND z |
danielk1977 | 0202b29 | 2004-06-09 09:55:16 +0000 | [diff] [blame] | 3262 | ** |
drh | 2dcef11 | 2008-01-12 19:03:48 +0000 | [diff] [blame] | 3263 | ** Is equivalent to |
| 3264 | ** |
| 3265 | ** x>=y AND x<=z |
| 3266 | ** |
| 3267 | ** Code it as such, taking care to do the common subexpression |
| 3268 | ** elementation of x. |
danielk1977 | 0202b29 | 2004-06-09 09:55:16 +0000 | [diff] [blame] | 3269 | */ |
drh | 2dcef11 | 2008-01-12 19:03:48 +0000 | [diff] [blame] | 3270 | Expr exprAnd; |
| 3271 | Expr compLeft; |
| 3272 | Expr compRight; |
| 3273 | Expr exprX; |
drh | be5c89a | 2004-07-26 00:31:09 +0000 | [diff] [blame] | 3274 | |
drh | 2dcef11 | 2008-01-12 19:03:48 +0000 | [diff] [blame] | 3275 | exprX = *pExpr->pLeft; |
| 3276 | exprAnd.op = TK_AND; |
| 3277 | exprAnd.pLeft = &compLeft; |
| 3278 | exprAnd.pRight = &compRight; |
| 3279 | compLeft.op = TK_GE; |
| 3280 | compLeft.pLeft = &exprX; |
| 3281 | compLeft.pRight = pExpr->pList->a[0].pExpr; |
| 3282 | compRight.op = TK_LE; |
| 3283 | compRight.pLeft = &exprX; |
| 3284 | compRight.pRight = pExpr->pList->a[1].pExpr; |
| 3285 | exprX.iTable = sqlite3ExprCodeTemp(pParse, &exprX, ®Free1); |
drh | c5499be | 2008-04-01 15:06:33 +0000 | [diff] [blame] | 3286 | testcase( regFree1==0 ); |
drh | 2dcef11 | 2008-01-12 19:03:48 +0000 | [diff] [blame] | 3287 | exprX.op = TK_REGISTER; |
drh | c5499be | 2008-04-01 15:06:33 +0000 | [diff] [blame] | 3288 | testcase( jumpIfNull==0 ); |
drh | 2dcef11 | 2008-01-12 19:03:48 +0000 | [diff] [blame] | 3289 | sqlite3ExprIfFalse(pParse, &exprAnd, dest, jumpIfNull); |
drh | fef5208 | 2000-06-06 01:50:43 +0000 | [diff] [blame] | 3290 | break; |
| 3291 | } |
drh | cce7d17 | 2000-05-31 15:34:51 +0000 | [diff] [blame] | 3292 | default: { |
drh | 2dcef11 | 2008-01-12 19:03:48 +0000 | [diff] [blame] | 3293 | r1 = sqlite3ExprCodeTemp(pParse, pExpr, ®Free1); |
| 3294 | sqlite3VdbeAddOp3(v, OP_IfNot, r1, dest, jumpIfNull!=0); |
drh | c5499be | 2008-04-01 15:06:33 +0000 | [diff] [blame] | 3295 | testcase( regFree1==0 ); |
| 3296 | testcase( jumpIfNull==0 ); |
drh | cce7d17 | 2000-05-31 15:34:51 +0000 | [diff] [blame] | 3297 | break; |
| 3298 | } |
| 3299 | } |
drh | 2dcef11 | 2008-01-12 19:03:48 +0000 | [diff] [blame] | 3300 | sqlite3ReleaseTempReg(pParse, regFree1); |
| 3301 | sqlite3ReleaseTempReg(pParse, regFree2); |
drh | cce7d17 | 2000-05-31 15:34:51 +0000 | [diff] [blame] | 3302 | } |
drh | 2282792 | 2000-06-06 17:27:05 +0000 | [diff] [blame] | 3303 | |
| 3304 | /* |
| 3305 | ** Do a deep comparison of two expression trees. Return TRUE (non-zero) |
| 3306 | ** if they are identical and return FALSE if they differ in any way. |
drh | d40aab0 | 2007-02-24 15:29:03 +0000 | [diff] [blame] | 3307 | ** |
| 3308 | ** Sometimes this routine will return FALSE even if the two expressions |
| 3309 | ** really are equivalent. If we cannot prove that the expressions are |
| 3310 | ** identical, we return FALSE just to be safe. So if this routine |
| 3311 | ** returns false, then you do not really know for certain if the two |
| 3312 | ** expressions are the same. But if you get a TRUE return, then you |
| 3313 | ** can be sure the expressions are the same. In the places where |
| 3314 | ** this routine is used, it does not hurt to get an extra FALSE - that |
| 3315 | ** just might result in some slightly slower code. But returning |
| 3316 | ** an incorrect TRUE could lead to a malfunction. |
drh | 2282792 | 2000-06-06 17:27:05 +0000 | [diff] [blame] | 3317 | */ |
danielk1977 | 4adee20 | 2004-05-08 08:23:19 +0000 | [diff] [blame] | 3318 | int sqlite3ExprCompare(Expr *pA, Expr *pB){ |
drh | 2282792 | 2000-06-06 17:27:05 +0000 | [diff] [blame] | 3319 | int i; |
danielk1977 | 4b202ae | 2006-01-23 05:50:58 +0000 | [diff] [blame] | 3320 | if( pA==0||pB==0 ){ |
| 3321 | return pB==pA; |
drh | 2282792 | 2000-06-06 17:27:05 +0000 | [diff] [blame] | 3322 | } |
| 3323 | if( pA->op!=pB->op ) return 0; |
drh | fd35797 | 2005-09-09 01:33:19 +0000 | [diff] [blame] | 3324 | if( (pA->flags & EP_Distinct)!=(pB->flags & EP_Distinct) ) return 0; |
danielk1977 | 4adee20 | 2004-05-08 08:23:19 +0000 | [diff] [blame] | 3325 | if( !sqlite3ExprCompare(pA->pLeft, pB->pLeft) ) return 0; |
| 3326 | if( !sqlite3ExprCompare(pA->pRight, pB->pRight) ) return 0; |
drh | 2282792 | 2000-06-06 17:27:05 +0000 | [diff] [blame] | 3327 | if( pA->pList ){ |
| 3328 | if( pB->pList==0 ) return 0; |
| 3329 | if( pA->pList->nExpr!=pB->pList->nExpr ) return 0; |
| 3330 | for(i=0; i<pA->pList->nExpr; i++){ |
danielk1977 | 4adee20 | 2004-05-08 08:23:19 +0000 | [diff] [blame] | 3331 | if( !sqlite3ExprCompare(pA->pList->a[i].pExpr, pB->pList->a[i].pExpr) ){ |
drh | 2282792 | 2000-06-06 17:27:05 +0000 | [diff] [blame] | 3332 | return 0; |
| 3333 | } |
| 3334 | } |
| 3335 | }else if( pB->pList ){ |
| 3336 | return 0; |
| 3337 | } |
| 3338 | if( pA->pSelect || pB->pSelect ) return 0; |
drh | 2f2c01e | 2002-07-02 13:05:04 +0000 | [diff] [blame] | 3339 | if( pA->iTable!=pB->iTable || pA->iColumn!=pB->iColumn ) return 0; |
drh | dd73521 | 2007-02-24 13:53:05 +0000 | [diff] [blame] | 3340 | if( pA->op!=TK_COLUMN && pA->token.z ){ |
drh | 2282792 | 2000-06-06 17:27:05 +0000 | [diff] [blame] | 3341 | if( pB->token.z==0 ) return 0; |
drh | 6977fea | 2002-10-22 23:38:04 +0000 | [diff] [blame] | 3342 | if( pB->token.n!=pA->token.n ) return 0; |
drh | 2646da7 | 2005-12-09 20:02:05 +0000 | [diff] [blame] | 3343 | if( sqlite3StrNICmp((char*)pA->token.z,(char*)pB->token.z,pB->token.n)!=0 ){ |
| 3344 | return 0; |
| 3345 | } |
drh | 2282792 | 2000-06-06 17:27:05 +0000 | [diff] [blame] | 3346 | } |
| 3347 | return 1; |
| 3348 | } |
| 3349 | |
drh | 1344989 | 2005-09-07 21:22:45 +0000 | [diff] [blame] | 3350 | |
drh | 2282792 | 2000-06-06 17:27:05 +0000 | [diff] [blame] | 3351 | /* |
drh | 1344989 | 2005-09-07 21:22:45 +0000 | [diff] [blame] | 3352 | ** Add a new element to the pAggInfo->aCol[] array. Return the index of |
| 3353 | ** the new element. Return a negative number if malloc fails. |
drh | 2282792 | 2000-06-06 17:27:05 +0000 | [diff] [blame] | 3354 | */ |
drh | 1743575 | 2007-08-16 04:30:38 +0000 | [diff] [blame] | 3355 | static int addAggInfoColumn(sqlite3 *db, AggInfo *pInfo){ |
drh | 1344989 | 2005-09-07 21:22:45 +0000 | [diff] [blame] | 3356 | int i; |
drh | cf64372 | 2007-03-27 13:36:37 +0000 | [diff] [blame] | 3357 | pInfo->aCol = sqlite3ArrayAllocate( |
drh | 1743575 | 2007-08-16 04:30:38 +0000 | [diff] [blame] | 3358 | db, |
drh | cf64372 | 2007-03-27 13:36:37 +0000 | [diff] [blame] | 3359 | pInfo->aCol, |
| 3360 | sizeof(pInfo->aCol[0]), |
| 3361 | 3, |
| 3362 | &pInfo->nColumn, |
| 3363 | &pInfo->nColumnAlloc, |
| 3364 | &i |
| 3365 | ); |
drh | 1344989 | 2005-09-07 21:22:45 +0000 | [diff] [blame] | 3366 | return i; |
| 3367 | } |
| 3368 | |
| 3369 | /* |
| 3370 | ** Add a new element to the pAggInfo->aFunc[] array. Return the index of |
| 3371 | ** the new element. Return a negative number if malloc fails. |
| 3372 | */ |
drh | 1743575 | 2007-08-16 04:30:38 +0000 | [diff] [blame] | 3373 | static int addAggInfoFunc(sqlite3 *db, AggInfo *pInfo){ |
drh | 1344989 | 2005-09-07 21:22:45 +0000 | [diff] [blame] | 3374 | int i; |
drh | cf64372 | 2007-03-27 13:36:37 +0000 | [diff] [blame] | 3375 | pInfo->aFunc = sqlite3ArrayAllocate( |
drh | 1743575 | 2007-08-16 04:30:38 +0000 | [diff] [blame] | 3376 | db, |
drh | cf64372 | 2007-03-27 13:36:37 +0000 | [diff] [blame] | 3377 | pInfo->aFunc, |
| 3378 | sizeof(pInfo->aFunc[0]), |
| 3379 | 3, |
| 3380 | &pInfo->nFunc, |
| 3381 | &pInfo->nFuncAlloc, |
| 3382 | &i |
| 3383 | ); |
drh | 1344989 | 2005-09-07 21:22:45 +0000 | [diff] [blame] | 3384 | return i; |
| 3385 | } |
drh | 2282792 | 2000-06-06 17:27:05 +0000 | [diff] [blame] | 3386 | |
| 3387 | /* |
drh | 626a879 | 2005-01-17 22:08:19 +0000 | [diff] [blame] | 3388 | ** This is an xFunc for walkExprTree() used to implement |
| 3389 | ** sqlite3ExprAnalyzeAggregates(). See sqlite3ExprAnalyzeAggregates |
| 3390 | ** for additional information. |
drh | 2282792 | 2000-06-06 17:27:05 +0000 | [diff] [blame] | 3391 | ** |
drh | 626a879 | 2005-01-17 22:08:19 +0000 | [diff] [blame] | 3392 | ** This routine analyzes the aggregate function at pExpr. |
drh | 2282792 | 2000-06-06 17:27:05 +0000 | [diff] [blame] | 3393 | */ |
drh | 626a879 | 2005-01-17 22:08:19 +0000 | [diff] [blame] | 3394 | static int analyzeAggregate(void *pArg, Expr *pExpr){ |
drh | 2282792 | 2000-06-06 17:27:05 +0000 | [diff] [blame] | 3395 | int i; |
danielk1977 | a58fdfb | 2005-02-08 07:50:40 +0000 | [diff] [blame] | 3396 | NameContext *pNC = (NameContext *)pArg; |
| 3397 | Parse *pParse = pNC->pParse; |
| 3398 | SrcList *pSrcList = pNC->pSrcList; |
drh | 1344989 | 2005-09-07 21:22:45 +0000 | [diff] [blame] | 3399 | AggInfo *pAggInfo = pNC->pAggInfo; |
drh | 2282792 | 2000-06-06 17:27:05 +0000 | [diff] [blame] | 3400 | |
drh | 2282792 | 2000-06-06 17:27:05 +0000 | [diff] [blame] | 3401 | switch( pExpr->op ){ |
drh | 89c69d0 | 2007-01-04 01:20:28 +0000 | [diff] [blame] | 3402 | case TK_AGG_COLUMN: |
drh | 967e8b7 | 2000-06-21 13:59:10 +0000 | [diff] [blame] | 3403 | case TK_COLUMN: { |
drh | 1344989 | 2005-09-07 21:22:45 +0000 | [diff] [blame] | 3404 | /* Check to see if the column is in one of the tables in the FROM |
| 3405 | ** clause of the aggregate query */ |
| 3406 | if( pSrcList ){ |
| 3407 | struct SrcList_item *pItem = pSrcList->a; |
| 3408 | for(i=0; i<pSrcList->nSrc; i++, pItem++){ |
| 3409 | struct AggInfo_col *pCol; |
| 3410 | if( pExpr->iTable==pItem->iCursor ){ |
| 3411 | /* If we reach this point, it means that pExpr refers to a table |
| 3412 | ** that is in the FROM clause of the aggregate query. |
| 3413 | ** |
| 3414 | ** Make an entry for the column in pAggInfo->aCol[] if there |
| 3415 | ** is not an entry there already. |
| 3416 | */ |
drh | 7f906d6 | 2007-03-12 23:48:52 +0000 | [diff] [blame] | 3417 | int k; |
drh | 1344989 | 2005-09-07 21:22:45 +0000 | [diff] [blame] | 3418 | pCol = pAggInfo->aCol; |
drh | 7f906d6 | 2007-03-12 23:48:52 +0000 | [diff] [blame] | 3419 | for(k=0; k<pAggInfo->nColumn; k++, pCol++){ |
drh | 1344989 | 2005-09-07 21:22:45 +0000 | [diff] [blame] | 3420 | if( pCol->iTable==pExpr->iTable && |
| 3421 | pCol->iColumn==pExpr->iColumn ){ |
| 3422 | break; |
| 3423 | } |
danielk1977 | a58fdfb | 2005-02-08 07:50:40 +0000 | [diff] [blame] | 3424 | } |
danielk1977 | 1e53695 | 2007-08-16 10:09:01 +0000 | [diff] [blame] | 3425 | if( (k>=pAggInfo->nColumn) |
| 3426 | && (k = addAggInfoColumn(pParse->db, pAggInfo))>=0 |
| 3427 | ){ |
drh | 7f906d6 | 2007-03-12 23:48:52 +0000 | [diff] [blame] | 3428 | pCol = &pAggInfo->aCol[k]; |
danielk1977 | 0817d0d | 2007-02-14 09:19:36 +0000 | [diff] [blame] | 3429 | pCol->pTab = pExpr->pTab; |
drh | 1344989 | 2005-09-07 21:22:45 +0000 | [diff] [blame] | 3430 | pCol->iTable = pExpr->iTable; |
| 3431 | pCol->iColumn = pExpr->iColumn; |
drh | 0a07c10 | 2008-01-03 18:03:08 +0000 | [diff] [blame] | 3432 | pCol->iMem = ++pParse->nMem; |
drh | 1344989 | 2005-09-07 21:22:45 +0000 | [diff] [blame] | 3433 | pCol->iSorterColumn = -1; |
drh | 5774b80 | 2005-09-07 22:48:16 +0000 | [diff] [blame] | 3434 | pCol->pExpr = pExpr; |
drh | 1344989 | 2005-09-07 21:22:45 +0000 | [diff] [blame] | 3435 | if( pAggInfo->pGroupBy ){ |
| 3436 | int j, n; |
| 3437 | ExprList *pGB = pAggInfo->pGroupBy; |
| 3438 | struct ExprList_item *pTerm = pGB->a; |
| 3439 | n = pGB->nExpr; |
| 3440 | for(j=0; j<n; j++, pTerm++){ |
| 3441 | Expr *pE = pTerm->pExpr; |
| 3442 | if( pE->op==TK_COLUMN && pE->iTable==pExpr->iTable && |
| 3443 | pE->iColumn==pExpr->iColumn ){ |
| 3444 | pCol->iSorterColumn = j; |
| 3445 | break; |
| 3446 | } |
| 3447 | } |
| 3448 | } |
| 3449 | if( pCol->iSorterColumn<0 ){ |
| 3450 | pCol->iSorterColumn = pAggInfo->nSortingColumn++; |
| 3451 | } |
| 3452 | } |
| 3453 | /* There is now an entry for pExpr in pAggInfo->aCol[] (either |
| 3454 | ** because it was there before or because we just created it). |
| 3455 | ** Convert the pExpr to be a TK_AGG_COLUMN referring to that |
| 3456 | ** pAggInfo->aCol[] entry. |
| 3457 | */ |
| 3458 | pExpr->pAggInfo = pAggInfo; |
| 3459 | pExpr->op = TK_AGG_COLUMN; |
drh | 7f906d6 | 2007-03-12 23:48:52 +0000 | [diff] [blame] | 3460 | pExpr->iAgg = k; |
drh | 1344989 | 2005-09-07 21:22:45 +0000 | [diff] [blame] | 3461 | break; |
| 3462 | } /* endif pExpr->iTable==pItem->iCursor */ |
| 3463 | } /* end loop over pSrcList */ |
drh | 2282792 | 2000-06-06 17:27:05 +0000 | [diff] [blame] | 3464 | } |
drh | 626a879 | 2005-01-17 22:08:19 +0000 | [diff] [blame] | 3465 | return 1; |
drh | 2282792 | 2000-06-06 17:27:05 +0000 | [diff] [blame] | 3466 | } |
| 3467 | case TK_AGG_FUNCTION: { |
drh | 1344989 | 2005-09-07 21:22:45 +0000 | [diff] [blame] | 3468 | /* The pNC->nDepth==0 test causes aggregate functions in subqueries |
| 3469 | ** to be ignored */ |
danielk1977 | a58fdfb | 2005-02-08 07:50:40 +0000 | [diff] [blame] | 3470 | if( pNC->nDepth==0 ){ |
drh | 1344989 | 2005-09-07 21:22:45 +0000 | [diff] [blame] | 3471 | /* Check to see if pExpr is a duplicate of another aggregate |
| 3472 | ** function that is already in the pAggInfo structure |
| 3473 | */ |
| 3474 | struct AggInfo_func *pItem = pAggInfo->aFunc; |
| 3475 | for(i=0; i<pAggInfo->nFunc; i++, pItem++){ |
| 3476 | if( sqlite3ExprCompare(pItem->pExpr, pExpr) ){ |
danielk1977 | a58fdfb | 2005-02-08 07:50:40 +0000 | [diff] [blame] | 3477 | break; |
| 3478 | } |
drh | 2282792 | 2000-06-06 17:27:05 +0000 | [diff] [blame] | 3479 | } |
drh | 1344989 | 2005-09-07 21:22:45 +0000 | [diff] [blame] | 3480 | if( i>=pAggInfo->nFunc ){ |
| 3481 | /* pExpr is original. Make a new entry in pAggInfo->aFunc[] |
| 3482 | */ |
danielk1977 | 14db266 | 2006-01-09 16:12:04 +0000 | [diff] [blame] | 3483 | u8 enc = ENC(pParse->db); |
danielk1977 | 1e53695 | 2007-08-16 10:09:01 +0000 | [diff] [blame] | 3484 | i = addAggInfoFunc(pParse->db, pAggInfo); |
drh | 1344989 | 2005-09-07 21:22:45 +0000 | [diff] [blame] | 3485 | if( i>=0 ){ |
| 3486 | pItem = &pAggInfo->aFunc[i]; |
| 3487 | pItem->pExpr = pExpr; |
drh | 0a07c10 | 2008-01-03 18:03:08 +0000 | [diff] [blame] | 3488 | pItem->iMem = ++pParse->nMem; |
drh | 1344989 | 2005-09-07 21:22:45 +0000 | [diff] [blame] | 3489 | pItem->pFunc = sqlite3FindFunction(pParse->db, |
drh | 2646da7 | 2005-12-09 20:02:05 +0000 | [diff] [blame] | 3490 | (char*)pExpr->token.z, pExpr->token.n, |
drh | 1344989 | 2005-09-07 21:22:45 +0000 | [diff] [blame] | 3491 | pExpr->pList ? pExpr->pList->nExpr : 0, enc, 0); |
drh | fd35797 | 2005-09-09 01:33:19 +0000 | [diff] [blame] | 3492 | if( pExpr->flags & EP_Distinct ){ |
| 3493 | pItem->iDistinct = pParse->nTab++; |
| 3494 | }else{ |
| 3495 | pItem->iDistinct = -1; |
| 3496 | } |
drh | 1344989 | 2005-09-07 21:22:45 +0000 | [diff] [blame] | 3497 | } |
danielk1977 | a58fdfb | 2005-02-08 07:50:40 +0000 | [diff] [blame] | 3498 | } |
drh | 1344989 | 2005-09-07 21:22:45 +0000 | [diff] [blame] | 3499 | /* Make pExpr point to the appropriate pAggInfo->aFunc[] entry |
| 3500 | */ |
danielk1977 | a58fdfb | 2005-02-08 07:50:40 +0000 | [diff] [blame] | 3501 | pExpr->iAgg = i; |
drh | 1344989 | 2005-09-07 21:22:45 +0000 | [diff] [blame] | 3502 | pExpr->pAggInfo = pAggInfo; |
danielk1977 | a58fdfb | 2005-02-08 07:50:40 +0000 | [diff] [blame] | 3503 | return 1; |
drh | 2282792 | 2000-06-06 17:27:05 +0000 | [diff] [blame] | 3504 | } |
drh | 2282792 | 2000-06-06 17:27:05 +0000 | [diff] [blame] | 3505 | } |
| 3506 | } |
drh | 1344989 | 2005-09-07 21:22:45 +0000 | [diff] [blame] | 3507 | |
| 3508 | /* Recursively walk subqueries looking for TK_COLUMN nodes that need |
| 3509 | ** to be changed to TK_AGG_COLUMN. But increment nDepth so that |
| 3510 | ** TK_AGG_FUNCTION nodes in subqueries will be unchanged. |
| 3511 | */ |
danielk1977 | a58fdfb | 2005-02-08 07:50:40 +0000 | [diff] [blame] | 3512 | if( pExpr->pSelect ){ |
| 3513 | pNC->nDepth++; |
| 3514 | walkSelectExpr(pExpr->pSelect, analyzeAggregate, pNC); |
| 3515 | pNC->nDepth--; |
| 3516 | } |
drh | 626a879 | 2005-01-17 22:08:19 +0000 | [diff] [blame] | 3517 | return 0; |
| 3518 | } |
| 3519 | |
| 3520 | /* |
| 3521 | ** Analyze the given expression looking for aggregate functions and |
| 3522 | ** for variables that need to be added to the pParse->aAgg[] array. |
| 3523 | ** Make additional entries to the pParse->aAgg[] array as necessary. |
| 3524 | ** |
| 3525 | ** This routine should only be called after the expression has been |
| 3526 | ** analyzed by sqlite3ExprResolveNames(). |
drh | 626a879 | 2005-01-17 22:08:19 +0000 | [diff] [blame] | 3527 | */ |
drh | d2b3e23 | 2008-01-23 14:51:49 +0000 | [diff] [blame] | 3528 | void sqlite3ExprAnalyzeAggregates(NameContext *pNC, Expr *pExpr){ |
danielk1977 | a58fdfb | 2005-02-08 07:50:40 +0000 | [diff] [blame] | 3529 | walkExprTree(pExpr, analyzeAggregate, pNC); |
drh | 2282792 | 2000-06-06 17:27:05 +0000 | [diff] [blame] | 3530 | } |
drh | 5d9a4af | 2005-08-30 00:54:01 +0000 | [diff] [blame] | 3531 | |
| 3532 | /* |
| 3533 | ** Call sqlite3ExprAnalyzeAggregates() for every expression in an |
| 3534 | ** expression list. Return the number of errors. |
| 3535 | ** |
| 3536 | ** If an error is found, the analysis is cut short. |
| 3537 | */ |
drh | d2b3e23 | 2008-01-23 14:51:49 +0000 | [diff] [blame] | 3538 | void sqlite3ExprAnalyzeAggList(NameContext *pNC, ExprList *pList){ |
drh | 5d9a4af | 2005-08-30 00:54:01 +0000 | [diff] [blame] | 3539 | struct ExprList_item *pItem; |
| 3540 | int i; |
drh | 5d9a4af | 2005-08-30 00:54:01 +0000 | [diff] [blame] | 3541 | if( pList ){ |
drh | d2b3e23 | 2008-01-23 14:51:49 +0000 | [diff] [blame] | 3542 | for(pItem=pList->a, i=0; i<pList->nExpr; i++, pItem++){ |
| 3543 | sqlite3ExprAnalyzeAggregates(pNC, pItem->pExpr); |
drh | 5d9a4af | 2005-08-30 00:54:01 +0000 | [diff] [blame] | 3544 | } |
| 3545 | } |
drh | 5d9a4af | 2005-08-30 00:54:01 +0000 | [diff] [blame] | 3546 | } |
drh | 892d317 | 2008-01-10 03:46:36 +0000 | [diff] [blame] | 3547 | |
| 3548 | /* |
| 3549 | ** Allocate or deallocate temporary use registers during code generation. |
| 3550 | */ |
| 3551 | int sqlite3GetTempReg(Parse *pParse){ |
drh | e55cbd7 | 2008-03-31 23:48:03 +0000 | [diff] [blame] | 3552 | int i, r; |
| 3553 | if( pParse->nTempReg==0 ){ |
drh | 892d317 | 2008-01-10 03:46:36 +0000 | [diff] [blame] | 3554 | return ++pParse->nMem; |
| 3555 | } |
drh | e55cbd7 | 2008-03-31 23:48:03 +0000 | [diff] [blame] | 3556 | for(i=0; i<pParse->nTempReg; i++){ |
| 3557 | r = pParse->aTempReg[i]; |
| 3558 | if( usedAsColumnCache(pParse, r, r) ) continue; |
| 3559 | } |
| 3560 | if( i>=pParse->nTempReg ){ |
| 3561 | return ++pParse->nMem; |
| 3562 | } |
| 3563 | while( i<pParse->nTempReg-1 ){ |
| 3564 | pParse->aTempReg[i] = pParse->aTempReg[i+1]; |
| 3565 | } |
| 3566 | pParse->nTempReg--; |
| 3567 | return r; |
drh | 892d317 | 2008-01-10 03:46:36 +0000 | [diff] [blame] | 3568 | } |
| 3569 | void sqlite3ReleaseTempReg(Parse *pParse, int iReg){ |
drh | 2dcef11 | 2008-01-12 19:03:48 +0000 | [diff] [blame] | 3570 | if( iReg && pParse->nTempReg<ArraySize(pParse->aTempReg) ){ |
drh | 892d317 | 2008-01-10 03:46:36 +0000 | [diff] [blame] | 3571 | pParse->aTempReg[pParse->nTempReg++] = iReg; |
| 3572 | } |
| 3573 | } |
| 3574 | |
| 3575 | /* |
| 3576 | ** Allocate or deallocate a block of nReg consecutive registers |
| 3577 | */ |
| 3578 | int sqlite3GetTempRange(Parse *pParse, int nReg){ |
drh | e55cbd7 | 2008-03-31 23:48:03 +0000 | [diff] [blame] | 3579 | int i, n; |
| 3580 | i = pParse->iRangeReg; |
| 3581 | n = pParse->nRangeReg; |
| 3582 | if( nReg<=n && !usedAsColumnCache(pParse, i, i+n-1) ){ |
drh | 892d317 | 2008-01-10 03:46:36 +0000 | [diff] [blame] | 3583 | pParse->iRangeReg += nReg; |
| 3584 | pParse->nRangeReg -= nReg; |
| 3585 | }else{ |
| 3586 | i = pParse->nMem+1; |
| 3587 | pParse->nMem += nReg; |
| 3588 | } |
| 3589 | return i; |
| 3590 | } |
| 3591 | void sqlite3ReleaseTempRange(Parse *pParse, int iReg, int nReg){ |
| 3592 | if( nReg>pParse->nRangeReg ){ |
| 3593 | pParse->nRangeReg = nReg; |
| 3594 | pParse->iRangeReg = iReg; |
| 3595 | } |
| 3596 | } |