drh | 7d10d5a | 2008-08-20 16:35:10 | [diff] [blame] | 1 | /* |
| 2 | ** 2008 August 18 |
| 3 | ** |
| 4 | ** The author disclaims copyright to this source code. In place of |
| 5 | ** a legal notice, here is a blessing: |
| 6 | ** |
| 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. |
| 10 | ** |
| 11 | ************************************************************************* |
| 12 | ** |
| 13 | ** This file contains routines used for walking the parser tree and |
| 14 | ** resolve all identifiers by associating them with a particular |
| 15 | ** table and column. |
drh | 7d10d5a | 2008-08-20 16:35:10 | [diff] [blame] | 16 | */ |
| 17 | #include "sqliteInt.h" |
drh | 7d10d5a | 2008-08-20 16:35:10 | [diff] [blame] | 18 | |
| 19 | /* |
drh | ec43d80 | 2020-06-29 20:20:40 | [diff] [blame] | 20 | ** Magic table number to mean the EXCLUDED table in an UPSERT statement. |
| 21 | */ |
| 22 | #define EXCLUDED_TABLE_NUMBER 2 |
| 23 | |
| 24 | /* |
drh | ed551b9 | 2012-08-23 19:46:11 | [diff] [blame] | 25 | ** Walk the expression tree pExpr and increase the aggregate function |
| 26 | ** depth (the Expr.op2 field) by N on every TK_AGG_FUNCTION node. |
| 27 | ** This needs to occur when copying a TK_AGG_FUNCTION node from an |
| 28 | ** outer query into an inner subquery. |
| 29 | ** |
| 30 | ** incrAggFunctionDepth(pExpr,n) is the main routine. incrAggDepth(..) |
| 31 | ** is a helper function - a callback for the tree walker. |
drh | c37577b | 2020-05-24 03:38:37 | [diff] [blame] | 32 | ** |
| 33 | ** See also the sqlite3WindowExtraAggFuncDepth() routine in window.c |
drh | ed551b9 | 2012-08-23 19:46:11 | [diff] [blame] | 34 | */ |
| 35 | static int incrAggDepth(Walker *pWalker, Expr *pExpr){ |
drh | 059b2d5 | 2014-10-24 19:28:09 | [diff] [blame] | 36 | if( pExpr->op==TK_AGG_FUNCTION ) pExpr->op2 += pWalker->u.n; |
drh | ed551b9 | 2012-08-23 19:46:11 | [diff] [blame] | 37 | return WRC_Continue; |
| 38 | } |
| 39 | static void incrAggFunctionDepth(Expr *pExpr, int N){ |
| 40 | if( N>0 ){ |
| 41 | Walker w; |
| 42 | memset(&w, 0, sizeof(w)); |
| 43 | w.xExprCallback = incrAggDepth; |
drh | 059b2d5 | 2014-10-24 19:28:09 | [diff] [blame] | 44 | w.u.n = N; |
drh | ed551b9 | 2012-08-23 19:46:11 | [diff] [blame] | 45 | sqlite3WalkExpr(&w, pExpr); |
| 46 | } |
| 47 | } |
| 48 | |
| 49 | /* |
drh | 8b21389 | 2008-08-29 02:14:02 | [diff] [blame] | 50 | ** Turn the pExpr expression into an alias for the iCol-th column of the |
| 51 | ** result set in pEList. |
| 52 | ** |
drh | 0a8a406 | 2012-12-07 18:38:16 | [diff] [blame] | 53 | ** If the reference is followed by a COLLATE operator, then make sure |
| 54 | ** the COLLATE operator is preserved. For example: |
| 55 | ** |
| 56 | ** SELECT a+b, c+d FROM t1 ORDER BY 1 COLLATE nocase; |
| 57 | ** |
| 58 | ** Should be transformed into: |
| 59 | ** |
| 60 | ** SELECT a+b, c+d FROM t1 ORDER BY (a+b) COLLATE nocase; |
| 61 | ** |
drh | ed551b9 | 2012-08-23 19:46:11 | [diff] [blame] | 62 | ** The nSubquery parameter specifies how many levels of subquery the |
drh | 41148f8 | 2015-04-23 13:37:05 | [diff] [blame] | 63 | ** alias is removed from the original expression. The usual value is |
drh | ed551b9 | 2012-08-23 19:46:11 | [diff] [blame] | 64 | ** zero but it might be more if the alias is contained within a subquery |
| 65 | ** of the original expression. The Expr.op2 field of TK_AGG_FUNCTION |
| 66 | ** structures must be increased by the nSubquery amount. |
drh | 8b21389 | 2008-08-29 02:14:02 | [diff] [blame] | 67 | */ |
| 68 | static void resolveAlias( |
| 69 | Parse *pParse, /* Parsing context */ |
| 70 | ExprList *pEList, /* A result set */ |
| 71 | int iCol, /* A column in the result set. 0..pEList->nExpr-1 */ |
| 72 | Expr *pExpr, /* Transform this into an alias to the result set */ |
drh | ed551b9 | 2012-08-23 19:46:11 | [diff] [blame] | 73 | int nSubquery /* Number of subqueries that the label is moving */ |
drh | 8b21389 | 2008-08-29 02:14:02 | [diff] [blame] | 74 | ){ |
| 75 | Expr *pOrig; /* The iCol-th column of the result set */ |
| 76 | Expr *pDup; /* Copy of pOrig */ |
| 77 | sqlite3 *db; /* The database connection */ |
| 78 | |
| 79 | assert( iCol>=0 && iCol<pEList->nExpr ); |
| 80 | pOrig = pEList->a[iCol].pExpr; |
| 81 | assert( pOrig!=0 ); |
drh | 3c5a810 | 2024-02-11 20:53:14 | [diff] [blame] | 82 | assert( !ExprHasProperty(pExpr, EP_Reduced|EP_TokenOnly) ); |
| 83 | if( pExpr->pAggInfo ) return; |
drh | 8b21389 | 2008-08-29 02:14:02 | [diff] [blame] | 84 | db = pParse->db; |
drh | 0a8a406 | 2012-12-07 18:38:16 | [diff] [blame] | 85 | pDup = sqlite3ExprDup(db, pOrig, 0); |
drh | 3c6edc8 | 2021-04-26 15:28:06 | [diff] [blame] | 86 | if( db->mallocFailed ){ |
| 87 | sqlite3ExprDelete(db, pDup); |
| 88 | pDup = 0; |
| 89 | }else{ |
drh | 3245f3b | 2022-07-20 16:42:40 | [diff] [blame] | 90 | Expr temp; |
drh | c7e93f5 | 2021-02-18 00:26:11 | [diff] [blame] | 91 | incrAggFunctionDepth(pDup, nSubquery); |
drh | 4344dbd | 2018-06-02 11:31:15 | [diff] [blame] | 92 | if( pExpr->op==TK_COLLATE ){ |
drh | f975107 | 2021-10-07 13:40:29 | [diff] [blame] | 93 | assert( !ExprHasProperty(pExpr, EP_IntValue) ); |
drh | 4344dbd | 2018-06-02 11:31:15 | [diff] [blame] | 94 | pDup = sqlite3ExprAddCollateString(pParse, pDup, pExpr->u.zToken); |
| 95 | } |
drh | 3245f3b | 2022-07-20 16:42:40 | [diff] [blame] | 96 | memcpy(&temp, pDup, sizeof(Expr)); |
| 97 | memcpy(pDup, pExpr, sizeof(Expr)); |
| 98 | memcpy(pExpr, &temp, sizeof(Expr)); |
dan | a51ddb1 | 2019-09-26 15:53:37 | [diff] [blame] | 99 | if( ExprHasProperty(pExpr, EP_WinFunc) ){ |
drh | feef447 | 2021-04-26 21:00:51 | [diff] [blame] | 100 | if( ALWAYS(pExpr->y.pWin!=0) ){ |
drh | d79cd92 | 2019-09-26 16:08:35 | [diff] [blame] | 101 | pExpr->y.pWin->pOwner = pExpr; |
drh | d79cd92 | 2019-09-26 16:08:35 | [diff] [blame] | 102 | } |
dan | a51ddb1 | 2019-09-26 15:53:37 | [diff] [blame] | 103 | } |
drh | 9553016 | 2022-07-20 20:36:26 | [diff] [blame] | 104 | sqlite3ExprDeferredDelete(pParse, pDup); |
drh | 0a8a406 | 2012-12-07 18:38:16 | [diff] [blame] | 105 | } |
drh | 8b21389 | 2008-08-29 02:14:02 | [diff] [blame] | 106 | } |
| 107 | |
drh | 3e3f1a5 | 2013-01-03 00:45:56 | [diff] [blame] | 108 | /* |
dan | 81b70d9 | 2023-09-15 18:36:51 | [diff] [blame] | 109 | ** Subqueries store the original database, table and column names for their |
| 110 | ** result sets in ExprList.a[].zSpan, in the form "DATABASE.TABLE.COLUMN", |
| 111 | ** and mark the expression-list item by setting ExprList.a[].fg.eEName |
| 112 | ** to ENAME_TAB. |
| 113 | ** |
| 114 | ** Check to see if the zSpan/eEName of the expression-list item passed to this |
| 115 | ** routine matches the zDb, zTab, and zCol. If any of zDb, zTab, and zCol are |
| 116 | ** NULL then those fields will match anything. Return true if there is a match, |
| 117 | ** or false otherwise. |
| 118 | ** |
| 119 | ** SF_NestedFrom subqueries also store an entry for the implicit rowid (or |
| 120 | ** _rowid_, or oid) column by setting ExprList.a[].fg.eEName to ENAME_ROWID, |
| 121 | ** and setting zSpan to "DATABASE.TABLE.<rowid-alias>". This type of pItem |
dan | 63702bc | 2023-09-15 20:57:05 | [diff] [blame] | 122 | ** argument matches if zCol is a rowid alias. If it is not NULL, (*pbRowid) |
| 123 | ** is set to 1 if there is this kind of match. |
drh | 3e3f1a5 | 2013-01-03 00:45:56 | [diff] [blame] | 124 | */ |
drh | c4938ea | 2019-12-13 00:49:42 | [diff] [blame] | 125 | int sqlite3MatchEName( |
| 126 | const struct ExprList_item *pItem, |
drh | 3e3f1a5 | 2013-01-03 00:45:56 | [diff] [blame] | 127 | const char *zCol, |
| 128 | const char *zTab, |
dan | 81b70d9 | 2023-09-15 18:36:51 | [diff] [blame] | 129 | const char *zDb, |
dan | 63702bc | 2023-09-15 20:57:05 | [diff] [blame] | 130 | int *pbRowid |
drh | 3e3f1a5 | 2013-01-03 00:45:56 | [diff] [blame] | 131 | ){ |
| 132 | int n; |
drh | c4938ea | 2019-12-13 00:49:42 | [diff] [blame] | 133 | const char *zSpan; |
dan | 81b70d9 | 2023-09-15 18:36:51 | [diff] [blame] | 134 | int eEName = pItem->fg.eEName; |
drh | a91fe45 | 2023-09-16 16:39:27 | [diff] [blame] | 135 | if( eEName!=ENAME_TAB && (eEName!=ENAME_ROWID || NEVER(pbRowid==0)) ){ |
dan | 81b70d9 | 2023-09-15 18:36:51 | [diff] [blame] | 136 | return 0; |
| 137 | } |
dan | 63702bc | 2023-09-15 20:57:05 | [diff] [blame] | 138 | assert( pbRowid==0 || *pbRowid==0 ); |
drh | c4938ea | 2019-12-13 00:49:42 | [diff] [blame] | 139 | zSpan = pItem->zEName; |
drh | 3e3f1a5 | 2013-01-03 00:45:56 | [diff] [blame] | 140 | for(n=0; ALWAYS(zSpan[n]) && zSpan[n]!='.'; n++){} |
drh | dd1dd48 | 2013-02-26 12:57:42 | [diff] [blame] | 141 | if( zDb && (sqlite3StrNICmp(zSpan, zDb, n)!=0 || zDb[n]!=0) ){ |
drh | 3e3f1a5 | 2013-01-03 00:45:56 | [diff] [blame] | 142 | return 0; |
| 143 | } |
| 144 | zSpan += n+1; |
| 145 | for(n=0; ALWAYS(zSpan[n]) && zSpan[n]!='.'; n++){} |
drh | dd1dd48 | 2013-02-26 12:57:42 | [diff] [blame] | 146 | if( zTab && (sqlite3StrNICmp(zSpan, zTab, n)!=0 || zTab[n]!=0) ){ |
drh | 3e3f1a5 | 2013-01-03 00:45:56 | [diff] [blame] | 147 | return 0; |
| 148 | } |
| 149 | zSpan += n+1; |
dan | 81b70d9 | 2023-09-15 18:36:51 | [diff] [blame] | 150 | if( zCol ){ |
| 151 | if( eEName==ENAME_TAB && sqlite3StrICmp(zSpan, zCol)!=0 ) return 0; |
| 152 | if( eEName==ENAME_ROWID && sqlite3IsRowid(zCol)==0 ) return 0; |
drh | 3e3f1a5 | 2013-01-03 00:45:56 | [diff] [blame] | 153 | } |
dan | 63702bc | 2023-09-15 20:57:05 | [diff] [blame] | 154 | if( eEName==ENAME_ROWID ) *pbRowid = 1; |
drh | 3e3f1a5 | 2013-01-03 00:45:56 | [diff] [blame] | 155 | return 1; |
| 156 | } |
drh | e802c5d | 2011-10-18 18:10:40 | [diff] [blame] | 157 | |
drh | 8b21389 | 2008-08-29 02:14:02 | [diff] [blame] | 158 | /* |
drh | d0ff601 | 2019-06-17 13:56:11 | [diff] [blame] | 159 | ** Return TRUE if the double-quoted string mis-feature should be supported. |
| 160 | */ |
| 161 | static int areDoubleQuotedStringsEnabled(sqlite3 *db, NameContext *pTopNC){ |
| 162 | if( db->init.busy ) return 1; /* Always support for legacy schemas */ |
| 163 | if( pTopNC->ncFlags & NC_IsDDL ){ |
| 164 | /* Currently parsing a DDL statement */ |
| 165 | if( sqlite3WritableSchema(db) && (db->flags & SQLITE_DqsDML)!=0 ){ |
| 166 | return 1; |
| 167 | } |
| 168 | return (db->flags & SQLITE_DqsDDL)!=0; |
| 169 | }else{ |
| 170 | /* Currently parsing a DML statement */ |
| 171 | return (db->flags & SQLITE_DqsDML)!=0; |
| 172 | } |
| 173 | } |
| 174 | |
| 175 | /* |
drh | 74a0798 | 2020-03-21 23:10:38 | [diff] [blame] | 176 | ** The argument is guaranteed to be a non-NULL Expr node of type TK_COLUMN. |
| 177 | ** return the appropriate colUsed mask. |
| 178 | */ |
| 179 | Bitmask sqlite3ExprColUsed(Expr *pExpr){ |
| 180 | int n; |
| 181 | Table *pExTab; |
| 182 | |
| 183 | n = pExpr->iColumn; |
drh | 477572b | 2021-10-07 20:46:29 | [diff] [blame] | 184 | assert( ExprUseYTab(pExpr) ); |
drh | 74a0798 | 2020-03-21 23:10:38 | [diff] [blame] | 185 | pExTab = pExpr->y.pTab; |
| 186 | assert( pExTab!=0 ); |
drh | 3e4195c | 2023-12-13 16:45:18 | [diff] [blame] | 187 | assert( n < pExTab->nCol ); |
drh | 74a0798 | 2020-03-21 23:10:38 | [diff] [blame] | 188 | if( (pExTab->tabFlags & TF_HasGenerated)!=0 |
larrybr | bc91738 | 2023-06-07 08:40:31 | [diff] [blame] | 189 | && (pExTab->aCol[n].colFlags & COLFLAG_GENERATED)!=0 |
drh | 74a0798 | 2020-03-21 23:10:38 | [diff] [blame] | 190 | ){ |
| 191 | testcase( pExTab->nCol==BMS-1 ); |
| 192 | testcase( pExTab->nCol==BMS ); |
| 193 | return pExTab->nCol>=BMS ? ALLBITS : MASKBIT(pExTab->nCol)-1; |
| 194 | }else{ |
| 195 | testcase( n==BMS-1 ); |
| 196 | testcase( n==BMS ); |
| 197 | if( n>=BMS ) n = BMS-1; |
| 198 | return ((Bitmask)1)<<n; |
| 199 | } |
| 200 | } |
| 201 | |
| 202 | /* |
drh | bb3c62a | 2022-04-15 19:27:02 | [diff] [blame] | 203 | ** Create a new expression term for the column specified by pMatch and |
| 204 | ** iColumn. Append this new expression term to the FULL JOIN Match set |
| 205 | ** in *ppList. Create a new *ppList if this is the first term in the |
| 206 | ** set. |
| 207 | */ |
| 208 | static void extendFJMatch( |
| 209 | Parse *pParse, /* Parsing context */ |
| 210 | ExprList **ppList, /* ExprList to extend */ |
| 211 | SrcItem *pMatch, /* Source table containing the column */ |
| 212 | i16 iColumn /* The column number */ |
| 213 | ){ |
| 214 | Expr *pNew = sqlite3ExprAlloc(pParse->db, TK_COLUMN, 0, 0); |
| 215 | if( pNew ){ |
drh | bb3c62a | 2022-04-15 19:27:02 | [diff] [blame] | 216 | pNew->iTable = pMatch->iCursor; |
drh | 79a25ee | 2022-04-18 00:57:11 | [diff] [blame] | 217 | pNew->iColumn = iColumn; |
drh | b204b6a | 2024-08-17 23:23:23 | [diff] [blame] | 218 | pNew->y.pTab = pMatch->pSTab; |
drh | bb3c62a | 2022-04-15 19:27:02 | [diff] [blame] | 219 | assert( (pMatch->fg.jointype & (JT_LEFT|JT_LTORJ))!=0 ); |
| 220 | ExprSetProperty(pNew, EP_CanBeNull); |
| 221 | *ppList = sqlite3ExprListAppend(pParse, *ppList, pNew); |
| 222 | } |
| 223 | } |
| 224 | |
| 225 | /* |
drh | 6f31eac | 2023-02-02 15:28:40 | [diff] [blame] | 226 | ** Return TRUE (non-zero) if zTab is a valid name for the schema table pTab. |
| 227 | */ |
| 228 | static SQLITE_NOINLINE int isValidSchemaTableName( |
| 229 | const char *zTab, /* Name as it appears in the SQL */ |
| 230 | Table *pTab, /* The schema table we are trying to match */ |
dan | e3eefe0 | 2024-06-05 11:36:58 | [diff] [blame] | 231 | const char *zDb /* non-NULL if a database qualifier is present */ |
drh | 6f31eac | 2023-02-02 15:28:40 | [diff] [blame] | 232 | ){ |
| 233 | const char *zLegacy; |
drh | 6f31eac | 2023-02-02 15:28:40 | [diff] [blame] | 234 | assert( pTab!=0 ); |
| 235 | assert( pTab->tnum==1 ); |
| 236 | if( sqlite3StrNICmp(zTab, "sqlite_", 7)!=0 ) return 0; |
| 237 | zLegacy = pTab->zName; |
drh | 2dd3b64 | 2023-02-02 16:30:32 | [diff] [blame] | 238 | if( strcmp(zLegacy+7, &LEGACY_TEMP_SCHEMA_TABLE[7])==0 ){ |
| 239 | if( sqlite3StrICmp(zTab+7, &PREFERRED_TEMP_SCHEMA_TABLE[7])==0 ){ |
drh | 6f31eac | 2023-02-02 15:28:40 | [diff] [blame] | 240 | return 1; |
| 241 | } |
dan | e3eefe0 | 2024-06-05 11:36:58 | [diff] [blame] | 242 | if( zDb==0 ) return 0; |
drh | 2dd3b64 | 2023-02-02 16:30:32 | [diff] [blame] | 243 | if( sqlite3StrICmp(zTab+7, &LEGACY_SCHEMA_TABLE[7])==0 ) return 1; |
| 244 | if( sqlite3StrICmp(zTab+7, &PREFERRED_SCHEMA_TABLE[7])==0 ) return 1; |
| 245 | }else{ |
| 246 | if( sqlite3StrICmp(zTab+7, &PREFERRED_SCHEMA_TABLE[7])==0 ) return 1; |
drh | 6f31eac | 2023-02-02 15:28:40 | [diff] [blame] | 247 | } |
| 248 | return 0; |
| 249 | } |
| 250 | |
| 251 | /* |
drh | 7d10d5a | 2008-08-20 16:35:10 | [diff] [blame] | 252 | ** Given the name of a column of the form X.Y.Z or Y.Z or just Z, look up |
larrybr | bc91738 | 2023-06-07 08:40:31 | [diff] [blame] | 253 | ** that name in the set of source tables in pSrcList and make the pExpr |
drh | 7d10d5a | 2008-08-20 16:35:10 | [diff] [blame] | 254 | ** expression node refer back to that source column. The following changes |
| 255 | ** are made to pExpr: |
| 256 | ** |
| 257 | ** pExpr->iDb Set the index in db->aDb[] of the database X |
| 258 | ** (even if X is implied). |
| 259 | ** pExpr->iTable Set to the cursor number for the table obtained |
| 260 | ** from pSrcList. |
drh | eda079c | 2018-09-20 19:02:15 | [diff] [blame] | 261 | ** pExpr->y.pTab Points to the Table structure of X.Y (even if |
drh | 7d10d5a | 2008-08-20 16:35:10 | [diff] [blame] | 262 | ** X and/or Y are implied.) |
| 263 | ** pExpr->iColumn Set to the column number within the table. |
| 264 | ** pExpr->op Set to TK_COLUMN. |
| 265 | ** pExpr->pLeft Any expression this points to is deleted |
| 266 | ** pExpr->pRight Any expression this points to is deleted. |
| 267 | ** |
drh | b7916a7 | 2009-05-27 10:31:29 | [diff] [blame] | 268 | ** The zDb variable is the name of the database (the "X"). This value may be |
drh | 7d10d5a | 2008-08-20 16:35:10 | [diff] [blame] | 269 | ** NULL meaning that name is of the form Y.Z or Z. Any available database |
drh | b7916a7 | 2009-05-27 10:31:29 | [diff] [blame] | 270 | ** can be used. The zTable variable is the name of the table (the "Y"). This |
| 271 | ** value can be NULL if zDb is also NULL. If zTable is NULL it |
drh | 7d10d5a | 2008-08-20 16:35:10 | [diff] [blame] | 272 | ** means that the form of the name is Z and that columns from any table |
| 273 | ** can be used. |
| 274 | ** |
| 275 | ** If the name cannot be resolved unambiguously, leave an error message |
drh | f7828b5 | 2009-06-15 23:15:59 | [diff] [blame] | 276 | ** in pParse and return WRC_Abort. Return WRC_Prune on success. |
drh | 7d10d5a | 2008-08-20 16:35:10 | [diff] [blame] | 277 | */ |
| 278 | static int lookupName( |
| 279 | Parse *pParse, /* The parsing context */ |
drh | b7916a7 | 2009-05-27 10:31:29 | [diff] [blame] | 280 | const char *zDb, /* Name of the database containing table, or NULL */ |
| 281 | const char *zTab, /* Name of table containing column, or NULL */ |
drh | a67d63b | 2024-01-22 15:26:33 | [diff] [blame] | 282 | const Expr *pRight, /* Name of the column. */ |
drh | 7d10d5a | 2008-08-20 16:35:10 | [diff] [blame] | 283 | NameContext *pNC, /* The name context used to resolve the name */ |
| 284 | Expr *pExpr /* Make this EXPR node point to the selected column */ |
| 285 | ){ |
drh | ed551b9 | 2012-08-23 19:46:11 | [diff] [blame] | 286 | int i, j; /* Loop counters */ |
drh | 7d10d5a | 2008-08-20 16:35:10 | [diff] [blame] | 287 | int cnt = 0; /* Number of matching column names */ |
dan | 2e8edc1 | 2023-09-16 14:42:18 | [diff] [blame] | 288 | int cntTab = 0; /* Number of potential "rowid" matches */ |
drh | ed551b9 | 2012-08-23 19:46:11 | [diff] [blame] | 289 | int nSubquery = 0; /* How many levels of subquery */ |
drh | 7d10d5a | 2008-08-20 16:35:10 | [diff] [blame] | 290 | sqlite3 *db = pParse->db; /* The database connection */ |
drh | 7601294 | 2021-02-21 21:04:54 | [diff] [blame] | 291 | SrcItem *pItem; /* Use for looping over pSrcList items */ |
| 292 | SrcItem *pMatch = 0; /* The matching pSrcList item */ |
drh | 7d10d5a | 2008-08-20 16:35:10 | [diff] [blame] | 293 | NameContext *pTopNC = pNC; /* First namecontext in the list */ |
| 294 | Schema *pSchema = 0; /* Schema of the expression */ |
drh | eac9fab | 2018-04-16 13:00:50 | [diff] [blame] | 295 | int eNewExprOp = TK_COLUMN; /* New value for pExpr->op on success */ |
drh | bb3c62a | 2022-04-15 19:27:02 | [diff] [blame] | 296 | Table *pTab = 0; /* Table holding the row */ |
drh | bb3c62a | 2022-04-15 19:27:02 | [diff] [blame] | 297 | ExprList *pFJMatch = 0; /* Matches for FULL JOIN .. USING */ |
drh | a67d63b | 2024-01-22 15:26:33 | [diff] [blame] | 298 | const char *zCol = pRight->u.zToken; |
drh | 7d10d5a | 2008-08-20 16:35:10 | [diff] [blame] | 299 | |
drh | b7916a7 | 2009-05-27 10:31:29 | [diff] [blame] | 300 | assert( pNC ); /* the name context cannot be NULL. */ |
| 301 | assert( zCol ); /* The Z in X.Y.Z cannot be NULL */ |
drh | 18f8600 | 2022-04-21 13:11:26 | [diff] [blame] | 302 | assert( zDb==0 || zTab!=0 ); |
drh | c5cd124 | 2013-09-12 16:50:49 | [diff] [blame] | 303 | assert( !ExprHasProperty(pExpr, EP_TokenOnly|EP_Reduced) ); |
drh | 7d10d5a | 2008-08-20 16:35:10 | [diff] [blame] | 304 | |
| 305 | /* Initialize the node to no-match */ |
| 306 | pExpr->iTable = -1; |
drh | ebb6a65 | 2013-09-12 23:42:22 | [diff] [blame] | 307 | ExprSetVVAProperty(pExpr, EP_NoReduce); |
drh | 7d10d5a | 2008-08-20 16:35:10 | [diff] [blame] | 308 | |
drh | 8f25d18 | 2012-12-19 02:36:45 | [diff] [blame] | 309 | /* Translate the schema name in zDb into a pointer to the corresponding |
| 310 | ** schema. If not found, pSchema will remain NULL and nothing will match |
| 311 | ** resulting in an appropriate error message toward the end of this routine |
| 312 | */ |
| 313 | if( zDb ){ |
drh | 1e7d43c | 2013-08-02 14:18:18 | [diff] [blame] | 314 | testcase( pNC->ncFlags & NC_PartIdx ); |
| 315 | testcase( pNC->ncFlags & NC_IsCheck ); |
| 316 | if( (pNC->ncFlags & (NC_PartIdx|NC_IsCheck))!=0 ){ |
drh | 31e7147 | 2015-02-09 10:20:19 | [diff] [blame] | 317 | /* Silently ignore database qualifiers inside CHECK constraints and |
| 318 | ** partial indices. Do not raise errors because that might break |
| 319 | ** legacy and because it does not hurt anything to just ignore the |
| 320 | ** database name. */ |
drh | 1e7d43c | 2013-08-02 14:18:18 | [diff] [blame] | 321 | zDb = 0; |
| 322 | }else{ |
| 323 | for(i=0; i<db->nDb; i++){ |
drh | 69c3382 | 2016-08-18 14:33:11 | [diff] [blame] | 324 | assert( db->aDb[i].zDbSName ); |
| 325 | if( sqlite3StrICmp(db->aDb[i].zDbSName,zDb)==0 ){ |
drh | 1e7d43c | 2013-08-02 14:18:18 | [diff] [blame] | 326 | pSchema = db->aDb[i].pSchema; |
| 327 | break; |
| 328 | } |
drh | 8f25d18 | 2012-12-19 02:36:45 | [diff] [blame] | 329 | } |
dan | 00bd55e | 2020-03-20 20:54:28 | [diff] [blame] | 330 | if( i==db->nDb && sqlite3StrICmp("main", zDb)==0 ){ |
| 331 | /* This branch is taken when the main database has been renamed |
| 332 | ** using SQLITE_DBCONFIG_MAINDBNAME. */ |
| 333 | pSchema = db->aDb[0].pSchema; |
| 334 | zDb = db->aDb[0].zDbSName; |
| 335 | } |
drh | 8f25d18 | 2012-12-19 02:36:45 | [diff] [blame] | 336 | } |
| 337 | } |
| 338 | |
drh | 7d10d5a | 2008-08-20 16:35:10 | [diff] [blame] | 339 | /* Start at the inner-most context and move outward until a match is found */ |
drh | a92b81f | 2017-03-07 03:40:48 | [diff] [blame] | 340 | assert( pNC && cnt==0 ); |
| 341 | do{ |
drh | 7d10d5a | 2008-08-20 16:35:10 | [diff] [blame] | 342 | ExprList *pEList; |
| 343 | SrcList *pSrcList = pNC->pSrcList; |
| 344 | |
| 345 | if( pSrcList ){ |
| 346 | for(i=0, pItem=pSrcList->a; i<pSrcList->nSrc; i++, pItem++){ |
drh | b204b6a | 2024-08-17 23:23:23 | [diff] [blame] | 347 | pTab = pItem->pSTab; |
drh | f436620 | 2008-08-25 12:14:08 | [diff] [blame] | 348 | assert( pTab!=0 && pTab->zName!=0 ); |
drh | 4d46669 | 2021-07-02 12:08:12 | [diff] [blame] | 349 | assert( pTab->nCol>0 || pParse->nErr ); |
drh | 1521ca4 | 2024-08-19 22:48:30 | [diff] [blame] | 350 | assert( (int)pItem->fg.isNestedFrom == IsNestedFrom(pItem)); |
drh | 815b782 | 2022-04-20 15:07:39 | [diff] [blame] | 351 | if( pItem->fg.isNestedFrom ){ |
drh | 2627cbd | 2022-04-20 12:02:52 | [diff] [blame] | 352 | /* In this case, pItem is a subquery that has been formed from a |
| 353 | ** parenthesized subset of the FROM clause terms. Example: |
| 354 | ** .... FROM t1 LEFT JOIN (t2 RIGHT JOIN t3 USING(x)) USING(y) ... |
| 355 | ** \_________________________/ |
| 356 | ** This pItem -------------^ |
| 357 | */ |
drh | 8f25d18 | 2012-12-19 02:36:45 | [diff] [blame] | 358 | int hit = 0; |
drh | 1521ca4 | 2024-08-19 22:48:30 | [diff] [blame] | 359 | Select *pSel; |
| 360 | assert( pItem->fg.isSubquery ); |
| 361 | assert( pItem->u4.pSubq!=0 ); |
| 362 | pSel = pItem->u4.pSubq->pSelect; |
| 363 | assert( pSel!=0 ); |
| 364 | pEList = pSel->pEList; |
drh | 815b782 | 2022-04-20 15:07:39 | [diff] [blame] | 365 | assert( pEList!=0 ); |
| 366 | assert( pEList->nExpr==pTab->nCol ); |
drh | 8f25d18 | 2012-12-19 02:36:45 | [diff] [blame] | 367 | for(j=0; j<pEList->nExpr; j++){ |
dan | 63702bc | 2023-09-15 20:57:05 | [diff] [blame] | 368 | int bRowid = 0; /* True if possible rowid match */ |
| 369 | if( !sqlite3MatchEName(&pEList->a[j], zCol, zTab, zDb, &bRowid) ){ |
drh | 18f8600 | 2022-04-21 13:11:26 | [diff] [blame] | 370 | continue; |
drh | 8f25d18 | 2012-12-19 02:36:45 | [diff] [blame] | 371 | } |
dan | 63702bc | 2023-09-15 20:57:05 | [diff] [blame] | 372 | if( bRowid==0 ){ |
| 373 | if( cnt>0 ){ |
| 374 | if( pItem->fg.isUsing==0 |
| 375 | || sqlite3IdListIndex(pItem->u3.pUsing, zCol)<0 |
| 376 | ){ |
| 377 | /* Two or more tables have the same column name which is |
| 378 | ** not joined by USING. This is an error. Signal as much |
| 379 | ** by clearing pFJMatch and letting cnt go above 1. */ |
| 380 | sqlite3ExprListDelete(db, pFJMatch); |
| 381 | pFJMatch = 0; |
| 382 | }else |
| 383 | if( (pItem->fg.jointype & JT_RIGHT)==0 ){ |
| 384 | /* An INNER or LEFT JOIN. Use the left-most table */ |
| 385 | continue; |
| 386 | }else |
| 387 | if( (pItem->fg.jointype & JT_LEFT)==0 ){ |
| 388 | /* A RIGHT JOIN. Use the right-most table */ |
| 389 | cnt = 0; |
| 390 | sqlite3ExprListDelete(db, pFJMatch); |
| 391 | pFJMatch = 0; |
| 392 | }else{ |
| 393 | /* For a FULL JOIN, we must construct a coalesce() func */ |
| 394 | extendFJMatch(pParse, &pFJMatch, pMatch, pExpr->iColumn); |
| 395 | } |
drh | 18f8600 | 2022-04-21 13:11:26 | [diff] [blame] | 396 | } |
dan | 63702bc | 2023-09-15 20:57:05 | [diff] [blame] | 397 | cnt++; |
| 398 | hit = 1; |
| 399 | }else if( cnt>0 ){ |
dan | 2e8edc1 | 2023-09-16 14:42:18 | [diff] [blame] | 400 | /* This is a potential rowid match, but there has already been |
| 401 | ** a real match found. So this can be ignored. */ |
dan | 63702bc | 2023-09-15 20:57:05 | [diff] [blame] | 402 | continue; |
drh | 18f8600 | 2022-04-21 13:11:26 | [diff] [blame] | 403 | } |
dan | 63702bc | 2023-09-15 20:57:05 | [diff] [blame] | 404 | cntTab++; |
drh | 18f8600 | 2022-04-21 13:11:26 | [diff] [blame] | 405 | pMatch = pItem; |
| 406 | pExpr->iColumn = j; |
drh | d88fd53 | 2022-05-02 20:49:30 | [diff] [blame] | 407 | pEList->a[j].fg.bUsed = 1; |
dan | 2e8edc1 | 2023-09-16 14:42:18 | [diff] [blame] | 408 | |
| 409 | /* rowid cannot be part of a USING clause - assert() this. */ |
| 410 | assert( bRowid==0 || pEList->a[j].fg.bUsingTerm==0 ); |
drh | d88fd53 | 2022-05-02 20:49:30 | [diff] [blame] | 411 | if( pEList->a[j].fg.bUsingTerm ) break; |
drh | 8f25d18 | 2012-12-19 02:36:45 | [diff] [blame] | 412 | } |
| 413 | if( hit || zTab==0 ) continue; |
| 414 | } |
drh | 18f8600 | 2022-04-21 13:11:26 | [diff] [blame] | 415 | assert( zDb==0 || zTab!=0 ); |
drh | 7d10d5a | 2008-08-20 16:35:10 | [diff] [blame] | 416 | if( zTab ){ |
drh | 18f8600 | 2022-04-21 13:11:26 | [diff] [blame] | 417 | if( zDb ){ |
| 418 | if( pTab->pSchema!=pSchema ) continue; |
| 419 | if( pSchema==0 && strcmp(zDb,"*")!=0 ) continue; |
| 420 | } |
drh | 6f31eac | 2023-02-02 15:28:40 | [diff] [blame] | 421 | if( pItem->zAlias!=0 ){ |
| 422 | if( sqlite3StrICmp(zTab, pItem->zAlias)!=0 ){ |
| 423 | continue; |
| 424 | } |
| 425 | }else if( sqlite3StrICmp(zTab, pTab->zName)!=0 ){ |
| 426 | if( pTab->tnum!=1 ) continue; |
dan | e3eefe0 | 2024-06-05 11:36:58 | [diff] [blame] | 427 | if( !isValidSchemaTableName(zTab, pTab, zDb) ) continue; |
drh | 7d10d5a | 2008-08-20 16:35:10 | [diff] [blame] | 428 | } |
drh | 477572b | 2021-10-07 20:46:29 | [diff] [blame] | 429 | assert( ExprUseYTab(pExpr) ); |
dan | c9461ec | 2018-08-29 21:00:16 | [diff] [blame] | 430 | if( IN_RENAME_OBJECT && pItem->zAlias ){ |
drh | eda079c | 2018-09-20 19:02:15 | [diff] [blame] | 431 | sqlite3RenameTokenRemap(pParse, 0, (void*)&pExpr->y.pTab); |
dan | c9461ec | 2018-08-29 21:00:16 | [diff] [blame] | 432 | } |
drh | 7d10d5a | 2008-08-20 16:35:10 | [diff] [blame] | 433 | } |
drh | 9d90a3a | 2025-02-08 14:15:42 | [diff] [blame] | 434 | j = sqlite3ColumnIndex(pTab, zCol); |
| 435 | if( j>=0 ){ |
| 436 | if( cnt>0 ){ |
| 437 | if( pItem->fg.isUsing==0 |
| 438 | || sqlite3IdListIndex(pItem->u3.pUsing, zCol)<0 |
| 439 | ){ |
| 440 | /* Two or more tables have the same column name which is |
| 441 | ** not joined by USING. This is an error. Signal as much |
| 442 | ** by clearing pFJMatch and letting cnt go above 1. */ |
| 443 | sqlite3ExprListDelete(db, pFJMatch); |
| 444 | pFJMatch = 0; |
| 445 | }else |
| 446 | if( (pItem->fg.jointype & JT_RIGHT)==0 ){ |
| 447 | /* An INNER or LEFT JOIN. Use the left-most table */ |
| 448 | continue; |
| 449 | }else |
| 450 | if( (pItem->fg.jointype & JT_LEFT)==0 ){ |
| 451 | /* A RIGHT JOIN. Use the right-most table */ |
| 452 | cnt = 0; |
| 453 | sqlite3ExprListDelete(db, pFJMatch); |
| 454 | pFJMatch = 0; |
| 455 | }else{ |
| 456 | /* For a FULL JOIN, we must construct a coalesce() func */ |
| 457 | extendFJMatch(pParse, &pFJMatch, pMatch, pExpr->iColumn); |
drh | e802c5d | 2011-10-18 18:10:40 | [diff] [blame] | 458 | } |
drh | 9d90a3a | 2025-02-08 14:15:42 | [diff] [blame] | 459 | } |
| 460 | cnt++; |
| 461 | pMatch = pItem; |
| 462 | /* Substitute the rowid (column -1) for the INTEGER PRIMARY KEY */ |
| 463 | pExpr->iColumn = j==pTab->iPKey ? -1 : (i16)j; |
| 464 | if( pItem->fg.isNestedFrom ){ |
| 465 | sqlite3SrcItemColumnUsed(pItem, j); |
drh | 7d10d5a | 2008-08-20 16:35:10 | [diff] [blame] | 466 | } |
| 467 | } |
dan | b9248ef | 2021-07-01 18:19:17 | [diff] [blame] | 468 | if( 0==cnt && VisibleRowid(pTab) ){ |
drh | e9feb0a | 2024-03-19 16:34:32 | [diff] [blame] | 469 | /* pTab is a potential ROWID match. Keep track of it and match |
| 470 | ** the ROWID later if that seems appropriate. (Search for "cntTab" |
| 471 | ** to find related code.) Only allow a ROWID match if there is |
| 472 | ** a single ROWID match candidate. |
| 473 | */ |
| 474 | #ifdef SQLITE_ALLOW_ROWID_IN_VIEW |
| 475 | /* In SQLITE_ALLOW_ROWID_IN_VIEW mode, allow a ROWID match |
| 476 | ** if there is a single VIEW candidate or if there is a single |
| 477 | ** non-VIEW candidate plus multiple VIEW candidates. In other |
| 478 | ** words non-VIEW candidate terms take precedence over VIEWs. |
| 479 | */ |
| 480 | if( cntTab==0 |
| 481 | || (cntTab==1 |
drh | 7d24e6b | 2024-09-24 00:53:27 | [diff] [blame] | 482 | && pMatch!=0 |
drh | b204b6a | 2024-08-17 23:23:23 | [diff] [blame] | 483 | && ALWAYS(pMatch->pSTab!=0) |
| 484 | && (pMatch->pSTab->tabFlags & TF_Ephemeral)!=0 |
drh | e9feb0a | 2024-03-19 16:34:32 | [diff] [blame] | 485 | && (pTab->tabFlags & TF_Ephemeral)==0) |
| 486 | ){ |
| 487 | cntTab = 1; |
| 488 | pMatch = pItem; |
| 489 | }else{ |
| 490 | cntTab++; |
| 491 | } |
| 492 | #else |
| 493 | /* The (much more common) non-SQLITE_ALLOW_ROWID_IN_VIEW case is |
| 494 | ** simpler since we require exactly one candidate, which will |
| 495 | ** always be a non-VIEW |
| 496 | */ |
dan | b9248ef | 2021-07-01 18:19:17 | [diff] [blame] | 497 | cntTab++; |
| 498 | pMatch = pItem; |
drh | e9feb0a | 2024-03-19 16:34:32 | [diff] [blame] | 499 | #endif |
dan | b9248ef | 2021-07-01 18:19:17 | [diff] [blame] | 500 | } |
drh | 7d10d5a | 2008-08-20 16:35:10 | [diff] [blame] | 501 | } |
drh | 8f25d18 | 2012-12-19 02:36:45 | [diff] [blame] | 502 | if( pMatch ){ |
| 503 | pExpr->iTable = pMatch->iCursor; |
drh | 477572b | 2021-10-07 20:46:29 | [diff] [blame] | 504 | assert( ExprUseYTab(pExpr) ); |
drh | b204b6a | 2024-08-17 23:23:23 | [diff] [blame] | 505 | pExpr->y.pTab = pMatch->pSTab; |
drh | a76ac88 | 2022-04-08 19:20:12 | [diff] [blame] | 506 | if( (pMatch->fg.jointype & (JT_LEFT|JT_LTORJ))!=0 ){ |
drh | 72673a2 | 2014-12-04 16:27:17 | [diff] [blame] | 507 | ExprSetProperty(pExpr, EP_CanBeNull); |
| 508 | } |
drh | eda079c | 2018-09-20 19:02:15 | [diff] [blame] | 509 | pSchema = pExpr->y.pTab->pSchema; |
drh | 8f25d18 | 2012-12-19 02:36:45 | [diff] [blame] | 510 | } |
| 511 | } /* if( pSrcList ) */ |
drh | 7d10d5a | 2008-08-20 16:35:10 | [diff] [blame] | 512 | |
drh | eac9fab | 2018-04-16 13:00:50 | [diff] [blame] | 513 | #if !defined(SQLITE_OMIT_TRIGGER) || !defined(SQLITE_OMIT_UPSERT) |
larrybr | bc91738 | 2023-06-07 08:40:31 | [diff] [blame] | 514 | /* If we have not already resolved the name, then maybe |
drh | eac9fab | 2018-04-16 13:00:50 | [diff] [blame] | 515 | ** it is a new.* or old.* trigger argument reference. Or |
drh | d510197 | 2021-02-08 13:41:17 | [diff] [blame] | 516 | ** maybe it is an excluded.* from an upsert. Or maybe it is |
| 517 | ** a reference in the RETURNING clause to a table being modified. |
drh | 7d10d5a | 2008-08-20 16:35:10 | [diff] [blame] | 518 | */ |
drh | d510197 | 2021-02-08 13:41:17 | [diff] [blame] | 519 | if( cnt==0 && zDb==0 ){ |
drh | eac9fab | 2018-04-16 13:00:50 | [diff] [blame] | 520 | pTab = 0; |
| 521 | #ifndef SQLITE_OMIT_TRIGGER |
| 522 | if( pParse->pTriggerTab!=0 ){ |
| 523 | int op = pParse->eTriggerOp; |
| 524 | assert( op==TK_DELETE || op==TK_UPDATE || op==TK_INSERT ); |
drh | 22af584 | 2021-03-31 23:56:55 | [diff] [blame] | 525 | if( pParse->bReturning ){ |
| 526 | if( (pNC->ncFlags & NC_UBaseReg)!=0 |
drh | 68e976b | 2023-03-21 14:20:10 | [diff] [blame] | 527 | && ALWAYS(zTab==0 |
dan | 7281493 | 2024-05-02 18:16:23 | [diff] [blame] | 528 | || sqlite3StrICmp(zTab,pParse->pTriggerTab->zName)==0 |
| 529 | || isValidSchemaTableName(zTab, pParse->pTriggerTab, 0)) |
drh | 22af584 | 2021-03-31 23:56:55 | [diff] [blame] | 530 | ){ |
drh | d75aeee | 2021-03-31 17:42:24 | [diff] [blame] | 531 | pExpr->iTable = op!=TK_DELETE; |
| 532 | pTab = pParse->pTriggerTab; |
| 533 | } |
| 534 | }else if( op!=TK_DELETE && zTab && sqlite3StrICmp("new",zTab) == 0 ){ |
drh | eac9fab | 2018-04-16 13:00:50 | [diff] [blame] | 535 | pExpr->iTable = 1; |
| 536 | pTab = pParse->pTriggerTab; |
drh | b835247 | 2021-01-29 19:32:17 | [diff] [blame] | 537 | }else if( op!=TK_INSERT && zTab && sqlite3StrICmp("old",zTab)==0 ){ |
drh | eac9fab | 2018-04-16 13:00:50 | [diff] [blame] | 538 | pExpr->iTable = 0; |
| 539 | pTab = pParse->pTriggerTab; |
| 540 | } |
drh | 7d10d5a | 2008-08-20 16:35:10 | [diff] [blame] | 541 | } |
drh | eac9fab | 2018-04-16 13:00:50 | [diff] [blame] | 542 | #endif /* SQLITE_OMIT_TRIGGER */ |
| 543 | #ifndef SQLITE_OMIT_UPSERT |
drh | d510197 | 2021-02-08 13:41:17 | [diff] [blame] | 544 | if( (pNC->ncFlags & NC_UUpsert)!=0 && zTab!=0 ){ |
drh | eac9fab | 2018-04-16 13:00:50 | [diff] [blame] | 545 | Upsert *pUpsert = pNC->uNC.pUpsert; |
| 546 | if( pUpsert && sqlite3StrICmp("excluded",zTab)==0 ){ |
drh | b204b6a | 2024-08-17 23:23:23 | [diff] [blame] | 547 | pTab = pUpsert->pUpsertSrc->a[0].pSTab; |
drh | ec43d80 | 2020-06-29 20:20:40 | [diff] [blame] | 548 | pExpr->iTable = EXCLUDED_TABLE_NUMBER; |
drh | eac9fab | 2018-04-16 13:00:50 | [diff] [blame] | 549 | } |
| 550 | } |
| 551 | #endif /* SQLITE_OMIT_UPSERT */ |
drh | 7d10d5a | 2008-08-20 16:35:10 | [diff] [blame] | 552 | |
larrybr | bc91738 | 2023-06-07 08:40:31 | [diff] [blame] | 553 | if( pTab ){ |
drh | 7d10d5a | 2008-08-20 16:35:10 | [diff] [blame] | 554 | int iCol; |
drh | 7d10d5a | 2008-08-20 16:35:10 | [diff] [blame] | 555 | pSchema = pTab->pSchema; |
| 556 | cntTab++; |
drh | 9d90a3a | 2025-02-08 14:15:42 | [diff] [blame] | 557 | iCol = sqlite3ColumnIndex(pTab, zCol); |
| 558 | if( iCol>=0 ){ |
| 559 | if( pTab->iPKey==iCol ) iCol = -1; |
| 560 | }else{ |
| 561 | if( sqlite3IsRowid(zCol) && VisibleRowid(pTab) ){ |
| 562 | iCol = -1; |
| 563 | }else{ |
| 564 | iCol = pTab->nCol; |
drh | 7d10d5a | 2008-08-20 16:35:10 | [diff] [blame] | 565 | } |
| 566 | } |
dan | 2bd9351 | 2009-08-31 08:22:46 | [diff] [blame] | 567 | if( iCol<pTab->nCol ){ |
| 568 | cnt++; |
dan | a7e16a2 | 2021-03-05 15:10:33 | [diff] [blame] | 569 | pMatch = 0; |
drh | eac9fab | 2018-04-16 13:00:50 | [diff] [blame] | 570 | #ifndef SQLITE_OMIT_UPSERT |
drh | ec43d80 | 2020-06-29 20:20:40 | [diff] [blame] | 571 | if( pExpr->iTable==EXCLUDED_TABLE_NUMBER ){ |
drh | 9916048 | 2018-04-18 01:34:39 | [diff] [blame] | 572 | testcase( iCol==(-1) ); |
drh | 477572b | 2021-10-07 20:46:29 | [diff] [blame] | 573 | assert( ExprUseYTab(pExpr) ); |
dan | c9461ec | 2018-08-29 21:00:16 | [diff] [blame] | 574 | if( IN_RENAME_OBJECT ){ |
dan | 85a9d50 | 2018-08-24 20:10:22 | [diff] [blame] | 575 | pExpr->iColumn = iCol; |
drh | eda079c | 2018-09-20 19:02:15 | [diff] [blame] | 576 | pExpr->y.pTab = pTab; |
dan | 85a9d50 | 2018-08-24 20:10:22 | [diff] [blame] | 577 | eNewExprOp = TK_COLUMN; |
| 578 | }else{ |
drh | b8fec21 | 2020-06-29 20:26:50 | [diff] [blame] | 579 | pExpr->iTable = pNC->uNC.pUpsert->regData + |
| 580 | sqlite3TableColumnToStorage(pTab, iCol); |
dan | 85a9d50 | 2018-08-24 20:10:22 | [diff] [blame] | 581 | eNewExprOp = TK_REGISTER; |
dan | 85a9d50 | 2018-08-24 20:10:22 | [diff] [blame] | 582 | } |
drh | eac9fab | 2018-04-16 13:00:50 | [diff] [blame] | 583 | }else |
drh | 0a6259f | 2018-04-16 13:26:53 | [diff] [blame] | 584 | #endif /* SQLITE_OMIT_UPSERT */ |
drh | eac9fab | 2018-04-16 13:00:50 | [diff] [blame] | 585 | { |
drh | 477572b | 2021-10-07 20:46:29 | [diff] [blame] | 586 | assert( ExprUseYTab(pExpr) ); |
drh | eda079c | 2018-09-20 19:02:15 | [diff] [blame] | 587 | pExpr->y.pTab = pTab; |
drh | 552562c | 2021-02-04 20:52:20 | [diff] [blame] | 588 | if( pParse->bReturning ){ |
| 589 | eNewExprOp = TK_REGISTER; |
drh | 41584df | 2021-12-29 04:31:54 | [diff] [blame] | 590 | pExpr->op2 = TK_COLUMN; |
drh | 475e76d | 2023-01-13 19:29:46 | [diff] [blame] | 591 | pExpr->iColumn = iCol; |
dan | 0d08072 | 2021-03-05 15:29:22 | [diff] [blame] | 592 | pExpr->iTable = pNC->uNC.iBaseReg + (pTab->nCol+1)*pExpr->iTable + |
| 593 | sqlite3TableColumnToStorage(pTab, iCol) + 1; |
drh | 552562c | 2021-02-04 20:52:20 | [diff] [blame] | 594 | }else{ |
| 595 | pExpr->iColumn = (i16)iCol; |
| 596 | eNewExprOp = TK_TRIGGER; |
| 597 | #ifndef SQLITE_OMIT_TRIGGER |
drh | 8873aea | 2021-02-06 14:37:36 | [diff] [blame] | 598 | if( iCol<0 ){ |
| 599 | pExpr->affExpr = SQLITE_AFF_INTEGER; |
| 600 | }else if( pExpr->iTable==0 ){ |
drh | 552562c | 2021-02-04 20:52:20 | [diff] [blame] | 601 | testcase( iCol==31 ); |
| 602 | testcase( iCol==32 ); |
| 603 | pParse->oldmask |= (iCol>=32 ? 0xffffffff : (((u32)1)<<iCol)); |
| 604 | }else{ |
| 605 | testcase( iCol==31 ); |
| 606 | testcase( iCol==32 ); |
| 607 | pParse->newmask |= (iCol>=32 ? 0xffffffff : (((u32)1)<<iCol)); |
| 608 | } |
drh | 0a6259f | 2018-04-16 13:26:53 | [diff] [blame] | 609 | #endif /* SQLITE_OMIT_TRIGGER */ |
drh | 552562c | 2021-02-04 20:52:20 | [diff] [blame] | 610 | } |
dan | 2bd9351 | 2009-08-31 08:22:46 | [diff] [blame] | 611 | } |
dan | 2bd9351 | 2009-08-31 08:22:46 | [diff] [blame] | 612 | } |
drh | 7d10d5a | 2008-08-20 16:35:10 | [diff] [blame] | 613 | } |
| 614 | } |
drh | eac9fab | 2018-04-16 13:00:50 | [diff] [blame] | 615 | #endif /* !defined(SQLITE_OMIT_TRIGGER) || !defined(SQLITE_OMIT_UPSERT) */ |
drh | 7d10d5a | 2008-08-20 16:35:10 | [diff] [blame] | 616 | |
| 617 | /* |
| 618 | ** Perhaps the name is a reference to the ROWID |
| 619 | */ |
drh | 4cbc54b | 2015-09-19 03:07:30 | [diff] [blame] | 620 | if( cnt==0 |
dan | a10c2a9 | 2024-03-19 14:55:49 | [diff] [blame] | 621 | && cntTab>=1 |
drh | 4cbc54b | 2015-09-19 03:07:30 | [diff] [blame] | 622 | && pMatch |
drh | 81f7b37 | 2019-10-16 12:18:59 | [diff] [blame] | 623 | && (pNC->ncFlags & (NC_IdxExpr|NC_GenCol))==0 |
drh | 4cbc54b | 2015-09-19 03:07:30 | [diff] [blame] | 624 | && sqlite3IsRowid(zCol) |
drh | b204b6a | 2024-08-17 23:23:23 | [diff] [blame] | 625 | && ALWAYS(VisibleRowid(pMatch->pSTab) || pMatch->fg.isNestedFrom) |
drh | 4cbc54b | 2015-09-19 03:07:30 | [diff] [blame] | 626 | ){ |
dan | a10c2a9 | 2024-03-19 14:55:49 | [diff] [blame] | 627 | cnt = cntTab; |
drh | 1152def | 2024-04-07 18:23:30 | [diff] [blame] | 628 | #if SQLITE_ALLOW_ROWID_IN_VIEW+0==2 |
drh | b204b6a | 2024-08-17 23:23:23 | [diff] [blame] | 629 | if( pMatch->pSTab!=0 && IsView(pMatch->pSTab) ){ |
drh | 1152def | 2024-04-07 18:23:30 | [diff] [blame] | 630 | eNewExprOp = TK_NULL; |
| 631 | } |
| 632 | #endif |
dan | 63702bc | 2023-09-15 20:57:05 | [diff] [blame] | 633 | if( pMatch->fg.isNestedFrom==0 ) pExpr->iColumn = -1; |
drh | 1194904 | 2019-08-05 18:01:42 | [diff] [blame] | 634 | pExpr->affExpr = SQLITE_AFF_INTEGER; |
drh | 7d10d5a | 2008-08-20 16:35:10 | [diff] [blame] | 635 | } |
| 636 | |
| 637 | /* |
| 638 | ** If the input is of the form Z (not Y.Z or X.Y.Z) then the name Z |
| 639 | ** might refer to an result-set alias. This happens, for example, when |
| 640 | ** we are resolving names in the WHERE clause of the following command: |
| 641 | ** |
| 642 | ** SELECT a+b AS x FROM table WHERE x<10; |
| 643 | ** |
| 644 | ** In cases like this, replace pExpr with a copy of the expression that |
| 645 | ** forms the result set entry ("a+b" in the example) and return immediately. |
| 646 | ** Note that the expression in the result set should have already been |
| 647 | ** resolved by the time the WHERE clause is resolved. |
drh | e35463b | 2013-08-15 20:24:27 | [diff] [blame] | 648 | ** |
| 649 | ** The ability to use an output result-set column in the WHERE, GROUP BY, |
drh | 5579d59 | 2015-08-26 14:01:41 | [diff] [blame] | 650 | ** or HAVING clauses, or as part of a larger expression in the ORDER BY |
drh | e35463b | 2013-08-15 20:24:27 | [diff] [blame] | 651 | ** clause is not standard SQL. This is a (goofy) SQLite extension, that |
drh | 5579d59 | 2015-08-26 14:01:41 | [diff] [blame] | 652 | ** is supported for backwards compatibility only. Hence, we issue a warning |
drh | e35463b | 2013-08-15 20:24:27 | [diff] [blame] | 653 | ** on sqlite3_log() whenever the capability is used. |
drh | 7d10d5a | 2008-08-20 16:35:10 | [diff] [blame] | 654 | */ |
drh | de0e1b1 | 2021-04-16 22:53:57 | [diff] [blame] | 655 | if( cnt==0 |
| 656 | && (pNC->ncFlags & NC_UEList)!=0 |
drh | 25c3b8c | 2018-04-16 10:34:13 | [diff] [blame] | 657 | && zTab==0 |
drh | a3a5bd9 | 2013-04-13 19:59:58 | [diff] [blame] | 658 | ){ |
drh | 25c3b8c | 2018-04-16 10:34:13 | [diff] [blame] | 659 | pEList = pNC->uNC.pEList; |
| 660 | assert( pEList!=0 ); |
drh | 7d10d5a | 2008-08-20 16:35:10 | [diff] [blame] | 661 | for(j=0; j<pEList->nExpr; j++){ |
drh | 9fc1b9a | 2020-01-02 22:23:01 | [diff] [blame] | 662 | char *zAs = pEList->a[j].zEName; |
drh | d88fd53 | 2022-05-02 20:49:30 | [diff] [blame] | 663 | if( pEList->a[j].fg.eEName==ENAME_NAME |
dan | 607dd6e | 2020-01-03 14:27:08 | [diff] [blame] | 664 | && sqlite3_stricmp(zAs, zCol)==0 |
drh | c4938ea | 2019-12-13 00:49:42 | [diff] [blame] | 665 | ){ |
drh | 8b21389 | 2008-08-29 02:14:02 | [diff] [blame] | 666 | Expr *pOrig; |
drh | 7d10d5a | 2008-08-20 16:35:10 | [diff] [blame] | 667 | assert( pExpr->pLeft==0 && pExpr->pRight==0 ); |
drh | a4eeccd | 2021-10-07 17:43:30 | [diff] [blame] | 668 | assert( ExprUseXList(pExpr)==0 || pExpr->x.pList==0 ); |
| 669 | assert( ExprUseXSelect(pExpr)==0 || pExpr->x.pSelect==0 ); |
drh | 7d10d5a | 2008-08-20 16:35:10 | [diff] [blame] | 670 | pOrig = pEList->a[j].pExpr; |
drh | a51009b | 2012-05-21 19:11:25 | [diff] [blame] | 671 | if( (pNC->ncFlags&NC_AllowAgg)==0 && ExprHasProperty(pOrig, EP_Agg) ){ |
drh | 7d10d5a | 2008-08-20 16:35:10 | [diff] [blame] | 672 | sqlite3ErrorMsg(pParse, "misuse of aliased aggregate %s", zAs); |
drh | f7828b5 | 2009-06-15 23:15:59 | [diff] [blame] | 673 | return WRC_Abort; |
drh | 7d10d5a | 2008-08-20 16:35:10 | [diff] [blame] | 674 | } |
dan | e3735bf | 2019-12-27 19:46:07 | [diff] [blame] | 675 | if( ExprHasProperty(pOrig, EP_Win) |
| 676 | && ((pNC->ncFlags&NC_AllowWin)==0 || pNC!=pTopNC ) |
| 677 | ){ |
dan | 4ded26a | 2019-03-28 16:15:05 | [diff] [blame] | 678 | sqlite3ErrorMsg(pParse, "misuse of aliased window function %s",zAs); |
| 679 | return WRC_Abort; |
| 680 | } |
dan | 3bafded | 2016-11-11 15:49:01 | [diff] [blame] | 681 | if( sqlite3ExprVectorSize(pOrig)!=1 ){ |
| 682 | sqlite3ErrorMsg(pParse, "row value misused"); |
| 683 | return WRC_Abort; |
| 684 | } |
drh | 6b37b76 | 2021-02-16 20:32:17 | [diff] [blame] | 685 | resolveAlias(pParse, pEList, j, pExpr, nSubquery); |
drh | 7d10d5a | 2008-08-20 16:35:10 | [diff] [blame] | 686 | cnt = 1; |
| 687 | pMatch = 0; |
| 688 | assert( zTab==0 && zDb==0 ); |
dan | c9461ec | 2018-08-29 21:00:16 | [diff] [blame] | 689 | if( IN_RENAME_OBJECT ){ |
dan | 1b0c5de | 2018-08-24 16:04:26 | [diff] [blame] | 690 | sqlite3RenameTokenRemap(pParse, 0, (void*)pExpr); |
| 691 | } |
drh | b7916a7 | 2009-05-27 10:31:29 | [diff] [blame] | 692 | goto lookupname_end; |
drh | 7d10d5a | 2008-08-20 16:35:10 | [diff] [blame] | 693 | } |
larrybr | bc91738 | 2023-06-07 08:40:31 | [diff] [blame] | 694 | } |
drh | 7d10d5a | 2008-08-20 16:35:10 | [diff] [blame] | 695 | } |
| 696 | |
| 697 | /* Advance to the next name context. The loop will exit when either |
| 698 | ** we have a match (cnt>0) or when we run out of name contexts. |
| 699 | */ |
drh | a92b81f | 2017-03-07 03:40:48 | [diff] [blame] | 700 | if( cnt ) break; |
| 701 | pNC = pNC->pNext; |
| 702 | nSubquery++; |
| 703 | }while( pNC ); |
| 704 | |
drh | 7d10d5a | 2008-08-20 16:35:10 | [diff] [blame] | 705 | |
| 706 | /* |
| 707 | ** If X and Y are NULL (in other words if only the column name Z is |
| 708 | ** supplied) and the value of Z is enclosed in double-quotes, then |
| 709 | ** Z is a string literal if it doesn't match any column names. In that |
| 710 | ** case, we need to return right away and not make any changes to |
| 711 | ** pExpr. |
| 712 | ** |
| 713 | ** Because no reference was made to outer contexts, the pNC->nRef |
| 714 | ** fields are not changed in any context. |
| 715 | */ |
drh | 007c843 | 2018-02-26 03:20:18 | [diff] [blame] | 716 | if( cnt==0 && zTab==0 ){ |
drh | 171d16b | 2018-02-26 20:15:54 | [diff] [blame] | 717 | assert( pExpr->op==TK_ID ); |
drh | d0ff601 | 2019-06-17 13:56:11 | [diff] [blame] | 718 | if( ExprHasProperty(pExpr,EP_DblQuoted) |
| 719 | && areDoubleQuotedStringsEnabled(db, pTopNC) |
dan | 0d92571 | 2019-05-20 17:14:25 | [diff] [blame] | 720 | ){ |
drh | ec8fc62 | 2018-12-06 16:11:14 | [diff] [blame] | 721 | /* If a double-quoted identifier does not match any known column name, |
| 722 | ** then treat it as a string. |
| 723 | ** |
| 724 | ** This hack was added in the early days of SQLite in a misguided attempt |
| 725 | ** to be compatible with MySQL 3.x, which used double-quotes for strings. |
| 726 | ** I now sorely regret putting in this hack. The effect of this hack is |
| 727 | ** that misspelled identifier names are silently converted into strings |
| 728 | ** rather than causing an error, to the frustration of countless |
| 729 | ** programmers. To all those frustrated programmers, my apologies. |
| 730 | ** |
| 731 | ** Someday, I hope to get rid of this hack. Unfortunately there is |
| 732 | ** a huge amount of legacy SQL that uses it. So for now, we just |
| 733 | ** issue a warning. |
| 734 | */ |
| 735 | sqlite3_log(SQLITE_WARNING, |
| 736 | "double-quoted string literal: \"%w\"", zCol); |
drh | 893bd37 | 2018-12-07 16:32:11 | [diff] [blame] | 737 | #ifdef SQLITE_ENABLE_NORMALIZE |
drh | 1a6c2b1 | 2018-12-10 20:01:40 | [diff] [blame] | 738 | sqlite3VdbeAddDblquoteStr(db, pParse->pVdbe, zCol); |
drh | 893bd37 | 2018-12-07 16:32:11 | [diff] [blame] | 739 | #endif |
drh | 007c843 | 2018-02-26 03:20:18 | [diff] [blame] | 740 | pExpr->op = TK_STRING; |
drh | 477572b | 2021-10-07 20:46:29 | [diff] [blame] | 741 | memset(&pExpr->y, 0, sizeof(pExpr->y)); |
drh | 007c843 | 2018-02-26 03:20:18 | [diff] [blame] | 742 | return WRC_Prune; |
| 743 | } |
drh | 171d16b | 2018-02-26 20:15:54 | [diff] [blame] | 744 | if( sqlite3ExprIdToTrueFalse(pExpr) ){ |
drh | 007c843 | 2018-02-26 03:20:18 | [diff] [blame] | 745 | return WRC_Prune; |
| 746 | } |
drh | 7d10d5a | 2008-08-20 16:35:10 | [diff] [blame] | 747 | } |
| 748 | |
drh | bb3c62a | 2022-04-15 19:27:02 | [diff] [blame] | 749 | /* |
| 750 | ** cnt==0 means there was not match. |
| 751 | ** cnt>1 means there were two or more matches. |
| 752 | ** |
| 753 | ** cnt==0 is always an error. cnt>1 is often an error, but might |
| 754 | ** be multiple matches for a NATURAL LEFT JOIN or a LEFT JOIN USING. |
| 755 | */ |
| 756 | assert( pFJMatch==0 || cnt>0 ); |
drh | bdbda1e | 2022-04-16 12:40:52 | [diff] [blame] | 757 | assert( !ExprHasProperty(pExpr, EP_xIsSelect|EP_IntValue) ); |
drh | 7d10d5a | 2008-08-20 16:35:10 | [diff] [blame] | 758 | if( cnt!=1 ){ |
| 759 | const char *zErr; |
drh | bb3c62a | 2022-04-15 19:27:02 | [diff] [blame] | 760 | if( pFJMatch ){ |
| 761 | if( pFJMatch->nExpr==cnt-1 ){ |
drh | 977eef6 | 2022-04-16 13:55:48 | [diff] [blame] | 762 | if( ExprHasProperty(pExpr,EP_Leaf) ){ |
| 763 | ExprClearProperty(pExpr,EP_Leaf); |
| 764 | }else{ |
drh | bdbda1e | 2022-04-16 12:40:52 | [diff] [blame] | 765 | sqlite3ExprDelete(db, pExpr->pLeft); |
| 766 | pExpr->pLeft = 0; |
| 767 | sqlite3ExprDelete(db, pExpr->pRight); |
| 768 | pExpr->pRight = 0; |
| 769 | } |
drh | bb3c62a | 2022-04-15 19:27:02 | [diff] [blame] | 770 | extendFJMatch(pParse, &pFJMatch, pMatch, pExpr->iColumn); |
| 771 | pExpr->op = TK_FUNCTION; |
| 772 | pExpr->u.zToken = "coalesce"; |
| 773 | pExpr->x.pList = pFJMatch; |
drh | a3e2518 | 2022-04-17 23:46:18 | [diff] [blame] | 774 | cnt = 1; |
drh | bb3c62a | 2022-04-15 19:27:02 | [diff] [blame] | 775 | goto lookupname_end; |
| 776 | }else{ |
| 777 | sqlite3ExprListDelete(db, pFJMatch); |
| 778 | pFJMatch = 0; |
| 779 | } |
| 780 | } |
drh | 7d10d5a | 2008-08-20 16:35:10 | [diff] [blame] | 781 | zErr = cnt==0 ? "no such column" : "ambiguous column name"; |
| 782 | if( zDb ){ |
| 783 | sqlite3ErrorMsg(pParse, "%s: %s.%s.%s", zErr, zDb, zTab, zCol); |
| 784 | }else if( zTab ){ |
| 785 | sqlite3ErrorMsg(pParse, "%s: %s.%s", zErr, zTab, zCol); |
drh | a67d63b | 2024-01-22 15:26:33 | [diff] [blame] | 786 | }else if( cnt==0 && ExprHasProperty(pRight,EP_DblQuoted) ){ |
drh | 727b35c | 2024-01-22 20:49:47 | [diff] [blame] | 787 | sqlite3ErrorMsg(pParse, "%s: \"%s\" - should this be a" |
drh | a67d63b | 2024-01-22 15:26:33 | [diff] [blame] | 788 | " string literal in single-quotes?", |
drh | 727b35c | 2024-01-22 20:49:47 | [diff] [blame] | 789 | zErr, zCol); |
drh | 7d10d5a | 2008-08-20 16:35:10 | [diff] [blame] | 790 | }else{ |
| 791 | sqlite3ErrorMsg(pParse, "%s: %s", zErr, zCol); |
| 792 | } |
drh | 4f77c92 | 2022-02-05 23:11:19 | [diff] [blame] | 793 | sqlite3RecordErrorOffsetOfExpr(pParse->db, pExpr); |
dan | 1db9510 | 2010-06-28 10:15:19 | [diff] [blame] | 794 | pParse->checkSchema = 1; |
drh | 050611a | 2021-04-10 13:37:04 | [diff] [blame] | 795 | pTopNC->nNcErr++; |
drh | 60c435d | 2023-12-19 12:49:35 | [diff] [blame] | 796 | eNewExprOp = TK_NULL; |
drh | 7d10d5a | 2008-08-20 16:35:10 | [diff] [blame] | 797 | } |
drh | bb3c62a | 2022-04-15 19:27:02 | [diff] [blame] | 798 | assert( pFJMatch==0 ); |
drh | 7d10d5a | 2008-08-20 16:35:10 | [diff] [blame] | 799 | |
drh | bdbda1e | 2022-04-16 12:40:52 | [diff] [blame] | 800 | /* Remove all substructure from pExpr */ |
| 801 | if( !ExprHasProperty(pExpr,(EP_TokenOnly|EP_Leaf)) ){ |
| 802 | sqlite3ExprDelete(db, pExpr->pLeft); |
| 803 | pExpr->pLeft = 0; |
| 804 | sqlite3ExprDelete(db, pExpr->pRight); |
| 805 | pExpr->pRight = 0; |
| 806 | ExprSetProperty(pExpr, EP_Leaf); |
| 807 | } |
| 808 | |
drh | 7d10d5a | 2008-08-20 16:35:10 | [diff] [blame] | 809 | /* If a column from a table in pSrcList is referenced, then record |
| 810 | ** this fact in the pSrcList.a[].colUsed bitmask. Column 0 causes |
drh | bf9d099 | 2019-12-08 00:06:39 | [diff] [blame] | 811 | ** bit 0 to be set. Column 1 sets bit 1. And so forth. Bit 63 is |
| 812 | ** set if the 63rd or any subsequent column is used. |
drh | 522ebfa | 2019-11-21 19:37:00 | [diff] [blame] | 813 | ** |
| 814 | ** The colUsed mask is an optimization used to help determine if an |
| 815 | ** index is a covering index. The correct answer is still obtained |
drh | bf9d099 | 2019-12-08 00:06:39 | [diff] [blame] | 816 | ** if the mask contains extra set bits. However, it is important to |
| 817 | ** avoid setting bits beyond the maximum column number of the table. |
| 818 | ** (See ticket [b92e5e8ec2cdbaa1]). |
drh | 522ebfa | 2019-11-21 19:37:00 | [diff] [blame] | 819 | ** |
drh | bf9d099 | 2019-12-08 00:06:39 | [diff] [blame] | 820 | ** If a generated column is referenced, set bits for every column |
| 821 | ** of the table. |
drh | 7d10d5a | 2008-08-20 16:35:10 | [diff] [blame] | 822 | */ |
drh | 0462499 | 2024-05-18 20:00:08 | [diff] [blame] | 823 | if( pMatch ){ |
| 824 | if( pExpr->iColumn>=0 ){ |
| 825 | pMatch->colUsed |= sqlite3ExprColUsed(pExpr); |
| 826 | }else{ |
| 827 | pMatch->fg.rowidUsed = 1; |
| 828 | } |
drh | 7d10d5a | 2008-08-20 16:35:10 | [diff] [blame] | 829 | } |
| 830 | |
drh | eac9fab | 2018-04-16 13:00:50 | [diff] [blame] | 831 | pExpr->op = eNewExprOp; |
drh | b7916a7 | 2009-05-27 10:31:29 | [diff] [blame] | 832 | lookupname_end: |
drh | 7d10d5a | 2008-08-20 16:35:10 | [diff] [blame] | 833 | if( cnt==1 ){ |
| 834 | assert( pNC!=0 ); |
drh | 2d7a691 | 2021-03-01 21:43:25 | [diff] [blame] | 835 | #ifndef SQLITE_OMIT_AUTHORIZATION |
drh | 552562c | 2021-02-04 20:52:20 | [diff] [blame] | 836 | if( pParse->db->xAuth |
drh | 29f6a36 | 2021-02-05 17:34:47 | [diff] [blame] | 837 | && (pExpr->op==TK_COLUMN || pExpr->op==TK_TRIGGER) |
drh | 552562c | 2021-02-04 20:52:20 | [diff] [blame] | 838 | ){ |
drh | a3a5bd9 | 2013-04-13 19:59:58 | [diff] [blame] | 839 | sqlite3AuthRead(pParse, pExpr, pSchema, pNC->pSrcList); |
| 840 | } |
drh | 2d7a691 | 2021-03-01 21:43:25 | [diff] [blame] | 841 | #endif |
drh | 7d10d5a | 2008-08-20 16:35:10 | [diff] [blame] | 842 | /* Increment the nRef value on all name contexts from TopNC up to |
| 843 | ** the point where the name matched. */ |
| 844 | for(;;){ |
| 845 | assert( pTopNC!=0 ); |
| 846 | pTopNC->nRef++; |
| 847 | if( pTopNC==pNC ) break; |
| 848 | pTopNC = pTopNC->pNext; |
| 849 | } |
drh | f7828b5 | 2009-06-15 23:15:59 | [diff] [blame] | 850 | return WRC_Prune; |
drh | 7d10d5a | 2008-08-20 16:35:10 | [diff] [blame] | 851 | } else { |
drh | f7828b5 | 2009-06-15 23:15:59 | [diff] [blame] | 852 | return WRC_Abort; |
drh | 7d10d5a | 2008-08-20 16:35:10 | [diff] [blame] | 853 | } |
| 854 | } |
| 855 | |
| 856 | /* |
dan | f7b0b0a | 2009-10-19 15:52:32 | [diff] [blame] | 857 | ** Allocate and return a pointer to an expression to load the column iCol |
drh | 9e48165 | 2010-04-08 17:35:34 | [diff] [blame] | 858 | ** from datasource iSrc in SrcList pSrc. |
dan | f7b0b0a | 2009-10-19 15:52:32 | [diff] [blame] | 859 | */ |
| 860 | Expr *sqlite3CreateColumnExpr(sqlite3 *db, SrcList *pSrc, int iSrc, int iCol){ |
| 861 | Expr *p = sqlite3ExprAlloc(db, TK_COLUMN, 0, 0); |
| 862 | if( p ){ |
drh | 7601294 | 2021-02-21 21:04:54 | [diff] [blame] | 863 | SrcItem *pItem = &pSrc->a[iSrc]; |
drh | 477572b | 2021-10-07 20:46:29 | [diff] [blame] | 864 | Table *pTab; |
| 865 | assert( ExprUseYTab(p) ); |
drh | b204b6a | 2024-08-17 23:23:23 | [diff] [blame] | 866 | pTab = p->y.pTab = pItem->pSTab; |
dan | f7b0b0a | 2009-10-19 15:52:32 | [diff] [blame] | 867 | p->iTable = pItem->iCursor; |
drh | eda079c | 2018-09-20 19:02:15 | [diff] [blame] | 868 | if( p->y.pTab->iPKey==iCol ){ |
dan | f7b0b0a | 2009-10-19 15:52:32 | [diff] [blame] | 869 | p->iColumn = -1; |
| 870 | }else{ |
drh | 8677d30 | 2009-11-04 13:17:14 | [diff] [blame] | 871 | p->iColumn = (ynVar)iCol; |
drh | 0824d5b | 2019-12-09 18:22:17 | [diff] [blame] | 872 | if( (pTab->tabFlags & TF_HasGenerated)!=0 |
| 873 | && (pTab->aCol[iCol].colFlags & COLFLAG_GENERATED)!=0 |
| 874 | ){ |
| 875 | testcase( pTab->nCol==63 ); |
| 876 | testcase( pTab->nCol==64 ); |
| 877 | pItem->colUsed = pTab->nCol>=64 ? ALLBITS : MASKBIT(pTab->nCol)-1; |
drh | 926f796 | 2019-12-09 17:14:48 | [diff] [blame] | 878 | }else{ |
| 879 | testcase( iCol==BMS ); |
| 880 | testcase( iCol==BMS-1 ); |
| 881 | pItem->colUsed |= ((Bitmask)1)<<(iCol>=BMS ? BMS-1 : iCol); |
| 882 | } |
dan | f7b0b0a | 2009-10-19 15:52:32 | [diff] [blame] | 883 | } |
dan | f7b0b0a | 2009-10-19 15:52:32 | [diff] [blame] | 884 | } |
| 885 | return p; |
| 886 | } |
| 887 | |
| 888 | /* |
drh | a514b8e | 2015-08-25 00:27:06 | [diff] [blame] | 889 | ** Report an error that an expression is not valid for some set of |
drh | 03bf26d | 2015-08-31 21:16:36 | [diff] [blame] | 890 | ** pNC->ncFlags values determined by validMask. |
drh | 77318a3 | 2019-12-12 15:19:18 | [diff] [blame] | 891 | ** |
| 892 | ** static void notValid( |
| 893 | ** Parse *pParse, // Leave error message here |
larrybr | bc91738 | 2023-06-07 08:40:31 | [diff] [blame] | 894 | ** NameContext *pNC, // The name context |
drh | 77318a3 | 2019-12-12 15:19:18 | [diff] [blame] | 895 | ** const char *zMsg, // Type of error |
| 896 | ** int validMask, // Set of contexts for which prohibited |
| 897 | ** Expr *pExpr // Invalidate this expression on error |
| 898 | ** ){...} |
| 899 | ** |
| 900 | ** As an optimization, since the conditional is almost always false |
| 901 | ** (because errors are rare), the conditional is moved outside of the |
| 902 | ** function call using a macro. |
drh | 3780be1 | 2013-07-31 19:05:22 | [diff] [blame] | 903 | */ |
drh | 77318a3 | 2019-12-12 15:19:18 | [diff] [blame] | 904 | static void notValidImpl( |
| 905 | Parse *pParse, /* Leave error message here */ |
| 906 | NameContext *pNC, /* The name context */ |
| 907 | const char *zMsg, /* Type of error */ |
drh | 62fc069 | 2022-02-06 00:30:04 | [diff] [blame] | 908 | Expr *pExpr, /* Invalidate this expression on error */ |
| 909 | Expr *pError /* Associate error with this expression */ |
drh | 3780be1 | 2013-07-31 19:05:22 | [diff] [blame] | 910 | ){ |
drh | 77318a3 | 2019-12-12 15:19:18 | [diff] [blame] | 911 | const char *zIn = "partial index WHERE clauses"; |
| 912 | if( pNC->ncFlags & NC_IdxExpr ) zIn = "index expressions"; |
drh | 3780be1 | 2013-07-31 19:05:22 | [diff] [blame] | 913 | #ifndef SQLITE_OMIT_CHECK |
drh | 77318a3 | 2019-12-12 15:19:18 | [diff] [blame] | 914 | else if( pNC->ncFlags & NC_IsCheck ) zIn = "CHECK constraints"; |
drh | a514b8e | 2015-08-25 00:27:06 | [diff] [blame] | 915 | #endif |
drh | 81f7b37 | 2019-10-16 12:18:59 | [diff] [blame] | 916 | #ifndef SQLITE_OMIT_GENERATED_COLUMNS |
drh | 77318a3 | 2019-12-12 15:19:18 | [diff] [blame] | 917 | else if( pNC->ncFlags & NC_GenCol ) zIn = "generated columns"; |
drh | 81f7b37 | 2019-10-16 12:18:59 | [diff] [blame] | 918 | #endif |
drh | 77318a3 | 2019-12-12 15:19:18 | [diff] [blame] | 919 | sqlite3ErrorMsg(pParse, "%s prohibited in %s", zMsg, zIn); |
| 920 | if( pExpr ) pExpr->op = TK_NULL; |
drh | 62fc069 | 2022-02-06 00:30:04 | [diff] [blame] | 921 | sqlite3RecordErrorOffsetOfExpr(pParse->db, pError); |
drh | 3780be1 | 2013-07-31 19:05:22 | [diff] [blame] | 922 | } |
drh | 62fc069 | 2022-02-06 00:30:04 | [diff] [blame] | 923 | #define sqlite3ResolveNotValid(P,N,M,X,E,R) \ |
drh | 77318a3 | 2019-12-12 15:19:18 | [diff] [blame] | 924 | assert( ((X)&~(NC_IsCheck|NC_PartIdx|NC_IdxExpr|NC_GenCol))==0 ); \ |
drh | 62fc069 | 2022-02-06 00:30:04 | [diff] [blame] | 925 | if( ((N)->ncFlags & (X))!=0 ) notValidImpl(P,N,M,E,R); |
drh | 3780be1 | 2013-07-31 19:05:22 | [diff] [blame] | 926 | |
drh | cca9f3d | 2013-09-06 15:23:29 | [diff] [blame] | 927 | /* |
| 928 | ** Expression p should encode a floating point value between 1.0 and 0.0. |
drh | daa4cdf | 2025-07-18 19:17:32 | [diff] [blame] | 929 | ** Return 134,217,728 (2^27) times this value. Or return -1 if p is not |
| 930 | ** a floating point value between 1.0 and 0.0. |
drh | cca9f3d | 2013-09-06 15:23:29 | [diff] [blame] | 931 | */ |
| 932 | static int exprProbability(Expr *p){ |
| 933 | double r = -1.0; |
| 934 | if( p->op!=TK_FLOAT ) return -1; |
drh | f975107 | 2021-10-07 13:40:29 | [diff] [blame] | 935 | assert( !ExprHasProperty(p, EP_IntValue) ); |
drh | cca9f3d | 2013-09-06 15:23:29 | [diff] [blame] | 936 | sqlite3AtoF(p->u.zToken, &r, sqlite3Strlen30(p->u.zToken), SQLITE_UTF8); |
drh | 09328c0 | 2013-09-11 14:34:58 | [diff] [blame] | 937 | assert( r>=0.0 ); |
| 938 | if( r>1.0 ) return -1; |
drh | d05ab6a | 2014-10-25 13:42:16 | [diff] [blame] | 939 | return (int)(r*134217728.0); |
drh | cca9f3d | 2013-09-06 15:23:29 | [diff] [blame] | 940 | } |
drh | 3780be1 | 2013-07-31 19:05:22 | [diff] [blame] | 941 | |
| 942 | /* |
drh | 7d10d5a | 2008-08-20 16:35:10 | [diff] [blame] | 943 | ** This routine is callback for sqlite3WalkExpr(). |
| 944 | ** |
| 945 | ** Resolve symbolic names into TK_COLUMN operators for the current |
| 946 | ** node in the expression tree. Return 0 to continue the search down |
| 947 | ** the tree or 2 to abort the tree walk. |
| 948 | ** |
| 949 | ** This routine also does error checking and name resolution for |
| 950 | ** function names. The operator for aggregate functions is changed |
| 951 | ** to TK_AGG_FUNCTION. |
| 952 | */ |
| 953 | static int resolveExprStep(Walker *pWalker, Expr *pExpr){ |
| 954 | NameContext *pNC; |
| 955 | Parse *pParse; |
| 956 | |
drh | 7d10d5a | 2008-08-20 16:35:10 | [diff] [blame] | 957 | pNC = pWalker->u.pNC; |
| 958 | assert( pNC!=0 ); |
| 959 | pParse = pNC->pParse; |
| 960 | assert( pParse==pWalker->pParse ); |
| 961 | |
drh | 7d10d5a | 2008-08-20 16:35:10 | [diff] [blame] | 962 | #ifndef NDEBUG |
| 963 | if( pNC->pSrcList && pNC->pSrcList->nAlloc>0 ){ |
| 964 | SrcList *pSrcList = pNC->pSrcList; |
| 965 | int i; |
| 966 | for(i=0; i<pNC->pSrcList->nSrc; i++){ |
| 967 | assert( pSrcList->a[i].iCursor>=0 && pSrcList->a[i].iCursor<pParse->nTab); |
| 968 | } |
| 969 | } |
| 970 | #endif |
| 971 | switch( pExpr->op ){ |
drh | 41204f1 | 2008-10-06 13:54:35 | [diff] [blame] | 972 | |
drh | 41204f1 | 2008-10-06 13:54:35 | [diff] [blame] | 973 | /* The special operator TK_ROW means use the rowid for the first |
| 974 | ** column in the FROM clause. This is used by the LIMIT and ORDER BY |
larrybr | bc91738 | 2023-06-07 08:40:31 | [diff] [blame] | 975 | ** clause processing on UPDATE and DELETE statements, and by |
dan | be952c1 | 2020-07-13 20:10:29 | [diff] [blame] | 976 | ** UPDATE ... FROM statement processing. |
drh | 41204f1 | 2008-10-06 13:54:35 | [diff] [blame] | 977 | */ |
| 978 | case TK_ROW: { |
| 979 | SrcList *pSrcList = pNC->pSrcList; |
drh | 7601294 | 2021-02-21 21:04:54 | [diff] [blame] | 980 | SrcItem *pItem; |
dan | f2972b6 | 2020-04-29 20:11:01 | [diff] [blame] | 981 | assert( pSrcList && pSrcList->nSrc>=1 ); |
drh | 2fba394 | 2017-11-09 03:55:09 | [diff] [blame] | 982 | pItem = pSrcList->a; |
drh | 41204f1 | 2008-10-06 13:54:35 | [diff] [blame] | 983 | pExpr->op = TK_COLUMN; |
drh | 477572b | 2021-10-07 20:46:29 | [diff] [blame] | 984 | assert( ExprUseYTab(pExpr) ); |
drh | b204b6a | 2024-08-17 23:23:23 | [diff] [blame] | 985 | pExpr->y.pTab = pItem->pSTab; |
drh | 41204f1 | 2008-10-06 13:54:35 | [diff] [blame] | 986 | pExpr->iTable = pItem->iCursor; |
dan | 576d5a8 | 2020-07-15 18:30:01 | [diff] [blame] | 987 | pExpr->iColumn--; |
drh | af97c3f | 2019-08-08 18:49:16 | [diff] [blame] | 988 | pExpr->affExpr = SQLITE_AFF_INTEGER; |
drh | 41204f1 | 2008-10-06 13:54:35 | [diff] [blame] | 989 | break; |
| 990 | } |
drh | 41204f1 | 2008-10-06 13:54:35 | [diff] [blame] | 991 | |
drh | 45e4cb8 | 2021-03-09 19:52:15 | [diff] [blame] | 992 | /* An optimization: Attempt to convert |
dan | 8ddf686 | 2021-02-26 20:14:32 | [diff] [blame] | 993 | ** |
drh | 45e4cb8 | 2021-03-09 19:52:15 | [diff] [blame] | 994 | ** "expr IS NOT NULL" --> "TRUE" |
| 995 | ** "expr IS NULL" --> "FALSE" |
| 996 | ** |
| 997 | ** if we can prove that "expr" is never NULL. Call this the |
| 998 | ** "NOT NULL strength reduction optimization". |
| 999 | ** |
| 1000 | ** If this optimization occurs, also restore the NameContext ref-counts |
| 1001 | ** to the state they where in before the "column" LHS expression was |
| 1002 | ** resolved. This prevents "column" from being counted as having been |
| 1003 | ** referenced, which might prevent a SELECT from being erroneously |
| 1004 | ** marked as correlated. |
drh | 61b77a6 | 2024-03-08 21:37:18 | [diff] [blame] | 1005 | ** |
| 1006 | ** 2024-03-28: Beware of aggregates. A bare column of aggregated table |
| 1007 | ** can still evaluate to NULL even though it is marked as NOT NULL. |
| 1008 | ** Example: |
| 1009 | ** |
| 1010 | ** CREATE TABLE t1(a INT NOT NULL); |
| 1011 | ** SELECT a, a IS NULL, a IS NOT NULL, count(*) FROM t1; |
| 1012 | ** |
| 1013 | ** The "a IS NULL" and "a IS NOT NULL" expressions cannot be optimized |
| 1014 | ** here because at the time this case is hit, we do not yet know whether |
| 1015 | ** or not t1 is being aggregated. We have to assume the worst and omit |
| 1016 | ** the optimization. The only time it is safe to apply this optimization |
| 1017 | ** is within the WHERE clause. |
dan | 8ddf686 | 2021-02-26 20:14:32 | [diff] [blame] | 1018 | */ |
| 1019 | case TK_NOTNULL: |
| 1020 | case TK_ISNULL: { |
| 1021 | int anRef[8]; |
| 1022 | NameContext *p; |
| 1023 | int i; |
larrybr | bc91738 | 2023-06-07 08:40:31 | [diff] [blame] | 1024 | for(i=0, p=pNC; p && i<ArraySize(anRef); p=p->pNext, i++){ |
dan | 8ddf686 | 2021-02-26 20:14:32 | [diff] [blame] | 1025 | anRef[i] = p->nRef; |
| 1026 | } |
| 1027 | sqlite3WalkExpr(pWalker, pExpr->pLeft); |
drh | 61b77a6 | 2024-03-08 21:37:18 | [diff] [blame] | 1028 | if( IN_RENAME_OBJECT ) return WRC_Prune; |
| 1029 | if( sqlite3ExprCanBeNull(pExpr->pLeft) ){ |
| 1030 | /* The expression can be NULL. So the optimization does not apply */ |
| 1031 | return WRC_Prune; |
dan | 8ddf686 | 2021-02-26 20:14:32 | [diff] [blame] | 1032 | } |
drh | 61b77a6 | 2024-03-08 21:37:18 | [diff] [blame] | 1033 | |
| 1034 | for(i=0, p=pNC; p; p=p->pNext, i++){ |
| 1035 | if( (p->ncFlags & NC_Where)==0 ){ |
| 1036 | return WRC_Prune; /* Not in a WHERE clause. Unsafe to optimize. */ |
| 1037 | } |
| 1038 | } |
| 1039 | testcase( ExprHasProperty(pExpr, EP_OuterON) ); |
| 1040 | assert( !ExprHasProperty(pExpr, EP_IntValue) ); |
| 1041 | #if TREETRACE_ENABLED |
| 1042 | if( sqlite3TreeTrace & 0x80000 ){ |
| 1043 | sqlite3DebugPrintf( |
| 1044 | "NOT NULL strength reduction converts the following to %d:\n", |
| 1045 | pExpr->op==TK_NOTNULL |
| 1046 | ); |
| 1047 | sqlite3ShowExpr(pExpr); |
| 1048 | } |
| 1049 | #endif /* TREETRACE_ENABLED */ |
| 1050 | pExpr->u.iValue = (pExpr->op==TK_NOTNULL); |
| 1051 | pExpr->flags |= EP_IntValue; |
| 1052 | pExpr->op = TK_INTEGER; |
| 1053 | for(i=0, p=pNC; p && i<ArraySize(anRef); p=p->pNext, i++){ |
| 1054 | p->nRef = anRef[i]; |
| 1055 | } |
| 1056 | sqlite3ExprDelete(pParse->db, pExpr->pLeft); |
| 1057 | pExpr->pLeft = 0; |
dan | 8ddf686 | 2021-02-26 20:14:32 | [diff] [blame] | 1058 | return WRC_Prune; |
| 1059 | } |
| 1060 | |
drh | 3cf48e3 | 2017-03-07 03:25:52 | [diff] [blame] | 1061 | /* A column name: ID |
| 1062 | ** Or table name and column name: ID.ID |
drh | 7d10d5a | 2008-08-20 16:35:10 | [diff] [blame] | 1063 | ** Or a database, table and column: ID.ID.ID |
drh | 3cf48e3 | 2017-03-07 03:25:52 | [diff] [blame] | 1064 | ** |
| 1065 | ** The TK_ID and TK_OUT cases are combined so that there will only |
larrybr | bc91738 | 2023-06-07 08:40:31 | [diff] [blame] | 1066 | ** be one call to lookupName(). Then the compiler will in-line |
drh | 3cf48e3 | 2017-03-07 03:25:52 | [diff] [blame] | 1067 | ** lookupName() for a size reduction and performance increase. |
drh | 7d10d5a | 2008-08-20 16:35:10 | [diff] [blame] | 1068 | */ |
drh | 3cf48e3 | 2017-03-07 03:25:52 | [diff] [blame] | 1069 | case TK_ID: |
drh | 7d10d5a | 2008-08-20 16:35:10 | [diff] [blame] | 1070 | case TK_DOT: { |
drh | b7916a7 | 2009-05-27 10:31:29 | [diff] [blame] | 1071 | const char *zTable; |
| 1072 | const char *zDb; |
drh | 7d10d5a | 2008-08-20 16:35:10 | [diff] [blame] | 1073 | Expr *pRight; |
| 1074 | |
drh | 3cf48e3 | 2017-03-07 03:25:52 | [diff] [blame] | 1075 | if( pExpr->op==TK_ID ){ |
drh | b7916a7 | 2009-05-27 10:31:29 | [diff] [blame] | 1076 | zDb = 0; |
drh | 3cf48e3 | 2017-03-07 03:25:52 | [diff] [blame] | 1077 | zTable = 0; |
drh | f975107 | 2021-10-07 13:40:29 | [diff] [blame] | 1078 | assert( !ExprHasProperty(pExpr, EP_IntValue) ); |
drh | a67d63b | 2024-01-22 15:26:33 | [diff] [blame] | 1079 | pRight = pExpr; |
drh | 7d10d5a | 2008-08-20 16:35:10 | [diff] [blame] | 1080 | }else{ |
dan | c9461ec | 2018-08-29 21:00:16 | [diff] [blame] | 1081 | Expr *pLeft = pExpr->pLeft; |
drh | 77318a3 | 2019-12-12 15:19:18 | [diff] [blame] | 1082 | testcase( pNC->ncFlags & NC_IdxExpr ); |
| 1083 | testcase( pNC->ncFlags & NC_GenCol ); |
| 1084 | sqlite3ResolveNotValid(pParse, pNC, "the \".\" operator", |
drh | 62fc069 | 2022-02-06 00:30:04 | [diff] [blame] | 1085 | NC_IdxExpr|NC_GenCol, 0, pExpr); |
drh | 3cf48e3 | 2017-03-07 03:25:52 | [diff] [blame] | 1086 | pRight = pExpr->pRight; |
| 1087 | if( pRight->op==TK_ID ){ |
| 1088 | zDb = 0; |
drh | 3cf48e3 | 2017-03-07 03:25:52 | [diff] [blame] | 1089 | }else{ |
| 1090 | assert( pRight->op==TK_DOT ); |
drh | f975107 | 2021-10-07 13:40:29 | [diff] [blame] | 1091 | assert( !ExprHasProperty(pRight, EP_IntValue) ); |
dan | c9461ec | 2018-08-29 21:00:16 | [diff] [blame] | 1092 | zDb = pLeft->u.zToken; |
| 1093 | pLeft = pRight->pLeft; |
dan | cf8f289 | 2018-08-09 20:47:01 | [diff] [blame] | 1094 | pRight = pRight->pRight; |
| 1095 | } |
drh | 477572b | 2021-10-07 20:46:29 | [diff] [blame] | 1096 | assert( ExprUseUToken(pLeft) && ExprUseUToken(pRight) ); |
dan | c9461ec | 2018-08-29 21:00:16 | [diff] [blame] | 1097 | zTable = pLeft->u.zToken; |
drh | 477572b | 2021-10-07 20:46:29 | [diff] [blame] | 1098 | assert( ExprUseYTab(pExpr) ); |
dan | c9461ec | 2018-08-29 21:00:16 | [diff] [blame] | 1099 | if( IN_RENAME_OBJECT ){ |
dan | 07e9523 | 2018-08-21 16:32:53 | [diff] [blame] | 1100 | sqlite3RenameTokenRemap(pParse, (void*)pExpr, (void*)pRight); |
drh | eda079c | 2018-09-20 19:02:15 | [diff] [blame] | 1101 | sqlite3RenameTokenRemap(pParse, (void*)&pExpr->y.pTab, (void*)pLeft); |
dan | c9461ec | 2018-08-29 21:00:16 | [diff] [blame] | 1102 | } |
drh | 7d10d5a | 2008-08-20 16:35:10 | [diff] [blame] | 1103 | } |
drh | a67d63b | 2024-01-22 15:26:33 | [diff] [blame] | 1104 | return lookupName(pParse, zDb, zTable, pRight, pNC, pExpr); |
drh | 7d10d5a | 2008-08-20 16:35:10 | [diff] [blame] | 1105 | } |
| 1106 | |
| 1107 | /* Resolve function names |
| 1108 | */ |
drh | 7d10d5a | 2008-08-20 16:35:10 | [diff] [blame] | 1109 | case TK_FUNCTION: { |
drh | 7f0e0c7 | 2024-09-20 12:58:15 | [diff] [blame] | 1110 | ExprList *pList; /* The argument list */ |
| 1111 | int n; /* Number of arguments */ |
drh | 7d10d5a | 2008-08-20 16:35:10 | [diff] [blame] | 1112 | int no_such_func = 0; /* True if no such function exists */ |
| 1113 | int wrong_num_args = 0; /* True if wrong number of arguments */ |
| 1114 | int is_agg = 0; /* True if is an aggregate function */ |
drh | 7d10d5a | 2008-08-20 16:35:10 | [diff] [blame] | 1115 | const char *zId; /* The function name. */ |
| 1116 | FuncDef *pDef; /* Information about the function */ |
drh | ea67883 | 2008-12-10 19:26:22 | [diff] [blame] | 1117 | u8 enc = ENC(pParse->db); /* The database encoding */ |
dan | 4ded26a | 2019-03-28 16:15:05 | [diff] [blame] | 1118 | int savedAllowFlags = (pNC->ncFlags & (NC_AllowAgg | NC_AllowWin)); |
dan | 4f9adee | 2019-07-13 16:22:50 | [diff] [blame] | 1119 | #ifndef SQLITE_OMIT_WINDOWFUNC |
| 1120 | Window *pWin = (IsWindowFunc(pExpr) ? pExpr->y.pWin : 0); |
| 1121 | #endif |
drh | f975107 | 2021-10-07 13:40:29 | [diff] [blame] | 1122 | assert( !ExprHasProperty(pExpr, EP_xIsSelect|EP_IntValue) ); |
drh | db19f48 | 2023-10-18 13:58:31 | [diff] [blame] | 1123 | assert( pExpr->pLeft==0 || pExpr->pLeft->op==TK_ORDER ); |
drh | 7f0e0c7 | 2024-09-20 12:58:15 | [diff] [blame] | 1124 | pList = pExpr->x.pList; |
| 1125 | n = pList ? pList->nExpr : 0; |
drh | 33e619f | 2009-05-28 01:00:55 | [diff] [blame] | 1126 | zId = pExpr->u.zToken; |
drh | 80738d9 | 2016-02-15 00:34:16 | [diff] [blame] | 1127 | pDef = sqlite3FindFunction(pParse->db, zId, n, enc, 0); |
drh | 7d10d5a | 2008-08-20 16:35:10 | [diff] [blame] | 1128 | if( pDef==0 ){ |
drh | 80738d9 | 2016-02-15 00:34:16 | [diff] [blame] | 1129 | pDef = sqlite3FindFunction(pParse->db, zId, -2, enc, 0); |
drh | 7d10d5a | 2008-08-20 16:35:10 | [diff] [blame] | 1130 | if( pDef==0 ){ |
| 1131 | no_such_func = 1; |
| 1132 | }else{ |
| 1133 | wrong_num_args = 1; |
| 1134 | } |
| 1135 | }else{ |
drh | 2d80151 | 2016-01-14 22:19:58 | [diff] [blame] | 1136 | is_agg = pDef->xFinalize!=0; |
drh | cca9f3d | 2013-09-06 15:23:29 | [diff] [blame] | 1137 | if( pDef->funcFlags & SQLITE_FUNC_UNLIKELY ){ |
drh | a7d6db6 | 2019-06-11 21:02:15 | [diff] [blame] | 1138 | ExprSetProperty(pExpr, EP_Unlikely); |
drh | cca9f3d | 2013-09-06 15:23:29 | [diff] [blame] | 1139 | if( n==2 ){ |
| 1140 | pExpr->iTable = exprProbability(pList->a[1].pExpr); |
| 1141 | if( pExpr->iTable<0 ){ |
drh | 31e7147 | 2015-02-09 10:20:19 | [diff] [blame] | 1142 | sqlite3ErrorMsg(pParse, |
drh | 62fc069 | 2022-02-06 00:30:04 | [diff] [blame] | 1143 | "second argument to %#T() must be a " |
| 1144 | "constant between 0.0 and 1.0", pExpr); |
drh | 050611a | 2021-04-10 13:37:04 | [diff] [blame] | 1145 | pNC->nNcErr++; |
drh | cca9f3d | 2013-09-06 15:23:29 | [diff] [blame] | 1146 | } |
| 1147 | }else{ |
drh | 31e7147 | 2015-02-09 10:20:19 | [diff] [blame] | 1148 | /* EVIDENCE-OF: R-61304-29449 The unlikely(X) function is |
| 1149 | ** equivalent to likelihood(X, 0.0625). |
| 1150 | ** EVIDENCE-OF: R-01283-11636 The unlikely(X) function is |
| 1151 | ** short-hand for likelihood(X,0.0625). |
| 1152 | ** EVIDENCE-OF: R-36850-34127 The likely(X) function is short-hand |
| 1153 | ** for likelihood(X,0.9375). |
| 1154 | ** EVIDENCE-OF: R-53436-40973 The likely(X) function is equivalent |
| 1155 | ** to likelihood(X,0.9375). */ |
drh | 03202a9 | 2014-06-17 16:11:28 | [diff] [blame] | 1156 | /* TUNING: unlikely() probability is 0.0625. likely() is 0.9375 */ |
drh | d05ab6a | 2014-10-25 13:42:16 | [diff] [blame] | 1157 | pExpr->iTable = pDef->zName[0]=='u' ? 8388608 : 125829120; |
larrybr | bc91738 | 2023-06-07 08:40:31 | [diff] [blame] | 1158 | } |
drh | cca9f3d | 2013-09-06 15:23:29 | [diff] [blame] | 1159 | } |
drh | 7d10d5a | 2008-08-20 16:35:10 | [diff] [blame] | 1160 | #ifndef SQLITE_OMIT_AUTHORIZATION |
drh | a0daa75 | 2016-09-16 11:53:10 | [diff] [blame] | 1161 | { |
| 1162 | int auth = sqlite3AuthCheck(pParse, SQLITE_FUNCTION, 0,pDef->zName,0); |
| 1163 | if( auth!=SQLITE_OK ){ |
| 1164 | if( auth==SQLITE_DENY ){ |
drh | 62fc069 | 2022-02-06 00:30:04 | [diff] [blame] | 1165 | sqlite3ErrorMsg(pParse, "not authorized to use function: %#T", |
| 1166 | pExpr); |
drh | 050611a | 2021-04-10 13:37:04 | [diff] [blame] | 1167 | pNC->nNcErr++; |
drh | a0daa75 | 2016-09-16 11:53:10 | [diff] [blame] | 1168 | } |
| 1169 | pExpr->op = TK_NULL; |
| 1170 | return WRC_Prune; |
drh | 7d10d5a | 2008-08-20 16:35:10 | [diff] [blame] | 1171 | } |
drh | 7d10d5a | 2008-08-20 16:35:10 | [diff] [blame] | 1172 | } |
drh | 9588ad9 | 2014-09-15 14:46:02 | [diff] [blame] | 1173 | #endif |
dan | d564bdb | 2024-10-05 18:10:02 | [diff] [blame] | 1174 | |
| 1175 | /* If the function may call sqlite3_value_subtype(), then set the |
| 1176 | ** EP_SubtArg flag on all of its argument expressions. This prevents |
| 1177 | ** where.c from replacing the expression with a value read from an |
| 1178 | ** index on the same expression, which will not have the correct |
| 1179 | ** subtype. Also set the flag if the function expression itself is |
| 1180 | ** an EP_SubtArg expression. In this case subtypes are required as |
| 1181 | ** the function may return a value with a subtype back to its |
| 1182 | ** caller using sqlite3_result_value(). */ |
| 1183 | if( (pDef->funcFlags & SQLITE_SUBTYPE) |
| 1184 | || ExprHasProperty(pExpr, EP_SubtArg) |
| 1185 | ){ |
| 1186 | int ii; |
| 1187 | for(ii=0; ii<n; ii++){ |
| 1188 | ExprSetProperty(pList->a[ii].pExpr, EP_SubtArg); |
| 1189 | } |
| 1190 | } |
| 1191 | |
drh | a7f910b | 2015-09-01 13:17:17 | [diff] [blame] | 1192 | if( pDef->funcFlags & (SQLITE_FUNC_CONSTANT|SQLITE_FUNC_SLOCHNG) ){ |
drh | 1d85e40 | 2015-08-31 17:34:41 | [diff] [blame] | 1193 | /* For the purposes of the EP_ConstFunc flag, date and time |
drh | 03bf26d | 2015-08-31 21:16:36 | [diff] [blame] | 1194 | ** functions and other functions that change slowly are considered |
drh | 20cee7d | 2019-10-30 18:50:08 | [diff] [blame] | 1195 | ** constant because they are constant for the duration of one query. |
| 1196 | ** This allows them to be factored out of inner loops. */ |
drh | 63f8457 | 2015-02-09 14:07:07 | [diff] [blame] | 1197 | ExprSetProperty(pExpr,EP_ConstFunc); |
drh | 1d85e40 | 2015-08-31 17:34:41 | [diff] [blame] | 1198 | } |
| 1199 | if( (pDef->funcFlags & SQLITE_FUNC_CONSTANT)==0 ){ |
drh | fe70460 | 2020-01-18 21:34:31 | [diff] [blame] | 1200 | /* Clearly non-deterministic functions like random(), but also |
| 1201 | ** date/time functions that use 'now', and other functions like |
drh | a7f910b | 2015-09-01 13:17:17 | [diff] [blame] | 1202 | ** sqlite_version() that might change over time cannot be used |
drh | fe70460 | 2020-01-18 21:34:31 | [diff] [blame] | 1203 | ** in an index or generated column. Curiously, they can be used |
| 1204 | ** in a CHECK constraint. SQLServer, MySQL, and PostgreSQL all |
stephan | cc28137 | 2025-05-08 13:51:55 | [diff] [blame] | 1205 | ** allow this. */ |
drh | 77318a3 | 2019-12-12 15:19:18 | [diff] [blame] | 1206 | sqlite3ResolveNotValid(pParse, pNC, "non-deterministic functions", |
drh | 62fc069 | 2022-02-06 00:30:04 | [diff] [blame] | 1207 | NC_IdxExpr|NC_PartIdx|NC_GenCol, 0, pExpr); |
drh | 20cee7d | 2019-10-30 18:50:08 | [diff] [blame] | 1208 | }else{ |
| 1209 | assert( (NC_SelfRef & 0xff)==NC_SelfRef ); /* Must fit in 8 bits */ |
| 1210 | pExpr->op2 = pNC->ncFlags & NC_SelfRef; |
drh | 31e7147 | 2015-02-09 10:20:19 | [diff] [blame] | 1211 | } |
drh | eea8eb6 | 2018-11-26 18:09:15 | [diff] [blame] | 1212 | if( (pDef->funcFlags & SQLITE_FUNC_INTERNAL)!=0 |
| 1213 | && pParse->nested==0 |
drh | 171c50e | 2020-01-01 15:43:30 | [diff] [blame] | 1214 | && (pParse->db->mDbFlags & DBFLAG_InternalFunc)==0 |
drh | eea8eb6 | 2018-11-26 18:09:15 | [diff] [blame] | 1215 | ){ |
| 1216 | /* Internal-use-only functions are disallowed unless the |
drh | 0dfa525 | 2020-01-08 17:28:19 | [diff] [blame] | 1217 | ** SQL is being compiled using sqlite3NestedParse() or |
| 1218 | ** the SQLITE_TESTCTRL_INTERNAL_FUNCTIONS test-control has be |
drh | 7f1c111 | 2022-02-03 18:14:22 | [diff] [blame] | 1219 | ** used to activate internal functions for testing purposes */ |
drh | eea8eb6 | 2018-11-26 18:09:15 | [diff] [blame] | 1220 | no_such_func = 1; |
| 1221 | pDef = 0; |
drh | 2eeca20 | 2020-01-08 20:37:45 | [diff] [blame] | 1222 | }else |
| 1223 | if( (pDef->funcFlags & (SQLITE_FUNC_DIRECT|SQLITE_FUNC_UNSAFE))!=0 |
| 1224 | && !IN_RENAME_OBJECT |
| 1225 | ){ |
drh | 733aff3 | 2025-05-08 16:18:18 | [diff] [blame] | 1226 | if( pNC->ncFlags & NC_FromDDL ) ExprSetProperty(pExpr, EP_FromDDL); |
drh | 2eeca20 | 2020-01-08 20:37:45 | [diff] [blame] | 1227 | sqlite3ExprFunctionUsable(pParse, pExpr, pDef); |
drh | eea8eb6 | 2018-11-26 18:09:15 | [diff] [blame] | 1228 | } |
drh | 7d10d5a | 2008-08-20 16:35:10 | [diff] [blame] | 1229 | } |
dan | 26522d1 | 2018-06-11 18:16:51 | [diff] [blame] | 1230 | |
dan | c9461ec | 2018-08-29 21:00:16 | [diff] [blame] | 1231 | if( 0==IN_RENAME_OBJECT ){ |
dan | 67a9b8e | 2018-06-22 20:51:35 | [diff] [blame] | 1232 | #ifndef SQLITE_OMIT_WINDOWFUNC |
dan | dabc268 | 2018-08-17 17:18:16 | [diff] [blame] | 1233 | assert( is_agg==0 || (pDef->funcFlags & SQLITE_FUNC_MINMAX) |
dan | 7262ca9 | 2018-07-02 12:07:32 | [diff] [blame] | 1234 | || (pDef->xValue==0 && pDef->xInverse==0) |
| 1235 | || (pDef->xValue && pDef->xInverse && pDef->xSFunc && pDef->xFinalize) |
dan | 26522d1 | 2018-06-11 18:16:51 | [diff] [blame] | 1236 | ); |
dan | 8117f11 | 2019-07-10 20:16:53 | [diff] [blame] | 1237 | if( pDef && pDef->xValue==0 && pWin ){ |
larrybr | bc91738 | 2023-06-07 08:40:31 | [diff] [blame] | 1238 | sqlite3ErrorMsg(pParse, |
drh | 62fc069 | 2022-02-06 00:30:04 | [diff] [blame] | 1239 | "%#T() may not be used as a window function", pExpr |
dan | dabc268 | 2018-08-17 17:18:16 | [diff] [blame] | 1240 | ); |
drh | 050611a | 2021-04-10 13:37:04 | [diff] [blame] | 1241 | pNC->nNcErr++; |
larrybr | bc91738 | 2023-06-07 08:40:31 | [diff] [blame] | 1242 | }else if( |
dan | dabc268 | 2018-08-17 17:18:16 | [diff] [blame] | 1243 | (is_agg && (pNC->ncFlags & NC_AllowAgg)==0) |
dan | 8117f11 | 2019-07-10 20:16:53 | [diff] [blame] | 1244 | || (is_agg && (pDef->funcFlags&SQLITE_FUNC_WINDOW) && !pWin) |
| 1245 | || (is_agg && pWin && (pNC->ncFlags & NC_AllowWin)==0) |
dan | dabc268 | 2018-08-17 17:18:16 | [diff] [blame] | 1246 | ){ |
| 1247 | const char *zType; |
dan | 8117f11 | 2019-07-10 20:16:53 | [diff] [blame] | 1248 | if( (pDef->funcFlags & SQLITE_FUNC_WINDOW) || pWin ){ |
dan | dabc268 | 2018-08-17 17:18:16 | [diff] [blame] | 1249 | zType = "window"; |
| 1250 | }else{ |
| 1251 | zType = "aggregate"; |
| 1252 | } |
drh | 62fc069 | 2022-02-06 00:30:04 | [diff] [blame] | 1253 | sqlite3ErrorMsg(pParse, "misuse of %s function %#T()",zType,pExpr); |
drh | 050611a | 2021-04-10 13:37:04 | [diff] [blame] | 1254 | pNC->nNcErr++; |
dan | dabc268 | 2018-08-17 17:18:16 | [diff] [blame] | 1255 | is_agg = 0; |
dan | 26522d1 | 2018-06-11 18:16:51 | [diff] [blame] | 1256 | } |
dan | c316307 | 2018-06-23 19:29:56 | [diff] [blame] | 1257 | #else |
dan | dabc268 | 2018-08-17 17:18:16 | [diff] [blame] | 1258 | if( (is_agg && (pNC->ncFlags & NC_AllowAgg)==0) ){ |
drh | 62fc069 | 2022-02-06 00:30:04 | [diff] [blame] | 1259 | sqlite3ErrorMsg(pParse,"misuse of aggregate function %#T()",pExpr); |
drh | 050611a | 2021-04-10 13:37:04 | [diff] [blame] | 1260 | pNC->nNcErr++; |
dan | dabc268 | 2018-08-17 17:18:16 | [diff] [blame] | 1261 | is_agg = 0; |
| 1262 | } |
dan | c316307 | 2018-06-23 19:29:56 | [diff] [blame] | 1263 | #endif |
drh | 3a843f5 | 2018-08-25 03:29:34 | [diff] [blame] | 1264 | else if( no_such_func && pParse->db->init.busy==0 |
drh | cc15313 | 2016-08-04 12:35:17 | [diff] [blame] | 1265 | #ifdef SQLITE_ENABLE_UNKNOWN_SQL_FUNCTION |
dan | dabc268 | 2018-08-17 17:18:16 | [diff] [blame] | 1266 | && pParse->explain==0 |
drh | cc15313 | 2016-08-04 12:35:17 | [diff] [blame] | 1267 | #endif |
dan | dabc268 | 2018-08-17 17:18:16 | [diff] [blame] | 1268 | ){ |
drh | 62fc069 | 2022-02-06 00:30:04 | [diff] [blame] | 1269 | sqlite3ErrorMsg(pParse, "no such function: %#T", pExpr); |
drh | 050611a | 2021-04-10 13:37:04 | [diff] [blame] | 1270 | pNC->nNcErr++; |
dan | dabc268 | 2018-08-17 17:18:16 | [diff] [blame] | 1271 | }else if( wrong_num_args ){ |
drh | 62fc069 | 2022-02-06 00:30:04 | [diff] [blame] | 1272 | sqlite3ErrorMsg(pParse,"wrong number of arguments to function %#T()", |
| 1273 | pExpr); |
drh | 050611a | 2021-04-10 13:37:04 | [diff] [blame] | 1274 | pNC->nNcErr++; |
dan | dabc268 | 2018-08-17 17:18:16 | [diff] [blame] | 1275 | } |
dan | 5e61c1b | 2019-07-13 17:45:25 | [diff] [blame] | 1276 | #ifndef SQLITE_OMIT_WINDOWFUNC |
| 1277 | else if( is_agg==0 && ExprHasProperty(pExpr, EP_WinFunc) ){ |
larrybr | bc91738 | 2023-06-07 08:40:31 | [diff] [blame] | 1278 | sqlite3ErrorMsg(pParse, |
drh | 62fc069 | 2022-02-06 00:30:04 | [diff] [blame] | 1279 | "FILTER may not be used with non-aggregate %#T()", |
| 1280 | pExpr |
dan | 5e61c1b | 2019-07-13 17:45:25 | [diff] [blame] | 1281 | ); |
drh | 050611a | 2021-04-10 13:37:04 | [diff] [blame] | 1282 | pNC->nNcErr++; |
dan | 5e61c1b | 2019-07-13 17:45:25 | [diff] [blame] | 1283 | } |
| 1284 | #endif |
drh | db19f48 | 2023-10-18 13:58:31 | [diff] [blame] | 1285 | else if( is_agg==0 && pExpr->pLeft ){ |
drh | 20b95f8 | 2023-10-18 22:03:48 | [diff] [blame] | 1286 | sqlite3ExprOrderByAggregateError(pParse, pExpr); |
drh | db19f48 | 2023-10-18 13:58:31 | [diff] [blame] | 1287 | pNC->nNcErr++; |
| 1288 | } |
dan | dabc268 | 2018-08-17 17:18:16 | [diff] [blame] | 1289 | if( is_agg ){ |
dan | 4ded26a | 2019-03-28 16:15:05 | [diff] [blame] | 1290 | /* Window functions may not be arguments of aggregate functions. |
| 1291 | ** Or arguments of other window functions. But aggregate functions |
| 1292 | ** may be arguments for window functions. */ |
dan | 34a7d79 | 2018-06-29 20:43:33 | [diff] [blame] | 1293 | #ifndef SQLITE_OMIT_WINDOWFUNC |
dan | 8117f11 | 2019-07-10 20:16:53 | [diff] [blame] | 1294 | pNC->ncFlags &= ~(NC_AllowWin | (!pWin ? NC_AllowAgg : 0)); |
dan | 34a7d79 | 2018-06-29 20:43:33 | [diff] [blame] | 1295 | #else |
dan | dabc268 | 2018-08-17 17:18:16 | [diff] [blame] | 1296 | pNC->ncFlags &= ~NC_AllowAgg; |
dan | 34a7d79 | 2018-06-29 20:43:33 | [diff] [blame] | 1297 | #endif |
dan | dabc268 | 2018-08-17 17:18:16 | [diff] [blame] | 1298 | } |
dan | c316307 | 2018-06-23 19:29:56 | [diff] [blame] | 1299 | } |
dan | 1550771 | 2024-05-15 21:38:04 | [diff] [blame] | 1300 | else if( ExprHasProperty(pExpr, EP_WinFunc) || pExpr->pLeft ){ |
dan | 9bf022d | 2019-07-19 14:32:42 | [diff] [blame] | 1301 | is_agg = 1; |
| 1302 | } |
drh | 7d10d5a | 2008-08-20 16:35:10 | [diff] [blame] | 1303 | sqlite3WalkExprList(pWalker, pList); |
drh | 030796d | 2012-08-23 16:18:10 | [diff] [blame] | 1304 | if( is_agg ){ |
drh | db19f48 | 2023-10-18 13:58:31 | [diff] [blame] | 1305 | if( pExpr->pLeft ){ |
| 1306 | assert( pExpr->pLeft->op==TK_ORDER ); |
| 1307 | assert( ExprUseXList(pExpr->pLeft) ); |
| 1308 | sqlite3WalkExprList(pWalker, pExpr->pLeft->x.pList); |
| 1309 | } |
dan | 67a9b8e | 2018-06-22 20:51:35 | [diff] [blame] | 1310 | #ifndef SQLITE_OMIT_WINDOWFUNC |
dan | 58b08d7 | 2024-08-24 15:54:15 | [diff] [blame] | 1311 | if( pWin && pParse->nErr==0 ){ |
dan | e3bf632 | 2018-06-08 20:58:27 | [diff] [blame] | 1312 | Select *pSel = pNC->pWinSelect; |
drh | f9eff90 | 2024-08-24 18:42:39 | [diff] [blame] | 1313 | assert( ExprUseYWin(pExpr) && pWin==pExpr->y.pWin ); |
dan | 490e6f2 | 2019-04-29 11:27:58 | [diff] [blame] | 1314 | if( IN_RENAME_OBJECT==0 ){ |
drh | 2e2c881 | 2019-12-13 11:42:56 | [diff] [blame] | 1315 | sqlite3WindowUpdate(pParse, pSel ? pSel->pWinDefn : 0, pWin, pDef); |
dan | e8dd6a4 | 2021-03-04 16:10:23 | [diff] [blame] | 1316 | if( pParse->db->mallocFailed ) break; |
dan | 490e6f2 | 2019-04-29 11:27:58 | [diff] [blame] | 1317 | } |
dan | 4f9adee | 2019-07-13 16:22:50 | [diff] [blame] | 1318 | sqlite3WalkExprList(pWalker, pWin->pPartition); |
| 1319 | sqlite3WalkExprList(pWalker, pWin->pOrderBy); |
| 1320 | sqlite3WalkExpr(pWalker, pWin->pFilter); |
dan | a3fcc00 | 2019-08-15 13:53:22 | [diff] [blame] | 1321 | sqlite3WindowLink(pSel, pWin); |
dan | 4ded26a | 2019-03-28 16:15:05 | [diff] [blame] | 1322 | pNC->ncFlags |= NC_HasWin; |
dan | 67a9b8e | 2018-06-22 20:51:35 | [diff] [blame] | 1323 | }else |
| 1324 | #endif /* SQLITE_OMIT_WINDOWFUNC */ |
| 1325 | { |
drh | 90cf38b | 2021-11-08 23:24:00 | [diff] [blame] | 1326 | NameContext *pNC2; /* For looping up thru outer contexts */ |
dan | 86fb6e1 | 2018-05-16 20:58:07 | [diff] [blame] | 1327 | pExpr->op = TK_AGG_FUNCTION; |
| 1328 | pExpr->op2 = 0; |
dan | 6ba7ab0 | 2019-07-02 11:56:47 | [diff] [blame] | 1329 | #ifndef SQLITE_OMIT_WINDOWFUNC |
dan | 4f9adee | 2019-07-13 16:22:50 | [diff] [blame] | 1330 | if( ExprHasProperty(pExpr, EP_WinFunc) ){ |
| 1331 | sqlite3WalkExpr(pWalker, pExpr->y.pWin->pFilter); |
| 1332 | } |
dan | 6ba7ab0 | 2019-07-02 11:56:47 | [diff] [blame] | 1333 | #endif |
drh | 90cf38b | 2021-11-08 23:24:00 | [diff] [blame] | 1334 | pNC2 = pNC; |
larrybr | bc91738 | 2023-06-07 08:40:31 | [diff] [blame] | 1335 | while( pNC2 |
drh | 90cf38b | 2021-11-08 23:24:00 | [diff] [blame] | 1336 | && sqlite3ReferencesSrcList(pParse, pExpr, pNC2->pSrcList)==0 |
| 1337 | ){ |
dan | 5e4233a | 2023-11-02 21:02:53 | [diff] [blame] | 1338 | pExpr->op2 += (1 + pNC2->nNestedSelect); |
dan | 86fb6e1 | 2018-05-16 20:58:07 | [diff] [blame] | 1339 | pNC2 = pNC2->pNext; |
| 1340 | } |
dan | 72d1eac | 2019-08-05 13:19:25 | [diff] [blame] | 1341 | assert( pDef!=0 || IN_RENAME_OBJECT ); |
| 1342 | if( pNC2 && pDef ){ |
dan | 5e4233a | 2023-11-02 21:02:53 | [diff] [blame] | 1343 | pExpr->op2 += pNC2->nNestedSelect; |
dan | 86fb6e1 | 2018-05-16 20:58:07 | [diff] [blame] | 1344 | assert( SQLITE_FUNC_MINMAX==NC_MinMaxAgg ); |
drh | bb30123 | 2021-07-15 19:29:43 | [diff] [blame] | 1345 | assert( SQLITE_FUNC_ANYORDER==NC_OrderAgg ); |
dan | 86fb6e1 | 2018-05-16 20:58:07 | [diff] [blame] | 1346 | testcase( (pDef->funcFlags & SQLITE_FUNC_MINMAX)!=0 ); |
drh | bb30123 | 2021-07-15 19:29:43 | [diff] [blame] | 1347 | testcase( (pDef->funcFlags & SQLITE_FUNC_ANYORDER)!=0 ); |
larrybr | bc91738 | 2023-06-07 08:40:31 | [diff] [blame] | 1348 | pNC2->ncFlags |= NC_HasAgg |
drh | bb30123 | 2021-07-15 19:29:43 | [diff] [blame] | 1349 | | ((pDef->funcFlags^SQLITE_FUNC_ANYORDER) |
| 1350 | & (SQLITE_FUNC_MINMAX|SQLITE_FUNC_ANYORDER)); |
dan | 86fb6e1 | 2018-05-16 20:58:07 | [diff] [blame] | 1351 | } |
drh | 9588ad9 | 2014-09-15 14:46:02 | [diff] [blame] | 1352 | } |
dan | 4ded26a | 2019-03-28 16:15:05 | [diff] [blame] | 1353 | pNC->ncFlags |= savedAllowFlags; |
drh | 030796d | 2012-08-23 16:18:10 | [diff] [blame] | 1354 | } |
drh | 7d10d5a | 2008-08-20 16:35:10 | [diff] [blame] | 1355 | /* FIX ME: Compute pExpr->affinity based on the expected return |
larrybr | bc91738 | 2023-06-07 08:40:31 | [diff] [blame] | 1356 | ** type of the function |
drh | 7d10d5a | 2008-08-20 16:35:10 | [diff] [blame] | 1357 | */ |
| 1358 | return WRC_Prune; |
| 1359 | } |
| 1360 | #ifndef SQLITE_OMIT_SUBQUERY |
drh | debc8f7 | 2025-07-03 00:17:27 | [diff] [blame] | 1361 | case TK_EXISTS: |
drh | 7d10d5a | 2008-08-20 16:35:10 | [diff] [blame] | 1362 | case TK_SELECT: |
drh | 7d10d5a | 2008-08-20 16:35:10 | [diff] [blame] | 1363 | #endif |
| 1364 | case TK_IN: { |
drh | 73c0fdc | 2009-06-15 18:32:36 | [diff] [blame] | 1365 | testcase( pExpr->op==TK_IN ); |
drh | debc8f7 | 2025-07-03 00:17:27 | [diff] [blame] | 1366 | testcase( pExpr->op==TK_EXISTS ); |
| 1367 | testcase( pExpr->op==TK_SELECT ); |
drh | a4eeccd | 2021-10-07 17:43:30 | [diff] [blame] | 1368 | if( ExprUseXSelect(pExpr) ){ |
drh | 7d10d5a | 2008-08-20 16:35:10 | [diff] [blame] | 1369 | int nRef = pNC->nRef; |
drh | 77318a3 | 2019-12-12 15:19:18 | [diff] [blame] | 1370 | testcase( pNC->ncFlags & NC_IsCheck ); |
| 1371 | testcase( pNC->ncFlags & NC_PartIdx ); |
| 1372 | testcase( pNC->ncFlags & NC_IdxExpr ); |
| 1373 | testcase( pNC->ncFlags & NC_GenCol ); |
drh | de6a4be | 2024-04-06 12:19:50 | [diff] [blame] | 1374 | assert( pExpr->x.pSelect ); |
drh | 8c8443a | 2025-07-07 19:03:50 | [diff] [blame] | 1375 | if( pExpr->op==TK_EXISTS ) pParse->bHasExists = 1; |
drh | 84d9031 | 2021-09-24 19:57:32 | [diff] [blame] | 1376 | if( pNC->ncFlags & NC_SelfRef ){ |
drh | 62fc069 | 2022-02-06 00:30:04 | [diff] [blame] | 1377 | notValidImpl(pParse, pNC, "subqueries", pExpr, pExpr); |
drh | 84d9031 | 2021-09-24 19:57:32 | [diff] [blame] | 1378 | }else{ |
| 1379 | sqlite3WalkSelect(pWalker, pExpr->x.pSelect); |
| 1380 | } |
drh | 7d10d5a | 2008-08-20 16:35:10 | [diff] [blame] | 1381 | assert( pNC->nRef>=nRef ); |
| 1382 | if( nRef!=pNC->nRef ){ |
| 1383 | ExprSetProperty(pExpr, EP_VarSelect); |
drh | de6a4be | 2024-04-06 12:19:50 | [diff] [blame] | 1384 | pExpr->x.pSelect->selFlags |= SF_Correlated; |
drh | 7d10d5a | 2008-08-20 16:35:10 | [diff] [blame] | 1385 | } |
drh | ffcad58 | 2023-03-15 17:58:51 | [diff] [blame] | 1386 | pNC->ncFlags |= NC_Subquery; |
drh | 7d10d5a | 2008-08-20 16:35:10 | [diff] [blame] | 1387 | } |
| 1388 | break; |
| 1389 | } |
drh | 7d10d5a | 2008-08-20 16:35:10 | [diff] [blame] | 1390 | case TK_VARIABLE: { |
drh | 77318a3 | 2019-12-12 15:19:18 | [diff] [blame] | 1391 | testcase( pNC->ncFlags & NC_IsCheck ); |
| 1392 | testcase( pNC->ncFlags & NC_PartIdx ); |
| 1393 | testcase( pNC->ncFlags & NC_IdxExpr ); |
| 1394 | testcase( pNC->ncFlags & NC_GenCol ); |
| 1395 | sqlite3ResolveNotValid(pParse, pNC, "parameters", |
drh | 62fc069 | 2022-02-06 00:30:04 | [diff] [blame] | 1396 | NC_IsCheck|NC_PartIdx|NC_IdxExpr|NC_GenCol, pExpr, pExpr); |
drh | 7d10d5a | 2008-08-20 16:35:10 | [diff] [blame] | 1397 | break; |
| 1398 | } |
drh | 007c843 | 2018-02-26 03:20:18 | [diff] [blame] | 1399 | case TK_IS: |
drh | 8abed7b | 2018-02-26 18:49:05 | [diff] [blame] | 1400 | case TK_ISNOT: { |
drh | 0d950af | 2019-08-22 16:38:42 | [diff] [blame] | 1401 | Expr *pRight = sqlite3ExprSkipCollateAndLikely(pExpr->pRight); |
drh | 8abed7b | 2018-02-26 18:49:05 | [diff] [blame] | 1402 | assert( !ExprHasProperty(pExpr, EP_Reduced) ); |
| 1403 | /* Handle special cases of "x IS TRUE", "x IS FALSE", "x IS NOT TRUE", |
| 1404 | ** and "x IS NOT FALSE". */ |
drh | 91be05a | 2021-03-04 18:34:54 | [diff] [blame] | 1405 | if( ALWAYS(pRight) && (pRight->op==TK_ID || pRight->op==TK_TRUEFALSE) ){ |
drh | 8abed7b | 2018-02-26 18:49:05 | [diff] [blame] | 1406 | int rc = resolveExprStep(pWalker, pRight); |
drh | 007c843 | 2018-02-26 03:20:18 | [diff] [blame] | 1407 | if( rc==WRC_Abort ) return WRC_Abort; |
drh | 8abed7b | 2018-02-26 18:49:05 | [diff] [blame] | 1408 | if( pRight->op==TK_TRUEFALSE ){ |
drh | 8abed7b | 2018-02-26 18:49:05 | [diff] [blame] | 1409 | pExpr->op2 = pExpr->op; |
| 1410 | pExpr->op = TK_TRUTH; |
drh | 007c843 | 2018-02-26 03:20:18 | [diff] [blame] | 1411 | return WRC_Continue; |
| 1412 | } |
| 1413 | } |
drh | 08b9208 | 2020-08-10 14:18:00 | [diff] [blame] | 1414 | /* no break */ deliberate_fall_through |
drh | 8abed7b | 2018-02-26 18:49:05 | [diff] [blame] | 1415 | } |
dan | 3bafded | 2016-11-11 15:49:01 | [diff] [blame] | 1416 | case TK_BETWEEN: |
drh | b29e60c | 2016-09-05 12:02:34 | [diff] [blame] | 1417 | case TK_EQ: |
| 1418 | case TK_NE: |
| 1419 | case TK_LT: |
| 1420 | case TK_LE: |
| 1421 | case TK_GT: |
drh | 8abed7b | 2018-02-26 18:49:05 | [diff] [blame] | 1422 | case TK_GE: { |
drh | b29e60c | 2016-09-05 12:02:34 | [diff] [blame] | 1423 | int nLeft, nRight; |
| 1424 | if( pParse->db->mallocFailed ) break; |
drh | b29e60c | 2016-09-05 12:02:34 | [diff] [blame] | 1425 | assert( pExpr->pLeft!=0 ); |
| 1426 | nLeft = sqlite3ExprVectorSize(pExpr->pLeft); |
dan | 3bafded | 2016-11-11 15:49:01 | [diff] [blame] | 1427 | if( pExpr->op==TK_BETWEEN ){ |
drh | a4eeccd | 2021-10-07 17:43:30 | [diff] [blame] | 1428 | assert( ExprUseXList(pExpr) ); |
dan | 3bafded | 2016-11-11 15:49:01 | [diff] [blame] | 1429 | nRight = sqlite3ExprVectorSize(pExpr->x.pList->a[0].pExpr); |
| 1430 | if( nRight==nLeft ){ |
| 1431 | nRight = sqlite3ExprVectorSize(pExpr->x.pList->a[1].pExpr); |
| 1432 | } |
| 1433 | }else{ |
| 1434 | assert( pExpr->pRight!=0 ); |
| 1435 | nRight = sqlite3ExprVectorSize(pExpr->pRight); |
| 1436 | } |
drh | b29e60c | 2016-09-05 12:02:34 | [diff] [blame] | 1437 | if( nLeft!=nRight ){ |
| 1438 | testcase( pExpr->op==TK_EQ ); |
| 1439 | testcase( pExpr->op==TK_NE ); |
| 1440 | testcase( pExpr->op==TK_LT ); |
| 1441 | testcase( pExpr->op==TK_LE ); |
| 1442 | testcase( pExpr->op==TK_GT ); |
| 1443 | testcase( pExpr->op==TK_GE ); |
| 1444 | testcase( pExpr->op==TK_IS ); |
| 1445 | testcase( pExpr->op==TK_ISNOT ); |
dan | 3bafded | 2016-11-11 15:49:01 | [diff] [blame] | 1446 | testcase( pExpr->op==TK_BETWEEN ); |
drh | b29e60c | 2016-09-05 12:02:34 | [diff] [blame] | 1447 | sqlite3ErrorMsg(pParse, "row value misused"); |
drh | 62fc069 | 2022-02-06 00:30:04 | [diff] [blame] | 1448 | sqlite3RecordErrorOffsetOfExpr(pParse->db, pExpr); |
drh | b29e60c | 2016-09-05 12:02:34 | [diff] [blame] | 1449 | } |
larrybr | bc91738 | 2023-06-07 08:40:31 | [diff] [blame] | 1450 | break; |
drh | b29e60c | 2016-09-05 12:02:34 | [diff] [blame] | 1451 | } |
drh | 7d10d5a | 2008-08-20 16:35:10 | [diff] [blame] | 1452 | } |
drh | 0c7d3d3 | 2022-01-24 16:47:12 | [diff] [blame] | 1453 | assert( pParse->db->mallocFailed==0 || pParse->nErr!=0 ); |
| 1454 | return pParse->nErr ? WRC_Abort : WRC_Continue; |
drh | 7d10d5a | 2008-08-20 16:35:10 | [diff] [blame] | 1455 | } |
| 1456 | |
| 1457 | /* |
| 1458 | ** pEList is a list of expressions which are really the result set of the |
| 1459 | ** a SELECT statement. pE is a term in an ORDER BY or GROUP BY clause. |
| 1460 | ** This routine checks to see if pE is a simple identifier which corresponds |
| 1461 | ** to the AS-name of one of the terms of the expression list. If it is, |
| 1462 | ** this routine return an integer between 1 and N where N is the number of |
| 1463 | ** elements in pEList, corresponding to the matching entry. If there is |
| 1464 | ** no match, or if pE is not a simple identifier, then this routine |
| 1465 | ** return 0. |
| 1466 | ** |
| 1467 | ** pEList has been resolved. pE has not. |
| 1468 | */ |
| 1469 | static int resolveAsName( |
| 1470 | Parse *pParse, /* Parsing context for error messages */ |
| 1471 | ExprList *pEList, /* List of expressions to scan */ |
| 1472 | Expr *pE /* Expression we are trying to match */ |
| 1473 | ){ |
| 1474 | int i; /* Loop counter */ |
| 1475 | |
shane | cf69739 | 2009-06-01 16:53:09 | [diff] [blame] | 1476 | UNUSED_PARAMETER(pParse); |
| 1477 | |
drh | 73c0fdc | 2009-06-15 18:32:36 | [diff] [blame] | 1478 | if( pE->op==TK_ID ){ |
drh | f975107 | 2021-10-07 13:40:29 | [diff] [blame] | 1479 | const char *zCol; |
| 1480 | assert( !ExprHasProperty(pE, EP_IntValue) ); |
| 1481 | zCol = pE->u.zToken; |
drh | 7d10d5a | 2008-08-20 16:35:10 | [diff] [blame] | 1482 | for(i=0; i<pEList->nExpr; i++){ |
drh | d88fd53 | 2022-05-02 20:49:30 | [diff] [blame] | 1483 | if( pEList->a[i].fg.eEName==ENAME_NAME |
dan | 607dd6e | 2020-01-03 14:27:08 | [diff] [blame] | 1484 | && sqlite3_stricmp(pEList->a[i].zEName, zCol)==0 |
drh | c4938ea | 2019-12-13 00:49:42 | [diff] [blame] | 1485 | ){ |
drh | 7d10d5a | 2008-08-20 16:35:10 | [diff] [blame] | 1486 | return i+1; |
| 1487 | } |
| 1488 | } |
drh | 7d10d5a | 2008-08-20 16:35:10 | [diff] [blame] | 1489 | } |
| 1490 | return 0; |
| 1491 | } |
| 1492 | |
| 1493 | /* |
| 1494 | ** pE is a pointer to an expression which is a single term in the |
| 1495 | ** ORDER BY of a compound SELECT. The expression has not been |
| 1496 | ** name resolved. |
| 1497 | ** |
| 1498 | ** At the point this routine is called, we already know that the |
| 1499 | ** ORDER BY term is not an integer index into the result set. That |
| 1500 | ** case is handled by the calling routine. |
| 1501 | ** |
| 1502 | ** Attempt to match pE against result set columns in the left-most |
| 1503 | ** SELECT statement. Return the index i of the matching column, |
| 1504 | ** as an indication to the caller that it should sort by the i-th column. |
| 1505 | ** The left-most column is 1. In other words, the value returned is the |
| 1506 | ** same integer value that would be used in the SQL statement to indicate |
| 1507 | ** the column. |
| 1508 | ** |
| 1509 | ** If there is no match, return 0. Return -1 if an error occurs. |
| 1510 | */ |
| 1511 | static int resolveOrderByTermToExprList( |
| 1512 | Parse *pParse, /* Parsing context for error messages */ |
| 1513 | Select *pSelect, /* The SELECT statement with the ORDER BY clause */ |
| 1514 | Expr *pE /* The specific ORDER BY term */ |
| 1515 | ){ |
| 1516 | int i; /* Loop counter */ |
| 1517 | ExprList *pEList; /* The columns of the result set */ |
| 1518 | NameContext nc; /* Name context for resolving pE */ |
drh | a756466 | 2010-02-22 19:32:31 | [diff] [blame] | 1519 | sqlite3 *db; /* Database connection */ |
| 1520 | int rc; /* Return code from subprocedures */ |
| 1521 | u8 savedSuppErr; /* Saved value of db->suppressErr */ |
drh | 7d10d5a | 2008-08-20 16:35:10 | [diff] [blame] | 1522 | |
drh | 4703b7d | 2024-06-06 15:03:16 | [diff] [blame] | 1523 | assert( sqlite3ExprIsInteger(pE, &i, 0)==0 ); |
drh | 7d10d5a | 2008-08-20 16:35:10 | [diff] [blame] | 1524 | pEList = pSelect->pEList; |
| 1525 | |
| 1526 | /* Resolve all names in the ORDER BY term expression |
| 1527 | */ |
| 1528 | memset(&nc, 0, sizeof(nc)); |
| 1529 | nc.pParse = pParse; |
| 1530 | nc.pSrcList = pSelect->pSrc; |
drh | 25c3b8c | 2018-04-16 10:34:13 | [diff] [blame] | 1531 | nc.uNC.pEList = pEList; |
dan | d487e37 | 2021-04-12 16:59:28 | [diff] [blame] | 1532 | nc.ncFlags = NC_AllowAgg|NC_UEList|NC_NoSelect; |
drh | 050611a | 2021-04-10 13:37:04 | [diff] [blame] | 1533 | nc.nNcErr = 0; |
drh | a756466 | 2010-02-22 19:32:31 | [diff] [blame] | 1534 | db = pParse->db; |
| 1535 | savedSuppErr = db->suppressErr; |
dan | b56a090 | 2021-06-01 15:37:14 | [diff] [blame] | 1536 | db->suppressErr = 1; |
drh | a756466 | 2010-02-22 19:32:31 | [diff] [blame] | 1537 | rc = sqlite3ResolveExprNames(&nc, pE); |
| 1538 | db->suppressErr = savedSuppErr; |
| 1539 | if( rc ) return 0; |
drh | 7d10d5a | 2008-08-20 16:35:10 | [diff] [blame] | 1540 | |
| 1541 | /* Try to match the ORDER BY expression against an expression |
| 1542 | ** in the result set. Return an 1-based index of the matching |
| 1543 | ** result-set entry. |
| 1544 | */ |
| 1545 | for(i=0; i<pEList->nExpr; i++){ |
dan | 5aa550c | 2017-06-24 18:10:29 | [diff] [blame] | 1546 | if( sqlite3ExprCompare(0, pEList->a[i].pExpr, pE, -1)<2 ){ |
drh | 7d10d5a | 2008-08-20 16:35:10 | [diff] [blame] | 1547 | return i+1; |
| 1548 | } |
| 1549 | } |
| 1550 | |
| 1551 | /* If no match, return 0. */ |
| 1552 | return 0; |
| 1553 | } |
| 1554 | |
| 1555 | /* |
| 1556 | ** Generate an ORDER BY or GROUP BY term out-of-range error. |
| 1557 | */ |
| 1558 | static void resolveOutOfRangeError( |
| 1559 | Parse *pParse, /* The error context into which to write the error */ |
| 1560 | const char *zType, /* "ORDER" or "GROUP" */ |
| 1561 | int i, /* The index (1-based) of the term out of range */ |
drh | 62fc069 | 2022-02-06 00:30:04 | [diff] [blame] | 1562 | int mx, /* Largest permissible value of i */ |
| 1563 | Expr *pError /* Associate the error with the expression */ |
drh | 7d10d5a | 2008-08-20 16:35:10 | [diff] [blame] | 1564 | ){ |
larrybr | bc91738 | 2023-06-07 08:40:31 | [diff] [blame] | 1565 | sqlite3ErrorMsg(pParse, |
drh | 7d10d5a | 2008-08-20 16:35:10 | [diff] [blame] | 1566 | "%r %s BY term out of range - should be " |
| 1567 | "between 1 and %d", i, zType, mx); |
drh | 62fc069 | 2022-02-06 00:30:04 | [diff] [blame] | 1568 | sqlite3RecordErrorOffsetOfExpr(pParse->db, pError); |
drh | 7d10d5a | 2008-08-20 16:35:10 | [diff] [blame] | 1569 | } |
| 1570 | |
| 1571 | /* |
| 1572 | ** Analyze the ORDER BY clause in a compound SELECT statement. Modify |
| 1573 | ** each term of the ORDER BY clause is a constant integer between 1 |
| 1574 | ** and N where N is the number of columns in the compound SELECT. |
| 1575 | ** |
| 1576 | ** ORDER BY terms that are already an integer between 1 and N are |
| 1577 | ** unmodified. ORDER BY terms that are integers outside the range of |
| 1578 | ** 1 through N generate an error. ORDER BY terms that are expressions |
| 1579 | ** are matched against result set expressions of compound SELECT |
| 1580 | ** beginning with the left-most SELECT and working toward the right. |
| 1581 | ** At the first match, the ORDER BY expression is transformed into |
| 1582 | ** the integer column number. |
| 1583 | ** |
| 1584 | ** Return the number of errors seen. |
| 1585 | */ |
| 1586 | static int resolveCompoundOrderBy( |
| 1587 | Parse *pParse, /* Parsing context. Leave error messages here */ |
| 1588 | Select *pSelect /* The SELECT statement containing the ORDER BY */ |
| 1589 | ){ |
| 1590 | int i; |
| 1591 | ExprList *pOrderBy; |
| 1592 | ExprList *pEList; |
| 1593 | sqlite3 *db; |
| 1594 | int moreToDo = 1; |
| 1595 | |
| 1596 | pOrderBy = pSelect->pOrderBy; |
| 1597 | if( pOrderBy==0 ) return 0; |
| 1598 | db = pParse->db; |
drh | 7d10d5a | 2008-08-20 16:35:10 | [diff] [blame] | 1599 | if( pOrderBy->nExpr>db->aLimit[SQLITE_LIMIT_COLUMN] ){ |
| 1600 | sqlite3ErrorMsg(pParse, "too many terms in ORDER BY clause"); |
| 1601 | return 1; |
| 1602 | } |
drh | 7d10d5a | 2008-08-20 16:35:10 | [diff] [blame] | 1603 | for(i=0; i<pOrderBy->nExpr; i++){ |
drh | d88fd53 | 2022-05-02 20:49:30 | [diff] [blame] | 1604 | pOrderBy->a[i].fg.done = 0; |
drh | 7d10d5a | 2008-08-20 16:35:10 | [diff] [blame] | 1605 | } |
| 1606 | pSelect->pNext = 0; |
| 1607 | while( pSelect->pPrior ){ |
| 1608 | pSelect->pPrior->pNext = pSelect; |
| 1609 | pSelect = pSelect->pPrior; |
| 1610 | } |
| 1611 | while( pSelect && moreToDo ){ |
| 1612 | struct ExprList_item *pItem; |
| 1613 | moreToDo = 0; |
| 1614 | pEList = pSelect->pEList; |
drh | 0a846f9 | 2008-08-25 17:23:29 | [diff] [blame] | 1615 | assert( pEList!=0 ); |
drh | 7d10d5a | 2008-08-20 16:35:10 | [diff] [blame] | 1616 | for(i=0, pItem=pOrderBy->a; i<pOrderBy->nExpr; i++, pItem++){ |
| 1617 | int iCol = -1; |
| 1618 | Expr *pE, *pDup; |
drh | d88fd53 | 2022-05-02 20:49:30 | [diff] [blame] | 1619 | if( pItem->fg.done ) continue; |
drh | 0d950af | 2019-08-22 16:38:42 | [diff] [blame] | 1620 | pE = sqlite3ExprSkipCollateAndLikely(pItem->pExpr); |
drh | 235667a | 2020-11-08 20:44:30 | [diff] [blame] | 1621 | if( NEVER(pE==0) ) continue; |
drh | 4703b7d | 2024-06-06 15:03:16 | [diff] [blame] | 1622 | if( sqlite3ExprIsInteger(pE, &iCol, 0) ){ |
drh | 73c0fdc | 2009-06-15 18:32:36 | [diff] [blame] | 1623 | if( iCol<=0 || iCol>pEList->nExpr ){ |
drh | 62fc069 | 2022-02-06 00:30:04 | [diff] [blame] | 1624 | resolveOutOfRangeError(pParse, "ORDER", i+1, pEList->nExpr, pE); |
drh | 7d10d5a | 2008-08-20 16:35:10 | [diff] [blame] | 1625 | return 1; |
| 1626 | } |
| 1627 | }else{ |
| 1628 | iCol = resolveAsName(pParse, pEList, pE); |
| 1629 | if( iCol==0 ){ |
dan | 5e970a8 | 2019-01-16 14:58:37 | [diff] [blame] | 1630 | /* Now test if expression pE matches one of the values returned |
larrybr | bc91738 | 2023-06-07 08:40:31 | [diff] [blame] | 1631 | ** by pSelect. In the usual case this is done by duplicating the |
dan | 5e970a8 | 2019-01-16 14:58:37 | [diff] [blame] | 1632 | ** expression, resolving any symbols in it, and then comparing |
| 1633 | ** it against each expression returned by the SELECT statement. |
| 1634 | ** Once the comparisons are finished, the duplicate expression |
| 1635 | ** is deleted. |
| 1636 | ** |
dan | b56a090 | 2021-06-01 15:37:14 | [diff] [blame] | 1637 | ** If this is running as part of an ALTER TABLE operation and |
| 1638 | ** the symbols resolve successfully, also resolve the symbols in the |
| 1639 | ** actual expression. This allows the code in alter.c to modify |
| 1640 | ** column references within the ORDER BY expression as required. */ |
| 1641 | pDup = sqlite3ExprDup(db, pE, 0); |
drh | 7d10d5a | 2008-08-20 16:35:10 | [diff] [blame] | 1642 | if( !db->mallocFailed ){ |
| 1643 | assert(pDup); |
| 1644 | iCol = resolveOrderByTermToExprList(pParse, pSelect, pDup); |
dan | b56a090 | 2021-06-01 15:37:14 | [diff] [blame] | 1645 | if( IN_RENAME_OBJECT && iCol>0 ){ |
| 1646 | resolveOrderByTermToExprList(pParse, pSelect, pE); |
| 1647 | } |
drh | 7d10d5a | 2008-08-20 16:35:10 | [diff] [blame] | 1648 | } |
dan | b56a090 | 2021-06-01 15:37:14 | [diff] [blame] | 1649 | sqlite3ExprDelete(db, pDup); |
drh | 7d10d5a | 2008-08-20 16:35:10 | [diff] [blame] | 1650 | } |
drh | 7d10d5a | 2008-08-20 16:35:10 | [diff] [blame] | 1651 | } |
| 1652 | if( iCol>0 ){ |
drh | bd13d34 | 2012-12-07 21:02:47 | [diff] [blame] | 1653 | /* Convert the ORDER BY term into an integer column number iCol, |
dan | b56a090 | 2021-06-01 15:37:14 | [diff] [blame] | 1654 | ** taking care to preserve the COLLATE clause if it exists. */ |
dan | a5f9f42 | 2019-01-23 19:50:46 | [diff] [blame] | 1655 | if( !IN_RENAME_OBJECT ){ |
| 1656 | Expr *pNew = sqlite3Expr(db, TK_INTEGER, 0); |
| 1657 | if( pNew==0 ) return 1; |
| 1658 | pNew->flags |= EP_IntValue; |
| 1659 | pNew->u.iValue = iCol; |
| 1660 | if( pItem->pExpr==pE ){ |
| 1661 | pItem->pExpr = pNew; |
| 1662 | }else{ |
| 1663 | Expr *pParent = pItem->pExpr; |
| 1664 | assert( pParent->op==TK_COLLATE ); |
| 1665 | while( pParent->pLeft->op==TK_COLLATE ) pParent = pParent->pLeft; |
| 1666 | assert( pParent->pLeft==pE ); |
| 1667 | pParent->pLeft = pNew; |
| 1668 | } |
| 1669 | sqlite3ExprDelete(db, pE); |
| 1670 | pItem->u.x.iOrderByCol = (u16)iCol; |
drh | bd13d34 | 2012-12-07 21:02:47 | [diff] [blame] | 1671 | } |
drh | d88fd53 | 2022-05-02 20:49:30 | [diff] [blame] | 1672 | pItem->fg.done = 1; |
drh | 7d10d5a | 2008-08-20 16:35:10 | [diff] [blame] | 1673 | }else{ |
| 1674 | moreToDo = 1; |
| 1675 | } |
| 1676 | } |
| 1677 | pSelect = pSelect->pNext; |
| 1678 | } |
| 1679 | for(i=0; i<pOrderBy->nExpr; i++){ |
drh | d88fd53 | 2022-05-02 20:49:30 | [diff] [blame] | 1680 | if( pOrderBy->a[i].fg.done==0 ){ |
drh | 7d10d5a | 2008-08-20 16:35:10 | [diff] [blame] | 1681 | sqlite3ErrorMsg(pParse, "%r ORDER BY term does not match any " |
| 1682 | "column in the result set", i+1); |
| 1683 | return 1; |
| 1684 | } |
| 1685 | } |
| 1686 | return 0; |
| 1687 | } |
| 1688 | |
| 1689 | /* |
| 1690 | ** Check every term in the ORDER BY or GROUP BY clause pOrderBy of |
| 1691 | ** the SELECT statement pSelect. If any term is reference to a |
drh | c2acc4e | 2013-11-15 18:15:19 | [diff] [blame] | 1692 | ** result set expression (as determined by the ExprList.a.u.x.iOrderByCol |
| 1693 | ** field) then convert that term into a copy of the corresponding result set |
drh | 7d10d5a | 2008-08-20 16:35:10 | [diff] [blame] | 1694 | ** column. |
| 1695 | ** |
| 1696 | ** If any errors are detected, add an error message to pParse and |
| 1697 | ** return non-zero. Return zero if no errors are seen. |
| 1698 | */ |
| 1699 | int sqlite3ResolveOrderGroupBy( |
| 1700 | Parse *pParse, /* Parsing context. Leave error messages here */ |
| 1701 | Select *pSelect, /* The SELECT statement containing the clause */ |
| 1702 | ExprList *pOrderBy, /* The ORDER BY or GROUP BY clause to be processed */ |
| 1703 | const char *zType /* "ORDER" or "GROUP" */ |
| 1704 | ){ |
| 1705 | int i; |
| 1706 | sqlite3 *db = pParse->db; |
| 1707 | ExprList *pEList; |
| 1708 | struct ExprList_item *pItem; |
| 1709 | |
dan | 71f059c | 2019-08-01 10:58:46 | [diff] [blame] | 1710 | if( pOrderBy==0 || pParse->db->mallocFailed || IN_RENAME_OBJECT ) return 0; |
drh | 7d10d5a | 2008-08-20 16:35:10 | [diff] [blame] | 1711 | if( pOrderBy->nExpr>db->aLimit[SQLITE_LIMIT_COLUMN] ){ |
| 1712 | sqlite3ErrorMsg(pParse, "too many terms in %s BY clause", zType); |
| 1713 | return 1; |
| 1714 | } |
drh | 7d10d5a | 2008-08-20 16:35:10 | [diff] [blame] | 1715 | pEList = pSelect->pEList; |
drh | 0a846f9 | 2008-08-25 17:23:29 | [diff] [blame] | 1716 | assert( pEList!=0 ); /* sqlite3SelectNew() guarantees this */ |
drh | 7d10d5a | 2008-08-20 16:35:10 | [diff] [blame] | 1717 | for(i=0, pItem=pOrderBy->a; i<pOrderBy->nExpr; i++, pItem++){ |
drh | c2acc4e | 2013-11-15 18:15:19 | [diff] [blame] | 1718 | if( pItem->u.x.iOrderByCol ){ |
| 1719 | if( pItem->u.x.iOrderByCol>pEList->nExpr ){ |
drh | 62fc069 | 2022-02-06 00:30:04 | [diff] [blame] | 1720 | resolveOutOfRangeError(pParse, zType, i+1, pEList->nExpr, 0); |
drh | 7d10d5a | 2008-08-20 16:35:10 | [diff] [blame] | 1721 | return 1; |
| 1722 | } |
drh | 6b37b76 | 2021-02-16 20:32:17 | [diff] [blame] | 1723 | resolveAlias(pParse, pEList, pItem->u.x.iOrderByCol-1, pItem->pExpr,0); |
drh | 7d10d5a | 2008-08-20 16:35:10 | [diff] [blame] | 1724 | } |
| 1725 | } |
| 1726 | return 0; |
| 1727 | } |
| 1728 | |
dan | f030b37 | 2019-02-22 19:24:16 | [diff] [blame] | 1729 | #ifndef SQLITE_OMIT_WINDOWFUNC |
| 1730 | /* |
dan | 75b0821 | 2019-07-22 16:20:03 | [diff] [blame] | 1731 | ** Walker callback for windowRemoveExprFromSelect(). |
dan | f030b37 | 2019-02-22 19:24:16 | [diff] [blame] | 1732 | */ |
| 1733 | static int resolveRemoveWindowsCb(Walker *pWalker, Expr *pExpr){ |
drh | 51755a7 | 2019-08-08 19:40:29 | [diff] [blame] | 1734 | UNUSED_PARAMETER(pWalker); |
dan | f030b37 | 2019-02-22 19:24:16 | [diff] [blame] | 1735 | if( ExprHasProperty(pExpr, EP_WinFunc) ){ |
dan | 75b0821 | 2019-07-22 16:20:03 | [diff] [blame] | 1736 | Window *pWin = pExpr->y.pWin; |
drh | e209457 | 2019-07-22 19:01:38 | [diff] [blame] | 1737 | sqlite3WindowUnlinkFromSelect(pWin); |
dan | f030b37 | 2019-02-22 19:24:16 | [diff] [blame] | 1738 | } |
| 1739 | return WRC_Continue; |
| 1740 | } |
| 1741 | |
| 1742 | /* |
| 1743 | ** Remove any Window objects owned by the expression pExpr from the |
| 1744 | ** Select.pWin list of Select object pSelect. |
| 1745 | */ |
dan | 75b0821 | 2019-07-22 16:20:03 | [diff] [blame] | 1746 | static void windowRemoveExprFromSelect(Select *pSelect, Expr *pExpr){ |
drh | fd15e18 | 2019-07-20 21:12:31 | [diff] [blame] | 1747 | if( pSelect->pWin ){ |
| 1748 | Walker sWalker; |
| 1749 | memset(&sWalker, 0, sizeof(Walker)); |
| 1750 | sWalker.xExprCallback = resolveRemoveWindowsCb; |
| 1751 | sWalker.u.pSelect = pSelect; |
| 1752 | sqlite3WalkExpr(&sWalker, pExpr); |
| 1753 | } |
dan | f030b37 | 2019-02-22 19:24:16 | [diff] [blame] | 1754 | } |
| 1755 | #else |
dan | 75b0821 | 2019-07-22 16:20:03 | [diff] [blame] | 1756 | # define windowRemoveExprFromSelect(a, b) |
drh | fd15e18 | 2019-07-20 21:12:31 | [diff] [blame] | 1757 | #endif /* SQLITE_OMIT_WINDOWFUNC */ |
dan | f030b37 | 2019-02-22 19:24:16 | [diff] [blame] | 1758 | |
drh | 7d10d5a | 2008-08-20 16:35:10 | [diff] [blame] | 1759 | /* |
| 1760 | ** pOrderBy is an ORDER BY or GROUP BY clause in SELECT statement pSelect. |
| 1761 | ** The Name context of the SELECT statement is pNC. zType is either |
| 1762 | ** "ORDER" or "GROUP" depending on which type of clause pOrderBy is. |
| 1763 | ** |
| 1764 | ** This routine resolves each term of the clause into an expression. |
| 1765 | ** If the order-by term is an integer I between 1 and N (where N is the |
| 1766 | ** number of columns in the result set of the SELECT) then the expression |
| 1767 | ** in the resolution is a copy of the I-th result-set expression. If |
drh | 26080d9 | 2013-08-15 14:27:42 | [diff] [blame] | 1768 | ** the order-by term is an identifier that corresponds to the AS-name of |
drh | 7d10d5a | 2008-08-20 16:35:10 | [diff] [blame] | 1769 | ** a result-set expression, then the term resolves to a copy of the |
| 1770 | ** result-set expression. Otherwise, the expression is resolved in |
| 1771 | ** the usual way - using sqlite3ResolveExprNames(). |
| 1772 | ** |
| 1773 | ** This routine returns the number of errors. If errors occur, then |
| 1774 | ** an appropriate error message might be left in pParse. (OOM errors |
| 1775 | ** excepted.) |
| 1776 | */ |
| 1777 | static int resolveOrderGroupBy( |
| 1778 | NameContext *pNC, /* The name context of the SELECT statement */ |
| 1779 | Select *pSelect, /* The SELECT statement holding pOrderBy */ |
| 1780 | ExprList *pOrderBy, /* An ORDER BY or GROUP BY clause to resolve */ |
| 1781 | const char *zType /* Either "ORDER" or "GROUP", as appropriate */ |
| 1782 | ){ |
drh | 70331cd | 2012-04-27 01:09:06 | [diff] [blame] | 1783 | int i, j; /* Loop counters */ |
drh | 7d10d5a | 2008-08-20 16:35:10 | [diff] [blame] | 1784 | int iCol; /* Column number */ |
| 1785 | struct ExprList_item *pItem; /* A term of the ORDER BY clause */ |
| 1786 | Parse *pParse; /* Parsing context */ |
| 1787 | int nResult; /* Number of terms in the result set */ |
| 1788 | |
drh | de0e1b1 | 2021-04-16 22:53:57 | [diff] [blame] | 1789 | assert( pOrderBy!=0 ); |
drh | 7d10d5a | 2008-08-20 16:35:10 | [diff] [blame] | 1790 | nResult = pSelect->pEList->nExpr; |
| 1791 | pParse = pNC->pParse; |
| 1792 | for(i=0, pItem=pOrderBy->a; i<pOrderBy->nExpr; i++, pItem++){ |
| 1793 | Expr *pE = pItem->pExpr; |
drh | 0d950af | 2019-08-22 16:38:42 | [diff] [blame] | 1794 | Expr *pE2 = sqlite3ExprSkipCollateAndLikely(pE); |
drh | 235667a | 2020-11-08 20:44:30 | [diff] [blame] | 1795 | if( NEVER(pE2==0) ) continue; |
drh | 0af16ab | 2013-08-15 22:40:21 | [diff] [blame] | 1796 | if( zType[0]!='G' ){ |
| 1797 | iCol = resolveAsName(pParse, pSelect->pEList, pE2); |
| 1798 | if( iCol>0 ){ |
| 1799 | /* If an AS-name match is found, mark this ORDER BY column as being |
| 1800 | ** a copy of the iCol-th result-set column. The subsequent call to |
| 1801 | ** sqlite3ResolveOrderGroupBy() will convert the expression to a |
| 1802 | ** copy of the iCol-th result-set expression. */ |
drh | c2acc4e | 2013-11-15 18:15:19 | [diff] [blame] | 1803 | pItem->u.x.iOrderByCol = (u16)iCol; |
drh | 0af16ab | 2013-08-15 22:40:21 | [diff] [blame] | 1804 | continue; |
| 1805 | } |
drh | 7d10d5a | 2008-08-20 16:35:10 | [diff] [blame] | 1806 | } |
drh | 4703b7d | 2024-06-06 15:03:16 | [diff] [blame] | 1807 | if( sqlite3ExprIsInteger(pE2, &iCol, 0) ){ |
drh | 7d10d5a | 2008-08-20 16:35:10 | [diff] [blame] | 1808 | /* The ORDER BY term is an integer constant. Again, set the column |
| 1809 | ** number so that sqlite3ResolveOrderGroupBy() will convert the |
| 1810 | ** order-by term to a copy of the result-set expression */ |
drh | 85d641f | 2012-12-07 23:23:53 | [diff] [blame] | 1811 | if( iCol<1 || iCol>0xffff ){ |
drh | 62fc069 | 2022-02-06 00:30:04 | [diff] [blame] | 1812 | resolveOutOfRangeError(pParse, zType, i+1, nResult, pE2); |
drh | 7d10d5a | 2008-08-20 16:35:10 | [diff] [blame] | 1813 | return 1; |
| 1814 | } |
drh | c2acc4e | 2013-11-15 18:15:19 | [diff] [blame] | 1815 | pItem->u.x.iOrderByCol = (u16)iCol; |
drh | 7d10d5a | 2008-08-20 16:35:10 | [diff] [blame] | 1816 | continue; |
| 1817 | } |
| 1818 | |
| 1819 | /* Otherwise, treat the ORDER BY term as an ordinary expression */ |
drh | c2acc4e | 2013-11-15 18:15:19 | [diff] [blame] | 1820 | pItem->u.x.iOrderByCol = 0; |
drh | 7d10d5a | 2008-08-20 16:35:10 | [diff] [blame] | 1821 | if( sqlite3ResolveExprNames(pNC, pE) ){ |
| 1822 | return 1; |
| 1823 | } |
drh | 70331cd | 2012-04-27 01:09:06 | [diff] [blame] | 1824 | for(j=0; j<pSelect->pEList->nExpr; j++){ |
dan | 5aa550c | 2017-06-24 18:10:29 | [diff] [blame] | 1825 | if( sqlite3ExprCompare(0, pE, pSelect->pEList->a[j].pExpr, -1)==0 ){ |
larrybr | bc91738 | 2023-06-07 08:40:31 | [diff] [blame] | 1826 | /* Since this expression is being changed into a reference |
dan | f030b37 | 2019-02-22 19:24:16 | [diff] [blame] | 1827 | ** to an identical expression in the result set, remove all Window |
| 1828 | ** objects belonging to the expression from the Select.pWin list. */ |
dan | 75b0821 | 2019-07-22 16:20:03 | [diff] [blame] | 1829 | windowRemoveExprFromSelect(pSelect, pE); |
drh | c2acc4e | 2013-11-15 18:15:19 | [diff] [blame] | 1830 | pItem->u.x.iOrderByCol = j+1; |
drh | 70331cd | 2012-04-27 01:09:06 | [diff] [blame] | 1831 | } |
| 1832 | } |
drh | 7d10d5a | 2008-08-20 16:35:10 | [diff] [blame] | 1833 | } |
| 1834 | return sqlite3ResolveOrderGroupBy(pParse, pSelect, pOrderBy, zType); |
| 1835 | } |
| 1836 | |
| 1837 | /* |
peter.d.reid | 60ec914 | 2014-09-06 16:39:46 | [diff] [blame] | 1838 | ** Resolve names in the SELECT statement p and all of its descendants. |
drh | 7d10d5a | 2008-08-20 16:35:10 | [diff] [blame] | 1839 | */ |
| 1840 | static int resolveSelectStep(Walker *pWalker, Select *p){ |
| 1841 | NameContext *pOuterNC; /* Context that contains this SELECT */ |
| 1842 | NameContext sNC; /* Name context of this SELECT */ |
| 1843 | int isCompound; /* True if p is a compound select */ |
| 1844 | int nCompound; /* Number of compound terms processed so far */ |
| 1845 | Parse *pParse; /* Parsing context */ |
drh | 7d10d5a | 2008-08-20 16:35:10 | [diff] [blame] | 1846 | int i; /* Loop counter */ |
| 1847 | ExprList *pGroupBy; /* The GROUP BY clause */ |
| 1848 | Select *pLeftmost; /* Left-most of SELECT of a compound */ |
| 1849 | sqlite3 *db; /* Database connection */ |
larrybr | bc91738 | 2023-06-07 08:40:31 | [diff] [blame] | 1850 | |
drh | 7d10d5a | 2008-08-20 16:35:10 | [diff] [blame] | 1851 | |
drh | 0a846f9 | 2008-08-25 17:23:29 | [diff] [blame] | 1852 | assert( p!=0 ); |
drh | 7d10d5a | 2008-08-20 16:35:10 | [diff] [blame] | 1853 | if( p->selFlags & SF_Resolved ){ |
| 1854 | return WRC_Prune; |
| 1855 | } |
| 1856 | pOuterNC = pWalker->u.pNC; |
| 1857 | pParse = pWalker->pParse; |
| 1858 | db = pParse->db; |
| 1859 | |
| 1860 | /* Normally sqlite3SelectExpand() will be called first and will have |
| 1861 | ** already expanded this SELECT. However, if this is a subquery within |
| 1862 | ** an expression, sqlite3ResolveExprNames() will be called without a |
| 1863 | ** prior call to sqlite3SelectExpand(). When that happens, let |
| 1864 | ** sqlite3SelectPrep() do all of the processing for this SELECT. |
| 1865 | ** sqlite3SelectPrep() will invoke both sqlite3SelectExpand() and |
| 1866 | ** this routine in the correct order. |
| 1867 | */ |
| 1868 | if( (p->selFlags & SF_Expanded)==0 ){ |
| 1869 | sqlite3SelectPrep(pParse, p, pOuterNC); |
drh | 0c7d3d3 | 2022-01-24 16:47:12 | [diff] [blame] | 1870 | return pParse->nErr ? WRC_Abort : WRC_Prune; |
drh | 7d10d5a | 2008-08-20 16:35:10 | [diff] [blame] | 1871 | } |
| 1872 | |
| 1873 | isCompound = p->pPrior!=0; |
| 1874 | nCompound = 0; |
| 1875 | pLeftmost = p; |
| 1876 | while( p ){ |
| 1877 | assert( (p->selFlags & SF_Expanded)!=0 ); |
| 1878 | assert( (p->selFlags & SF_Resolved)==0 ); |
| 1879 | p->selFlags |= SF_Resolved; |
| 1880 | |
| 1881 | /* Resolve the expressions in the LIMIT and OFFSET clauses. These |
| 1882 | ** are not allowed to refer to any names, so pass an empty NameContext. |
| 1883 | */ |
| 1884 | memset(&sNC, 0, sizeof(sNC)); |
| 1885 | sNC.pParse = pParse; |
dan | e3bf632 | 2018-06-08 20:58:27 | [diff] [blame] | 1886 | sNC.pWinSelect = p; |
drh | 8c0833f | 2017-11-14 23:48:23 | [diff] [blame] | 1887 | if( sqlite3ResolveExprNames(&sNC, p->pLimit) ){ |
drh | 7d10d5a | 2008-08-20 16:35:10 | [diff] [blame] | 1888 | return WRC_Abort; |
| 1889 | } |
dan | b33c50f | 2015-04-04 16:43:16 | [diff] [blame] | 1890 | |
| 1891 | /* If the SF_Converted flags is set, then this Select object was |
| 1892 | ** was created by the convertCompoundSelectToSubquery() function. |
| 1893 | ** In this case the ORDER BY clause (p->pOrderBy) should be resolved |
| 1894 | ** as if it were part of the sub-query, not the parent. This block |
| 1895 | ** moves the pOrderBy down to the sub-query. It will be moved back |
| 1896 | ** after the names have been resolved. */ |
| 1897 | if( p->selFlags & SF_Converted ){ |
drh | 1521ca4 | 2024-08-19 22:48:30 | [diff] [blame] | 1898 | Select *pSub; |
| 1899 | assert( p->pSrc->a[0].fg.isSubquery ); |
| 1900 | assert( p->pSrc->a[0].u4.pSubq!=0 ); |
| 1901 | pSub = p->pSrc->a[0].u4.pSubq->pSelect; |
| 1902 | assert( pSub!=0 ); |
drh | a43f02e | 2015-04-15 06:45:13 | [diff] [blame] | 1903 | assert( p->pSrc->nSrc==1 && p->pOrderBy ); |
dan | b33c50f | 2015-04-04 16:43:16 | [diff] [blame] | 1904 | assert( pSub->pPrior && pSub->pOrderBy==0 ); |
| 1905 | pSub->pOrderBy = p->pOrderBy; |
| 1906 | p->pOrderBy = 0; |
| 1907 | } |
larrybr | bc91738 | 2023-06-07 08:40:31 | [diff] [blame] | 1908 | |
drh | ee612e2 | 2021-07-16 20:16:19 | [diff] [blame] | 1909 | /* Recursively resolve names in all subqueries in the FROM clause |
drh | 7d10d5a | 2008-08-20 16:35:10 | [diff] [blame] | 1910 | */ |
dan | 5e4233a | 2023-11-02 21:02:53 | [diff] [blame] | 1911 | if( pOuterNC ) pOuterNC->nNestedSelect++; |
drh | 7d10d5a | 2008-08-20 16:35:10 | [diff] [blame] | 1912 | for(i=0; i<p->pSrc->nSrc; i++){ |
drh | 7601294 | 2021-02-21 21:04:54 | [diff] [blame] | 1913 | SrcItem *pItem = &p->pSrc->a[i]; |
drh | b204b6a | 2024-08-17 23:23:23 | [diff] [blame] | 1914 | assert( pItem->zName!=0 |
drh | 1521ca4 | 2024-08-19 22:48:30 | [diff] [blame] | 1915 | || pItem->fg.isSubquery ); /* Test of tag-20240424-1*/ |
| 1916 | if( pItem->fg.isSubquery |
| 1917 | && (pItem->u4.pSubq->pSelect->selFlags & SF_Resolved)==0 |
| 1918 | ){ |
dan | 4b17455 | 2021-02-26 15:20:17 | [diff] [blame] | 1919 | int nRef = pOuterNC ? pOuterNC->nRef : 0; |
drh | 7d10d5a | 2008-08-20 16:35:10 | [diff] [blame] | 1920 | const char *zSavedContext = pParse->zAuthContext; |
dan | da79cf0 | 2011-07-08 16:10:54 | [diff] [blame] | 1921 | |
drh | 7d10d5a | 2008-08-20 16:35:10 | [diff] [blame] | 1922 | if( pItem->zName ) pParse->zAuthContext = pItem->zName; |
drh | 1521ca4 | 2024-08-19 22:48:30 | [diff] [blame] | 1923 | sqlite3ResolveSelectNames(pParse, pItem->u4.pSubq->pSelect, pOuterNC); |
drh | 7d10d5a | 2008-08-20 16:35:10 | [diff] [blame] | 1924 | pParse->zAuthContext = zSavedContext; |
drh | 0c7d3d3 | 2022-01-24 16:47:12 | [diff] [blame] | 1925 | if( pParse->nErr ) return WRC_Abort; |
| 1926 | assert( db->mallocFailed==0 ); |
dan | da79cf0 | 2011-07-08 16:10:54 | [diff] [blame] | 1927 | |
dan | 4b17455 | 2021-02-26 15:20:17 | [diff] [blame] | 1928 | /* If the number of references to the outer context changed when |
| 1929 | ** expressions in the sub-select were resolved, the sub-select |
| 1930 | ** is correlated. It is not required to check the refcount on any |
| 1931 | ** but the innermost outer context object, as lookupName() increments |
| 1932 | ** the refcount on all contexts between the current one and the |
| 1933 | ** context containing the column when it resolves a name. */ |
| 1934 | if( pOuterNC ){ |
| 1935 | assert( pItem->fg.isCorrelated==0 && pOuterNC->nRef>=nRef ); |
| 1936 | pItem->fg.isCorrelated = (pOuterNC->nRef>nRef); |
| 1937 | } |
drh | 7d10d5a | 2008-08-20 16:35:10 | [diff] [blame] | 1938 | } |
| 1939 | } |
drh | 792103a | 2023-11-02 22:11:35 | [diff] [blame] | 1940 | if( pOuterNC && ALWAYS(pOuterNC->nNestedSelect>0) ){ |
| 1941 | pOuterNC->nNestedSelect--; |
| 1942 | } |
larrybr | bc91738 | 2023-06-07 08:40:31 | [diff] [blame] | 1943 | |
drh | 92689d2 | 2012-12-18 16:07:08 | [diff] [blame] | 1944 | /* Set up the local name-context to pass to sqlite3ResolveExprNames() to |
| 1945 | ** resolve the result-set expression list. |
| 1946 | */ |
dan | 86fb6e1 | 2018-05-16 20:58:07 | [diff] [blame] | 1947 | sNC.ncFlags = NC_AllowAgg|NC_AllowWin; |
drh | 92689d2 | 2012-12-18 16:07:08 | [diff] [blame] | 1948 | sNC.pSrcList = p->pSrc; |
| 1949 | sNC.pNext = pOuterNC; |
larrybr | bc91738 | 2023-06-07 08:40:31 | [diff] [blame] | 1950 | |
drh | 92689d2 | 2012-12-18 16:07:08 | [diff] [blame] | 1951 | /* Resolve names in the result set. */ |
drh | 01d230c | 2015-08-19 17:11:37 | [diff] [blame] | 1952 | if( sqlite3ResolveExprListNames(&sNC, p->pEList) ) return WRC_Abort; |
dan | 86fb6e1 | 2018-05-16 20:58:07 | [diff] [blame] | 1953 | sNC.ncFlags &= ~NC_AllowWin; |
larrybr | bc91738 | 2023-06-07 08:40:31 | [diff] [blame] | 1954 | |
| 1955 | /* If there are no aggregate functions in the result-set, and no GROUP BY |
drh | 7d10d5a | 2008-08-20 16:35:10 | [diff] [blame] | 1956 | ** expression, do not allow aggregates in any of the other expressions. |
| 1957 | */ |
| 1958 | assert( (p->selFlags & SF_Aggregate)==0 ); |
| 1959 | pGroupBy = p->pGroupBy; |
drh | a51009b | 2012-05-21 19:11:25 | [diff] [blame] | 1960 | if( pGroupBy || (sNC.ncFlags & NC_HasAgg)!=0 ){ |
drh | 9588ad9 | 2014-09-15 14:46:02 | [diff] [blame] | 1961 | assert( NC_MinMaxAgg==SF_MinMaxAgg ); |
drh | bb30123 | 2021-07-15 19:29:43 | [diff] [blame] | 1962 | assert( NC_OrderAgg==SF_OrderByReqd ); |
| 1963 | p->selFlags |= SF_Aggregate | (sNC.ncFlags&(NC_MinMaxAgg|NC_OrderAgg)); |
drh | 7d10d5a | 2008-08-20 16:35:10 | [diff] [blame] | 1964 | }else{ |
drh | a51009b | 2012-05-21 19:11:25 | [diff] [blame] | 1965 | sNC.ncFlags &= ~NC_AllowAgg; |
drh | 7d10d5a | 2008-08-20 16:35:10 | [diff] [blame] | 1966 | } |
larrybr | bc91738 | 2023-06-07 08:40:31 | [diff] [blame] | 1967 | |
drh | 26080d9 | 2013-08-15 14:27:42 | [diff] [blame] | 1968 | /* Add the output column list to the name-context before parsing the |
drh | 7d10d5a | 2008-08-20 16:35:10 | [diff] [blame] | 1969 | ** other expressions in the SELECT statement. This is so that |
| 1970 | ** expressions in the WHERE clause (etc.) can refer to expressions by |
| 1971 | ** aliases in the result set. |
| 1972 | ** |
| 1973 | ** Minor point: If this is the case, then the expression will be |
| 1974 | ** re-evaluated for each reference to it. |
| 1975 | */ |
drh | d510197 | 2021-02-08 13:41:17 | [diff] [blame] | 1976 | assert( (sNC.ncFlags & (NC_UAggInfo|NC_UUpsert|NC_UBaseReg))==0 ); |
drh | 25c3b8c | 2018-04-16 10:34:13 | [diff] [blame] | 1977 | sNC.uNC.pEList = p->pEList; |
| 1978 | sNC.ncFlags |= NC_UEList; |
drh | de0e1b1 | 2021-04-16 22:53:57 | [diff] [blame] | 1979 | if( p->pHaving ){ |
drh | b9294de | 2022-06-21 13:41:24 | [diff] [blame] | 1980 | if( (p->selFlags & SF_Aggregate)==0 ){ |
| 1981 | sqlite3ErrorMsg(pParse, "HAVING clause on a non-aggregate query"); |
drh | de0e1b1 | 2021-04-16 22:53:57 | [diff] [blame] | 1982 | return WRC_Abort; |
| 1983 | } |
| 1984 | if( sqlite3ResolveExprNames(&sNC, p->pHaving) ) return WRC_Abort; |
| 1985 | } |
drh | 61b77a6 | 2024-03-08 21:37:18 | [diff] [blame] | 1986 | sNC.ncFlags |= NC_Where; |
drh | a3a5bd9 | 2013-04-13 19:59:58 | [diff] [blame] | 1987 | if( sqlite3ResolveExprNames(&sNC, p->pWhere) ) return WRC_Abort; |
drh | 61b77a6 | 2024-03-08 21:37:18 | [diff] [blame] | 1988 | sNC.ncFlags &= ~NC_Where; |
drh | 7d10d5a | 2008-08-20 16:35:10 | [diff] [blame] | 1989 | |
drh | 01d230c | 2015-08-19 17:11:37 | [diff] [blame] | 1990 | /* Resolve names in table-valued-function arguments */ |
| 1991 | for(i=0; i<p->pSrc->nSrc; i++){ |
drh | 7601294 | 2021-02-21 21:04:54 | [diff] [blame] | 1992 | SrcItem *pItem = &p->pSrc->a[i]; |
drh | 01d230c | 2015-08-19 17:11:37 | [diff] [blame] | 1993 | if( pItem->fg.isTabFunc |
larrybr | bc91738 | 2023-06-07 08:40:31 | [diff] [blame] | 1994 | && sqlite3ResolveExprListNames(&sNC, pItem->u1.pFuncArg) |
drh | 01d230c | 2015-08-19 17:11:37 | [diff] [blame] | 1995 | ){ |
| 1996 | return WRC_Abort; |
| 1997 | } |
| 1998 | } |
| 1999 | |
dan | be12083 | 2021-05-17 16:20:41 | [diff] [blame] | 2000 | #ifndef SQLITE_OMIT_WINDOWFUNC |
| 2001 | if( IN_RENAME_OBJECT ){ |
| 2002 | Window *pWin; |
| 2003 | for(pWin=p->pWinDefn; pWin; pWin=pWin->pNextWin){ |
| 2004 | if( sqlite3ResolveExprListNames(&sNC, pWin->pOrderBy) |
| 2005 | || sqlite3ResolveExprListNames(&sNC, pWin->pPartition) |
| 2006 | ){ |
| 2007 | return WRC_Abort; |
| 2008 | } |
| 2009 | } |
| 2010 | } |
| 2011 | #endif |
| 2012 | |
drh | 7d10d5a | 2008-08-20 16:35:10 | [diff] [blame] | 2013 | /* The ORDER BY and GROUP BY clauses may not refer to terms in |
larrybr | bc91738 | 2023-06-07 08:40:31 | [diff] [blame] | 2014 | ** outer queries |
drh | 7d10d5a | 2008-08-20 16:35:10 | [diff] [blame] | 2015 | */ |
| 2016 | sNC.pNext = 0; |
dan | 86fb6e1 | 2018-05-16 20:58:07 | [diff] [blame] | 2017 | sNC.ncFlags |= NC_AllowAgg|NC_AllowWin; |
drh | 7d10d5a | 2008-08-20 16:35:10 | [diff] [blame] | 2018 | |
larrybr | bc91738 | 2023-06-07 08:40:31 | [diff] [blame] | 2019 | /* If this is a converted compound query, move the ORDER BY clause from |
dan | b33c50f | 2015-04-04 16:43:16 | [diff] [blame] | 2020 | ** the sub-query back to the parent query. At this point each term |
| 2021 | ** within the ORDER BY clause has been transformed to an integer value. |
| 2022 | ** These integers will be replaced by copies of the corresponding result |
| 2023 | ** set expressions by the call to resolveOrderGroupBy() below. */ |
| 2024 | if( p->selFlags & SF_Converted ){ |
drh | 1521ca4 | 2024-08-19 22:48:30 | [diff] [blame] | 2025 | Select *pSub; |
| 2026 | assert( p->pSrc->a[0].fg.isSubquery ); |
| 2027 | pSub = p->pSrc->a[0].u4.pSubq->pSelect; |
| 2028 | assert( pSub!=0 ); |
dan | b33c50f | 2015-04-04 16:43:16 | [diff] [blame] | 2029 | p->pOrderBy = pSub->pOrderBy; |
| 2030 | pSub->pOrderBy = 0; |
| 2031 | } |
| 2032 | |
drh | 7d10d5a | 2008-08-20 16:35:10 | [diff] [blame] | 2033 | /* Process the ORDER BY clause for singleton SELECT statements. |
| 2034 | ** The ORDER BY clause for compounds SELECT statements is handled |
| 2035 | ** below, after all of the result-sets for all of the elements of |
| 2036 | ** the compound have been resolved. |
drh | 7b4da15 | 2015-04-17 18:52:37 | [diff] [blame] | 2037 | ** |
| 2038 | ** If there is an ORDER BY clause on a term of a compound-select other |
| 2039 | ** than the right-most term, then that is a syntax error. But the error |
| 2040 | ** is not detected until much later, and so we need to go ahead and |
| 2041 | ** resolve those symbols on the incorrect ORDER BY for consistency. |
drh | 7d10d5a | 2008-08-20 16:35:10 | [diff] [blame] | 2042 | */ |
drh | de0e1b1 | 2021-04-16 22:53:57 | [diff] [blame] | 2043 | if( p->pOrderBy!=0 |
| 2044 | && isCompound<=nCompound /* Defer right-most ORDER BY of a compound */ |
drh | 7b4da15 | 2015-04-17 18:52:37 | [diff] [blame] | 2045 | && resolveOrderGroupBy(&sNC, p, p->pOrderBy, "ORDER") |
| 2046 | ){ |
drh | 7d10d5a | 2008-08-20 16:35:10 | [diff] [blame] | 2047 | return WRC_Abort; |
| 2048 | } |
| 2049 | if( db->mallocFailed ){ |
| 2050 | return WRC_Abort; |
| 2051 | } |
dan | 86fb6e1 | 2018-05-16 20:58:07 | [diff] [blame] | 2052 | sNC.ncFlags &= ~NC_AllowWin; |
larrybr | bc91738 | 2023-06-07 08:40:31 | [diff] [blame] | 2053 | |
| 2054 | /* Resolve the GROUP BY clause. At the same time, make sure |
drh | 7d10d5a | 2008-08-20 16:35:10 | [diff] [blame] | 2055 | ** the GROUP BY clause does not contain aggregate functions. |
| 2056 | */ |
| 2057 | if( pGroupBy ){ |
| 2058 | struct ExprList_item *pItem; |
larrybr | bc91738 | 2023-06-07 08:40:31 | [diff] [blame] | 2059 | |
drh | 7d10d5a | 2008-08-20 16:35:10 | [diff] [blame] | 2060 | if( resolveOrderGroupBy(&sNC, p, pGroupBy, "GROUP") || db->mallocFailed ){ |
| 2061 | return WRC_Abort; |
| 2062 | } |
| 2063 | for(i=0, pItem=pGroupBy->a; i<pGroupBy->nExpr; i++, pItem++){ |
| 2064 | if( ExprHasProperty(pItem->pExpr, EP_Agg) ){ |
| 2065 | sqlite3ErrorMsg(pParse, "aggregate functions are not allowed in " |
| 2066 | "the GROUP BY clause"); |
| 2067 | return WRC_Abort; |
| 2068 | } |
| 2069 | } |
| 2070 | } |
| 2071 | |
dan | 923cadb | 2015-06-23 12:19:55 | [diff] [blame] | 2072 | /* If this is part of a compound SELECT, check that it has the right |
| 2073 | ** number of expressions in the select list. */ |
| 2074 | if( p->pNext && p->pEList->nExpr!=p->pNext->pEList->nExpr ){ |
| 2075 | sqlite3SelectWrongNumTermsError(pParse, p->pNext); |
| 2076 | return WRC_Abort; |
| 2077 | } |
| 2078 | |
drh | 7d10d5a | 2008-08-20 16:35:10 | [diff] [blame] | 2079 | /* Advance to the next term of the compound |
| 2080 | */ |
| 2081 | p = p->pPrior; |
| 2082 | nCompound++; |
| 2083 | } |
| 2084 | |
| 2085 | /* Resolve the ORDER BY on a compound SELECT after all terms of |
| 2086 | ** the compound have been resolved. |
| 2087 | */ |
| 2088 | if( isCompound && resolveCompoundOrderBy(pParse, pLeftmost) ){ |
| 2089 | return WRC_Abort; |
| 2090 | } |
| 2091 | |
| 2092 | return WRC_Prune; |
| 2093 | } |
| 2094 | |
| 2095 | /* |
| 2096 | ** This routine walks an expression tree and resolves references to |
| 2097 | ** table columns and result-set columns. At the same time, do error |
| 2098 | ** checking on function usage and set a flag if any aggregate functions |
| 2099 | ** are seen. |
| 2100 | ** |
larrybr | bc91738 | 2023-06-07 08:40:31 | [diff] [blame] | 2101 | ** To resolve table columns references we look for nodes (or subtrees) of the |
drh | 7d10d5a | 2008-08-20 16:35:10 | [diff] [blame] | 2102 | ** form X.Y.Z or Y.Z or just Z where |
| 2103 | ** |
| 2104 | ** X: The name of a database. Ex: "main" or "temp" or |
| 2105 | ** the symbolic name assigned to an ATTACH-ed database. |
| 2106 | ** |
| 2107 | ** Y: The name of a table in a FROM clause. Or in a trigger |
| 2108 | ** one of the special names "old" or "new". |
| 2109 | ** |
| 2110 | ** Z: The name of a column in table Y. |
| 2111 | ** |
| 2112 | ** The node at the root of the subtree is modified as follows: |
| 2113 | ** |
| 2114 | ** Expr.op Changed to TK_COLUMN |
| 2115 | ** Expr.pTab Points to the Table object for X.Y |
| 2116 | ** Expr.iColumn The column index in X.Y. -1 for the rowid. |
| 2117 | ** Expr.iTable The VDBE cursor number for X.Y |
| 2118 | ** |
| 2119 | ** |
| 2120 | ** To resolve result-set references, look for expression nodes of the |
| 2121 | ** form Z (with no X and Y prefix) where the Z matches the right-hand |
| 2122 | ** size of an AS clause in the result-set of a SELECT. The Z expression |
| 2123 | ** is replaced by a copy of the left-hand side of the result-set expression. |
| 2124 | ** Table-name and function resolution occurs on the substituted expression |
| 2125 | ** tree. For example, in: |
| 2126 | ** |
| 2127 | ** SELECT a+b AS x, c+d AS y FROM t1 ORDER BY x; |
| 2128 | ** |
| 2129 | ** The "x" term of the order by is replaced by "a+b" to render: |
| 2130 | ** |
| 2131 | ** SELECT a+b AS x, c+d AS y FROM t1 ORDER BY a+b; |
| 2132 | ** |
larrybr | bc91738 | 2023-06-07 08:40:31 | [diff] [blame] | 2133 | ** Function calls are checked to make sure that the function is |
drh | 7d10d5a | 2008-08-20 16:35:10 | [diff] [blame] | 2134 | ** defined and that the correct number of arguments are specified. |
drh | a51009b | 2012-05-21 19:11:25 | [diff] [blame] | 2135 | ** If the function is an aggregate function, then the NC_HasAgg flag is |
drh | 7d10d5a | 2008-08-20 16:35:10 | [diff] [blame] | 2136 | ** set and the opcode is changed from TK_FUNCTION to TK_AGG_FUNCTION. |
| 2137 | ** If an expression contains aggregate functions then the EP_Agg |
| 2138 | ** property on the expression is set. |
| 2139 | ** |
| 2140 | ** An error message is left in pParse if anything is amiss. The number |
| 2141 | ** if errors is returned. |
| 2142 | */ |
larrybr | bc91738 | 2023-06-07 08:40:31 | [diff] [blame] | 2143 | int sqlite3ResolveExprNames( |
drh | 7d10d5a | 2008-08-20 16:35:10 | [diff] [blame] | 2144 | NameContext *pNC, /* Namespace to resolve expressions in. */ |
| 2145 | Expr *pExpr /* The expression to be analyzed. */ |
| 2146 | ){ |
dan | 0d92571 | 2019-05-20 17:14:25 | [diff] [blame] | 2147 | int savedHasAgg; |
drh | 7d10d5a | 2008-08-20 16:35:10 | [diff] [blame] | 2148 | Walker w; |
| 2149 | |
drh | d03257c | 2017-05-31 00:49:40 | [diff] [blame] | 2150 | if( pExpr==0 ) return SQLITE_OK; |
drh | bb30123 | 2021-07-15 19:29:43 | [diff] [blame] | 2151 | savedHasAgg = pNC->ncFlags & (NC_HasAgg|NC_MinMaxAgg|NC_HasWin|NC_OrderAgg); |
| 2152 | pNC->ncFlags &= ~(NC_HasAgg|NC_MinMaxAgg|NC_HasWin|NC_OrderAgg); |
drh | 9bfb024 | 2016-01-20 02:21:50 | [diff] [blame] | 2153 | w.pParse = pNC->pParse; |
drh | 7d10d5a | 2008-08-20 16:35:10 | [diff] [blame] | 2154 | w.xExprCallback = resolveExprStep; |
dan | d487e37 | 2021-04-12 16:59:28 | [diff] [blame] | 2155 | w.xSelectCallback = (pNC->ncFlags & NC_NoSelect) ? 0 : resolveSelectStep; |
drh | 9bfb024 | 2016-01-20 02:21:50 | [diff] [blame] | 2156 | w.xSelectCallback2 = 0; |
drh | 7d10d5a | 2008-08-20 16:35:10 | [diff] [blame] | 2157 | w.u.pNC = pNC; |
drh | d03257c | 2017-05-31 00:49:40 | [diff] [blame] | 2158 | #if SQLITE_MAX_EXPR_DEPTH>0 |
| 2159 | w.pParse->nHeight += pExpr->nHeight; |
| 2160 | if( sqlite3ExprCheckHeight(w.pParse, w.pParse->nHeight) ){ |
| 2161 | return SQLITE_ERROR; |
| 2162 | } |
| 2163 | #endif |
drh | f82c8cb | 2023-06-19 23:27:22 | [diff] [blame] | 2164 | assert( pExpr!=0 ); |
| 2165 | sqlite3WalkExprNN(&w, pExpr); |
drh | 7d10d5a | 2008-08-20 16:35:10 | [diff] [blame] | 2166 | #if SQLITE_MAX_EXPR_DEPTH>0 |
drh | d03257c | 2017-05-31 00:49:40 | [diff] [blame] | 2167 | w.pParse->nHeight -= pExpr->nHeight; |
drh | 7d10d5a | 2008-08-20 16:35:10 | [diff] [blame] | 2168 | #endif |
drh | d137f4e | 2019-03-29 01:15:11 | [diff] [blame] | 2169 | assert( EP_Agg==NC_HasAgg ); |
| 2170 | assert( EP_Win==NC_HasWin ); |
| 2171 | testcase( pNC->ncFlags & NC_HasAgg ); |
| 2172 | testcase( pNC->ncFlags & NC_HasWin ); |
| 2173 | ExprSetProperty(pExpr, pNC->ncFlags & (NC_HasAgg|NC_HasWin) ); |
drh | 9588ad9 | 2014-09-15 14:46:02 | [diff] [blame] | 2174 | pNC->ncFlags |= savedHasAgg; |
drh | 050611a | 2021-04-10 13:37:04 | [diff] [blame] | 2175 | return pNC->nNcErr>0 || w.pParse->nErr>0; |
drh | 7d10d5a | 2008-08-20 16:35:10 | [diff] [blame] | 2176 | } |
drh | 7d10d5a | 2008-08-20 16:35:10 | [diff] [blame] | 2177 | |
drh | 01d230c | 2015-08-19 17:11:37 | [diff] [blame] | 2178 | /* |
| 2179 | ** Resolve all names for all expression in an expression list. This is |
| 2180 | ** just like sqlite3ResolveExprNames() except that it works for an expression |
| 2181 | ** list rather than a single expression. |
drh | 610a5ba | 2024-08-09 18:18:11 | [diff] [blame] | 2182 | ** |
| 2183 | ** The return value is SQLITE_OK (0) for success or SQLITE_ERROR (1) for a |
| 2184 | ** failure. |
drh | 01d230c | 2015-08-19 17:11:37 | [diff] [blame] | 2185 | */ |
larrybr | bc91738 | 2023-06-07 08:40:31 | [diff] [blame] | 2186 | int sqlite3ResolveExprListNames( |
drh | 01d230c | 2015-08-19 17:11:37 | [diff] [blame] | 2187 | NameContext *pNC, /* Namespace to resolve expressions in. */ |
| 2188 | ExprList *pList /* The expression list to be analyzed. */ |
| 2189 | ){ |
drh | d8b1bfc | 2015-08-20 23:21:34 | [diff] [blame] | 2190 | int i; |
drh | 4047bdf | 2020-04-06 20:35:52 | [diff] [blame] | 2191 | int savedHasAgg = 0; |
| 2192 | Walker w; |
drh | 610a5ba | 2024-08-09 18:18:11 | [diff] [blame] | 2193 | if( pList==0 ) return SQLITE_OK; |
drh | 4047bdf | 2020-04-06 20:35:52 | [diff] [blame] | 2194 | w.pParse = pNC->pParse; |
| 2195 | w.xExprCallback = resolveExprStep; |
| 2196 | w.xSelectCallback = resolveSelectStep; |
| 2197 | w.xSelectCallback2 = 0; |
| 2198 | w.u.pNC = pNC; |
drh | bb30123 | 2021-07-15 19:29:43 | [diff] [blame] | 2199 | savedHasAgg = pNC->ncFlags & (NC_HasAgg|NC_MinMaxAgg|NC_HasWin|NC_OrderAgg); |
| 2200 | pNC->ncFlags &= ~(NC_HasAgg|NC_MinMaxAgg|NC_HasWin|NC_OrderAgg); |
drh | 4047bdf | 2020-04-06 20:35:52 | [diff] [blame] | 2201 | for(i=0; i<pList->nExpr; i++){ |
| 2202 | Expr *pExpr = pList->a[i].pExpr; |
| 2203 | if( pExpr==0 ) continue; |
| 2204 | #if SQLITE_MAX_EXPR_DEPTH>0 |
| 2205 | w.pParse->nHeight += pExpr->nHeight; |
| 2206 | if( sqlite3ExprCheckHeight(w.pParse, w.pParse->nHeight) ){ |
drh | 610a5ba | 2024-08-09 18:18:11 | [diff] [blame] | 2207 | return SQLITE_ERROR; |
drh | 2029231 | 2015-11-21 13:24:46 | [diff] [blame] | 2208 | } |
drh | 4047bdf | 2020-04-06 20:35:52 | [diff] [blame] | 2209 | #endif |
drh | f82c8cb | 2023-06-19 23:27:22 | [diff] [blame] | 2210 | sqlite3WalkExprNN(&w, pExpr); |
drh | 4047bdf | 2020-04-06 20:35:52 | [diff] [blame] | 2211 | #if SQLITE_MAX_EXPR_DEPTH>0 |
| 2212 | w.pParse->nHeight -= pExpr->nHeight; |
| 2213 | #endif |
| 2214 | assert( EP_Agg==NC_HasAgg ); |
| 2215 | assert( EP_Win==NC_HasWin ); |
| 2216 | testcase( pNC->ncFlags & NC_HasAgg ); |
| 2217 | testcase( pNC->ncFlags & NC_HasWin ); |
drh | bb30123 | 2021-07-15 19:29:43 | [diff] [blame] | 2218 | if( pNC->ncFlags & (NC_HasAgg|NC_MinMaxAgg|NC_HasWin|NC_OrderAgg) ){ |
drh | 4047bdf | 2020-04-06 20:35:52 | [diff] [blame] | 2219 | ExprSetProperty(pExpr, pNC->ncFlags & (NC_HasAgg|NC_HasWin) ); |
drh | bb30123 | 2021-07-15 19:29:43 | [diff] [blame] | 2220 | savedHasAgg |= pNC->ncFlags & |
| 2221 | (NC_HasAgg|NC_MinMaxAgg|NC_HasWin|NC_OrderAgg); |
| 2222 | pNC->ncFlags &= ~(NC_HasAgg|NC_MinMaxAgg|NC_HasWin|NC_OrderAgg); |
drh | 4047bdf | 2020-04-06 20:35:52 | [diff] [blame] | 2223 | } |
drh | 610a5ba | 2024-08-09 18:18:11 | [diff] [blame] | 2224 | if( w.pParse->nErr>0 ) return SQLITE_ERROR; |
drh | 01d230c | 2015-08-19 17:11:37 | [diff] [blame] | 2225 | } |
drh | 4047bdf | 2020-04-06 20:35:52 | [diff] [blame] | 2226 | pNC->ncFlags |= savedHasAgg; |
drh | 610a5ba | 2024-08-09 18:18:11 | [diff] [blame] | 2227 | return SQLITE_OK; |
drh | 01d230c | 2015-08-19 17:11:37 | [diff] [blame] | 2228 | } |
drh | 7d10d5a | 2008-08-20 16:35:10 | [diff] [blame] | 2229 | |
| 2230 | /* |
| 2231 | ** Resolve all names in all expressions of a SELECT and in all |
larrybr | bc91738 | 2023-06-07 08:40:31 | [diff] [blame] | 2232 | ** descendants of the SELECT, including compounds off of p->pPrior, |
drh | 7d10d5a | 2008-08-20 16:35:10 | [diff] [blame] | 2233 | ** subqueries in expressions, and subqueries used as FROM clause |
| 2234 | ** terms. |
| 2235 | ** |
| 2236 | ** See sqlite3ResolveExprNames() for a description of the kinds of |
| 2237 | ** transformations that occur. |
| 2238 | ** |
| 2239 | ** All SELECT statements should have been expanded using |
| 2240 | ** sqlite3SelectExpand() prior to invoking this routine. |
| 2241 | */ |
| 2242 | void sqlite3ResolveSelectNames( |
| 2243 | Parse *pParse, /* The parser context */ |
| 2244 | Select *p, /* The SELECT statement being coded. */ |
| 2245 | NameContext *pOuterNC /* Name context for parent SELECT statement */ |
| 2246 | ){ |
| 2247 | Walker w; |
| 2248 | |
drh | 0a846f9 | 2008-08-25 17:23:29 | [diff] [blame] | 2249 | assert( p!=0 ); |
drh | 0a846f9 | 2008-08-25 17:23:29 | [diff] [blame] | 2250 | w.xExprCallback = resolveExprStep; |
| 2251 | w.xSelectCallback = resolveSelectStep; |
drh | 979dd1b | 2017-05-29 14:26:07 | [diff] [blame] | 2252 | w.xSelectCallback2 = 0; |
drh | 0a846f9 | 2008-08-25 17:23:29 | [diff] [blame] | 2253 | w.pParse = pParse; |
| 2254 | w.u.pNC = pOuterNC; |
| 2255 | sqlite3WalkSelect(&w, p); |
drh | 7d10d5a | 2008-08-20 16:35:10 | [diff] [blame] | 2256 | } |
drh | 3780be1 | 2013-07-31 19:05:22 | [diff] [blame] | 2257 | |
| 2258 | /* |
drh | ee751fa | 2019-01-02 14:34:46 | [diff] [blame] | 2259 | ** Resolve names in expressions that can only reference a single table |
| 2260 | ** or which cannot reference any tables at all. Examples: |
drh | 3780be1 | 2013-07-31 19:05:22 | [diff] [blame] | 2261 | ** |
drh | 20cee7d | 2019-10-30 18:50:08 | [diff] [blame] | 2262 | ** "type" flag |
| 2263 | ** ------------ |
| 2264 | ** (1) CHECK constraints NC_IsCheck |
| 2265 | ** (2) WHERE clauses on partial indices NC_PartIdx |
| 2266 | ** (3) Expressions in indexes on expressions NC_IdxExpr |
| 2267 | ** (4) Expression arguments to VACUUM INTO. 0 |
| 2268 | ** (5) GENERATED ALWAYS as expressions NC_GenCol |
drh | 3780be1 | 2013-07-31 19:05:22 | [diff] [blame] | 2269 | ** |
drh | ee751fa | 2019-01-02 14:34:46 | [diff] [blame] | 2270 | ** In all cases except (4), the Expr.iTable value for Expr.op==TK_COLUMN |
| 2271 | ** nodes of the expression is set to -1 and the Expr.iColumn value is |
| 2272 | ** set to the column number. In case (4), TK_COLUMN nodes cause an error. |
drh | 3780be1 | 2013-07-31 19:05:22 | [diff] [blame] | 2273 | ** |
| 2274 | ** Any errors cause an error message to be set in pParse. |
| 2275 | */ |
drh | ee751fa | 2019-01-02 14:34:46 | [diff] [blame] | 2276 | int sqlite3ResolveSelfReference( |
drh | 81f7b37 | 2019-10-16 12:18:59 | [diff] [blame] | 2277 | Parse *pParse, /* Parsing context */ |
| 2278 | Table *pTab, /* The table being referenced, or NULL */ |
| 2279 | int type, /* NC_IsCheck, NC_PartIdx, NC_IdxExpr, NC_GenCol, or 0 */ |
| 2280 | Expr *pExpr, /* Expression to resolve. May be NULL. */ |
| 2281 | ExprList *pList /* Expression list to resolve. May be NULL. */ |
drh | 3780be1 | 2013-07-31 19:05:22 | [diff] [blame] | 2282 | ){ |
drh | cebf06c | 2025-03-14 18:10:02 | [diff] [blame] | 2283 | SrcList *pSrc; /* Fake SrcList for pParse->pNewTable */ |
drh | 3780be1 | 2013-07-31 19:05:22 | [diff] [blame] | 2284 | NameContext sNC; /* Name context for pParse->pNewTable */ |
drh | ee751fa | 2019-01-02 14:34:46 | [diff] [blame] | 2285 | int rc; |
drh | cebf06c | 2025-03-14 18:10:02 | [diff] [blame] | 2286 | u8 srcSpace[SZ_SRCLIST_1]; /* Memory space for the fake SrcList */ |
drh | 3780be1 | 2013-07-31 19:05:22 | [diff] [blame] | 2287 | |
drh | ee751fa | 2019-01-02 14:34:46 | [diff] [blame] | 2288 | assert( type==0 || pTab!=0 ); |
drh | 81f7b37 | 2019-10-16 12:18:59 | [diff] [blame] | 2289 | assert( type==NC_IsCheck || type==NC_PartIdx || type==NC_IdxExpr |
| 2290 | || type==NC_GenCol || pTab==0 ); |
drh | 3780be1 | 2013-07-31 19:05:22 | [diff] [blame] | 2291 | memset(&sNC, 0, sizeof(sNC)); |
drh | cebf06c | 2025-03-14 18:10:02 | [diff] [blame] | 2292 | pSrc = (SrcList*)srcSpace; |
| 2293 | memset(pSrc, 0, SZ_SRCLIST_1); |
drh | ee751fa | 2019-01-02 14:34:46 | [diff] [blame] | 2294 | if( pTab ){ |
drh | cebf06c | 2025-03-14 18:10:02 | [diff] [blame] | 2295 | pSrc->nSrc = 1; |
| 2296 | pSrc->a[0].zName = pTab->zName; |
| 2297 | pSrc->a[0].pSTab = pTab; |
| 2298 | pSrc->a[0].iCursor = -1; |
drh | 05b32ee | 2020-01-09 01:20:03 | [diff] [blame] | 2299 | if( pTab->pSchema!=pParse->db->aDb[1].pSchema ){ |
| 2300 | /* Cause EP_FromDDL to be set on TK_FUNCTION nodes of non-TEMP |
| 2301 | ** schema elements */ |
| 2302 | type |= NC_FromDDL; |
| 2303 | } |
drh | ee751fa | 2019-01-02 14:34:46 | [diff] [blame] | 2304 | } |
drh | 3780be1 | 2013-07-31 19:05:22 | [diff] [blame] | 2305 | sNC.pParse = pParse; |
drh | cebf06c | 2025-03-14 18:10:02 | [diff] [blame] | 2306 | sNC.pSrcList = pSrc; |
drh | d0ff601 | 2019-06-17 13:56:11 | [diff] [blame] | 2307 | sNC.ncFlags = type | NC_IsDDL; |
drh | ee751fa | 2019-01-02 14:34:46 | [diff] [blame] | 2308 | if( (rc = sqlite3ResolveExprNames(&sNC, pExpr))!=SQLITE_OK ) return rc; |
| 2309 | if( pList ) rc = sqlite3ResolveExprListNames(&sNC, pList); |
| 2310 | return rc; |
drh | 3780be1 | 2013-07-31 19:05:22 | [diff] [blame] | 2311 | } |