diff options
author | drh <> | 2025-07-02 02:03:43 +0000 |
---|---|---|
committer | drh <> | 2025-07-02 02:03:43 +0000 |
commit | caf047365280f9e92e2bca250d79aeb20e78fb7e (patch) | |
tree | 8b64a06a8457f28ab80156d1f0aec0059d474643 /src | |
parent | 2427ce16d9a340ff9790f62db1371f5ef83f8afe (diff) | |
download | sqlite-caf047365280f9e92e2bca250d79aeb20e78fb7e.tar.gz sqlite-caf047365280f9e92e2bca250d79aeb20e78fb7e.zip |
Ensure that Expr.op2 values for TK_AGG_FUNCTION nodes are adjusted when
query flattening.
FossilOrigin-Name: d27d34fb746280e7e81335db4e195914b15403ef0da7b2955550553dd78fbe9a
Diffstat (limited to 'src')
-rw-r--r-- | src/select.c | 7 |
1 files changed, 7 insertions, 0 deletions
diff --git a/src/select.c b/src/select.c index 2723d069b..95b2925e3 100644 --- a/src/select.c +++ b/src/select.c @@ -3872,6 +3872,7 @@ typedef struct SubstContext { int iTable; /* Replace references to this table */ int iNewTable; /* New table number */ int isOuterJoin; /* Add TK_IF_NULL_ROW opcodes on each replacement */ + int nSelDepth; /* Depth of sub-query recursion. Top==1 */ ExprList *pEList; /* Replacement expressions */ ExprList *pCList; /* Collation sequences for replacement expr */ } SubstContext; @@ -3979,6 +3980,9 @@ static Expr *substExpr( if( pExpr->op==TK_IF_NULL_ROW && pExpr->iTable==pSubst->iTable ){ pExpr->iTable = pSubst->iNewTable; } + if( pExpr->op==TK_AGG_FUNCTION && pExpr->op2>=pSubst->nSelDepth ){ + pExpr->op2--; + } pExpr->pLeft = substExpr(pSubst, pExpr->pLeft); pExpr->pRight = substExpr(pSubst, pExpr->pRight); if( ExprUseXSelect(pExpr) ){ @@ -4016,6 +4020,7 @@ static void substSelect( SrcItem *pItem; int i; if( !p ) return; + pSubst->nSelDepth++; do{ substExprList(pSubst, p->pEList); substExprList(pSubst, p->pGroupBy); @@ -4033,6 +4038,7 @@ static void substSelect( } } }while( doPrior && (p = p->pPrior)!=0 ); + pSubst->nSelDepth--; } #endif /* !defined(SQLITE_OMIT_SUBQUERY) || !defined(SQLITE_OMIT_VIEW) */ @@ -4774,6 +4780,7 @@ static int flattenSubquery( x.iTable = iParent; x.iNewTable = iNewParent; x.isOuterJoin = isOuterJoin; + x.nSelDepth = 0; x.pEList = pSub->pEList; x.pCList = findLeftmostExprlist(pSub); substSelect(&x, pParent, 0); |