1diff -Npur sqlite-version-3.32.2-new/src/expr.c sqlite-version-3.32.2/src/expr.c 2--- sqlite-version-3.32.2-new/src/expr.c 2020-06-04 08:58:43.000000000 -0400 3+++ sqlite-version-3.32.2/src/expr.c 2021-08-04 11:57:45.029230992 -0400 4@@ -3813,6 +3813,7 @@ expr_code_doover: 5 AggInfo *pAggInfo = pExpr->pAggInfo; 6 struct AggInfo_col *pCol; 7 assert( pAggInfo!=0 ); 8+ assert( AggInfoValid(pAggInfo) ); 9 assert( pExpr->iAgg>=0 && pExpr->iAgg<pAggInfo->nColumn ); 10 pCol = &pAggInfo->aCol[pExpr->iAgg]; 11 if( !pAggInfo->directMode ){ 12@@ -4121,6 +4122,7 @@ expr_code_doover: 13 assert( !ExprHasProperty(pExpr, EP_IntValue) ); 14 sqlite3ErrorMsg(pParse, "misuse of aggregate: %s()", pExpr->u.zToken); 15 }else{ 16+ assert( AggInfoValid(pInfo) ); 17 return pInfo->aFunc[pExpr->iAgg].iMem; 18 } 19 break; 20@@ -5658,13 +5660,7 @@ struct SrcCount { 21 ** Count the number of references to columns. 22 */ 23 static int exprSrcCount(Walker *pWalker, Expr *pExpr){ 24- /* There was once a NEVER() on the second term on the grounds that 25- ** sqlite3FunctionUsesThisSrc() was always called before 26- ** sqlite3ExprAnalyzeAggregates() and so the TK_COLUMNs have not yet 27- ** been converted into TK_AGG_COLUMN. But this is no longer true due 28- ** to window functions - sqlite3WindowRewrite() may now indirectly call 29- ** FunctionUsesThisSrc() when creating a new sub-select. */ 30- if( pExpr->op==TK_COLUMN || pExpr->op==TK_AGG_COLUMN ){ 31+ if( pExpr->op==TK_COLUMN || NEVER(pExpr->op==TK_AGG_COLUMN) ){ 32 int i; 33 struct SrcCount *p = pWalker->u.pSrcCount; 34 SrcList *pSrc = p->pSrc; 35diff -Npur sqlite-version-3.32.2-new/src/global.c sqlite-version-3.32.2/src/global.c 36--- sqlite-version-3.32.2-new/src/global.c 2020-06-04 08:58:43.000000000 -0400 37+++ sqlite-version-3.32.2/src/global.c 2021-08-04 11:57:45.033230992 -0400 38@@ -300,6 +300,11 @@ sqlite3_uint64 sqlite3NProfileCnt = 0; 39 int sqlite3PendingByte = 0x40000000; 40 #endif 41 42+/* 43+** Flags for select tracing and the ".selecttrace" macro of the CLI 44+*/ 45+/**/ u32 sqlite3SelectTrace = 0; 46+ 47 #include "opcodes.h" 48 /* 49 ** Properties of opcodes. The OPFLG_INITIALIZER macro is 50diff -Npur sqlite-version-3.32.2-new/src/resolve.c sqlite-version-3.32.2/src/resolve.c 51--- sqlite-version-3.32.2-new/src/resolve.c 2020-06-04 08:58:43.000000000 -0400 52+++ sqlite-version-3.32.2/src/resolve.c 2021-08-04 11:57:45.033230992 -0400 53@@ -1715,6 +1715,14 @@ static int resolveSelectStep(Walker *pWa 54 return WRC_Abort; 55 } 56 } 57+ }else if( p->pWin && ALWAYS( (p->selFlags & SF_WinRewrite)==0 ) ){ 58+ sqlite3WindowRewrite(pParse, p); 59+#if SELECTTRACE_ENABLED 60+ if( (sqlite3SelectTrace & 0x108)!=0 ){ 61+ SELECTTRACE(0x104,pParse,p, ("after window rewrite:\n")); 62+ sqlite3TreeViewSelect(0, p, 0); 63+ } 64+#endif 65 } 66 #endif 67 68diff -Npur sqlite-version-3.32.2-new/src/select.c sqlite-version-3.32.2/src/select.c 69--- sqlite-version-3.32.2-new/src/select.c 2020-06-04 08:58:43.000000000 -0400 70+++ sqlite-version-3.32.2/src/select.c 2021-08-04 12:27:34.737267443 -0400 71@@ -15,20 +15,6 @@ 72 #include "sqliteInt.h" 73 74 /* 75-** Trace output macros 76-*/ 77-#if SELECTTRACE_ENABLED 78-/***/ int sqlite3SelectTrace = 0; 79-# define SELECTTRACE(K,P,S,X) \ 80- if(sqlite3SelectTrace&(K)) \ 81- sqlite3DebugPrintf("%u/%d/%p: ",(S)->selId,(P)->addrExplain,(S)),\ 82- sqlite3DebugPrintf X 83-#else 84-# define SELECTTRACE(K,P,S,X) 85-#endif 86- 87- 88-/* 89 ** An instance of the following object is used to record information about 90 ** how to process the DISTINCT keyword, to simplify passing that information 91 ** into the selectInnerLoop() routine. 92@@ -2717,9 +2703,7 @@ static int multiSelect( 93 selectOpName(p->op))); 94 rc = sqlite3Select(pParse, p, &uniondest); 95 testcase( rc!=SQLITE_OK ); 96- /* Query flattening in sqlite3Select() might refill p->pOrderBy. 97- ** Be sure to delete p->pOrderBy, therefore, to avoid a memory leak. */ 98- sqlite3ExprListDelete(db, p->pOrderBy); 99+ assert( p->pOrderBy==0 ); 100 pDelete = p->pPrior; 101 p->pPrior = pPrior; 102 p->pOrderBy = 0; 103@@ -4105,7 +4089,7 @@ static int flattenSubquery( 104 ** We look at every expression in the outer query and every place we see 105 ** "a" we substitute "x*3" and every place we see "b" we substitute "y+10". 106 */ 107- if( pSub->pOrderBy ){ 108+ if( pSub->pOrderBy && (pParent->selFlags & SF_NoopOrderBy)==0 ){ 109 /* At this point, any non-zero iOrderByCol values indicate that the 110 ** ORDER BY column expression is identical to the iOrderByCol'th 111 ** expression returned by SELECT statement pSub. Since these values 112@@ -4426,11 +4410,14 @@ static int pushDownWhereTerms( 113 ){ 114 Expr *pNew; 115 int nChng = 0; 116+ Select *pSel; 117 if( pWhere==0 ) return 0; 118 if( pSubq->selFlags & SF_Recursive ) return 0; /* restriction (2) */ 119 120 #ifndef SQLITE_OMIT_WINDOWFUNC 121- if( pSubq->pWin ) return 0; /* restriction (6) */ 122+ for(pSel=pSubq; pSel; pSel=pSel->pPrior){ 123+ if( pSel->pWin ) return 0; /* restriction (6) */ 124+ } 125 #endif 126 127 #ifdef SQLITE_DEBUG 128@@ -5553,7 +5540,9 @@ static void explainSimpleCount( 129 static int havingToWhereExprCb(Walker *pWalker, Expr *pExpr){ 130 if( pExpr->op!=TK_AND ){ 131 Select *pS = pWalker->u.pSelect; 132- if( sqlite3ExprIsConstantOrGroupBy(pWalker->pParse, pExpr, pS->pGroupBy) ){ 133+ if( sqlite3ExprIsConstantOrGroupBy(pWalker->pParse, pExpr, pS->pGroupBy) 134+ && ExprAlwaysFalse(pExpr)==0 135+ ){ 136 sqlite3 *db = pWalker->pParse->db; 137 Expr *pNew = sqlite3Expr(db, TK_INTEGER, "1"); 138 if( pNew ){ 139@@ -5766,6 +5755,9 @@ int sqlite3Select( 140 } 141 if( sqlite3AuthCheck(pParse, SQLITE_SELECT, 0, 0, 0) ) return 1; 142 memset(&sAggInfo, 0, sizeof(sAggInfo)); 143+#ifdef SQLITE_DEBUG 144+ sAggInfo.iAggMagic = SQLITE_AGGMAGIC_VALID; 145+#endif 146 #if SELECTTRACE_ENABLED 147 SELECTTRACE(1,pParse,p, ("begin processing:\n", pParse->addrExplain)); 148 if( sqlite3SelectTrace & 0x100 ){ 149@@ -5787,6 +5779,7 @@ int sqlite3Select( 150 sqlite3ExprListDelete(db, p->pOrderBy); 151 p->pOrderBy = 0; 152 p->selFlags &= ~SF_Distinct; 153+ p->selFlags |= SF_NoopOrderBy; 154 } 155 sqlite3SelectPrep(pParse, p, 0); 156 if( pParse->nErr || db->mallocFailed ){ 157@@ -5804,19 +5797,6 @@ int sqlite3Select( 158 generateColumnNames(pParse, p); 159 } 160 161-#ifndef SQLITE_OMIT_WINDOWFUNC 162- rc = sqlite3WindowRewrite(pParse, p); 163- if( rc ){ 164- assert( db->mallocFailed || pParse->nErr>0 ); 165- goto select_end; 166- } 167-#if SELECTTRACE_ENABLED 168- if( p->pWin && (sqlite3SelectTrace & 0x108)!=0 ){ 169- SELECTTRACE(0x104,pParse,p, ("after window rewrite:\n")); 170- sqlite3TreeViewSelect(0, p, 0); 171- } 172-#endif 173-#endif /* SQLITE_OMIT_WINDOWFUNC */ 174 pTabList = p->pSrc; 175 isAgg = (p->selFlags & SF_Aggregate)!=0; 176 memset(&sSort, 0, sizeof(sSort)); 177@@ -6144,7 +6124,7 @@ int sqlite3Select( 178 if( (p->selFlags & (SF_Distinct|SF_Aggregate))==SF_Distinct 179 && sqlite3ExprListCompare(sSort.pOrderBy, pEList, -1)==0 180 #ifndef SQLITE_OMIT_WINDOWFUNC 181- && p->pWin==0 182+ && ALWAYS(p->pWin==0) 183 #endif 184 ){ 185 p->selFlags &= ~SF_Distinct; 186@@ -6791,6 +6771,14 @@ int sqlite3Select( 187 select_end: 188 sqlite3ExprListDelete(db, pMinMaxOrderBy); 189 sqlite3DbFree(db, sAggInfo.aCol); 190+#ifdef SQLITE_DEBUG 191+ for(i=0; i<sAggInfo.nFunc; i++){ 192+ assert( sAggInfo.aFunc[i].pExpr!=0 ); 193+ assert( sAggInfo.aFunc[i].pExpr->pAggInfo==&sAggInfo ); 194+ sAggInfo.aFunc[i].pExpr->pAggInfo = 0; 195+ } 196+ sAggInfo.iAggMagic = 0; 197+#endif 198 sqlite3DbFree(db, sAggInfo.aFunc); 199 #if SELECTTRACE_ENABLED 200 SELECTTRACE(0x1,pParse,p,("end processing\n")); 201diff -Npur sqlite-version-3.32.2-new/src/sqliteInt.h sqlite-version-3.32.2/src/sqliteInt.h 202--- sqlite-version-3.32.2-new/src/sqliteInt.h 2020-06-04 08:58:43.000000000 -0400 203+++ sqlite-version-3.32.2/src/sqliteInt.h 2021-08-04 12:28:22.825268422 -0400 204@@ -976,7 +976,12 @@ typedef INT16_TYPE LogEst; 205 */ 206 #if defined(SQLITE_ENABLE_SELECTTRACE) 207 # define SELECTTRACE_ENABLED 1 208+# define SELECTTRACE(K,P,S,X) \ 209+ if(sqlite3SelectTrace&(K)) \ 210+ sqlite3DebugPrintf("%u/%d/%p: ",(S)->selId,(P)->addrExplain,(S)),\ 211+ sqlite3DebugPrintf X 212 #else 213+# define SELECTTRACE(K,P,S,X) 214 # define SELECTTRACE_ENABLED 0 215 #endif 216 217@@ -2523,9 +2528,24 @@ struct AggInfo { 218 int iDistinct; /* Ephemeral table used to enforce DISTINCT */ 219 } *aFunc; 220 int nFunc; /* Number of entries in aFunc[] */ 221+#ifdef SQLITE_DEBUG 222+ u32 iAggMagic; /* Sanity checking constant */ 223+#endif 224 }; 225 226 /* 227+** Allowed values for AggInfo.iAggMagic 228+*/ 229+#define SQLITE_AGGMAGIC_VALID 0x05cadade 230+ 231+/* 232+** True if the AggInfo object is valid. Used inside of assert() only. 233+*/ 234+#ifdef SQLITE_DEBUG 235+# define AggInfoValid(P) ((P)->iAggMagic==SQLITE_AGGMAGIC_VALID) 236+#endif 237+ 238+/* 239 ** The datatype ynVar is a signed integer, either 16-bit or 32-bit. 240 ** Usually it is 16-bits. But if SQLITE_MAX_VARIABLE_NUMBER is greater 241 ** than 32767 we have to make it 32-bit. 16-bit is preferred because 242@@ -3105,6 +3125,7 @@ struct Select { 243 #define SF_WhereBegin 0x0080000 /* Really a WhereBegin() call. Debug Only */ 244 #define SF_WinRewrite 0x0100000 /* Window function rewrite accomplished */ 245 #define SF_View 0x0200000 /* SELECT statement is a view */ 246+#define SF_NoopOrderBy 0x0400000 /* ORDER BY is ignored for this query */ 247 248 /* 249 ** The results of a SELECT can be distributed in several ways, as defined 250@@ -4546,10 +4567,11 @@ extern const unsigned char sqlite3UpperT 251 extern const unsigned char sqlite3CtypeMap[]; 252 extern SQLITE_WSD struct Sqlite3Config sqlite3Config; 253 extern FuncDefHash sqlite3BuiltinFunctions; 254+extern u32 sqlite3SelectTrace; 255 #ifndef SQLITE_OMIT_WSD 256 extern int sqlite3PendingByte; 257 #endif 258-#endif 259+#endif /* !defined(SQLITE_AMALGAMATION) */ 260 #ifdef VDBE_PROFILE 261 extern sqlite3_uint64 sqlite3NProfileCnt; 262 #endif 263diff -Npur sqlite-version-3.32.2-new/src/test1.c sqlite-version-3.32.2/src/test1.c 264--- sqlite-version-3.32.2-new/src/test1.c 2020-06-04 08:58:43.000000000 -0400 265+++ sqlite-version-3.32.2/src/test1.c 2021-08-04 11:57:45.037230992 -0400 266@@ -8164,7 +8164,7 @@ int Sqlitetest1_Init(Tcl_Interp *interp) 267 #endif 268 #endif 269 #if defined(SQLITE_ENABLE_SELECTTRACE) 270- extern int sqlite3SelectTrace; 271+ extern u32 sqlite3SelectTrace; 272 #endif 273 274 for(i=0; i<sizeof(aCmd)/sizeof(aCmd[0]); i++){ 275diff -Npur sqlite-version-3.32.2-new/src/window.c sqlite-version-3.32.2/src/window.c 276--- sqlite-version-3.32.2-new/src/window.c 2020-06-04 08:58:43.000000000 -0400 277+++ sqlite-version-3.32.2/src/window.c 2021-08-04 11:57:45.041230992 -0400 278@@ -942,7 +942,7 @@ static int sqlite3WindowExtraAggFuncDept 279 */ 280 int sqlite3WindowRewrite(Parse *pParse, Select *p){ 281 int rc = SQLITE_OK; 282- if( p->pWin && p->pPrior==0 && (p->selFlags & SF_WinRewrite)==0 ){ 283+ if( ALWAYS(p->pWin && (p->selFlags & SF_WinRewrite)==0) ){ 284 Vdbe *v = sqlite3GetVdbe(pParse); 285 sqlite3 *db = pParse->db; 286 Select *pSub = 0; /* The subquery */ 287diff -Npur sqlite-version-3.32.2-new/test/having.test sqlite-version-3.32.2/test/having.test 288--- sqlite-version-3.32.2-new/test/having.test 2020-06-04 08:58:43.000000000 -0400 289+++ sqlite-version-3.32.2/test/having.test 2021-08-04 11:57:45.041230992 -0400 290@@ -154,5 +154,24 @@ do_execsql_test 4.3 { 291 SELECT a, sum(b) FROM t3 WHERE nondeter(a) GROUP BY a 292 } {1 4 2 2} 293 294+#------------------------------------------------------------------------- 295+reset_db 296+do_execsql_test 5.0 { 297+ CREATE TABLE t1(a, b); 298+ CREATE TABLE t2(x, y); 299+ INSERT INTO t1 VALUES('a', 'b'); 300+} 301+ 302+# The WHERE clause (a=2), uses an aggregate column from the outer query. 303+# If the HAVING term (0) is moved into the WHERE clause in this case, 304+# SQLite would at one point optimize (a=2 AND 0) to simply (0). Which 305+# is logically correct, but happened to cause problems in aggregate 306+# processing for the outer query. This test case verifies that those 307+# problems are no longer present. 308+do_execsql_test 5.1 { 309+ SELECT min(b), ( 310+ SELECT x FROM t2 WHERE a=2 GROUP BY y HAVING 0 311+ ) FROM t1; 312+} {b {}} 313 314 finish_test 315diff -Npur sqlite-version-3.32.2-new/test/selectA.test sqlite-version-3.32.2/test/selectA.test 316--- sqlite-version-3.32.2-new/test/selectA.test 2020-06-04 08:58:43.000000000 -0400 317+++ sqlite-version-3.32.2/test/selectA.test 2021-08-04 12:29:43.021270055 -0400 318@@ -1446,5 +1446,26 @@ do_execsql_test 6.1 { 319 SELECT * FROM (SELECT a FROM t1 UNION SELECT b FROM t2) WHERE a=a; 320 } {12345} 321 322+# 2020-06-15 ticket 8f157e8010b22af0 323+# 324+reset_db 325+do_execsql_test 7.1 { 326+ CREATE TABLE t1(c1); INSERT INTO t1 VALUES(12),(123),(1234),(NULL),('abc'); 327+ CREATE TABLE t2(c2); INSERT INTO t2 VALUES(44),(55),(123); 328+ CREATE TABLE t3(c3,c4); INSERT INTO t3 VALUES(66,1),(123,2),(77,3); 329+ CREATE VIEW t4 AS SELECT c3 FROM t3; 330+ CREATE VIEW t5 AS SELECT c3 FROM t3 ORDER BY c4; 331+} 332+do_execsql_test 7.2 { 333+ SELECT * FROM t1, t2 WHERE c1=(SELECT 123 INTERSECT SELECT c2 FROM t4) AND c1=123; 334+} {123 123} 335+do_execsql_test 7.3 { 336+ SELECT * FROM t1, t2 WHERE c1=(SELECT 123 INTERSECT SELECT c2 FROM t5) AND c1=123; 337+} {123 123} 338+do_execsql_test 7.4 { 339+ CREATE TABLE a(b); 340+ CREATE VIEW c(d) AS SELECT b FROM a ORDER BY b; 341+ SELECT sum(d) OVER( PARTITION BY(SELECT 0 FROM c JOIN a WHERE b =(SELECT b INTERSECT SELECT d FROM c) AND b = 123)) FROM c; 342+} {} 343 344 finish_test 345diff -Npur sqlite-version-3.32.2-new/test/window1.test sqlite-version-3.32.2/test/window1.test 346--- sqlite-version-3.32.2-new/test/window1.test 2020-06-04 08:58:43.000000000 -0400 347+++ sqlite-version-3.32.2/test/window1.test 2021-08-04 11:57:45.041230992 -0400 348@@ -1743,5 +1743,47 @@ do_execsql_test 53.0 { 349 WHERE a.c); 350 } {4 4 4 4} 351 352+#------------------------------------------------------------------------- 353+reset_db 354+do_execsql_test 54.1 { 355+ CREATE TABLE t1(a VARCHAR(20), b FLOAT); 356+ INSERT INTO t1 VALUES('1',10.0); 357+} 358+ 359+do_execsql_test 54.2 { 360+ SELECT * FROM ( 361+ SELECT sum(b) OVER() AS c FROM t1 362+ UNION 363+ SELECT b AS c FROM t1 364+ ) WHERE c>10; 365+} 366+ 367+do_execsql_test 54.3 { 368+ INSERT INTO t1 VALUES('2',5.0); 369+ INSERT INTO t1 VALUES('3',15.0); 370+} 371+ 372+do_execsql_test 54.4 { 373+ SELECT * FROM ( 374+ SELECT sum(b) OVER() AS c FROM t1 375+ UNION 376+ SELECT b AS c FROM t1 377+ ) WHERE c>10; 378+} {15.0 30.0} 379+ 380+# 2020-06-05 ticket c8d3b9f0a750a529 381+reset_db 382+do_execsql_test 55.1 { 383+ CREATE TABLE a(b); 384+ SELECT 385+ (SELECT b FROM a 386+ GROUP BY b 387+ HAVING (SELECT COUNT()OVER() + lead(b)OVER(ORDER BY SUM(DISTINCT b) + b)) 388+ ) 389+ FROM a 390+ UNION 391+ SELECT 99 392+ ORDER BY 1; 393+} {99} 394 395 finish_test 396