diff -Npur sqlite-version-3.32.2/src/expr.c sqlite-version-3.32.2-patched/src/expr.c --- sqlite-version-3.32.2/src/expr.c 2020-06-04 20:58:43.000000000 +0800 +++ sqlite-version-3.32.2-patched/src/expr.c 2020-06-15 16:03:29.343573250 +0800 @@ -3813,6 +3813,7 @@ expr_code_doover: AggInfo *pAggInfo = pExpr->pAggInfo; struct AggInfo_col *pCol; assert( pAggInfo!=0 ); + assert( AggInfoValid(pAggInfo) ); assert( pExpr->iAgg>=0 && pExpr->iAggnColumn ); pCol = &pAggInfo->aCol[pExpr->iAgg]; if( !pAggInfo->directMode ){ @@ -4121,6 +4122,7 @@ expr_code_doover: assert( !ExprHasProperty(pExpr, EP_IntValue) ); sqlite3ErrorMsg(pParse, "misuse of aggregate: %s()", pExpr->u.zToken); }else{ + assert( AggInfoValid(pInfo) ); return pInfo->aFunc[pExpr->iAgg].iMem; } break; @@ -5658,13 +5660,7 @@ struct SrcCount { ** Count the number of references to columns. */ static int exprSrcCount(Walker *pWalker, Expr *pExpr){ - /* There was once a NEVER() on the second term on the grounds that - ** sqlite3FunctionUsesThisSrc() was always called before - ** sqlite3ExprAnalyzeAggregates() and so the TK_COLUMNs have not yet - ** been converted into TK_AGG_COLUMN. But this is no longer true due - ** to window functions - sqlite3WindowRewrite() may now indirectly call - ** FunctionUsesThisSrc() when creating a new sub-select. */ - if( pExpr->op==TK_COLUMN || pExpr->op==TK_AGG_COLUMN ){ + if( pExpr->op==TK_COLUMN || NEVER(pExpr->op==TK_AGG_COLUMN) ){ int i; struct SrcCount *p = pWalker->u.pSrcCount; SrcList *pSrc = p->pSrc; diff -Npur sqlite-version-3.32.2/src/global.c sqlite-version-3.32.2-patched/src/global.c --- sqlite-version-3.32.2/src/global.c 2020-06-04 20:58:43.000000000 +0800 +++ sqlite-version-3.32.2-patched/src/global.c 2020-06-15 16:03:29.343573250 +0800 @@ -300,6 +300,11 @@ sqlite3_uint64 sqlite3NProfileCnt = 0; int sqlite3PendingByte = 0x40000000; #endif +/* +** Flags for select tracing and the ".selecttrace" macro of the CLI +*/ +/**/ u32 sqlite3SelectTrace = 0; + #include "opcodes.h" /* ** Properties of opcodes. The OPFLG_INITIALIZER macro is diff -Npur sqlite-version-3.32.2/src/resolve.c sqlite-version-3.32.2-patched/src/resolve.c --- sqlite-version-3.32.2/src/resolve.c 2020-06-04 20:58:43.000000000 +0800 +++ sqlite-version-3.32.2-patched/src/resolve.c 2020-06-15 16:03:29.343573250 +0800 @@ -1715,6 +1715,14 @@ static int resolveSelectStep(Walker *pWa return WRC_Abort; } } + }else if( p->pWin && ALWAYS( (p->selFlags & SF_WinRewrite)==0 ) ){ + sqlite3WindowRewrite(pParse, p); +#if SELECTTRACE_ENABLED + if( (sqlite3SelectTrace & 0x108)!=0 ){ + SELECTTRACE(0x104,pParse,p, ("after window rewrite:\n")); + sqlite3TreeViewSelect(0, p, 0); + } +#endif } #endif diff -Npur sqlite-version-3.32.2/src/select.c sqlite-version-3.32.2-patched/src/select.c --- sqlite-version-3.32.2/src/select.c 2020-06-04 20:58:43.000000000 +0800 +++ sqlite-version-3.32.2-patched/src/select.c 2020-06-15 16:03:29.343573250 +0800 @@ -15,20 +15,6 @@ #include "sqliteInt.h" /* -** Trace output macros -*/ -#if SELECTTRACE_ENABLED -/***/ int sqlite3SelectTrace = 0; -# define SELECTTRACE(K,P,S,X) \ - if(sqlite3SelectTrace&(K)) \ - sqlite3DebugPrintf("%u/%d/%p: ",(S)->selId,(P)->addrExplain,(S)),\ - sqlite3DebugPrintf X -#else -# define SELECTTRACE(K,P,S,X) -#endif - - -/* ** An instance of the following object is used to record information about ** how to process the DISTINCT keyword, to simplify passing that information ** into the selectInnerLoop() routine. @@ -4426,11 +4412,14 @@ static int pushDownWhereTerms( ){ Expr *pNew; int nChng = 0; + Select *pSel; if( pWhere==0 ) return 0; if( pSubq->selFlags & SF_Recursive ) return 0; /* restriction (2) */ #ifndef SQLITE_OMIT_WINDOWFUNC - if( pSubq->pWin ) return 0; /* restriction (6) */ + for(pSel=pSubq; pSel; pSel=pSel->pPrior){ + if( pSel->pWin ) return 0; /* restriction (6) */ + } #endif #ifdef SQLITE_DEBUG @@ -5766,6 +5755,9 @@ int sqlite3Select( } if( sqlite3AuthCheck(pParse, SQLITE_SELECT, 0, 0, 0) ) return 1; memset(&sAggInfo, 0, sizeof(sAggInfo)); +#ifdef SQLITE_DEBUG + sAggInfo.iAggMagic = SQLITE_AGGMAGIC_VALID; +#endif #if SELECTTRACE_ENABLED SELECTTRACE(1,pParse,p, ("begin processing:\n", pParse->addrExplain)); if( sqlite3SelectTrace & 0x100 ){ @@ -5804,19 +5796,6 @@ int sqlite3Select( generateColumnNames(pParse, p); } -#ifndef SQLITE_OMIT_WINDOWFUNC - rc = sqlite3WindowRewrite(pParse, p); - if( rc ){ - assert( db->mallocFailed || pParse->nErr>0 ); - goto select_end; - } -#if SELECTTRACE_ENABLED - if( p->pWin && (sqlite3SelectTrace & 0x108)!=0 ){ - SELECTTRACE(0x104,pParse,p, ("after window rewrite:\n")); - sqlite3TreeViewSelect(0, p, 0); - } -#endif -#endif /* SQLITE_OMIT_WINDOWFUNC */ pTabList = p->pSrc; isAgg = (p->selFlags & SF_Aggregate)!=0; memset(&sSort, 0, sizeof(sSort)); @@ -6144,7 +6123,7 @@ int sqlite3Select( if( (p->selFlags & (SF_Distinct|SF_Aggregate))==SF_Distinct && sqlite3ExprListCompare(sSort.pOrderBy, pEList, -1)==0 #ifndef SQLITE_OMIT_WINDOWFUNC - && p->pWin==0 + && ALWAYS(p->pWin==0) #endif ){ p->selFlags &= ~SF_Distinct; @@ -6791,6 +6770,14 @@ int sqlite3Select( select_end: sqlite3ExprListDelete(db, pMinMaxOrderBy); sqlite3DbFree(db, sAggInfo.aCol); +#ifdef SQLITE_DEBUG + for(i=0; ipAggInfo==&sAggInfo ); + sAggInfo.aFunc[i].pExpr->pAggInfo = 0; + } + sAggInfo.iAggMagic = 0; +#endif sqlite3DbFree(db, sAggInfo.aFunc); #if SELECTTRACE_ENABLED SELECTTRACE(0x1,pParse,p,("end processing\n")); diff -Npur sqlite-version-3.32.2/src/sqliteInt.h sqlite-version-3.32.2-patched/src/sqliteInt.h --- sqlite-version-3.32.2/src/sqliteInt.h 2020-06-04 20:58:43.000000000 +0800 +++ sqlite-version-3.32.2-patched/src/sqliteInt.h 2020-06-15 16:03:29.347573247 +0800 @@ -976,7 +976,12 @@ typedef INT16_TYPE LogEst; */ #if defined(SQLITE_ENABLE_SELECTTRACE) # define SELECTTRACE_ENABLED 1 +# define SELECTTRACE(K,P,S,X) \ + if(sqlite3SelectTrace&(K)) \ + sqlite3DebugPrintf("%u/%d/%p: ",(S)->selId,(P)->addrExplain,(S)),\ + sqlite3DebugPrintf X #else +# define SELECTTRACE(K,P,S,X) # define SELECTTRACE_ENABLED 0 #endif @@ -2523,9 +2528,24 @@ struct AggInfo { int iDistinct; /* Ephemeral table used to enforce DISTINCT */ } *aFunc; int nFunc; /* Number of entries in aFunc[] */ +#ifdef SQLITE_DEBUG + u32 iAggMagic; /* Sanity checking constant */ +#endif }; /* +** Allowed values for AggInfo.iAggMagic +*/ +#define SQLITE_AGGMAGIC_VALID 0x05cadade + +/* +** True if the AggInfo object is valid. Used inside of assert() only. +*/ +#ifdef SQLITE_DEBUG +# define AggInfoValid(P) ((P)->iAggMagic==SQLITE_AGGMAGIC_VALID) +#endif + +/* ** The datatype ynVar is a signed integer, either 16-bit or 32-bit. ** Usually it is 16-bits. But if SQLITE_MAX_VARIABLE_NUMBER is greater ** than 32767 we have to make it 32-bit. 16-bit is preferred because @@ -4546,10 +4566,11 @@ extern const unsigned char sqlite3UpperT extern const unsigned char sqlite3CtypeMap[]; extern SQLITE_WSD struct Sqlite3Config sqlite3Config; extern FuncDefHash sqlite3BuiltinFunctions; +extern u32 sqlite3SelectTrace; #ifndef SQLITE_OMIT_WSD extern int sqlite3PendingByte; #endif -#endif +#endif /* !defined(SQLITE_AMALGAMATION) */ #ifdef VDBE_PROFILE extern sqlite3_uint64 sqlite3NProfileCnt; #endif diff -Npur sqlite-version-3.32.2/src/test1.c sqlite-version-3.32.2-patched/src/test1.c --- sqlite-version-3.32.2/src/test1.c 2020-06-04 20:58:43.000000000 +0800 +++ sqlite-version-3.32.2-patched/src/test1.c 2020-06-15 16:03:29.347573247 +0800 @@ -8164,7 +8164,7 @@ int Sqlitetest1_Init(Tcl_Interp *interp) #endif #endif #if defined(SQLITE_ENABLE_SELECTTRACE) - extern int sqlite3SelectTrace; + extern u32 sqlite3SelectTrace; #endif for(i=0; ipWin && p->pPrior==0 && (p->selFlags & SF_WinRewrite)==0 ){ + if( ALWAYS(p->pWin && (p->selFlags & SF_WinRewrite)==0) ){ Vdbe *v = sqlite3GetVdbe(pParse); sqlite3 *db = pParse->db; Select *pSub = 0; /* The subquery */ diff -Npur sqlite-version-3.32.2/test/window1.test sqlite-version-3.32.2-patched/test/window1.test --- sqlite-version-3.32.2/test/window1.test 2020-06-04 20:58:43.000000000 +0800 +++ sqlite-version-3.32.2-patched/test/window1.test 2020-06-15 16:03:29.347573247 +0800 @@ -1743,5 +1743,47 @@ do_execsql_test 53.0 { WHERE a.c); } {4 4 4 4} +#------------------------------------------------------------------------- +reset_db +do_execsql_test 54.1 { + CREATE TABLE t1(a VARCHAR(20), b FLOAT); + INSERT INTO t1 VALUES('1',10.0); +} + +do_execsql_test 54.2 { + SELECT * FROM ( + SELECT sum(b) OVER() AS c FROM t1 + UNION + SELECT b AS c FROM t1 + ) WHERE c>10; +} + +do_execsql_test 54.3 { + INSERT INTO t1 VALUES('2',5.0); + INSERT INTO t1 VALUES('3',15.0); +} + +do_execsql_test 54.4 { + SELECT * FROM ( + SELECT sum(b) OVER() AS c FROM t1 + UNION + SELECT b AS c FROM t1 + ) WHERE c>10; +} {15.0 30.0} + +# 2020-06-05 ticket c8d3b9f0a750a529 +reset_db +do_execsql_test 55.1 { + CREATE TABLE a(b); + SELECT + (SELECT b FROM a + GROUP BY b + HAVING (SELECT COUNT()OVER() + lead(b)OVER(ORDER BY SUM(DISTINCT b) + b)) + ) + FROM a + UNION + SELECT 99 + ORDER BY 1; +} {99} finish_test