AryaWu/sqlite
0
1/*2** 2008 August 183**4** The author disclaims copyright to this source code. In place of5** 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 and14** resolve all identifiers by associating them with a particular15** table and column.16*/17#include "sqliteInt.h"18 19/*20** Magic table number to mean the EXCLUDED table in an UPSERT statement.21*/22#define EXCLUDED_TABLE_NUMBER 223 24/*25** Walk the expression tree pExpr and increase the aggregate function26** 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 an28** 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.32**33** See also the sqlite3WindowExtraAggFuncDepth() routine in window.c34*/35static int incrAggDepth(Walker *pWalker, Expr *pExpr){36 if( pExpr->op==TK_AGG_FUNCTION ) pExpr->op2 += pWalker->u.n;37 return WRC_Continue;38}39static void incrAggFunctionDepth(Expr *pExpr, int N){40 if( N>0 ){41 Walker w;42 memset(&w, 0, sizeof(w));43 w.xExprCallback = incrAggDepth;44 w.u.n = N;45 sqlite3WalkExpr(&w, pExpr);46 }47}48 49/*50** Turn the pExpr expression into an alias for the iCol-th column of the51** result set in pEList.52**53** If the reference is followed by a COLLATE operator, then make sure54** 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**62** The nSubquery parameter specifies how many levels of subquery the63** alias is removed from the original expression. The usual value is64** zero but it might be more if the alias is contained within a subquery65** of the original expression. The Expr.op2 field of TK_AGG_FUNCTION66** structures must be increased by the nSubquery amount.67*/68static 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 */73 int nSubquery /* Number of subqueries that the label is moving */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 );82 assert( !ExprHasProperty(pExpr, EP_Reduced|EP_TokenOnly) );83 if( pExpr->pAggInfo ) return;84 db = pParse->db;85 pDup = sqlite3ExprDup(db, pOrig, 0);86 if( db->mallocFailed ){87 sqlite3ExprDelete(db, pDup);88 pDup = 0;89 }else{90 Expr temp;91 incrAggFunctionDepth(pDup, nSubquery);92 if( pExpr->op==TK_COLLATE ){93 assert( !ExprHasProperty(pExpr, EP_IntValue) );94 pDup = sqlite3ExprAddCollateString(pParse, pDup, pExpr->u.zToken);95 }96 memcpy(&temp, pDup, sizeof(Expr));97 memcpy(pDup, pExpr, sizeof(Expr));98 memcpy(pExpr, &temp, sizeof(Expr));99 if( ExprHasProperty(pExpr, EP_WinFunc) ){100 if( ALWAYS(pExpr->y.pWin!=0) ){101 pExpr->y.pWin->pOwner = pExpr;102 }103 }104 sqlite3ExprDeferredDelete(pParse, pDup);105 }106}107 108/*109** Subqueries store the original database, table and column names for their110** result sets in ExprList.a[].zSpan, in the form "DATABASE.TABLE.COLUMN",111** and mark the expression-list item by setting ExprList.a[].fg.eEName112** to ENAME_TAB.113**114** Check to see if the zSpan/eEName of the expression-list item passed to this115** routine matches the zDb, zTab, and zCol. If any of zDb, zTab, and zCol are116** 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 (or120** _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 pItem122** 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.124*/125int sqlite3MatchEName(126 const struct ExprList_item *pItem,127 const char *zCol,128 const char *zTab,129 const char *zDb,130 int *pbRowid131){132 int n;133 const char *zSpan;134 int eEName = pItem->fg.eEName;135 if( eEName!=ENAME_TAB && (eEName!=ENAME_ROWID || NEVER(pbRowid==0)) ){136 return 0;137 }138 assert( pbRowid==0 || *pbRowid==0 );139 zSpan = pItem->zEName;140 for(n=0; ALWAYS(zSpan[n]) && zSpan[n]!='.'; n++){}141 if( zDb && (sqlite3StrNICmp(zSpan, zDb, n)!=0 || zDb[n]!=0) ){142 return 0;143 }144 zSpan += n+1;145 for(n=0; ALWAYS(zSpan[n]) && zSpan[n]!='.'; n++){}146 if( zTab && (sqlite3StrNICmp(zSpan, zTab, n)!=0 || zTab[n]!=0) ){147 return 0;148 }149 zSpan += n+1;150 if( zCol ){151 if( eEName==ENAME_TAB && sqlite3StrICmp(zSpan, zCol)!=0 ) return 0;152 if( eEName==ENAME_ROWID && sqlite3IsRowid(zCol)==0 ) return 0;153 }154 if( eEName==ENAME_ROWID ) *pbRowid = 1;155 return 1;156}157 158/*159** Return TRUE if the double-quoted string mis-feature should be supported.160*/161static 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/*176** The argument is guaranteed to be a non-NULL Expr node of type TK_COLUMN.177** return the appropriate colUsed mask.178*/179Bitmask sqlite3ExprColUsed(Expr *pExpr){180 int n;181 Table *pExTab;182 183 n = pExpr->iColumn;184 assert( ExprUseYTab(pExpr) );185 pExTab = pExpr->y.pTab;186 assert( pExTab!=0 );187 assert( n < pExTab->nCol );188 if( (pExTab->tabFlags & TF_HasGenerated)!=0189 && (pExTab->aCol[n].colFlags & COLFLAG_GENERATED)!=0190 ){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/*203** Create a new expression term for the column specified by pMatch and204** iColumn. Append this new expression term to the FULL JOIN Match set205** in *ppList. Create a new *ppList if this is the first term in the206** set.207*/208static 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 ){216 pNew->iTable = pMatch->iCursor;217 pNew->iColumn = iColumn;218 pNew->y.pTab = pMatch->pSTab;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/*226** Return TRUE (non-zero) if zTab is a valid name for the schema table pTab.227*/228static 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 */231 const char *zDb /* non-NULL if a database qualifier is present */232){233 const char *zLegacy;234 assert( pTab!=0 );235 assert( pTab->tnum==1 );236 if( sqlite3StrNICmp(zTab, "sqlite_", 7)!=0 ) return 0;237 zLegacy = pTab->zName;238 if( strcmp(zLegacy+7, &LEGACY_TEMP_SCHEMA_TABLE[7])==0 ){239 if( sqlite3StrICmp(zTab+7, &PREFERRED_TEMP_SCHEMA_TABLE[7])==0 ){240 return 1;241 }242 if( zDb==0 ) return 0;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;247 }248 return 0;249}250 251/*252** Given the name of a column of the form X.Y.Z or Y.Z or just Z, look up253** that name in the set of source tables in pSrcList and make the pExpr254** expression node refer back to that source column. The following changes255** are made to pExpr:256**257** pExpr->iDb Set the index in db->aDb[] of the database X258** (even if X is implied).259** pExpr->iTable Set to the cursor number for the table obtained260** from pSrcList.261** pExpr->y.pTab Points to the Table structure of X.Y (even if262** 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 deleted266** pExpr->pRight Any expression this points to is deleted.267**268** The zDb variable is the name of the database (the "X"). This value may be269** NULL meaning that name is of the form Y.Z or Z. Any available database270** can be used. The zTable variable is the name of the table (the "Y"). This271** value can be NULL if zDb is also NULL. If zTable is NULL it272** means that the form of the name is Z and that columns from any table273** can be used.274**275** If the name cannot be resolved unambiguously, leave an error message276** in pParse and return WRC_Abort. Return WRC_Prune on success.277*/278static int lookupName(279 Parse *pParse, /* The parsing context */280 const char *zDb, /* Name of the database containing table, or NULL */281 const char *zTab, /* Name of table containing column, or NULL */282 const Expr *pRight, /* Name of the column. */283 NameContext *pNC, /* The name context used to resolve the name */284 Expr *pExpr /* Make this EXPR node point to the selected column */285){286 int i, j; /* Loop counters */287 int cnt = 0; /* Number of matching column names */288 int cntTab = 0; /* Number of potential "rowid" matches */289 int nSubquery = 0; /* How many levels of subquery */290 sqlite3 *db = pParse->db; /* The database connection */291 SrcItem *pItem; /* Use for looping over pSrcList items */292 SrcItem *pMatch = 0; /* The matching pSrcList item */293 NameContext *pTopNC = pNC; /* First namecontext in the list */294 Schema *pSchema = 0; /* Schema of the expression */295 int eNewExprOp = TK_COLUMN; /* New value for pExpr->op on success */296 Table *pTab = 0; /* Table holding the row */297 ExprList *pFJMatch = 0; /* Matches for FULL JOIN .. USING */298 const char *zCol = pRight->u.zToken;299 300 assert( pNC ); /* the name context cannot be NULL. */301 assert( zCol ); /* The Z in X.Y.Z cannot be NULL */302 assert( zDb==0 || zTab!=0 );303 assert( !ExprHasProperty(pExpr, EP_TokenOnly|EP_Reduced) );304 305 /* Initialize the node to no-match */306 pExpr->iTable = -1;307 ExprSetVVAProperty(pExpr, EP_NoReduce);308 309 /* Translate the schema name in zDb into a pointer to the corresponding310 ** schema. If not found, pSchema will remain NULL and nothing will match311 ** resulting in an appropriate error message toward the end of this routine312 */313 if( zDb ){314 testcase( pNC->ncFlags & NC_PartIdx );315 testcase( pNC->ncFlags & NC_IsCheck );316 if( (pNC->ncFlags & (NC_PartIdx|NC_IsCheck))!=0 ){317 /* Silently ignore database qualifiers inside CHECK constraints and318 ** partial indices. Do not raise errors because that might break319 ** legacy and because it does not hurt anything to just ignore the320 ** database name. */321 zDb = 0;322 }else{323 for(i=0; i<db->nDb; i++){324 assert( db->aDb[i].zDbSName );325 if( sqlite3StrICmp(db->aDb[i].zDbSName,zDb)==0 ){326 pSchema = db->aDb[i].pSchema;327 break;328 }329 }330 if( i==db->nDb && sqlite3StrICmp("main", zDb)==0 ){331 /* This branch is taken when the main database has been renamed332 ** using SQLITE_DBCONFIG_MAINDBNAME. */333 pSchema = db->aDb[0].pSchema;334 zDb = db->aDb[0].zDbSName;335 }336 }337 }338 339 /* Start at the inner-most context and move outward until a match is found */340 assert( pNC && cnt==0 );341 do{342 ExprList *pEList;343 SrcList *pSrcList = pNC->pSrcList;344 345 if( pSrcList ){346 for(i=0, pItem=pSrcList->a; i<pSrcList->nSrc; i++, pItem++){347 pTab = pItem->pSTab;348 assert( pTab!=0 && pTab->zName!=0 );349 assert( pTab->nCol>0 || pParse->nErr );350 assert( (int)pItem->fg.isNestedFrom == IsNestedFrom(pItem));351 if( pItem->fg.isNestedFrom ){352 /* In this case, pItem is a subquery that has been formed from a353 ** 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 */358 int hit = 0;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;365 assert( pEList!=0 );366 assert( pEList->nExpr==pTab->nCol );367 for(j=0; j<pEList->nExpr; j++){368 int bRowid = 0; /* True if possible rowid match */369 if( !sqlite3MatchEName(&pEList->a[j], zCol, zTab, zDb, &bRowid) ){370 continue;371 }372 if( bRowid==0 ){373 if( cnt>0 ){374 if( pItem->fg.isUsing==0375 || sqlite3IdListIndex(pItem->u3.pUsing, zCol)<0376 || pMatch==pItem377 ){378 /* Two or more tables have the same column name which is379 ** not joined by USING. Or, a single table has two columns380 ** that match a USING term (if pMatch==pItem). These are both381 ** "ambiguous column name" errors. Signal as much by clearing382 ** pFJMatch and letting cnt go above 1. */383 sqlite3ExprListDelete(db, pFJMatch);384 pFJMatch = 0;385 }else386 if( (pItem->fg.jointype & JT_RIGHT)==0 ){387 /* An INNER or LEFT JOIN. Use the left-most table */388 continue;389 }else390 if( (pItem->fg.jointype & JT_LEFT)==0 ){391 /* A RIGHT JOIN. Use the right-most table */392 cnt = 0;393 sqlite3ExprListDelete(db, pFJMatch);394 pFJMatch = 0;395 }else{396 /* For a FULL JOIN, we must construct a coalesce() func */397 extendFJMatch(pParse, &pFJMatch, pMatch, pExpr->iColumn);398 }399 }400 cnt++;401 hit = 1;402 }else if( cnt>0 ){403 /* This is a potential rowid match, but there has already been404 ** a real match found. So this can be ignored. */405 continue;406 }407 cntTab++;408 pMatch = pItem;409 pExpr->iColumn = j;410 pEList->a[j].fg.bUsed = 1;411 412 /* rowid cannot be part of a USING clause - assert() this. */413 assert( bRowid==0 || pEList->a[j].fg.bUsingTerm==0 );414 if( pEList->a[j].fg.bUsingTerm ) break;415 }416 if( hit || zTab==0 ) continue;417 }418 assert( zDb==0 || zTab!=0 );419 if( zTab ){420 if( zDb ){421 if( pTab->pSchema!=pSchema ) continue;422 if( pSchema==0 && strcmp(zDb,"*")!=0 ) continue;423 }424 if( pItem->zAlias!=0 ){425 if( sqlite3StrICmp(zTab, pItem->zAlias)!=0 ){426 continue;427 }428 }else if( sqlite3StrICmp(zTab, pTab->zName)!=0 ){429 if( pTab->tnum!=1 ) continue;430 if( !isValidSchemaTableName(zTab, pTab, zDb) ) continue;431 }432 assert( ExprUseYTab(pExpr) );433 if( IN_RENAME_OBJECT && pItem->zAlias ){434 sqlite3RenameTokenRemap(pParse, 0, (void*)&pExpr->y.pTab);435 }436 }437 j = sqlite3ColumnIndex(pTab, zCol);438 if( j>=0 ){439 if( cnt>0 ){440 if( pItem->fg.isUsing==0441 || sqlite3IdListIndex(pItem->u3.pUsing, zCol)<0442 ){443 /* Two or more tables have the same column name which is444 ** not joined by USING. This is an error. Signal as much445 ** by clearing pFJMatch and letting cnt go above 1. */446 sqlite3ExprListDelete(db, pFJMatch);447 pFJMatch = 0;448 }else449 if( (pItem->fg.jointype & JT_RIGHT)==0 ){450 /* An INNER or LEFT JOIN. Use the left-most table */451 continue;452 }else453 if( (pItem->fg.jointype & JT_LEFT)==0 ){454 /* A RIGHT JOIN. Use the right-most table */455 cnt = 0;456 sqlite3ExprListDelete(db, pFJMatch);457 pFJMatch = 0;458 }else{459 /* For a FULL JOIN, we must construct a coalesce() func */460 extendFJMatch(pParse, &pFJMatch, pMatch, pExpr->iColumn);461 }462 }463 cnt++;464 pMatch = pItem;465 /* Substitute the rowid (column -1) for the INTEGER PRIMARY KEY */466 pExpr->iColumn = j==pTab->iPKey ? -1 : (i16)j;467 if( pItem->fg.isNestedFrom ){468 sqlite3SrcItemColumnUsed(pItem, j);469 }470 }471 if( 0==cnt && VisibleRowid(pTab) ){472 /* pTab is a potential ROWID match. Keep track of it and match473 ** the ROWID later if that seems appropriate. (Search for "cntTab"474 ** to find related code.) Only allow a ROWID match if there is475 ** a single ROWID match candidate.476 */477#ifdef SQLITE_ALLOW_ROWID_IN_VIEW478 /* In SQLITE_ALLOW_ROWID_IN_VIEW mode, allow a ROWID match479 ** if there is a single VIEW candidate or if there is a single480 ** non-VIEW candidate plus multiple VIEW candidates. In other481 ** words non-VIEW candidate terms take precedence over VIEWs.482 */483 if( cntTab==0484 || (cntTab==1485 && pMatch!=0486 && ALWAYS(pMatch->pSTab!=0)487 && (pMatch->pSTab->tabFlags & TF_Ephemeral)!=0488 && (pTab->tabFlags & TF_Ephemeral)==0)489 ){490 cntTab = 1;491 pMatch = pItem;492 }else{493 cntTab++;494 }495#else496 /* The (much more common) non-SQLITE_ALLOW_ROWID_IN_VIEW case is497 ** simpler since we require exactly one candidate, which will498 ** always be a non-VIEW499 */500 cntTab++;501 pMatch = pItem;502#endif503 }504 }505 if( pMatch ){506 pExpr->iTable = pMatch->iCursor;507 assert( ExprUseYTab(pExpr) );508 pExpr->y.pTab = pMatch->pSTab;509 if( (pMatch->fg.jointype & (JT_LEFT|JT_LTORJ))!=0 ){510 ExprSetProperty(pExpr, EP_CanBeNull);511 }512 pSchema = pExpr->y.pTab->pSchema;513 }514 } /* if( pSrcList ) */515 516#if !defined(SQLITE_OMIT_TRIGGER) || !defined(SQLITE_OMIT_UPSERT)517 /* If we have not already resolved the name, then maybe518 ** it is a new.* or old.* trigger argument reference. Or519 ** maybe it is an excluded.* from an upsert. Or maybe it is520 ** a reference in the RETURNING clause to a table being modified.521 */522 if( cnt==0 && zDb==0 ){523 pTab = 0;524#ifndef SQLITE_OMIT_TRIGGER525 if( pParse->pTriggerTab!=0 ){526 int op = pParse->eTriggerOp;527 assert( op==TK_DELETE || op==TK_UPDATE || op==TK_INSERT );528 if( pParse->bReturning ){529 if( (pNC->ncFlags & NC_UBaseReg)!=0530 && ALWAYS(zTab==0531 || sqlite3StrICmp(zTab,pParse->pTriggerTab->zName)==0532 || isValidSchemaTableName(zTab, pParse->pTriggerTab, 0))533 ){534 pExpr->iTable = op!=TK_DELETE;535 pTab = pParse->pTriggerTab;536 }537 }else if( op!=TK_DELETE && zTab && sqlite3StrICmp("new",zTab) == 0 ){538 pExpr->iTable = 1;539 pTab = pParse->pTriggerTab;540 }else if( op!=TK_INSERT && zTab && sqlite3StrICmp("old",zTab)==0 ){541 pExpr->iTable = 0;542 pTab = pParse->pTriggerTab;543 }544 }545#endif /* SQLITE_OMIT_TRIGGER */546#ifndef SQLITE_OMIT_UPSERT547 if( (pNC->ncFlags & NC_UUpsert)!=0 && zTab!=0 ){548 Upsert *pUpsert = pNC->uNC.pUpsert;549 if( pUpsert && sqlite3StrICmp("excluded",zTab)==0 ){550 pTab = pUpsert->pUpsertSrc->a[0].pSTab;551 pExpr->iTable = EXCLUDED_TABLE_NUMBER;552 }553 }554#endif /* SQLITE_OMIT_UPSERT */555 556 if( pTab ){557 int iCol;558 pSchema = pTab->pSchema;559 cntTab++;560 iCol = sqlite3ColumnIndex(pTab, zCol);561 if( iCol>=0 ){562 if( pTab->iPKey==iCol ) iCol = -1;563 }else{564 if( sqlite3IsRowid(zCol) && VisibleRowid(pTab) ){565 iCol = -1;566 }else{567 iCol = pTab->nCol;568 }569 }570 if( iCol<pTab->nCol ){571 cnt++;572 pMatch = 0;573#ifndef SQLITE_OMIT_UPSERT574 if( pExpr->iTable==EXCLUDED_TABLE_NUMBER ){575 testcase( iCol==(-1) );576 assert( ExprUseYTab(pExpr) );577 if( IN_RENAME_OBJECT ){578 pExpr->iColumn = iCol;579 pExpr->y.pTab = pTab;580 eNewExprOp = TK_COLUMN;581 }else{582 pExpr->iTable = pNC->uNC.pUpsert->regData +583 sqlite3TableColumnToStorage(pTab, iCol);584 eNewExprOp = TK_REGISTER;585 }586 }else587#endif /* SQLITE_OMIT_UPSERT */588 {589 assert( ExprUseYTab(pExpr) );590 pExpr->y.pTab = pTab;591 if( pParse->bReturning ){592 eNewExprOp = TK_REGISTER;593 pExpr->op2 = TK_COLUMN;594 pExpr->iColumn = iCol;595 pExpr->iTable = pNC->uNC.iBaseReg + (pTab->nCol+1)*pExpr->iTable +596 sqlite3TableColumnToStorage(pTab, iCol) + 1;597 }else{598 pExpr->iColumn = (i16)iCol;599 eNewExprOp = TK_TRIGGER;600#ifndef SQLITE_OMIT_TRIGGER601 if( iCol<0 ){602 pExpr->affExpr = SQLITE_AFF_INTEGER;603 }else if( pExpr->iTable==0 ){604 testcase( iCol==31 );605 testcase( iCol==32 );606 pParse->oldmask |= (iCol>=32 ? 0xffffffff : (((u32)1)<<iCol));607 }else{608 testcase( iCol==31 );609 testcase( iCol==32 );610 pParse->newmask |= (iCol>=32 ? 0xffffffff : (((u32)1)<<iCol));611 }612#endif /* SQLITE_OMIT_TRIGGER */613 }614 }615 }616 }617 }618#endif /* !defined(SQLITE_OMIT_TRIGGER) || !defined(SQLITE_OMIT_UPSERT) */619 620 /*621 ** Perhaps the name is a reference to the ROWID622 */623 if( cnt==0624 && cntTab>=1625 && pMatch626 && (pNC->ncFlags & (NC_IdxExpr|NC_GenCol))==0627 && sqlite3IsRowid(zCol)628 && ALWAYS(VisibleRowid(pMatch->pSTab) || pMatch->fg.isNestedFrom)629 ){630 cnt = cntTab;631#if SQLITE_ALLOW_ROWID_IN_VIEW+0==2632 if( pMatch->pSTab!=0 && IsView(pMatch->pSTab) ){633 eNewExprOp = TK_NULL;634 }635#endif636 if( pMatch->fg.isNestedFrom==0 ) pExpr->iColumn = -1;637 pExpr->affExpr = SQLITE_AFF_INTEGER;638 }639 640 /*641 ** If the input is of the form Z (not Y.Z or X.Y.Z) then the name Z642 ** might refer to an result-set alias. This happens, for example, when643 ** we are resolving names in the WHERE clause of the following command:644 **645 ** SELECT a+b AS x FROM table WHERE x<10;646 **647 ** In cases like this, replace pExpr with a copy of the expression that648 ** forms the result set entry ("a+b" in the example) and return immediately.649 ** Note that the expression in the result set should have already been650 ** resolved by the time the WHERE clause is resolved.651 **652 ** The ability to use an output result-set column in the WHERE, GROUP BY,653 ** or HAVING clauses, or as part of a larger expression in the ORDER BY654 ** clause is not standard SQL. This is a (goofy) SQLite extension, that655 ** is supported for backwards compatibility only. Hence, we issue a warning656 ** on sqlite3_log() whenever the capability is used.657 */658 if( cnt==0659 && (pNC->ncFlags & NC_UEList)!=0660 && zTab==0661 ){662 pEList = pNC->uNC.pEList;663 assert( pEList!=0 );664 for(j=0; j<pEList->nExpr; j++){665 char *zAs = pEList->a[j].zEName;666 if( pEList->a[j].fg.eEName==ENAME_NAME667 && sqlite3_stricmp(zAs, zCol)==0668 ){669 Expr *pOrig;670 assert( pExpr->pLeft==0 && pExpr->pRight==0 );671 assert( ExprUseXList(pExpr)==0 || pExpr->x.pList==0 );672 assert( ExprUseXSelect(pExpr)==0 || pExpr->x.pSelect==0 );673 pOrig = pEList->a[j].pExpr;674 if( (pNC->ncFlags&NC_AllowAgg)==0 && ExprHasProperty(pOrig, EP_Agg) ){675 sqlite3ErrorMsg(pParse, "misuse of aliased aggregate %s", zAs);676 return WRC_Abort;677 }678 if( ExprHasProperty(pOrig, EP_Win)679 && ((pNC->ncFlags&NC_AllowWin)==0 || pNC!=pTopNC )680 ){681 sqlite3ErrorMsg(pParse, "misuse of aliased window function %s",zAs);682 return WRC_Abort;683 }684 if( sqlite3ExprVectorSize(pOrig)!=1 ){685 sqlite3ErrorMsg(pParse, "row value misused");686 return WRC_Abort;687 }688 resolveAlias(pParse, pEList, j, pExpr, nSubquery);689 cnt = 1;690 pMatch = 0;691 assert( zTab==0 && zDb==0 );692 if( IN_RENAME_OBJECT ){693 sqlite3RenameTokenRemap(pParse, 0, (void*)pExpr);694 }695 goto lookupname_end;696 }697 }698 }699 700 /* Advance to the next name context. The loop will exit when either701 ** we have a match (cnt>0) or when we run out of name contexts.702 */703 if( cnt ) break;704 pNC = pNC->pNext;705 nSubquery++;706 }while( pNC );707 708 709 /*710 ** If X and Y are NULL (in other words if only the column name Z is711 ** supplied) and the value of Z is enclosed in double-quotes, then712 ** Z is a string literal if it doesn't match any column names. In that713 ** case, we need to return right away and not make any changes to714 ** pExpr.715 **716 ** Because no reference was made to outer contexts, the pNC->nRef717 ** fields are not changed in any context.718 */719 if( cnt==0 && zTab==0 ){720 assert( pExpr->op==TK_ID );721 if( ExprHasProperty(pExpr,EP_DblQuoted)722 && areDoubleQuotedStringsEnabled(db, pTopNC)723 ){724 /* If a double-quoted identifier does not match any known column name,725 ** then treat it as a string.726 **727 ** This hack was added in the early days of SQLite in a misguided attempt728 ** to be compatible with MySQL 3.x, which used double-quotes for strings.729 ** I now sorely regret putting in this hack. The effect of this hack is730 ** that misspelled identifier names are silently converted into strings731 ** rather than causing an error, to the frustration of countless732 ** programmers. To all those frustrated programmers, my apologies.733 **734 ** Someday, I hope to get rid of this hack. Unfortunately there is735 ** a huge amount of legacy SQL that uses it. So for now, we just736 ** issue a warning.737 */738 sqlite3_log(SQLITE_WARNING,739 "double-quoted string literal: \"%w\"", zCol);740#ifdef SQLITE_ENABLE_NORMALIZE741 sqlite3VdbeAddDblquoteStr(db, pParse->pVdbe, zCol);742#endif743 pExpr->op = TK_STRING;744 memset(&pExpr->y, 0, sizeof(pExpr->y));745 return WRC_Prune;746 }747 if( sqlite3ExprIdToTrueFalse(pExpr) ){748 return WRC_Prune;749 }750 }751 752 /*753 ** cnt==0 means there was not match.754 ** cnt>1 means there were two or more matches.755 **756 ** cnt==0 is always an error. cnt>1 is often an error, but might757 ** be multiple matches for a NATURAL LEFT JOIN or a LEFT JOIN USING.758 */759 assert( pFJMatch==0 || cnt>0 );760 assert( !ExprHasProperty(pExpr, EP_xIsSelect|EP_IntValue) );761 if( cnt!=1 ){762 const char *zErr;763 if( pFJMatch ){764 if( pFJMatch->nExpr==cnt-1 ){765 if( ExprHasProperty(pExpr,EP_Leaf) ){766 ExprClearProperty(pExpr,EP_Leaf);767 }else{768 sqlite3ExprDelete(db, pExpr->pLeft);769 pExpr->pLeft = 0;770 sqlite3ExprDelete(db, pExpr->pRight);771 pExpr->pRight = 0;772 }773 extendFJMatch(pParse, &pFJMatch, pMatch, pExpr->iColumn);774 pExpr->op = TK_FUNCTION;775 pExpr->u.zToken = "coalesce";776 pExpr->x.pList = pFJMatch;777 cnt = 1;778 goto lookupname_end;779 }else{780 sqlite3ExprListDelete(db, pFJMatch);781 pFJMatch = 0;782 }783 }784 zErr = cnt==0 ? "no such column" : "ambiguous column name";785 if( zDb ){786 sqlite3ErrorMsg(pParse, "%s: %s.%s.%s", zErr, zDb, zTab, zCol);787 }else if( zTab ){788 sqlite3ErrorMsg(pParse, "%s: %s.%s", zErr, zTab, zCol);789 }else if( cnt==0 && ExprHasProperty(pRight,EP_DblQuoted) ){790 sqlite3ErrorMsg(pParse, "%s: \"%s\" - should this be a"791 " string literal in single-quotes?",792 zErr, zCol);793 }else{794 sqlite3ErrorMsg(pParse, "%s: %s", zErr, zCol);795 }796 sqlite3RecordErrorOffsetOfExpr(pParse->db, pExpr);797 pParse->checkSchema = 1;798 pTopNC->nNcErr++;799 eNewExprOp = TK_NULL;800 }801 assert( pFJMatch==0 );802 803 /* Remove all substructure from pExpr */804 if( !ExprHasProperty(pExpr,(EP_TokenOnly|EP_Leaf)) ){805 sqlite3ExprDelete(db, pExpr->pLeft);806 pExpr->pLeft = 0;807 sqlite3ExprDelete(db, pExpr->pRight);808 pExpr->pRight = 0;809 ExprSetProperty(pExpr, EP_Leaf);810 }811 812 /* If a column from a table in pSrcList is referenced, then record813 ** this fact in the pSrcList.a[].colUsed bitmask. Column 0 causes814 ** bit 0 to be set. Column 1 sets bit 1. And so forth. Bit 63 is815 ** set if the 63rd or any subsequent column is used.816 **817 ** The colUsed mask is an optimization used to help determine if an818 ** index is a covering index. The correct answer is still obtained819 ** if the mask contains extra set bits. However, it is important to820 ** avoid setting bits beyond the maximum column number of the table.821 ** (See ticket [b92e5e8ec2cdbaa1]).822 **823 ** If a generated column is referenced, set bits for every column824 ** of the table.825 */826 if( pMatch ){827 if( pExpr->iColumn>=0 ){828 pMatch->colUsed |= sqlite3ExprColUsed(pExpr);829 }else{830 pMatch->fg.rowidUsed = 1;831 }832 }833 834 pExpr->op = eNewExprOp;835lookupname_end:836 if( cnt==1 ){837 assert( pNC!=0 );838#ifndef SQLITE_OMIT_AUTHORIZATION839 if( pParse->db->xAuth840 && (pExpr->op==TK_COLUMN || pExpr->op==TK_TRIGGER)841 ){842 sqlite3AuthRead(pParse, pExpr, pSchema, pNC->pSrcList);843 }844#endif845 /* Increment the nRef value on all name contexts from TopNC up to846 ** the point where the name matched. */847 for(;;){848 assert( pTopNC!=0 );849 pTopNC->nRef++;850 if( pTopNC==pNC ) break;851 pTopNC = pTopNC->pNext;852 }853 return WRC_Prune;854 } else {855 return WRC_Abort;856 }857}858 859/*860** Allocate and return a pointer to an expression to load the column iCol861** from datasource iSrc in SrcList pSrc.862*/863Expr *sqlite3CreateColumnExpr(sqlite3 *db, SrcList *pSrc, int iSrc, int iCol){864 Expr *p = sqlite3ExprAlloc(db, TK_COLUMN, 0, 0);865 if( p ){866 SrcItem *pItem = &pSrc->a[iSrc];867 Table *pTab;868 assert( ExprUseYTab(p) );869 pTab = p->y.pTab = pItem->pSTab;870 p->iTable = pItem->iCursor;871 if( p->y.pTab->iPKey==iCol ){872 p->iColumn = -1;873 }else{874 p->iColumn = (ynVar)iCol;875 if( (pTab->tabFlags & TF_HasGenerated)!=0876 && (pTab->aCol[iCol].colFlags & COLFLAG_GENERATED)!=0877 ){878 testcase( pTab->nCol==63 );879 testcase( pTab->nCol==64 );880 pItem->colUsed = pTab->nCol>=64 ? ALLBITS : MASKBIT(pTab->nCol)-1;881 }else{882 testcase( iCol==BMS );883 testcase( iCol==BMS-1 );884 pItem->colUsed |= ((Bitmask)1)<<(iCol>=BMS ? BMS-1 : iCol);885 }886 }887 }888 return p;889}890 891/*892** Report an error that an expression is not valid for some set of893** pNC->ncFlags values determined by validMask.894**895** static void notValid(896** Parse *pParse, // Leave error message here897** NameContext *pNC, // The name context898** const char *zMsg, // Type of error899** int validMask, // Set of contexts for which prohibited900** Expr *pExpr // Invalidate this expression on error901** ){...}902**903** As an optimization, since the conditional is almost always false904** (because errors are rare), the conditional is moved outside of the905** function call using a macro.906*/907static void notValidImpl(908 Parse *pParse, /* Leave error message here */909 NameContext *pNC, /* The name context */910 const char *zMsg, /* Type of error */911 Expr *pExpr, /* Invalidate this expression on error */912 Expr *pError /* Associate error with this expression */913){914 const char *zIn = "partial index WHERE clauses";915 if( pNC->ncFlags & NC_IdxExpr ) zIn = "index expressions";916#ifndef SQLITE_OMIT_CHECK917 else if( pNC->ncFlags & NC_IsCheck ) zIn = "CHECK constraints";918#endif919#ifndef SQLITE_OMIT_GENERATED_COLUMNS920 else if( pNC->ncFlags & NC_GenCol ) zIn = "generated columns";921#endif922 sqlite3ErrorMsg(pParse, "%s prohibited in %s", zMsg, zIn);923 if( pExpr ) pExpr->op = TK_NULL;924 sqlite3RecordErrorOffsetOfExpr(pParse->db, pError);925}926#define sqlite3ResolveNotValid(P,N,M,X,E,R) \927 assert( ((X)&~(NC_IsCheck|NC_PartIdx|NC_IdxExpr|NC_GenCol))==0 ); \928 if( ((N)->ncFlags & (X))!=0 ) notValidImpl(P,N,M,E,R);929 930/*931** Expression p should encode a floating point value between 1.0 and 0.0.932** Return 134,217,728 (2^27) times this value. Or return -1 if p is not933** a floating point value between 1.0 and 0.0.934*/935static int exprProbability(Expr *p){936 double r = -1.0;937 if( p->op!=TK_FLOAT ) return -1;938 assert( !ExprHasProperty(p, EP_IntValue) );939 sqlite3AtoF(p->u.zToken, &r, sqlite3Strlen30(p->u.zToken), SQLITE_UTF8);940 assert( r>=0.0 );941 if( r>1.0 ) return -1;942 return (int)(r*134217728.0);943}944 945/*946** This routine is callback for sqlite3WalkExpr().947**948** Resolve symbolic names into TK_COLUMN operators for the current949** node in the expression tree. Return 0 to continue the search down950** the tree or 2 to abort the tree walk.951**952** This routine also does error checking and name resolution for953** function names. The operator for aggregate functions is changed954** to TK_AGG_FUNCTION.955*/956static int resolveExprStep(Walker *pWalker, Expr *pExpr){957 NameContext *pNC;958 Parse *pParse;959 960 pNC = pWalker->u.pNC;961 assert( pNC!=0 );962 pParse = pNC->pParse;963 assert( pParse==pWalker->pParse );964 965#ifndef NDEBUG966 if( pNC->pSrcList && pNC->pSrcList->nAlloc>0 ){967 SrcList *pSrcList = pNC->pSrcList;968 int i;969 for(i=0; i<pNC->pSrcList->nSrc; i++){970 assert( pSrcList->a[i].iCursor>=0 && pSrcList->a[i].iCursor<pParse->nTab);971 }972 }973#endif974 switch( pExpr->op ){975 976 /* The special operator TK_ROW means use the rowid for the first977 ** column in the FROM clause. This is used by the LIMIT and ORDER BY978 ** clause processing on UPDATE and DELETE statements, and by979 ** UPDATE ... FROM statement processing.980 */981 case TK_ROW: {982 SrcList *pSrcList = pNC->pSrcList;983 SrcItem *pItem;984 assert( pSrcList && pSrcList->nSrc>=1 );985 pItem = pSrcList->a;986 pExpr->op = TK_COLUMN;987 assert( ExprUseYTab(pExpr) );988 pExpr->y.pTab = pItem->pSTab;989 pExpr->iTable = pItem->iCursor;990 pExpr->iColumn--;991 pExpr->affExpr = SQLITE_AFF_INTEGER;992 break;993 }994 995 /* An optimization: Attempt to convert996 **997 ** "expr IS NOT NULL" --> "TRUE"998 ** "expr IS NULL" --> "FALSE"999 **1000 ** if we can prove that "expr" is never NULL. Call this the1001 ** "NOT NULL strength reduction optimization".1002 **1003 ** If this optimization occurs, also restore the NameContext ref-counts1004 ** to the state they where in before the "column" LHS expression was1005 ** resolved. This prevents "column" from being counted as having been1006 ** referenced, which might prevent a SELECT from being erroneously1007 ** marked as correlated.1008 **1009 ** 2024-03-28: Beware of aggregates. A bare column of aggregated table1010 ** can still evaluate to NULL even though it is marked as NOT NULL.1011 ** Example:1012 **1013 ** CREATE TABLE t1(a INT NOT NULL);1014 ** SELECT a, a IS NULL, a IS NOT NULL, count(*) FROM t1;1015 **1016 ** The "a IS NULL" and "a IS NOT NULL" expressions cannot be optimized1017 ** here because at the time this case is hit, we do not yet know whether1018 ** or not t1 is being aggregated. We have to assume the worst and omit1019 ** the optimization. The only time it is safe to apply this optimization1020 ** is within the WHERE clause.1021 */1022 case TK_NOTNULL:1023 case TK_ISNULL: {1024 int anRef[8];1025 NameContext *p;1026 int i;1027 for(i=0, p=pNC; p && i<ArraySize(anRef); p=p->pNext, i++){1028 anRef[i] = p->nRef;1029 }1030 sqlite3WalkExpr(pWalker, pExpr->pLeft);1031 if( IN_RENAME_OBJECT ) return WRC_Prune;1032 if( sqlite3ExprCanBeNull(pExpr->pLeft) ){1033 /* The expression can be NULL. So the optimization does not apply */1034 return WRC_Prune;1035 }1036 1037 for(i=0, p=pNC; p; p=p->pNext, i++){1038 if( (p->ncFlags & NC_Where)==0 ){1039 return WRC_Prune; /* Not in a WHERE clause. Unsafe to optimize. */1040 }1041 }1042 testcase( ExprHasProperty(pExpr, EP_OuterON) );1043 assert( !ExprHasProperty(pExpr, EP_IntValue) );1044#if TREETRACE_ENABLED1045 if( sqlite3TreeTrace & 0x80000 ){1046 sqlite3DebugPrintf(1047 "NOT NULL strength reduction converts the following to %d:\n",1048 pExpr->op==TK_NOTNULL1049 );1050 sqlite3ShowExpr(pExpr);1051 }1052#endif /* TREETRACE_ENABLED */1053 pExpr->u.iValue = (pExpr->op==TK_NOTNULL);1054 pExpr->flags |= EP_IntValue;1055 pExpr->op = TK_INTEGER;1056 for(i=0, p=pNC; p && i<ArraySize(anRef); p=p->pNext, i++){1057 p->nRef = anRef[i];1058 }1059 sqlite3ExprDelete(pParse->db, pExpr->pLeft);1060 pExpr->pLeft = 0;1061 return WRC_Prune;1062 }1063 1064 /* A column name: ID1065 ** Or table name and column name: ID.ID1066 ** Or a database, table and column: ID.ID.ID1067 **1068 ** The TK_ID and TK_OUT cases are combined so that there will only1069 ** be one call to lookupName(). Then the compiler will in-line1070 ** lookupName() for a size reduction and performance increase.1071 */1072 case TK_ID:1073 case TK_DOT: {1074 const char *zTable;1075 const char *zDb;1076 Expr *pRight;1077 1078 if( pExpr->op==TK_ID ){1079 zDb = 0;1080 zTable = 0;1081 assert( !ExprHasProperty(pExpr, EP_IntValue) );1082 pRight = pExpr;1083 }else{1084 Expr *pLeft = pExpr->pLeft;1085 testcase( pNC->ncFlags & NC_IdxExpr );1086 testcase( pNC->ncFlags & NC_GenCol );1087 sqlite3ResolveNotValid(pParse, pNC, "the \".\" operator",1088 NC_IdxExpr|NC_GenCol, 0, pExpr);1089 pRight = pExpr->pRight;1090 if( pRight->op==TK_ID ){1091 zDb = 0;1092 }else{1093 assert( pRight->op==TK_DOT );1094 assert( !ExprHasProperty(pRight, EP_IntValue) );1095 zDb = pLeft->u.zToken;1096 pLeft = pRight->pLeft;1097 pRight = pRight->pRight;1098 }1099 assert( ExprUseUToken(pLeft) && ExprUseUToken(pRight) );1100 zTable = pLeft->u.zToken;1101 assert( ExprUseYTab(pExpr) );1102 if( IN_RENAME_OBJECT ){1103 sqlite3RenameTokenRemap(pParse, (void*)pExpr, (void*)pRight);1104 sqlite3RenameTokenRemap(pParse, (void*)&pExpr->y.pTab, (void*)pLeft);1105 }1106 }1107 return lookupName(pParse, zDb, zTable, pRight, pNC, pExpr);1108 }1109 1110 /* Resolve function names1111 */1112 case TK_FUNCTION: {1113 ExprList *pList; /* The argument list */1114 int n; /* Number of arguments */1115 int no_such_func = 0; /* True if no such function exists */1116 int wrong_num_args = 0; /* True if wrong number of arguments */1117 int is_agg = 0; /* True if is an aggregate function */1118 const char *zId; /* The function name. */1119 FuncDef *pDef; /* Information about the function */1120 u8 enc = ENC(pParse->db); /* The database encoding */1121 int savedAllowFlags = (pNC->ncFlags & (NC_AllowAgg | NC_AllowWin));1122#ifndef SQLITE_OMIT_WINDOWFUNC1123 Window *pWin = (IsWindowFunc(pExpr) ? pExpr->y.pWin : 0);1124#endif1125 assert( !ExprHasProperty(pExpr, EP_xIsSelect|EP_IntValue) );1126 assert( pExpr->pLeft==0 || pExpr->pLeft->op==TK_ORDER );1127 pList = pExpr->x.pList;1128 n = pList ? pList->nExpr : 0;1129 zId = pExpr->u.zToken;1130 pDef = sqlite3FindFunction(pParse->db, zId, n, enc, 0);1131 if( pDef==0 ){1132 pDef = sqlite3FindFunction(pParse->db, zId, -2, enc, 0);1133 if( pDef==0 ){1134 no_such_func = 1;1135 }else{1136 wrong_num_args = 1;1137 }1138 }else{1139 is_agg = pDef->xFinalize!=0;1140 if( pDef->funcFlags & SQLITE_FUNC_UNLIKELY ){1141 ExprSetProperty(pExpr, EP_Unlikely);1142 if( n==2 ){1143 pExpr->iTable = exprProbability(pList->a[1].pExpr);1144 if( pExpr->iTable<0 ){1145 sqlite3ErrorMsg(pParse,1146 "second argument to %#T() must be a "1147 "constant between 0.0 and 1.0", pExpr);1148 pNC->nNcErr++;1149 }1150 }else{1151 /* EVIDENCE-OF: R-61304-29449 The unlikely(X) function is1152 ** equivalent to likelihood(X, 0.0625).1153 ** EVIDENCE-OF: R-01283-11636 The unlikely(X) function is1154 ** short-hand for likelihood(X,0.0625).1155 ** EVIDENCE-OF: R-36850-34127 The likely(X) function is short-hand1156 ** for likelihood(X,0.9375).1157 ** EVIDENCE-OF: R-53436-40973 The likely(X) function is equivalent1158 ** to likelihood(X,0.9375). */1159 /* TUNING: unlikely() probability is 0.0625. likely() is 0.9375 */1160 pExpr->iTable = pDef->zName[0]=='u' ? 8388608 : 125829120;1161 } 1162 }1163#ifndef SQLITE_OMIT_AUTHORIZATION1164 {1165 int auth = sqlite3AuthCheck(pParse, SQLITE_FUNCTION, 0,pDef->zName,0);1166 if( auth!=SQLITE_OK ){1167 if( auth==SQLITE_DENY ){1168 sqlite3ErrorMsg(pParse, "not authorized to use function: %#T",1169 pExpr);1170 pNC->nNcErr++;1171 }1172 pExpr->op = TK_NULL;1173 return WRC_Prune;1174 }1175 }1176#endif1177 1178 /* If the function may call sqlite3_value_subtype(), then set the 1179 ** EP_SubtArg flag on all of its argument expressions. This prevents1180 ** where.c from replacing the expression with a value read from an1181 ** index on the same expression, which will not have the correct 1182 ** subtype. Also set the flag if the function expression itself is1183 ** an EP_SubtArg expression. In this case subtypes are required as 1184 ** the function may return a value with a subtype back to its 1185 ** caller using sqlite3_result_value(). */1186 if( (pDef->funcFlags & SQLITE_SUBTYPE) 1187 || ExprHasProperty(pExpr, EP_SubtArg) 1188 ){1189 int ii;1190 for(ii=0; ii<n; ii++){1191 ExprSetProperty(pList->a[ii].pExpr, EP_SubtArg);1192 }1193 }1194 1195 if( pDef->funcFlags & (SQLITE_FUNC_CONSTANT|SQLITE_FUNC_SLOCHNG) ){1196 /* For the purposes of the EP_ConstFunc flag, date and time1197 ** functions and other functions that change slowly are considered1198 ** constant because they are constant for the duration of one query.1199 ** This allows them to be factored out of inner loops. */1200 ExprSetProperty(pExpr,EP_ConstFunc);