@@ -52,7 +52,12 @@ char sqlite3ExprAffinity(const Expr *pExpr){
5252 op = pExpr -> op ;
5353 if ( op == TK_SELECT ){
5454 assert ( pExpr -> flags & EP_xIsSelect );
55- return sqlite3ExprAffinity (pExpr -> x .pSelect -> pEList -> a [0 ].pExpr );
55+ if ( ALWAYS (pExpr -> x .pSelect )
56+ && pExpr -> x .pSelect -> pEList
57+ && ALWAYS (pExpr -> x .pSelect -> pEList -> a [0 ].pExpr )
58+ ){
59+ return sqlite3ExprAffinity (pExpr -> x .pSelect -> pEList -> a [0 ].pExpr );
60+ }
5661 }
5762 if ( op == TK_REGISTER ) op = pExpr -> op2 ;
5863#ifndef SQLITE_OMIT_CAST
@@ -2043,8 +2048,10 @@ static int exprNodeIsConstant(Walker *pWalker, Expr *pExpr){
20432048 /* Fall through */
20442049 case TK_IF_NULL_ROW :
20452050 case TK_REGISTER :
2051+ case TK_DOT :
20462052 testcase ( pExpr -> op == TK_REGISTER );
20472053 testcase ( pExpr -> op == TK_IF_NULL_ROW );
2054+ testcase ( pExpr -> op == TK_DOT );
20482055 pWalker -> eCode = 0 ;
20492056 return WRC_Abort ;
20502057 case TK_VARIABLE :
@@ -5650,10 +5657,25 @@ int sqlite3ExprCoveredByIndex(
56505657*/
56515658struct SrcCount {
56525659 SrcList * pSrc ; /* One particular FROM clause in a nested query */
5660+ int iSrcInner ; /* Smallest cursor number in this context */
56535661 int nThis ; /* Number of references to columns in pSrcList */
56545662 int nOther ; /* Number of references to columns in other FROM clauses */
56555663};
56565664
5665+ /*
5666+ ** xSelect callback for sqlite3FunctionUsesThisSrc(). If this is the first
5667+ ** SELECT with a FROM clause encountered during this iteration, set
5668+ ** SrcCount.iSrcInner to the cursor number of the leftmost object in
5669+ ** the FROM cause.
5670+ */
5671+ static int selectSrcCount (Walker * pWalker , Select * pSel ){
5672+ struct SrcCount * p = pWalker -> u .pSrcCount ;
5673+ if ( p -> iSrcInner == 0x7FFFFFFF && ALWAYS (pSel -> pSrc ) && pSel -> pSrc -> nSrc ){
5674+ pWalker -> u .pSrcCount -> iSrcInner = pSel -> pSrc -> a [0 ].iCursor ;
5675+ }
5676+ return WRC_Continue ;
5677+ }
5678+
56575679/*
56585680** Count the number of references to columns.
56595681*/
@@ -5674,7 +5696,7 @@ static int exprSrcCount(Walker *pWalker, Expr *pExpr){
56745696 }
56755697 if ( i < nSrc ){
56765698 p -> nThis ++ ;
5677- }else if ( nSrc == 0 || pExpr -> iTable < pSrc -> a [ 0 ]. iCursor ){
5699+ }else if ( pExpr -> iTable < p -> iSrcInner ){
56785700 /* In a well-formed parse tree (no name resolution errors),
56795701 ** TK_COLUMN nodes with smaller Expr.iTable values are in an
56805702 ** outer context. Those are the only ones to count as "other" */
@@ -5696,9 +5718,10 @@ int sqlite3FunctionUsesThisSrc(Expr *pExpr, SrcList *pSrcList){
56965718 assert ( pExpr -> op == TK_AGG_FUNCTION );
56975719 memset (& w , 0 , sizeof (w ));
56985720 w .xExprCallback = exprSrcCount ;
5699- w .xSelectCallback = sqlite3SelectWalkNoop ;
5721+ w .xSelectCallback = selectSrcCount ;
57005722 w .u .pSrcCount = & cnt ;
57015723 cnt .pSrc = pSrcList ;
5724+ cnt .iSrcInner = (pSrcList && pSrcList -> nSrc )?pSrcList -> a [0 ].iCursor :0x7FFFFFFF ;
57025725 cnt .nThis = 0 ;
57035726 cnt .nOther = 0 ;
57045727 sqlite3WalkExprList (& w , pExpr -> x .pList );
@@ -5710,6 +5733,64 @@ int sqlite3FunctionUsesThisSrc(Expr *pExpr, SrcList *pSrcList){
57105733 return cnt .nThis > 0 || cnt .nOther == 0 ;
57115734}
57125735
5736+ /*
5737+ ** This is a Walker expression node callback.
5738+ **
5739+ ** For Expr nodes that contain pAggInfo pointers, make sure the AggInfo
5740+ ** object that is referenced does not refer directly to the Expr. If
5741+ ** it does, make a copy. This is done because the pExpr argument is
5742+ ** subject to change.
5743+ **
5744+ ** The copy is stored on pParse->pConstExpr with a register number of 0.
5745+ ** This will cause the expression to be deleted automatically when the
5746+ ** Parse object is destroyed, but the zero register number means that it
5747+ ** will not generate any code in the preamble.
5748+ */
5749+ static int agginfoPersistExprCb (Walker * pWalker , Expr * pExpr ){
5750+ if ( ALWAYS (!ExprHasProperty (pExpr , EP_TokenOnly |EP_Reduced ))
5751+ && pExpr -> pAggInfo != 0
5752+ ){
5753+ AggInfo * pAggInfo = pExpr -> pAggInfo ;
5754+ int iAgg = pExpr -> iAgg ;
5755+ Parse * pParse = pWalker -> pParse ;
5756+ sqlite3 * db = pParse -> db ;
5757+ assert ( pExpr -> op == TK_AGG_COLUMN || pExpr -> op == TK_AGG_FUNCTION );
5758+ if ( pExpr -> op == TK_AGG_COLUMN ){
5759+ assert ( iAgg >=0 && iAgg < pAggInfo -> nColumn );
5760+ if ( pAggInfo -> aCol [iAgg ].pExpr == pExpr ){
5761+ pExpr = sqlite3ExprDup (db , pExpr , 0 );
5762+ if ( pExpr ){
5763+ pAggInfo -> aCol [iAgg ].pExpr = pExpr ;
5764+ pParse -> pConstExpr =
5765+ sqlite3ExprListAppend (pParse , pParse -> pConstExpr , pExpr );
5766+ }
5767+ }
5768+ }else {
5769+ assert ( iAgg >=0 && iAgg < pAggInfo -> nFunc );
5770+ if ( pAggInfo -> aFunc [iAgg ].pExpr == pExpr ){
5771+ pExpr = sqlite3ExprDup (db , pExpr , 0 );
5772+ if ( pExpr ){
5773+ pAggInfo -> aFunc [iAgg ].pExpr = pExpr ;
5774+ pParse -> pConstExpr =
5775+ sqlite3ExprListAppend (pParse , pParse -> pConstExpr , pExpr );
5776+ }
5777+ }
5778+ }
5779+ }
5780+ return WRC_Continue ;
5781+ }
5782+
5783+ /*
5784+ ** Initialize a Walker object so that will persist AggInfo entries referenced
5785+ ** by the tree that is walked.
5786+ */
5787+ void sqlite3AggInfoPersistWalkerInit (Walker * pWalker , Parse * pParse ){
5788+ memset (pWalker , 0 , sizeof (* pWalker ));
5789+ pWalker -> pParse = pParse ;
5790+ pWalker -> xExprCallback = agginfoPersistExprCb ;
5791+ pWalker -> xSelectCallback = sqlite3SelectWalkNoop ;
5792+ }
5793+
57135794/*
57145795** Add a new element to the pAggInfo->aCol[] array. Return the index of
57155796** the new element. Return a negative number if malloc fails.
@@ -5740,7 +5821,7 @@ static int addAggInfoFunc(sqlite3 *db, AggInfo *pInfo){
57405821 & i
57415822 );
57425823 return i ;
5743- }
5824+ }
57445825
57455826/*
57465827** This is the xExprCallback for a tree walker. It is used to
0 commit comments