From 6ba7ab0d2524748479376e70043be9fc4cfbeccc Mon Sep 17 00:00:00 2001 From: dan Date: Tue, 2 Jul 2019 11:56:47 +0000 Subject: [PATCH 1/9] Experimental implementation of FILTER clause for aggregate functions. FossilOrigin-Name: 1f1ae2d6ac8dcbb62e5aa3dc17bc67d559cb565fc0d0a8c00a596075d35f8130 --- manifest | 32 ++++++++++------- manifest.uuid | 2 +- src/expr.c | 27 +++++++++++--- src/parse.y | 23 ++++++------ src/resolve.c | 11 +++++- src/select.c | 24 ++++++++++--- src/sqliteInt.h | 8 +++-- src/walker.c | 3 ++ src/window.c | 12 ++++--- test/filter1.test | 90 +++++++++++++++++++++++++++++++++++++++++++++++ test/filter2.tcl | 73 ++++++++++++++++++++++++++++++++++++++ test/filter2.test | 82 ++++++++++++++++++++++++++++++++++++++++++ 12 files changed, 342 insertions(+), 45 deletions(-) create mode 100644 test/filter1.test create mode 100644 test/filter2.tcl create mode 100644 test/filter2.test diff --git a/manifest b/manifest index a320ae1e91..e9ba34353b 100644 --- a/manifest +++ b/manifest @@ -1,5 +1,5 @@ -C Use\sthe\sOP_Sequence\sopcode\sfor\sgenerating\sunique\srowid\svalues\sfor\san\nautoindex\son\sa\sco-routine\simplementation\sof\sa\ssubquery. -D 2019-06-28T07:08:13.998 +C Experimental\simplementation\sof\sFILTER\sclause\sfor\saggregate\sfunctions. +D 2019-07-02T11:56:47.637 F .fossil-settings/empty-dirs dbb81e8fc0401ac46a1491ab34a7f2c7c0452f2f06b54ebb845d024ca8283ef1 F .fossil-settings/ignore-glob 35175cdfcf539b2318cb04a9901442804be81cd677d8b889fcc9149c21f239ea F LICENSE.md df5091916dbb40e6e9686186587125e1b2ff51f022cc334e886c19a0e9982724 @@ -474,7 +474,7 @@ F src/date.c e1d8ac7102f3f283e63e13867acb0efa33861cf34f0faf4cdbaf9fa7a1eb7041 F src/dbpage.c 135eb3b5e74f9ef74bde5cec2571192c90c86984fa534c88bf4a055076fa19b7 F src/dbstat.c c12833de69cb655751487d2c5a59607e36be1c58ba1f4bd536609909ad47b319 F src/delete.c d08c9e01a2664afd12edcfa3a9c6578517e8ff8735f35509582693adbe0edeaf -F src/expr.c 2756d2875ac5b7a7efcc54edea3c8a31f4126684ded18da6fb406f4d3780a5f5 +F src/expr.c 33267aaf04145af9f1f06c1f4c10f8e7a9acce1c681d75526e6a3fa7e58c1d79 F src/fault.c 460f3e55994363812d9d60844b2a6de88826e007 F src/fkey.c 0e14d4bef8eac2d87bbd517e492d9084c65008d117823f8922c5e7b2b599bd33 F src/func.c 905c0424900979fade3731b4a271592059425c988cb7d5b245032253712dd7c8 @@ -511,7 +511,7 @@ F src/os_win.c 85d9e532d0444ab6c16d7431490c2e279e282aa0917b0e988996b1ae0de5c5a0 F src/os_win.h 7b073010f1451abe501be30d12f6bc599824944a F src/pager.c 422fd8cfa59fb9173eff36a95878904a0eeb0dcc62ba49350acc8b1e51c4dc7b F src/pager.h 217921e81eb5fe455caa5cda96061959706bcdd29ddb57166198645ef7822ac3 -F src/parse.y 026e3e6fff32432166a4eae0bdb78ad09a53c84eb49acb44dc32e49b4c0b8a48 +F src/parse.y 2902f393b08e33f4af42e2b63f943be62f5de096138461459de60084f598a056 F src/pcache.c fd2d0553b3222d6b9f7cb251079e5bca1299d1161da3027b525932d8bf46340a F src/pcache.h 4f87acd914cef5016fae3030343540d75f5b85a1877eed1a2a19b9f284248586 F src/pcache1.c be64b2f3908a7f97c56c963676eb12f0d6254c95b28cdc1d73a186eff213219d @@ -520,14 +520,14 @@ F src/pragma.h 4a9fabff14db4487a734dfeeb4be984ce662bfdccfae16145b9c732327735e13 F src/prepare.c 78027c6231fbb19ca186a5f5f0c0a1375d9c2cec0655273f9bd90d9ff74a34b3 F src/printf.c 9be6945837c839ba57837b4bc3af349eba630920fa5532aa518816defe42a7d4 F src/random.c 80f5d666f23feb3e6665a6ce04c7197212a88384 -F src/resolve.c 93b7bc7c45efa6322d92293361c51a873690daed50cf77eeff88a448246b0d5a +F src/resolve.c 65514625b68e79a4087d218107d51d489477509d55543e1be375d1e58f27c2f3 F src/rowset.c d977b011993aaea002cab3e0bb2ce50cf346000dff94e944d547b989f4b1fe93 -F src/select.c 9187f2c65744e975b191ccee49946732ee922f8bf40da998b322aca1633405ea +F src/select.c 1774cc457eb0b6cd757a350fd9f5600c4d13f3e899d6615033aecaa4f00a5e7d F src/shell.c.in 82f8a473c01662f52233c6c75b9bf88d0d2cab276086d5d4ca6f2ff57a3e48eb F src/sqlite.h.in bc4aa09a59dcf3c34b8e57ce9ade8ff6aed8ef7a7ddc59951f59f9b009fc7cc0 F src/sqlite3.rc 5121c9e10c3964d5755191c80dd1180c122fc3a8 F src/sqlite3ext.h 9ecc93b8493bd20c0c07d52e2ac0ed8bab9b549c7f7955b59869597b650dd8b5 -F src/sqliteInt.h bf65dfa6565fae2e0d501088fc9c5212244adc1429a543827a16b2f5eaf41982 +F src/sqliteInt.h 13fa73fd67968a4e3c58c84fbd655770989a82e0902880b0446747cd6711ad74 F src/sqliteLimit.h 1513bfb7b20378aa0041e7022d04acb73525de35b80b252f1b83fedb4de6a76b F src/status.c 46e7aec11f79dad50965a5ca5fa9de009f7d6bde08be2156f1538a0a296d4d0e F src/table.c b46ad567748f24a326d9de40e5b9659f96ffff34 @@ -607,12 +607,12 @@ F src/vtab.c 1fa256c6ddad7a81e2a4dc080d015d4b0a7135767717d311298e47f6fca64bb3 F src/vxworks.h d2988f4e5a61a4dfe82c6524dd3d6e4f2ce3cdb9 F src/wal.c b09a2a9cab50efa08451a8c81d47052120ad5da174048c6d0b08d405384abdf2 F src/wal.h 606292549f5a7be50b6227bd685fa76e3a4affad71bb8ac5ce4cb5c79f6a176a -F src/walker.c 7607f1a68130c028255d8d56094ea602fc402c79e1e35a46e6282849d90d5fe4 +F src/walker.c 31844651eacdc408474c62df526f20d5ea7994b5b8ffcd23eed5dc8f688c26f5 F src/where.c 2f11eeb14335b7640f886b2fb441f54a94c35ab5cde8b53461a1074bfd587081 F src/whereInt.h 1b728f71654ebf8421a1715497a587f02d6f538e819af58dc826908f8577e810 F src/wherecode.c 37a1004237d630d785c47bba2290eac652a7a8b0047518eba3cb7c808b604c4a F src/whereexpr.c 5e559bdd24b06e3bc2e68f258bf751302954dc1e432daf71fdd8098a71462326 -F src/window.c 5be2cf7d8763cc97137fc44d015aed8a1a4a56fe9700d7933ed560172617c756 +F src/window.c c595e250528b0f5c31d26d96e0077d137aa3be2aec3b33ab30fbf3e7f69110c2 F test/8_3_names.test ebbb5cd36741350040fd28b432ceadf495be25b2 F test/affinity2.test a6d901b436328bd67a79b41bb0ac2663918fe3bd F test/affinity3.test 6a101af2fc945ce2912f6fe54dd646018551710d @@ -856,6 +856,9 @@ F test/extraquick.test cb254400bd42bfb777ff675356aabf3287978f79 F test/fallocate.test 37a62e396a68eeede8f8d2ecf23573a80faceb630788d314d0a073d862616717 F test/filectrl.test 6e871c2d35dead1d9a88e176e8d2ca094fec6bb3 F test/filefmt.test f393e80c4b8d493b7a7f8f3809a8425bbf4292af1f5140f01cb1427798a2bbd4 +F test/filter1.test d706edd2c32c6b148143d885927e5cf4ea141106b803e243e5d1bfbdc7e2b5cf +F test/filter2.tcl 87e24fd0a2ab502bd4d0350c4eee17a50e2b4364a1b67230f82b8f2a5029f6bf +F test/filter2.test dfddced42a534e71a730aa3d4c6b148d9fbf29aa306f9cec6b51f31b8cf7e224 F test/fkey1.test d11dbb8a93ead9b5c46ae5d02da016d61245d47662fb2d844c99214f6163f768 F test/fkey2.test d35d1c81e7569bdd2b872e91750f7098117d2e8291369f70b7e3d50a0e523dc2 F test/fkey3.test 76d475c80b84ee7a5d062e56ccb6ea68882e2b49 @@ -1830,7 +1833,10 @@ F vsixtest/vsixtest.tcl 6a9a6ab600c25a91a7acc6293828957a386a8a93 F vsixtest/vsixtest.vcxproj.data 2ed517e100c66dc455b492e1a33350c1b20fbcdc F vsixtest/vsixtest.vcxproj.filters 37e51ffedcdb064aad6ff33b6148725226cd608e F vsixtest/vsixtest_TemporaryKey.pfx e5b1b036facdb453873e7084e1cae9102ccc67a0 -P 5fd20e09a522b62a529cf4d76fbdf0a09426f67ffa30430cac6b81ebf32ba43e -R d8e38b1a6647f90f85dd30ba60740431 -U drh -Z 069759ad3085e2b5f17cc34ec369471a +P eab4297577e4d325fed4757867fc77860de7448998d86f098c8a50272e17d35e +R 43e2b0402b61e905810368aafa77ca67 +T *branch * filter-clause +T *sym-filter-clause * +T -sym-trunk * +U dan +Z 9e8394b31320bbcccfabf9cff0d64121 diff --git a/manifest.uuid b/manifest.uuid index 82a0a5af46..959245f94c 100644 --- a/manifest.uuid +++ b/manifest.uuid @@ -1 +1 @@ -eab4297577e4d325fed4757867fc77860de7448998d86f098c8a50272e17d35e \ No newline at end of file +1f1ae2d6ac8dcbb62e5aa3dc17bc67d559cb565fc0d0a8c00a596075d35f8130 \ No newline at end of file diff --git a/src/expr.c b/src/expr.c index c4f201a131..75d0dbe9da 100644 --- a/src/expr.c +++ b/src/expr.c @@ -1027,7 +1027,7 @@ static SQLITE_NOINLINE void sqlite3ExprDeleteNN(sqlite3 *db, Expr *p){ assert( !ExprHasProperty(p, EP_WinFunc) || p->y.pWin!=0 || db->mallocFailed ); assert( p->op!=TK_FUNCTION || ExprHasProperty(p, EP_TokenOnly|EP_Reduced) - || p->y.pWin==0 || ExprHasProperty(p, EP_WinFunc) ); + || p->y.pWin==0 || ExprHasProperty(p, EP_WinFunc|EP_Filter) ); #ifdef SQLITE_DEBUG if( ExprHasProperty(p, EP_Leaf) && !ExprHasProperty(p, EP_TokenOnly) ){ assert( p->pLeft==0 ); @@ -1046,10 +1046,15 @@ static SQLITE_NOINLINE void sqlite3ExprDeleteNN(sqlite3 *db, Expr *p){ }else{ sqlite3ExprListDelete(db, p->x.pList); } +#ifndef SQLITE_OMIT_WINDOWFUNC if( ExprHasProperty(p, EP_WinFunc) ){ - assert( p->op==TK_FUNCTION ); + assert( p->op==TK_FUNCTION && !ExprHasProperty(p, EP_Filter) ); sqlite3WindowDelete(db, p->y.pWin); + }else if( ExprHasProperty(p, EP_Filter) ){ + assert( p->op==TK_FUNCTION || p->op==TK_AGG_FUNCTION ); + sqlite3ExprDelete(db, p->y.pFilter); } +#endif } if( ExprHasProperty(p, EP_MemToken) ) sqlite3DbFree(db, p->u.zToken); if( !ExprHasProperty(p, EP_Static) ){ @@ -1264,7 +1269,7 @@ static Expr *exprDup(sqlite3 *db, Expr *p, int dupFlags, u8 **pzBuffer){ } /* Fill in pNew->pLeft and pNew->pRight. */ - if( ExprHasProperty(pNew, EP_Reduced|EP_TokenOnly|EP_WinFunc) ){ + if( ExprHasProperty(pNew, EP_Reduced|EP_TokenOnly|EP_WinFunc|EP_Filter) ){ zAlloc += dupedExprNodeSize(p, dupFlags); if( !ExprHasProperty(pNew, EP_TokenOnly|EP_Leaf) ){ pNew->pLeft = p->pLeft ? @@ -1277,6 +1282,10 @@ static Expr *exprDup(sqlite3 *db, Expr *p, int dupFlags, u8 **pzBuffer){ pNew->y.pWin = sqlite3WindowDup(db, pNew, p->y.pWin); assert( ExprHasProperty(pNew, EP_WinFunc) ); } + if( ExprHasProperty(p, EP_Filter) ){ + pNew->y.pFilter = sqlite3ExprDup(db, p->y.pFilter, 0); + assert( ExprHasProperty(pNew, EP_Filter) ); + } #endif /* SQLITE_OMIT_WINDOWFUNC */ if( pzBuffer ){ *pzBuffer = zAlloc; @@ -1332,8 +1341,8 @@ static With *withDup(sqlite3 *db, With *p){ ** objects found there, assembling them onto the linked list at Select->pWin. */ static int gatherSelectWindowsCallback(Walker *pWalker, Expr *pExpr){ - if( pExpr->op==TK_FUNCTION && pExpr->y.pWin!=0 ){ - assert( ExprHasProperty(pExpr, EP_WinFunc) ); + if( pExpr->op==TK_FUNCTION && ExprHasProperty(pExpr, EP_WinFunc) ){ + assert( pExpr->y.pWin ); pExpr->y.pWin->pNextWin = pWalker->u.pSelect->pWin; pWalker->u.pSelect->pWin = pExpr->y.pWin; } @@ -4862,6 +4871,14 @@ int sqlite3ExprCompare(Parse *pParse, Expr *pA, Expr *pB, int iTab){ }else if( ALWAYS(pB->u.zToken!=0) && strcmp(pA->u.zToken,pB->u.zToken)!=0 ){ return 2; } +#ifndef SQLITE_OMIT_WINDOWFUNC + else if( pA->op==TK_AGG_FUNCTION ){ + assert( ExprHasProperty(pA, EP_WinFunc)==0 ); + if( sqlite3ExprCompare(pParse, pA->y.pFilter, pB->y.pFilter, iTab) ){ + return 2; + } + } +#endif } if( (pA->flags & EP_Distinct)!=(pB->flags & EP_Distinct) ) return 2; if( (combinedFlags & EP_TokenOnly)==0 ){ diff --git a/src/parse.y b/src/parse.y index fc5bff16dc..dda9f627aa 100644 --- a/src/parse.y +++ b/src/parse.y @@ -1036,21 +1036,23 @@ expr(A) ::= CAST LP expr(E) AS typetoken(T) RP. { %endif SQLITE_OMIT_CAST +%ifdef SQLITE_OMIT_WINDOWFUNC expr(A) ::= id(X) LP distinct(D) exprlist(Y) RP. { A = sqlite3ExprFunction(pParse, Y, &X, D); } expr(A) ::= id(X) LP STAR RP. { A = sqlite3ExprFunction(pParse, 0, &X, 0); } +%endif %ifndef SQLITE_OMIT_WINDOWFUNC -expr(A) ::= id(X) LP distinct(D) exprlist(Y) RP over_clause(Z). { +expr(A) ::= id(X) LP distinct(D) exprlist(Y) RP filter_opt(F) over_opt(Z). { A = sqlite3ExprFunction(pParse, Y, &X, D); - sqlite3WindowAttach(pParse, A, Z); + sqlite3WindowAttach(pParse, A, F, Z); } -expr(A) ::= id(X) LP STAR RP over_clause(Z). { +expr(A) ::= id(X) LP STAR RP filter_opt(F) over_opt(Z). { A = sqlite3ExprFunction(pParse, 0, &X, 0); - sqlite3WindowAttach(pParse, A, Z); + sqlite3WindowAttach(pParse, A, F, Z); } %endif @@ -1724,20 +1726,17 @@ frame_exclude(A) ::= GROUP|TIES(X). {A = @X; /*A-overwrites-X*/} %destructor window_clause {sqlite3WindowListDelete(pParse->db, $$);} window_clause(A) ::= WINDOW windowdefn_list(B). { A = B; } -%type over_clause {Window*} -%destructor over_clause {sqlite3WindowDelete(pParse->db, $$);} -over_clause(A) ::= filter_opt(W) OVER LP window(Z) RP. { +%type over_opt {Window*} +%destructor over_opt {sqlite3WindowDelete(pParse->db, $$);} +over_opt(A) ::= . { A=0; } +over_opt(A) ::= OVER LP window(Z) RP. { A = Z; assert( A!=0 ); - A->pFilter = W; } -over_clause(A) ::= filter_opt(W) OVER nm(Z). { +over_opt(A) ::= OVER nm(Z). { A = (Window*)sqlite3DbMallocZero(pParse->db, sizeof(Window)); if( A ){ A->zName = sqlite3DbStrNDup(pParse->db, Z.z, Z.n); - A->pFilter = W; - }else{ - sqlite3ExprDelete(pParse->db, W); } } diff --git a/src/resolve.c b/src/resolve.c index 8dac077eed..0a8aff5652 100644 --- a/src/resolve.c +++ b/src/resolve.c @@ -849,6 +849,12 @@ static int resolveExprStep(Walker *pWalker, Expr *pExpr){ sqlite3ErrorMsg(pParse, "misuse of %s function %.*s()",zType,nId,zId); pNC->nErr++; is_agg = 0; + }else if( is_agg==0 && ExprHasProperty(pExpr, EP_Filter) ){ + sqlite3ErrorMsg(pParse, + "filter clause may not be used with non-aggregate %.*s()", + nId, zId + ); + pNC->nErr++; } #else if( (is_agg && (pNC->ncFlags & NC_AllowAgg)==0) ){ @@ -883,7 +889,7 @@ static int resolveExprStep(Walker *pWalker, Expr *pExpr){ sqlite3WalkExprList(pWalker, pList); if( is_agg ){ #ifndef SQLITE_OMIT_WINDOWFUNC - if( pExpr->y.pWin ){ + if( ExprHasProperty(pExpr, EP_WinFunc) ){ Select *pSel = pNC->pWinSelect; if( IN_RENAME_OBJECT==0 ){ sqlite3WindowUpdate(pParse, pSel->pWinDefn, pExpr->y.pWin, pDef); @@ -904,6 +910,9 @@ static int resolveExprStep(Walker *pWalker, Expr *pExpr){ NameContext *pNC2 = pNC; pExpr->op = TK_AGG_FUNCTION; pExpr->op2 = 0; +#ifndef SQLITE_OMIT_WINDOWFUNC + sqlite3WalkExpr(pWalker, pExpr->y.pFilter); +#endif while( pNC2 && !sqlite3FunctionUsesThisSrc(pExpr, pNC2->pSrcList) ){ pExpr->op2++; pNC2 = pNC2->pNext; diff --git a/src/select.c b/src/select.c index bd16acb027..e5f57fede6 100644 --- a/src/select.c +++ b/src/select.c @@ -4406,7 +4406,9 @@ static u8 minMaxQuery(sqlite3 *db, Expr *pFunc, ExprList **ppMinMax){ assert( *ppMinMax==0 ); assert( pFunc->op==TK_AGG_FUNCTION ); - if( pEList==0 || pEList->nExpr!=1 ) return eRet; + if( pEList==0 || pEList->nExpr!=1 || ExprHasProperty(pFunc, EP_Filter) ){ + return eRet; + } zFunc = pFunc->u.zToken; if( sqlite3StrICmp(zFunc, "min")==0 ){ eRet = WHERE_ORDERBY_MIN; @@ -4453,7 +4455,7 @@ static Table *isSimpleCount(Select *p, AggInfo *pAggInfo){ if( pExpr->op!=TK_AGG_FUNCTION ) return 0; if( NEVER(pAggInfo->nFunc==0) ) return 0; if( (pAggInfo->aFunc[0].pFunc->funcFlags&SQLITE_FUNC_COUNT)==0 ) return 0; - if( pExpr->flags&EP_Distinct ) return 0; + if( ExprHasProperty(pExpr, EP_Distinct|EP_Filter) ) return 0; return pTab; } @@ -5333,6 +5335,11 @@ static void updateAccumulator(Parse *pParse, int regAcc, AggInfo *pAggInfo){ int regAgg; ExprList *pList = pF->pExpr->x.pList; assert( !ExprHasProperty(pF->pExpr, EP_xIsSelect) ); + if( ExprHasProperty(pF->pExpr, EP_Filter) ){ + Expr *pFilter = pF->pExpr->y.pFilter; + addrNext = sqlite3VdbeMakeLabel(pParse); + sqlite3ExprIfFalse(pParse, pFilter, addrNext, SQLITE_JUMPIFNULL); + } if( pList ){ nArg = pList->nExpr; regAgg = sqlite3GetTempRange(pParse, nArg); @@ -5342,7 +5349,9 @@ static void updateAccumulator(Parse *pParse, int regAcc, AggInfo *pAggInfo){ regAgg = 0; } if( pF->iDistinct>=0 ){ - addrNext = sqlite3VdbeMakeLabel(pParse); + if( addrNext==0 ){ + addrNext = sqlite3VdbeMakeLabel(pParse); + } testcase( nArg==0 ); /* Error condition */ testcase( nArg>1 ); /* Also an error */ codeDistinct(pParse, pF->iDistinct, addrNext, 1, regAgg); @@ -6225,9 +6234,14 @@ int sqlite3Select( minMaxFlag = WHERE_ORDERBY_NORMAL; } for(i=0; ix.pList); + sqlite3ExprAnalyzeAggList(&sNC, pExpr->x.pList); +#ifndef SQLITE_OMIT_WINDOWFUNC + assert( !ExprHasProperty(pExpr, EP_WinFunc) ); + sqlite3ExprAnalyzeAggregates(&sNC, pExpr->y.pFilter); +#endif sNC.ncFlags &= ~NC_InAggFunc; } sAggInfo.mxReg = pParse->nMem; diff --git a/src/sqliteInt.h b/src/sqliteInt.h index 1c46183312..c0f2e0a186 100644 --- a/src/sqliteInt.h +++ b/src/sqliteInt.h @@ -2491,7 +2491,8 @@ struct Expr { union { Table *pTab; /* TK_COLUMN: Table containing column. Can be NULL ** for a column of an index on an expression */ - Window *pWin; /* TK_FUNCTION: Window definition for the func */ + Window *pWin; /* EP_WinFunc: Window definition for the func */ + Expr *pFilter; /* EP_Filter: Filter definition for the func */ struct { /* TK_IN, TK_SELECT, and TK_EXISTS */ int iAddr; /* Subroutine entry address */ int regReturn; /* Register used to hold return address */ @@ -2536,6 +2537,7 @@ struct Expr { #define EP_Static 0x8000000 /* Held in memory not obtained from malloc() */ #define EP_IsTrue 0x10000000 /* Always has boolean value of TRUE */ #define EP_IsFalse 0x20000000 /* Always has boolean value of FALSE */ +#define EP_Filter 0x40000000 /* TK_[AGG_]FUNCTION with Expr.y.pFilter set */ /* ** The EP_Propagate mask is a set of properties that automatically propagate @@ -3602,7 +3604,7 @@ struct Window { void sqlite3WindowDelete(sqlite3*, Window*); void sqlite3WindowListDelete(sqlite3 *db, Window *p); Window *sqlite3WindowAlloc(Parse*, int, int, Expr*, int , Expr*, u8); -void sqlite3WindowAttach(Parse*, Expr*, Window*); +void sqlite3WindowAttach(Parse*, Expr*, Expr*, Window*); int sqlite3WindowCompare(Parse*, Window*, Window*); void sqlite3WindowCodeInit(Parse*, Window*); void sqlite3WindowCodeStep(Parse*, Select*, WhereInfo*, int, int); @@ -3617,7 +3619,7 @@ Window *sqlite3WindowAssemble(Parse*, Window*, ExprList*, ExprList*, Token*); #else # define sqlite3WindowDelete(a,b) # define sqlite3WindowFunctions() -# define sqlite3WindowAttach(a,b,c) +# define sqlite3WindowAttach(a,b,c,d) #endif /* diff --git a/src/walker.c b/src/walker.c index eff3585250..766a345add 100644 --- a/src/walker.c +++ b/src/walker.c @@ -74,6 +74,9 @@ static SQLITE_NOINLINE int walkExpr(Walker *pWalker, Expr *pExpr){ if( ExprHasProperty(pExpr, EP_WinFunc) ){ if( walkWindowList(pWalker, pExpr->y.pWin) ) return WRC_Abort; } + if( ExprHasProperty(pExpr, EP_Filter) ){ + if( walkExpr(pWalker, pExpr->y.pFilter) ) return WRC_Abort; + } #endif } break; diff --git a/src/window.c b/src/window.c index 737261e85c..4d7ebf6086 100644 --- a/src/window.c +++ b/src/window.c @@ -1178,13 +1178,10 @@ void sqlite3WindowChain(Parse *pParse, Window *pWin, Window *pList){ /* ** Attach window object pWin to expression p. */ -void sqlite3WindowAttach(Parse *pParse, Expr *p, Window *pWin){ +void sqlite3WindowAttach(Parse *pParse, Expr *p, Expr *pFilter, Window *pWin){ if( p ){ assert( p->op==TK_FUNCTION ); - /* This routine is only called for the parser. If pWin was not - ** allocated due to an OOM, then the parser would fail before ever - ** invoking this routine */ - if( ALWAYS(pWin) ){ + if( pWin ){ p->y.pWin = pWin; ExprSetProperty(p, EP_WinFunc); pWin->pOwner = p; @@ -1192,9 +1189,14 @@ void sqlite3WindowAttach(Parse *pParse, Expr *p, Window *pWin){ sqlite3ErrorMsg(pParse, "DISTINCT is not supported for window functions"); } + pWin->pFilter = pFilter; + }else if( pFilter ){ + p->y.pFilter = pFilter; + ExprSetProperty(p, EP_Filter); } }else{ sqlite3WindowDelete(pParse->db, pWin); + sqlite3ExprDelete(pParse->db, pFilter); } } diff --git a/test/filter1.test b/test/filter1.test new file mode 100644 index 0000000000..2f5a5b56d4 --- /dev/null +++ b/test/filter1.test @@ -0,0 +1,90 @@ +# 2018 May 8 +# +# The author disclaims copyright to this source code. In place of +# a legal notice, here is a blessing: +# +# May you do good and not evil. +# May you find forgiveness for yourself and forgive others. +# May you share freely, never taking more than you give. +# +#*********************************************************************** +# This file implements regression tests for SQLite library. +# + +set testdir [file dirname $argv0] +source $testdir/tester.tcl +set testprefix filter1 + +ifcapable !windowfunc { + finish_test + return +} + +do_execsql_test 1.0 { + CREATE TABLE t1(a); + CREATE INDEX i1 ON t1(a); + INSERT INTO t1 VALUES(1), (2), (3), (4), (5), (6), (7), (8), (9); +} + +do_execsql_test 1.1 { SELECT sum(a) FROM t1; } 45 +do_execsql_test 1.2 { SELECT sum(a) FILTER( WHERE a<5 ) FROM t1; } 10 + +do_execsql_test 1.3 { + SELECT sum(a) FILTER( WHERE a>9 ), + sum(a) FILTER( WHERE a>8 ), + sum(a) FILTER( WHERE a>7 ), + sum(a) FILTER( WHERE a>6 ), + sum(a) FILTER( WHERE a>5 ), + sum(a) FILTER( WHERE a>4 ), + sum(a) FILTER( WHERE a>3 ), + sum(a) FILTER( WHERE a>2 ), + sum(a) FILTER( WHERE a>1 ), + sum(a) FILTER( WHERE a>0 ) + FROM t1; +} {{} 9 17 24 30 35 39 42 44 45} + +do_execsql_test 1.4 { + SELECT max(a) FILTER (WHERE (a % 2)==0) FROM t1 +} {8} + +do_execsql_test 1.5 { + SELECT min(a) FILTER (WHERE a>4) FROM t1 +} {5} + +do_execsql_test 1.6 { + SELECT count(*) FILTER (WHERE a!=5) FROM t1 +} {8} + +do_execsql_test 1.6 { + SELECT min(a) FILTER (WHERE a>3) FROM t1 GROUP BY (a%2) ORDER BY 1; +} {4 5} + + +#------------------------------------------------------------------------- +# Test some errors: +# +# .1 FILTER on a non-aggregate function, +# .2 Window function in FILTER clause, +# .3 Aggregate function in FILTER clause, +# +reset_db +do_execsql_test 2.0 { + CREATE TABLE t1(a); + INSERT INTO t1 VALUES(1), (2), (3), (4), (5), (6), (7), (8), (9); +} + +do_catchsql_test 2.1 { + SELECT upper(a) FILTER (WHERE a=1) FROM t1 +} {1 {filter clause may not be used with non-aggregate upper()}} + +do_catchsql_test 2.2 { + SELECT sum(a) FILTER (WHERE 1 - max(a) OVER () > 0) FROM t1 +} {1 {misuse of window function max()}} + +do_catchsql_test 2.3 { + SELECT sum(a) FILTER (WHERE 1 - count(a)) FROM t1 +} {1 {misuse of aggregate: count()}} + +finish_test + + diff --git a/test/filter2.tcl b/test/filter2.tcl new file mode 100644 index 0000000000..0a7e2a3dd0 --- /dev/null +++ b/test/filter2.tcl @@ -0,0 +1,73 @@ +# 2018 May 19 +# +# The author disclaims copyright to this source code. In place of +# a legal notice, here is a blessing: +# +# May you do good and not evil. +# May you find forgiveness for yourself and forgive others. +# May you share freely, never taking more than you give. +# +#*********************************************************************** +# + +source [file join [file dirname $argv0] pg_common.tcl] + +#========================================================================= + + +start_test filter2 "2019 July 2" + +ifcapable !windowfunc + +execsql_test 1.0 { + DROP TABLE IF EXISTS t1; + CREATE TABLE t1(a INTEGER PRIMARY KEY, b INTEGER); + INSERT INTO t1 VALUES + (1, 7), (2, 3), (3, 5), (4, 30), (5, 26), (6, 23), (7, 27), + (8, 3), (9, 17), (10, 26), (11, 33), (12, 25), (13, NULL), (14, 47), + (15, 36), (16, 13), (17, 45), (18, 31), (19, 11), (20, 36), (21, 37), + (22, 21), (23, 22), (24, 14), (25, 16), (26, 3), (27, 7), (28, 29), + (29, 50), (30, 38), (31, 3), (32, 36), (33, 12), (34, 4), (35, 46), + (36, 3), (37, 48), (38, 23), (39, NULL), (40, 24), (41, 5), (42, 46), + (43, 11), (44, NULL), (45, 18), (46, 25), (47, 15), (48, 18), (49, 23); +} + +execsql_test 1.1 { SELECT sum(b) FROM t1 } + +execsql_test 1.2 { SELECT sum(b) FILTER (WHERE a<10) FROM t1 } + +execsql_test 1.3 { SELECT count(DISTINCT b) FROM t1 } + +execsql_test 1.4 { SELECT count(DISTINCT b) FILTER (WHERE a!=19) FROM t1 } + +execsql_test 1.5 { + SELECT min(b) FILTER (WHERE a>19), + min(b) FILTER (WHERE a>0), + max(a+b) FILTER (WHERE a>19), + max(b+a) FILTER (WHERE a BETWEEN 10 AND 40) + FROM t1; +} + +execsql_test 1.6 { + SELECT min(b), + min(b), + max(a+b), + max(b+a) + FROM t1 + GROUP BY (a%10) + ORDER BY 1, 2, 3, 4; +} + +execsql_test 1.7 { + SELECT min(b) FILTER (WHERE a>19), + min(b) FILTER (WHERE a>0), + max(a+b) FILTER (WHERE a>19), + max(b+a) FILTER (WHERE a BETWEEN 10 AND 40) + FROM t1 + GROUP BY (a%10) + ORDER BY 1, 2, 3, 4; +} + +finish_test + + diff --git a/test/filter2.test b/test/filter2.test new file mode 100644 index 0000000000..5b98f2e38b --- /dev/null +++ b/test/filter2.test @@ -0,0 +1,82 @@ +# 2019 July 2 +# +# The author disclaims copyright to this source code. In place of +# a legal notice, here is a blessing: +# +# May you do good and not evil. +# May you find forgiveness for yourself and forgive others. +# May you share freely, never taking more than you give. +# +#*********************************************************************** +# This file implements regression tests for SQLite library. +# + +#################################################### +# DO NOT EDIT! THIS FILE IS AUTOMATICALLY GENERATED! +#################################################### + +set testdir [file dirname $argv0] +source $testdir/tester.tcl +set testprefix filter2 + +ifcapable !windowfunc { finish_test ; return } +do_execsql_test 1.0 { + DROP TABLE IF EXISTS t1; + CREATE TABLE t1(a INTEGER PRIMARY KEY, b INTEGER); + INSERT INTO t1 VALUES + (1, 7), (2, 3), (3, 5), (4, 30), (5, 26), (6, 23), (7, 27), + (8, 3), (9, 17), (10, 26), (11, 33), (12, 25), (13, NULL), (14, 47), + (15, 36), (16, 13), (17, 45), (18, 31), (19, 11), (20, 36), (21, 37), + (22, 21), (23, 22), (24, 14), (25, 16), (26, 3), (27, 7), (28, 29), + (29, 50), (30, 38), (31, 3), (32, 36), (33, 12), (34, 4), (35, 46), + (36, 3), (37, 48), (38, 23), (39, NULL), (40, 24), (41, 5), (42, 46), + (43, 11), (44, NULL), (45, 18), (46, 25), (47, 15), (48, 18), (49, 23); +} {} + +do_execsql_test 1.1 { + SELECT sum(b) FROM t1 +} {1041} + +do_execsql_test 1.2 { + SELECT sum(b) FILTER (WHERE a<10) FROM t1 +} {141} + +do_execsql_test 1.3 { + SELECT count(DISTINCT b) FROM t1 +} {31} + +do_execsql_test 1.4 { + SELECT count(DISTINCT b) FILTER (WHERE a!=19) FROM t1 +} {31} + +do_execsql_test 1.5 { + SELECT min(b) FILTER (WHERE a>19), + min(b) FILTER (WHERE a>0), + max(a+b) FILTER (WHERE a>19), + max(b+a) FILTER (WHERE a BETWEEN 10 AND 40) + FROM t1; +} {3 3 88 85} + +do_execsql_test 1.6 { + SELECT min(b), + min(b), + max(a+b), + max(b+a) + FROM t1 + GROUP BY (a%10) + ORDER BY 1, 2, 3, 4; +} {3 3 58 58 3 3 66 66 3 3 71 71 3 3 88 88 4 4 61 61 5 5 54 54 + 7 7 85 85 11 11 79 79 16 16 81 81 24 24 68 68} + +do_execsql_test 1.7 { + SELECT min(b) FILTER (WHERE a>19), + min(b) FILTER (WHERE a>0), + max(a+b) FILTER (WHERE a>19), + max(b+a) FILTER (WHERE a BETWEEN 10 AND 40) + FROM t1 + GROUP BY (a%10) + ORDER BY 1, 2, 3, 4; +} {3 3 58 58 3 3 71 39 4 4 38 61 7 7 85 85 11 5 54 45 16 16 81 81 + 18 3 66 61 21 3 88 68 23 11 79 79 24 24 68 68} + +finish_test From 16e12c573dbf4afb49bb1f59b2634b4acfee168b Mon Sep 17 00:00:00 2001 From: dan Date: Wed, 3 Jul 2019 18:31:20 +0000 Subject: [PATCH 2/9] Add tests for the FILTER clause. And a bugfix. FossilOrigin-Name: 28aa1702f7f0334abd1b30e7aa48ea3679539b11bfbba32bc9f0d6049cf18a7b --- manifest | 21 ++++++-------- manifest.uuid | 2 +- src/resolve.c | 11 +++---- test/filter1.test | 2 +- test/filter2.tcl | 59 +++++++++++++++++++++++++++++++++++++ test/filter2.test | 74 +++++++++++++++++++++++++++++++++++++++++++++++ 6 files changed, 150 insertions(+), 19 deletions(-) diff --git a/manifest b/manifest index e9ba34353b..53814fe073 100644 --- a/manifest +++ b/manifest @@ -1,5 +1,5 @@ -C Experimental\simplementation\sof\sFILTER\sclause\sfor\saggregate\sfunctions. -D 2019-07-02T11:56:47.637 +C Add\stests\sfor\sthe\sFILTER\sclause.\sAnd\sa\sbugfix. +D 2019-07-03T18:31:20.108 F .fossil-settings/empty-dirs dbb81e8fc0401ac46a1491ab34a7f2c7c0452f2f06b54ebb845d024ca8283ef1 F .fossil-settings/ignore-glob 35175cdfcf539b2318cb04a9901442804be81cd677d8b889fcc9149c21f239ea F LICENSE.md df5091916dbb40e6e9686186587125e1b2ff51f022cc334e886c19a0e9982724 @@ -520,7 +520,7 @@ F src/pragma.h 4a9fabff14db4487a734dfeeb4be984ce662bfdccfae16145b9c732327735e13 F src/prepare.c 78027c6231fbb19ca186a5f5f0c0a1375d9c2cec0655273f9bd90d9ff74a34b3 F src/printf.c 9be6945837c839ba57837b4bc3af349eba630920fa5532aa518816defe42a7d4 F src/random.c 80f5d666f23feb3e6665a6ce04c7197212a88384 -F src/resolve.c 65514625b68e79a4087d218107d51d489477509d55543e1be375d1e58f27c2f3 +F src/resolve.c 3c0dc44956a909e135b92e7691e77fb7cf65ab55f03f0439b4e9758b29841fda F src/rowset.c d977b011993aaea002cab3e0bb2ce50cf346000dff94e944d547b989f4b1fe93 F src/select.c 1774cc457eb0b6cd757a350fd9f5600c4d13f3e899d6615033aecaa4f00a5e7d F src/shell.c.in 82f8a473c01662f52233c6c75b9bf88d0d2cab276086d5d4ca6f2ff57a3e48eb @@ -856,9 +856,9 @@ F test/extraquick.test cb254400bd42bfb777ff675356aabf3287978f79 F test/fallocate.test 37a62e396a68eeede8f8d2ecf23573a80faceb630788d314d0a073d862616717 F test/filectrl.test 6e871c2d35dead1d9a88e176e8d2ca094fec6bb3 F test/filefmt.test f393e80c4b8d493b7a7f8f3809a8425bbf4292af1f5140f01cb1427798a2bbd4 -F test/filter1.test d706edd2c32c6b148143d885927e5cf4ea141106b803e243e5d1bfbdc7e2b5cf -F test/filter2.tcl 87e24fd0a2ab502bd4d0350c4eee17a50e2b4364a1b67230f82b8f2a5029f6bf -F test/filter2.test dfddced42a534e71a730aa3d4c6b148d9fbf29aa306f9cec6b51f31b8cf7e224 +F test/filter1.test 8a0f541d2138a10b41c513a9b8193af09d5218ca4e670bc6fd9ed121db1b7bf3 +F test/filter2.tcl 44e525497ce07382915f01bd29ffd0fa49dab3adb87253b5e5103ba8f93393e8 +F test/filter2.test 485cf95d1f6d6ceee5632201ca52a71868599836f430cdee42e5f7f14666e30a F test/fkey1.test d11dbb8a93ead9b5c46ae5d02da016d61245d47662fb2d844c99214f6163f768 F test/fkey2.test d35d1c81e7569bdd2b872e91750f7098117d2e8291369f70b7e3d50a0e523dc2 F test/fkey3.test 76d475c80b84ee7a5d062e56ccb6ea68882e2b49 @@ -1833,10 +1833,7 @@ F vsixtest/vsixtest.tcl 6a9a6ab600c25a91a7acc6293828957a386a8a93 F vsixtest/vsixtest.vcxproj.data 2ed517e100c66dc455b492e1a33350c1b20fbcdc F vsixtest/vsixtest.vcxproj.filters 37e51ffedcdb064aad6ff33b6148725226cd608e F vsixtest/vsixtest_TemporaryKey.pfx e5b1b036facdb453873e7084e1cae9102ccc67a0 -P eab4297577e4d325fed4757867fc77860de7448998d86f098c8a50272e17d35e -R 43e2b0402b61e905810368aafa77ca67 -T *branch * filter-clause -T *sym-filter-clause * -T -sym-trunk * +P 1f1ae2d6ac8dcbb62e5aa3dc17bc67d559cb565fc0d0a8c00a596075d35f8130 +R 384c2722df91a465eadfc4c7f6abdf98 U dan -Z 9e8394b31320bbcccfabf9cff0d64121 +Z 0118bb47bb74c9177a9d95a1278bf2e1 diff --git a/manifest.uuid b/manifest.uuid index 959245f94c..9d6a593943 100644 --- a/manifest.uuid +++ b/manifest.uuid @@ -1 +1 @@ -1f1ae2d6ac8dcbb62e5aa3dc17bc67d559cb565fc0d0a8c00a596075d35f8130 \ No newline at end of file +28aa1702f7f0334abd1b30e7aa48ea3679539b11bfbba32bc9f0d6049cf18a7b \ No newline at end of file diff --git a/src/resolve.c b/src/resolve.c index 0a8aff5652..d65344e2a8 100644 --- a/src/resolve.c +++ b/src/resolve.c @@ -826,22 +826,23 @@ static int resolveExprStep(Walker *pWalker, Expr *pExpr){ if( 0==IN_RENAME_OBJECT ){ #ifndef SQLITE_OMIT_WINDOWFUNC + int is_win = ExprHasProperty(pExpr, EP_WinFunc); assert( is_agg==0 || (pDef->funcFlags & SQLITE_FUNC_MINMAX) || (pDef->xValue==0 && pDef->xInverse==0) || (pDef->xValue && pDef->xInverse && pDef->xSFunc && pDef->xFinalize) ); - if( pDef && pDef->xValue==0 && ExprHasProperty(pExpr, EP_WinFunc) ){ + if( pDef && pDef->xValue==0 && is_win ){ sqlite3ErrorMsg(pParse, "%.*s() may not be used as a window function", nId, zId ); pNC->nErr++; }else if( (is_agg && (pNC->ncFlags & NC_AllowAgg)==0) - || (is_agg && (pDef->funcFlags&SQLITE_FUNC_WINDOW) && !pExpr->y.pWin) - || (is_agg && pExpr->y.pWin && (pNC->ncFlags & NC_AllowWin)==0) + || (is_agg && (pDef->funcFlags&SQLITE_FUNC_WINDOW) && !is_win) + || (is_agg && is_win && (pNC->ncFlags & NC_AllowWin)==0) ){ const char *zType; - if( (pDef->funcFlags & SQLITE_FUNC_WINDOW) || pExpr->y.pWin ){ + if( (pDef->funcFlags & SQLITE_FUNC_WINDOW) || is_win ){ zType = "window"; }else{ zType = "aggregate"; @@ -880,7 +881,7 @@ static int resolveExprStep(Walker *pWalker, Expr *pExpr){ ** Or arguments of other window functions. But aggregate functions ** may be arguments for window functions. */ #ifndef SQLITE_OMIT_WINDOWFUNC - pNC->ncFlags &= ~(NC_AllowWin | (!pExpr->y.pWin ? NC_AllowAgg : 0)); + pNC->ncFlags &= ~(NC_AllowWin | (!is_win ? NC_AllowAgg : 0)); #else pNC->ncFlags &= ~NC_AllowAgg; #endif diff --git a/test/filter1.test b/test/filter1.test index 2f5a5b56d4..8c0c14d87f 100644 --- a/test/filter1.test +++ b/test/filter1.test @@ -83,7 +83,7 @@ do_catchsql_test 2.2 { do_catchsql_test 2.3 { SELECT sum(a) FILTER (WHERE 1 - count(a)) FROM t1 -} {1 {misuse of aggregate: count()}} +} {1 {misuse of aggregate function count()}} finish_test diff --git a/test/filter2.tcl b/test/filter2.tcl index 0a7e2a3dd0..f3871f80bc 100644 --- a/test/filter2.tcl +++ b/test/filter2.tcl @@ -68,6 +68,65 @@ execsql_test 1.7 { ORDER BY 1, 2, 3, 4; } +execsql_test 1.8 { + SELECT sum(a+b) FILTER (WHERE a=NULL) FROM t1 +} + +execsql_test 1.9 { + SELECT (a%5) FROM t1 GROUP BY (a%5) + HAVING sum(b) FILTER (WHERE b<20) > 34 + ORDER BY 1 +} + +execsql_test 1.10 { + SELECT (a%5), sum(b) FILTER (WHERE b<20) AS bbb + FROM t1 + GROUP BY (a%5) HAVING sum(b) FILTER (WHERE b<20) >34 + ORDER BY 1 +} + +execsql_test 1.11 { + SELECT (a%5), sum(b) FILTER (WHERE b<20) AS bbb + FROM t1 + GROUP BY (a%5) HAVING sum(b) FILTER (WHERE b<20) >34 + ORDER BY 2 +} + +execsql_test 1.12 { + SELECT (a%5), + sum(b) FILTER (WHERE b<20) AS bbb, + count(distinct b) FILTER (WHERE b<20 OR a=13) AS ccc + FROM t1 GROUP BY (a%5) + ORDER BY 2 +} + +execsql_test 1.13 { + SELECT + string_agg(CAST(b AS TEXT), '_') FILTER (WHERE b%2!=0), + string_agg(CAST(b AS TEXT), '_') FILTER (WHERE b%2!=1), + count(*) FILTER (WHERE b%2!=0), + count(*) FILTER (WHERE b%2!=1) + FROM t1; +} + +execsql_float_test 1.14 { + SELECT + avg(b) FILTER (WHERE b>a), + avg(b) FILTER (WHERE b 34 + ORDER BY 1 +} {3 4} + +do_execsql_test 1.10 { + SELECT (a%5), sum(b) FILTER (WHERE b<20) AS bbb + FROM t1 + GROUP BY (a%5) HAVING sum(b) FILTER (WHERE b<20) >34 + ORDER BY 1 +} {3 49 4 46} + +do_execsql_test 1.11 { + SELECT (a%5), sum(b) FILTER (WHERE b<20) AS bbb + FROM t1 + GROUP BY (a%5) HAVING sum(b) FILTER (WHERE b<20) >34 + ORDER BY 2 +} {4 46 3 49} + +do_execsql_test 1.12 { + SELECT (a%5), + sum(b) FILTER (WHERE b<20) AS bbb, + count(distinct b) FILTER (WHERE b<20 OR a=13) AS ccc + FROM t1 GROUP BY (a%5) + ORDER BY 2 +} {2 25 3 0 34 2 1 34 4 4 46 4 3 49 5} + +do_execsql_test 1.13 { + SELECT + group_concat(CAST(b AS TEXT), '_') FILTER (WHERE b%2!=0), + group_concat(CAST(b AS TEXT), '_') FILTER (WHERE b%2!=1), + count(*) FILTER (WHERE b%2!=0), + count(*) FILTER (WHERE b%2!=1) + FROM t1; +} {7_3_5_23_27_3_17_33_25_47_13_45_31_11_37_21_3_7_29_3_3_23_5_11_25_15_23 30_26_26_36_36_22_14_16_50_38_36_12_4_46_48_24_46_18_18 27 19} + + +do_test 1.14 { + set myres {} + foreach r [db eval {SELECT + avg(b) FILTER (WHERE b>a), + avg(b) FILTER (WHERE b([set r2]+0.0001)} { + error "list element [set i] does not match: got=[set r] expected=[set r2]" + } + incr i + } + set {} {} +} {} + +do_execsql_test 1.15 { + SELECT + a/5, + sum(b) FILTER (WHERE a%5=0), + sum(b) FILTER (WHERE a%5=1), + sum(b) FILTER (WHERE a%5=2), + sum(b) FILTER (WHERE a%5=3), + sum(b) FILTER (WHERE a%5=4) + FROM t1 GROUP BY (a/5) ORDER BY 1; +} {0 {} 7 3 5 30 1 26 23 27 3 17 2 26 33 25 {} 47 3 36 13 45 31 11 + 4 36 37 21 22 14 5 16 3 7 29 50 6 38 3 36 12 4 7 46 3 48 23 {} + 8 24 5 46 11 {} 9 18 25 15 18 23} + finish_test From b28c4e564ab4ac8603f28e662a3a591a40a069b2 Mon Sep 17 00:00:00 2001 From: dan Date: Fri, 5 Jul 2019 17:38:55 +0000 Subject: [PATCH 3/9] Minor tweak to patch on this branch to reclaim some cycles. FossilOrigin-Name: 81eed055de856068109a7e332cca0c3ff1120ebe2bfa5ed6383b165c726e0a7c --- manifest | 16 ++++++++-------- manifest.uuid | 2 +- src/select.c | 4 +++- test/window2.tcl | 4 ++++ test/window2.test | 4 ++++ 5 files changed, 20 insertions(+), 10 deletions(-) diff --git a/manifest b/manifest index 53814fe073..5ba4ac2d79 100644 --- a/manifest +++ b/manifest @@ -1,5 +1,5 @@ -C Add\stests\sfor\sthe\sFILTER\sclause.\sAnd\sa\sbugfix. -D 2019-07-03T18:31:20.108 +C Minor\stweak\sto\spatch\son\sthis\sbranch\sto\sreclaim\ssome\scycles. +D 2019-07-05T17:38:55.826 F .fossil-settings/empty-dirs dbb81e8fc0401ac46a1491ab34a7f2c7c0452f2f06b54ebb845d024ca8283ef1 F .fossil-settings/ignore-glob 35175cdfcf539b2318cb04a9901442804be81cd677d8b889fcc9149c21f239ea F LICENSE.md df5091916dbb40e6e9686186587125e1b2ff51f022cc334e886c19a0e9982724 @@ -522,7 +522,7 @@ F src/printf.c 9be6945837c839ba57837b4bc3af349eba630920fa5532aa518816defe42a7d4 F src/random.c 80f5d666f23feb3e6665a6ce04c7197212a88384 F src/resolve.c 3c0dc44956a909e135b92e7691e77fb7cf65ab55f03f0439b4e9758b29841fda F src/rowset.c d977b011993aaea002cab3e0bb2ce50cf346000dff94e944d547b989f4b1fe93 -F src/select.c 1774cc457eb0b6cd757a350fd9f5600c4d13f3e899d6615033aecaa4f00a5e7d +F src/select.c 696107b0e89df923a1991fb638454767258bd67959249da09badfa6c94b4bd9e F src/shell.c.in 82f8a473c01662f52233c6c75b9bf88d0d2cab276086d5d4ca6f2ff57a3e48eb F src/sqlite.h.in bc4aa09a59dcf3c34b8e57ce9ade8ff6aed8ef7a7ddc59951f59f9b009fc7cc0 F src/sqlite3.rc 5121c9e10c3964d5755191c80dd1180c122fc3a8 @@ -1699,8 +1699,8 @@ F test/win32lock.test fbf107c91d8f5512be5a5b87c4c42ab9fdd54972 F test/win32longpath.test 169c75a3b2e43481f4a62122510210c67b08f26d F test/win32nolock.test ac4f08811a562e45a5755e661f45ca85892bdbbc F test/window1.test 8d453bfaa3f8f0873ba16ca1270c7368f18445065a0003a1b5954ac4e95797b4 -F test/window2.tcl 0c2918ef2a1640553fd791972d458356808a608418c64c02a0a379cecfc7fb0d -F test/window2.test 96ef949f0197c025652f6c6e5812cdbfb948989bd40cf79cbb02104249a89513 +F test/window2.tcl a0de5e1e50538a9c491d894474d4708bbb2a3c043107ac5e05be7de99373a237 +F test/window2.test d7236a84c9104319fb0980a73565559d5470beb65d11136c5c1356d67de299c9 F test/window3.tcl acea6e86a4324a210fd608d06741010ca83ded9fde438341cb978c49928faf03 F test/window3.test e9959a993c8a71e96433be8daaa1827d78b8921e4f12debd7bdbeb3c856ef3cb F test/window4.tcl 5fbaab489677914ee5686b2008426e336daf88a2f58be7df92757f780a5ebf91 @@ -1833,7 +1833,7 @@ F vsixtest/vsixtest.tcl 6a9a6ab600c25a91a7acc6293828957a386a8a93 F vsixtest/vsixtest.vcxproj.data 2ed517e100c66dc455b492e1a33350c1b20fbcdc F vsixtest/vsixtest.vcxproj.filters 37e51ffedcdb064aad6ff33b6148725226cd608e F vsixtest/vsixtest_TemporaryKey.pfx e5b1b036facdb453873e7084e1cae9102ccc67a0 -P 1f1ae2d6ac8dcbb62e5aa3dc17bc67d559cb565fc0d0a8c00a596075d35f8130 -R 384c2722df91a465eadfc4c7f6abdf98 +P 28aa1702f7f0334abd1b30e7aa48ea3679539b11bfbba32bc9f0d6049cf18a7b +R 32352038f0215cf6311f9df6007b16f8 U dan -Z 0118bb47bb74c9177a9d95a1278bf2e1 +Z 0e8a7c5368f99c56c1c2d766fdc13e32 diff --git a/manifest.uuid b/manifest.uuid index 9d6a593943..4b6312c88e 100644 --- a/manifest.uuid +++ b/manifest.uuid @@ -1 +1 @@ -28aa1702f7f0334abd1b30e7aa48ea3679539b11bfbba32bc9f0d6049cf18a7b \ No newline at end of file +81eed055de856068109a7e332cca0c3ff1120ebe2bfa5ed6383b165c726e0a7c \ No newline at end of file diff --git a/src/select.c b/src/select.c index e5f57fede6..e6471fb848 100644 --- a/src/select.c +++ b/src/select.c @@ -6240,7 +6240,9 @@ int sqlite3Select( sqlite3ExprAnalyzeAggList(&sNC, pExpr->x.pList); #ifndef SQLITE_OMIT_WINDOWFUNC assert( !ExprHasProperty(pExpr, EP_WinFunc) ); - sqlite3ExprAnalyzeAggregates(&sNC, pExpr->y.pFilter); + if( pExpr->y.pFilter ){ + sqlite3ExprAnalyzeAggregates(&sNC, pExpr->y.pFilter); + } #endif sNC.ncFlags &= ~NC_InAggFunc; } diff --git a/test/window2.tcl b/test/window2.tcl index 093d693b62..3f781a7252 100644 --- a/test/window2.tcl +++ b/test/window2.tcl @@ -424,6 +424,10 @@ execsql_float_test 4.9 { WINDOW win AS (ORDER BY 1); } +execsql_test 4.10 { + SELECT count(*) OVER (ORDER BY b) FROM t1 +} + finish_test diff --git a/test/window2.test b/test/window2.test index 4bed0cdcc9..26370e24d2 100644 --- a/test/window2.test +++ b/test/window2.test @@ -893,4 +893,8 @@ do_test 4.9 { set {} {} } {} +do_execsql_test 4.10 { + SELECT count(*) OVER (ORDER BY b) FROM t1 +} {3 3 3 6 6 6} + finish_test From 1efcc9dd965c5b48a1a9e27ce760b38f9d892cb4 Mon Sep 17 00:00:00 2001 From: dan Date: Fri, 5 Jul 2019 19:10:41 +0000 Subject: [PATCH 4/9] Another very small performance improvement. FossilOrigin-Name: 7a1e30a17f57ca006dd84b6f97b0c7811bf4c6da4b02903452ffc4bc363cab9b --- manifest | 16 ++++++++-------- manifest.uuid | 2 +- src/prepare.c | 6 +++--- test/window2.tcl | 3 +++ test/window2.test | 4 ++++ 5 files changed, 19 insertions(+), 12 deletions(-) diff --git a/manifest b/manifest index 5ba4ac2d79..3cf1f3b315 100644 --- a/manifest +++ b/manifest @@ -1,5 +1,5 @@ -C Minor\stweak\sto\spatch\son\sthis\sbranch\sto\sreclaim\ssome\scycles. -D 2019-07-05T17:38:55.826 +C Another\svery\ssmall\sperformance\simprovement. +D 2019-07-05T19:10:41.614 F .fossil-settings/empty-dirs dbb81e8fc0401ac46a1491ab34a7f2c7c0452f2f06b54ebb845d024ca8283ef1 F .fossil-settings/ignore-glob 35175cdfcf539b2318cb04a9901442804be81cd677d8b889fcc9149c21f239ea F LICENSE.md df5091916dbb40e6e9686186587125e1b2ff51f022cc334e886c19a0e9982724 @@ -517,7 +517,7 @@ F src/pcache.h 4f87acd914cef5016fae3030343540d75f5b85a1877eed1a2a19b9f284248586 F src/pcache1.c be64b2f3908a7f97c56c963676eb12f0d6254c95b28cdc1d73a186eff213219d F src/pragma.c 925bcac0afd98a0d4255c7524b93239ab4d83893d96f7f8fdccd78d4929a39bb F src/pragma.h 4a9fabff14db4487a734dfeeb4be984ce662bfdccfae16145b9c732327735e13 -F src/prepare.c 78027c6231fbb19ca186a5f5f0c0a1375d9c2cec0655273f9bd90d9ff74a34b3 +F src/prepare.c d633c3610c23dc98239ea445803c2a6188c2b8ab647e3df4243f369516e6c8d1 F src/printf.c 9be6945837c839ba57837b4bc3af349eba630920fa5532aa518816defe42a7d4 F src/random.c 80f5d666f23feb3e6665a6ce04c7197212a88384 F src/resolve.c 3c0dc44956a909e135b92e7691e77fb7cf65ab55f03f0439b4e9758b29841fda @@ -1699,8 +1699,8 @@ F test/win32lock.test fbf107c91d8f5512be5a5b87c4c42ab9fdd54972 F test/win32longpath.test 169c75a3b2e43481f4a62122510210c67b08f26d F test/win32nolock.test ac4f08811a562e45a5755e661f45ca85892bdbbc F test/window1.test 8d453bfaa3f8f0873ba16ca1270c7368f18445065a0003a1b5954ac4e95797b4 -F test/window2.tcl a0de5e1e50538a9c491d894474d4708bbb2a3c043107ac5e05be7de99373a237 -F test/window2.test d7236a84c9104319fb0980a73565559d5470beb65d11136c5c1356d67de299c9 +F test/window2.tcl a44834af6267be6e14882311ef8790bf5047f1b9bc25685ee1762d48dc24f1e7 +F test/window2.test 2f3ae63e171dce7f2ac29a41020f4da413b1d7bdaba48ee124fd336b152e34c5 F test/window3.tcl acea6e86a4324a210fd608d06741010ca83ded9fde438341cb978c49928faf03 F test/window3.test e9959a993c8a71e96433be8daaa1827d78b8921e4f12debd7bdbeb3c856ef3cb F test/window4.tcl 5fbaab489677914ee5686b2008426e336daf88a2f58be7df92757f780a5ebf91 @@ -1833,7 +1833,7 @@ F vsixtest/vsixtest.tcl 6a9a6ab600c25a91a7acc6293828957a386a8a93 F vsixtest/vsixtest.vcxproj.data 2ed517e100c66dc455b492e1a33350c1b20fbcdc F vsixtest/vsixtest.vcxproj.filters 37e51ffedcdb064aad6ff33b6148725226cd608e F vsixtest/vsixtest_TemporaryKey.pfx e5b1b036facdb453873e7084e1cae9102ccc67a0 -P 28aa1702f7f0334abd1b30e7aa48ea3679539b11bfbba32bc9f0d6049cf18a7b -R 32352038f0215cf6311f9df6007b16f8 +P 81eed055de856068109a7e332cca0c3ff1120ebe2bfa5ed6383b165c726e0a7c +R 46b156a94f9cfdf367fb299674af587d U dan -Z 0e8a7c5368f99c56c1c2d766fdc13e32 +Z 61e3b9e66b66516dc5ac189aabd81a74 diff --git a/manifest.uuid b/manifest.uuid index 4b6312c88e..9481a3b9f0 100644 --- a/manifest.uuid +++ b/manifest.uuid @@ -1 +1 @@ -81eed055de856068109a7e332cca0c3ff1120ebe2bfa5ed6383b165c726e0a7c \ No newline at end of file +7a1e30a17f57ca006dd84b6f97b0c7811bf4c6da4b02903452ffc4bc363cab9b \ No newline at end of file diff --git a/src/prepare.c b/src/prepare.c index 3f1a79b14b..f385c1fc71 100644 --- a/src/prepare.c +++ b/src/prepare.c @@ -635,7 +635,7 @@ static int sqlite3Prepare( rc = sParse.rc; #ifndef SQLITE_OMIT_EXPLAIN - if( rc==SQLITE_OK && sParse.pVdbe && sParse.explain ){ + if( sParse.explain && rc==SQLITE_OK && sParse.pVdbe ){ static const char * const azColName[] = { "addr", "opcode", "p1", "p2", "p3", "p4", "p5", "comment", "id", "parent", "notused", "detail" @@ -660,8 +660,8 @@ static int sqlite3Prepare( if( db->init.busy==0 ){ sqlite3VdbeSetSql(sParse.pVdbe, zSql, (int)(sParse.zTail-zSql), prepFlags); } - if( sParse.pVdbe && (rc!=SQLITE_OK || db->mallocFailed) ){ - sqlite3VdbeFinalize(sParse.pVdbe); + if( rc!=SQLITE_OK || db->mallocFailed ){ + if( sParse.pVdbe ) sqlite3VdbeFinalize(sParse.pVdbe); assert(!(*ppStmt)); }else{ *ppStmt = (sqlite3_stmt*)sParse.pVdbe; diff --git a/test/window2.tcl b/test/window2.tcl index 3f781a7252..60bf68196c 100644 --- a/test/window2.tcl +++ b/test/window2.tcl @@ -428,6 +428,9 @@ execsql_test 4.10 { SELECT count(*) OVER (ORDER BY b) FROM t1 } +execsql_test 4.11 { + SELECT count(distinct a) FILTER (WHERE b='odd') FROM t1 +} finish_test diff --git a/test/window2.test b/test/window2.test index 26370e24d2..c4358ded74 100644 --- a/test/window2.test +++ b/test/window2.test @@ -897,4 +897,8 @@ do_execsql_test 4.10 { SELECT count(*) OVER (ORDER BY b) FROM t1 } {3 3 3 6 6 6} +do_execsql_test 4.11 { + SELECT count(distinct a) FILTER (WHERE b='odd') FROM t1 +} {3} + finish_test From 8117f113bc0468d4c955ffa3f04cea26a39a3bd3 Mon Sep 17 00:00:00 2001 From: dan Date: Wed, 10 Jul 2019 20:16:53 +0000 Subject: [PATCH 5/9] Minor performance improvement in sqlite3ExprDeleteNN(). FossilOrigin-Name: bcc8b38ac75b731a4cd2873ab83f423be036467a511b617c779869de9bbb5383 --- manifest | 16 ++++++++-------- manifest.uuid | 2 +- src/expr.c | 20 ++++++++++++-------- src/resolve.c | 12 ++++++------ src/walker.c | 23 ++++++++++++++--------- 5 files changed, 41 insertions(+), 32 deletions(-) diff --git a/manifest b/manifest index 3cf1f3b315..07f6d39ac5 100644 --- a/manifest +++ b/manifest @@ -1,5 +1,5 @@ -C Another\svery\ssmall\sperformance\simprovement. -D 2019-07-05T19:10:41.614 +C Minor\sperformance\simprovement\sin\ssqlite3ExprDeleteNN(). +D 2019-07-10T20:16:53.830 F .fossil-settings/empty-dirs dbb81e8fc0401ac46a1491ab34a7f2c7c0452f2f06b54ebb845d024ca8283ef1 F .fossil-settings/ignore-glob 35175cdfcf539b2318cb04a9901442804be81cd677d8b889fcc9149c21f239ea F LICENSE.md df5091916dbb40e6e9686186587125e1b2ff51f022cc334e886c19a0e9982724 @@ -474,7 +474,7 @@ F src/date.c e1d8ac7102f3f283e63e13867acb0efa33861cf34f0faf4cdbaf9fa7a1eb7041 F src/dbpage.c 135eb3b5e74f9ef74bde5cec2571192c90c86984fa534c88bf4a055076fa19b7 F src/dbstat.c c12833de69cb655751487d2c5a59607e36be1c58ba1f4bd536609909ad47b319 F src/delete.c d08c9e01a2664afd12edcfa3a9c6578517e8ff8735f35509582693adbe0edeaf -F src/expr.c 33267aaf04145af9f1f06c1f4c10f8e7a9acce1c681d75526e6a3fa7e58c1d79 +F src/expr.c 01bbd69f29936bba85dba6188a409c1e21a0c260bf04b9fc78d5e9702a77acb5 F src/fault.c 460f3e55994363812d9d60844b2a6de88826e007 F src/fkey.c 0e14d4bef8eac2d87bbd517e492d9084c65008d117823f8922c5e7b2b599bd33 F src/func.c 905c0424900979fade3731b4a271592059425c988cb7d5b245032253712dd7c8 @@ -520,7 +520,7 @@ F src/pragma.h 4a9fabff14db4487a734dfeeb4be984ce662bfdccfae16145b9c732327735e13 F src/prepare.c d633c3610c23dc98239ea445803c2a6188c2b8ab647e3df4243f369516e6c8d1 F src/printf.c 9be6945837c839ba57837b4bc3af349eba630920fa5532aa518816defe42a7d4 F src/random.c 80f5d666f23feb3e6665a6ce04c7197212a88384 -F src/resolve.c 3c0dc44956a909e135b92e7691e77fb7cf65ab55f03f0439b4e9758b29841fda +F src/resolve.c 138c9e9d8828f199709bd7c970d98aaff47a21d33c6c42ffb6e1e809620fcc9e F src/rowset.c d977b011993aaea002cab3e0bb2ce50cf346000dff94e944d547b989f4b1fe93 F src/select.c 696107b0e89df923a1991fb638454767258bd67959249da09badfa6c94b4bd9e F src/shell.c.in 82f8a473c01662f52233c6c75b9bf88d0d2cab276086d5d4ca6f2ff57a3e48eb @@ -607,7 +607,7 @@ F src/vtab.c 1fa256c6ddad7a81e2a4dc080d015d4b0a7135767717d311298e47f6fca64bb3 F src/vxworks.h d2988f4e5a61a4dfe82c6524dd3d6e4f2ce3cdb9 F src/wal.c b09a2a9cab50efa08451a8c81d47052120ad5da174048c6d0b08d405384abdf2 F src/wal.h 606292549f5a7be50b6227bd685fa76e3a4affad71bb8ac5ce4cb5c79f6a176a -F src/walker.c 31844651eacdc408474c62df526f20d5ea7994b5b8ffcd23eed5dc8f688c26f5 +F src/walker.c 588e7f5faf30d2e68aa9b176258bb3225d0553171b998b06b69ed74a0528e986 F src/where.c 2f11eeb14335b7640f886b2fb441f54a94c35ab5cde8b53461a1074bfd587081 F src/whereInt.h 1b728f71654ebf8421a1715497a587f02d6f538e819af58dc826908f8577e810 F src/wherecode.c 37a1004237d630d785c47bba2290eac652a7a8b0047518eba3cb7c808b604c4a @@ -1833,7 +1833,7 @@ F vsixtest/vsixtest.tcl 6a9a6ab600c25a91a7acc6293828957a386a8a93 F vsixtest/vsixtest.vcxproj.data 2ed517e100c66dc455b492e1a33350c1b20fbcdc F vsixtest/vsixtest.vcxproj.filters 37e51ffedcdb064aad6ff33b6148725226cd608e F vsixtest/vsixtest_TemporaryKey.pfx e5b1b036facdb453873e7084e1cae9102ccc67a0 -P 81eed055de856068109a7e332cca0c3ff1120ebe2bfa5ed6383b165c726e0a7c -R 46b156a94f9cfdf367fb299674af587d +P 7a1e30a17f57ca006dd84b6f97b0c7811bf4c6da4b02903452ffc4bc363cab9b +R 6bd4ab9e9ad32527f5b65731f0494801 U dan -Z 61e3b9e66b66516dc5ac189aabd81a74 +Z 7ff155988343772e802f3ddf15426065 diff --git a/manifest.uuid b/manifest.uuid index 9481a3b9f0..5793e74a4c 100644 --- a/manifest.uuid +++ b/manifest.uuid @@ -1 +1 @@ -7a1e30a17f57ca006dd84b6f97b0c7811bf4c6da4b02903452ffc4bc363cab9b \ No newline at end of file +bcc8b38ac75b731a4cd2873ab83f423be036467a511b617c779869de9bbb5383 \ No newline at end of file diff --git a/src/expr.c b/src/expr.c index 75d0dbe9da..6fbeb827fb 100644 --- a/src/expr.c +++ b/src/expr.c @@ -1040,21 +1040,25 @@ static SQLITE_NOINLINE void sqlite3ExprDeleteNN(sqlite3 *db, Expr *p){ assert( p->x.pList==0 || p->pRight==0 ); if( p->pLeft && p->op!=TK_SELECT_COLUMN ) sqlite3ExprDeleteNN(db, p->pLeft); if( p->pRight ){ + assert( !ExprHasProperty(p, (EP_WinFunc|EP_Filter)) ); sqlite3ExprDeleteNN(db, p->pRight); }else if( ExprHasProperty(p, EP_xIsSelect) ){ + assert( !ExprHasProperty(p, (EP_WinFunc|EP_Filter)) ); sqlite3SelectDelete(db, p->x.pSelect); }else{ sqlite3ExprListDelete(db, p->x.pList); - } #ifndef SQLITE_OMIT_WINDOWFUNC - if( ExprHasProperty(p, EP_WinFunc) ){ - assert( p->op==TK_FUNCTION && !ExprHasProperty(p, EP_Filter) ); - sqlite3WindowDelete(db, p->y.pWin); - }else if( ExprHasProperty(p, EP_Filter) ){ - assert( p->op==TK_FUNCTION || p->op==TK_AGG_FUNCTION ); - sqlite3ExprDelete(db, p->y.pFilter); - } + if( ExprHasProperty(p, (EP_WinFunc|EP_Filter)) ){ + if( ExprHasProperty(p, EP_WinFunc) ){ + assert( p->op==TK_FUNCTION && !ExprHasProperty(p, EP_Filter) ); + sqlite3WindowDelete(db, p->y.pWin); + }else{ + assert( p->op==TK_FUNCTION || p->op==TK_AGG_FUNCTION ); + sqlite3ExprDeleteNN(db, p->y.pFilter); + } + } #endif + } } if( ExprHasProperty(p, EP_MemToken) ) sqlite3DbFree(db, p->u.zToken); if( !ExprHasProperty(p, EP_Static) ){ diff --git a/src/resolve.c b/src/resolve.c index d65344e2a8..37aaa92606 100644 --- a/src/resolve.c +++ b/src/resolve.c @@ -826,23 +826,23 @@ static int resolveExprStep(Walker *pWalker, Expr *pExpr){ if( 0==IN_RENAME_OBJECT ){ #ifndef SQLITE_OMIT_WINDOWFUNC - int is_win = ExprHasProperty(pExpr, EP_WinFunc); + Window *pWin = (ExprHasProperty(pExpr, EP_WinFunc) ? pExpr->y.pWin : 0); assert( is_agg==0 || (pDef->funcFlags & SQLITE_FUNC_MINMAX) || (pDef->xValue==0 && pDef->xInverse==0) || (pDef->xValue && pDef->xInverse && pDef->xSFunc && pDef->xFinalize) ); - if( pDef && pDef->xValue==0 && is_win ){ + if( pDef && pDef->xValue==0 && pWin ){ sqlite3ErrorMsg(pParse, "%.*s() may not be used as a window function", nId, zId ); pNC->nErr++; }else if( (is_agg && (pNC->ncFlags & NC_AllowAgg)==0) - || (is_agg && (pDef->funcFlags&SQLITE_FUNC_WINDOW) && !is_win) - || (is_agg && is_win && (pNC->ncFlags & NC_AllowWin)==0) + || (is_agg && (pDef->funcFlags&SQLITE_FUNC_WINDOW) && !pWin) + || (is_agg && pWin && (pNC->ncFlags & NC_AllowWin)==0) ){ const char *zType; - if( (pDef->funcFlags & SQLITE_FUNC_WINDOW) || is_win ){ + if( (pDef->funcFlags & SQLITE_FUNC_WINDOW) || pWin ){ zType = "window"; }else{ zType = "aggregate"; @@ -881,7 +881,7 @@ static int resolveExprStep(Walker *pWalker, Expr *pExpr){ ** Or arguments of other window functions. But aggregate functions ** may be arguments for window functions. */ #ifndef SQLITE_OMIT_WINDOWFUNC - pNC->ncFlags &= ~(NC_AllowWin | (!is_win ? NC_AllowAgg : 0)); + pNC->ncFlags &= ~(NC_AllowWin | (!pWin ? NC_AllowAgg : 0)); #else pNC->ncFlags &= ~NC_AllowAgg; #endif diff --git a/src/walker.c b/src/walker.c index 766a345add..44e3d18363 100644 --- a/src/walker.c +++ b/src/walker.c @@ -63,21 +63,26 @@ static SQLITE_NOINLINE int walkExpr(Walker *pWalker, Expr *pExpr){ if( pExpr->pLeft && walkExpr(pWalker, pExpr->pLeft) ) return WRC_Abort; assert( pExpr->x.pList==0 || pExpr->pRight==0 ); if( pExpr->pRight ){ + assert( !ExprHasProperty(pExpr, EP_WinFunc|EP_Filter) ); pExpr = pExpr->pRight; continue; }else if( ExprHasProperty(pExpr, EP_xIsSelect) ){ + assert( !ExprHasProperty(pExpr, EP_WinFunc|EP_Filter) ); if( sqlite3WalkSelect(pWalker, pExpr->x.pSelect) ) return WRC_Abort; - }else if( pExpr->x.pList ){ - if( sqlite3WalkExprList(pWalker, pExpr->x.pList) ) return WRC_Abort; - } + }else{ + if( pExpr->x.pList ){ + if( sqlite3WalkExprList(pWalker, pExpr->x.pList) ) return WRC_Abort; + } #ifndef SQLITE_OMIT_WINDOWFUNC - if( ExprHasProperty(pExpr, EP_WinFunc) ){ - if( walkWindowList(pWalker, pExpr->y.pWin) ) return WRC_Abort; - } - if( ExprHasProperty(pExpr, EP_Filter) ){ - if( walkExpr(pWalker, pExpr->y.pFilter) ) return WRC_Abort; - } + if( ExprHasProperty(pExpr, EP_WinFunc|EP_Filter) ){ + if( ExprHasProperty(pExpr, EP_WinFunc) ){ + if( walkWindowList(pWalker, pExpr->y.pWin) ) return WRC_Abort; + }else if( ExprHasProperty(pExpr, EP_Filter) ){ + if( walkExpr(pWalker, pExpr->y.pFilter) ) return WRC_Abort; + } + } #endif + } } break; } From 6488271f055df2c2f403883cb8eb6ad459da2222 Mon Sep 17 00:00:00 2001 From: dan Date: Thu, 11 Jul 2019 18:43:33 +0000 Subject: [PATCH 6/9] Change the parser on this branch to more closely match trunk. This saves a few more cycles. FossilOrigin-Name: be01b801fbc258f0ab9542130cec12a5ea5e2d0bf087684a9eda909ad459c211 --- manifest | 12 ++++++------ manifest.uuid | 2 +- src/parse.y | 52 +++++++++++++++++++++++++++++++++++---------------- 3 files changed, 43 insertions(+), 23 deletions(-) diff --git a/manifest b/manifest index 07f6d39ac5..8bde9a3060 100644 --- a/manifest +++ b/manifest @@ -1,5 +1,5 @@ -C Minor\sperformance\simprovement\sin\ssqlite3ExprDeleteNN(). -D 2019-07-10T20:16:53.830 +C Change\sthe\sparser\son\sthis\sbranch\sto\smore\sclosely\smatch\strunk.\sThis\ssaves\sa\sfew\smore\scycles. +D 2019-07-11T18:43:33.538 F .fossil-settings/empty-dirs dbb81e8fc0401ac46a1491ab34a7f2c7c0452f2f06b54ebb845d024ca8283ef1 F .fossil-settings/ignore-glob 35175cdfcf539b2318cb04a9901442804be81cd677d8b889fcc9149c21f239ea F LICENSE.md df5091916dbb40e6e9686186587125e1b2ff51f022cc334e886c19a0e9982724 @@ -511,7 +511,7 @@ F src/os_win.c 85d9e532d0444ab6c16d7431490c2e279e282aa0917b0e988996b1ae0de5c5a0 F src/os_win.h 7b073010f1451abe501be30d12f6bc599824944a F src/pager.c 422fd8cfa59fb9173eff36a95878904a0eeb0dcc62ba49350acc8b1e51c4dc7b F src/pager.h 217921e81eb5fe455caa5cda96061959706bcdd29ddb57166198645ef7822ac3 -F src/parse.y 2902f393b08e33f4af42e2b63f943be62f5de096138461459de60084f598a056 +F src/parse.y 6f2e8ec7df1e4d4ab57c3c4f9e4815b13a78357685a917a512d6f3d949d8c435 F src/pcache.c fd2d0553b3222d6b9f7cb251079e5bca1299d1161da3027b525932d8bf46340a F src/pcache.h 4f87acd914cef5016fae3030343540d75f5b85a1877eed1a2a19b9f284248586 F src/pcache1.c be64b2f3908a7f97c56c963676eb12f0d6254c95b28cdc1d73a186eff213219d @@ -1833,7 +1833,7 @@ F vsixtest/vsixtest.tcl 6a9a6ab600c25a91a7acc6293828957a386a8a93 F vsixtest/vsixtest.vcxproj.data 2ed517e100c66dc455b492e1a33350c1b20fbcdc F vsixtest/vsixtest.vcxproj.filters 37e51ffedcdb064aad6ff33b6148725226cd608e F vsixtest/vsixtest_TemporaryKey.pfx e5b1b036facdb453873e7084e1cae9102ccc67a0 -P 7a1e30a17f57ca006dd84b6f97b0c7811bf4c6da4b02903452ffc4bc363cab9b -R 6bd4ab9e9ad32527f5b65731f0494801 +P bcc8b38ac75b731a4cd2873ab83f423be036467a511b617c779869de9bbb5383 +R 7366949ada14c091807ed4dad610ae76 U dan -Z 7ff155988343772e802f3ddf15426065 +Z 9f13574d0ae5d3c1b08c0a4ecde014da diff --git a/manifest.uuid b/manifest.uuid index 5793e74a4c..0fa6ef7732 100644 --- a/manifest.uuid +++ b/manifest.uuid @@ -1 +1 @@ -bcc8b38ac75b731a4cd2873ab83f423be036467a511b617c779869de9bbb5383 \ No newline at end of file +be01b801fbc258f0ab9542130cec12a5ea5e2d0bf087684a9eda909ad459c211 \ No newline at end of file diff --git a/src/parse.y b/src/parse.y index dda9f627aa..3cef3e5e6b 100644 --- a/src/parse.y +++ b/src/parse.y @@ -1036,23 +1036,31 @@ expr(A) ::= CAST LP expr(E) AS typetoken(T) RP. { %endif SQLITE_OMIT_CAST -%ifdef SQLITE_OMIT_WINDOWFUNC expr(A) ::= id(X) LP distinct(D) exprlist(Y) RP. { A = sqlite3ExprFunction(pParse, Y, &X, D); } expr(A) ::= id(X) LP STAR RP. { A = sqlite3ExprFunction(pParse, 0, &X, 0); } -%endif %ifndef SQLITE_OMIT_WINDOWFUNC -expr(A) ::= id(X) LP distinct(D) exprlist(Y) RP filter_opt(F) over_opt(Z). { - A = sqlite3ExprFunction(pParse, Y, &X, D); - sqlite3WindowAttach(pParse, A, F, Z); +%type filter_over { + struct FunctionTail { + Window *pWin; + Expr *pFilter; + } } -expr(A) ::= id(X) LP STAR RP filter_opt(F) over_opt(Z). { +%destructor filter_over { + sqlite3WindowDelete(pParse->db, $$.pWin); + sqlite3ExprDelete(pParse->db, $$.pFilter); +} +expr(A) ::= id(X) LP distinct(D) exprlist(Y) RP filter_over(F). { + A = sqlite3ExprFunction(pParse, Y, &X, D); + sqlite3WindowAttach(pParse, A, F.pFilter, F.pWin); +} +expr(A) ::= id(X) LP STAR RP filter_over(F). { A = sqlite3ExprFunction(pParse, 0, &X, 0); - sqlite3WindowAttach(pParse, A, F, Z); + sqlite3WindowAttach(pParse, A, F.pFilter, F.pWin); } %endif @@ -1659,8 +1667,11 @@ windowdefn(A) ::= nm(X) AS LP window(Y) RP. { %type part_opt {ExprList*} %destructor part_opt {sqlite3ExprListDelete(pParse->db, $$);} -%type filter_opt {Expr*} -%destructor filter_opt {sqlite3ExprDelete(pParse->db, $$);} +%type filter_clause {Expr*} +%destructor filter_clause {sqlite3ExprDelete(pParse->db, $$);} + +%type over_clause {Window*} +%destructor over_clause {sqlite3WindowDelete(pParse->db, $$);} %type range_or_rows {int} @@ -1726,22 +1737,31 @@ frame_exclude(A) ::= GROUP|TIES(X). {A = @X; /*A-overwrites-X*/} %destructor window_clause {sqlite3WindowListDelete(pParse->db, $$);} window_clause(A) ::= WINDOW windowdefn_list(B). { A = B; } -%type over_opt {Window*} -%destructor over_opt {sqlite3WindowDelete(pParse->db, $$);} -over_opt(A) ::= . { A=0; } -over_opt(A) ::= OVER LP window(Z) RP. { +filter_over(F) ::= filter_clause(A) over_clause(B). { + F.pFilter = A; + F.pWin = B; +} +filter_over(F) ::= over_clause(B). { + F.pFilter = 0; + F.pWin = B; +} +filter_over(F) ::= filter_clause(A). { + F.pFilter = A; + F.pWin = 0; +} + +over_clause(A) ::= OVER LP window(Z) RP. { A = Z; assert( A!=0 ); } -over_opt(A) ::= OVER nm(Z). { +over_clause(A) ::= OVER nm(Z). { A = (Window*)sqlite3DbMallocZero(pParse->db, sizeof(Window)); if( A ){ A->zName = sqlite3DbStrNDup(pParse->db, Z.z, Z.n); } } -filter_opt(A) ::= . { A = 0; } -filter_opt(A) ::= FILTER LP WHERE expr(X) RP. { A = X; } +filter_clause(A) ::= FILTER LP WHERE expr(X) RP. { A = X; } %endif /* SQLITE_OMIT_WINDOWFUNC */ /* From 52d9a3c2137bac76a826a24d2c466d06f3ac49d4 Mon Sep 17 00:00:00 2001 From: dan Date: Fri, 12 Jul 2019 15:15:43 +0000 Subject: [PATCH 7/9] Minor performance improvement in vdbeRecordCompareString(). FossilOrigin-Name: 777fec84a5db968e033883920586ec0afef65578be0c1eb3021fce525111edde --- manifest | 12 ++++++------ manifest.uuid | 2 +- src/vdbeaux.c | 10 +++++----- 3 files changed, 12 insertions(+), 12 deletions(-) diff --git a/manifest b/manifest index 8bde9a3060..5625d1bf81 100644 --- a/manifest +++ b/manifest @@ -1,5 +1,5 @@ -C Change\sthe\sparser\son\sthis\sbranch\sto\smore\sclosely\smatch\strunk.\sThis\ssaves\sa\sfew\smore\scycles. -D 2019-07-11T18:43:33.538 +C Minor\sperformance\simprovement\sin\svdbeRecordCompareString(). +D 2019-07-12T15:15:43.883 F .fossil-settings/empty-dirs dbb81e8fc0401ac46a1491ab34a7f2c7c0452f2f06b54ebb845d024ca8283ef1 F .fossil-settings/ignore-glob 35175cdfcf539b2318cb04a9901442804be81cd677d8b889fcc9149c21f239ea F LICENSE.md df5091916dbb40e6e9686186587125e1b2ff51f022cc334e886c19a0e9982724 @@ -598,7 +598,7 @@ F src/vdbe.c aaa36d1ac7d55baf007e9c03ee7c826834a51dfe7a56ba4c386318695dd87c99 F src/vdbe.h 712bca562eaed1c25506b9faf9680bdc75fc42e2f4a1cd518d883fa79c7a4237 F src/vdbeInt.h 3ba14553508d66f58753952d6dd287dce4ec735de02c6440858b4891aed51c17 F src/vdbeapi.c f9161e5c77f512fbb80091ce8af621d19c9556bda5e734cffaac1198407400da -F src/vdbeaux.c 3a803d75875031309204df90977059b12ffb706d16b4baa5e2d99f4353962582 +F src/vdbeaux.c 6e690cf78b75cb30a35498be14d86656eda2a350e21b7f34723bac6fb4b06504 F src/vdbeblob.c f5c70f973ea3a9e915d1693278a5f890dc78594300cf4d54e64f2b0917c94191 F src/vdbemem.c 9ee3c0373bfc05dc8bf5307a4a92be6bea3055928c4846fdced7e708993b2d6d F src/vdbesort.c 66592d478dbb46f19aed0b42222325eadb84deb40a90eebe25c6e7c1d8468f47 @@ -1833,7 +1833,7 @@ F vsixtest/vsixtest.tcl 6a9a6ab600c25a91a7acc6293828957a386a8a93 F vsixtest/vsixtest.vcxproj.data 2ed517e100c66dc455b492e1a33350c1b20fbcdc F vsixtest/vsixtest.vcxproj.filters 37e51ffedcdb064aad6ff33b6148725226cd608e F vsixtest/vsixtest_TemporaryKey.pfx e5b1b036facdb453873e7084e1cae9102ccc67a0 -P bcc8b38ac75b731a4cd2873ab83f423be036467a511b617c779869de9bbb5383 -R 7366949ada14c091807ed4dad610ae76 +P be01b801fbc258f0ab9542130cec12a5ea5e2d0bf087684a9eda909ad459c211 +R 2db1f8ff35294de6464c20bf61e9dedc U dan -Z 9f13574d0ae5d3c1b08c0a4ecde014da +Z ac0afc3f61428d63511d6d358e65ee50 diff --git a/manifest.uuid b/manifest.uuid index 0fa6ef7732..2466c0473c 100644 --- a/manifest.uuid +++ b/manifest.uuid @@ -1 +1 @@ -be01b801fbc258f0ab9542130cec12a5ea5e2d0bf087684a9eda909ad459c211 \ No newline at end of file +777fec84a5db968e033883920586ec0afef65578be0c1eb3021fce525111edde \ No newline at end of file diff --git a/src/vdbeaux.c b/src/vdbeaux.c index 8dd2b421aa..70f56c3a53 100644 --- a/src/vdbeaux.c +++ b/src/vdbeaux.c @@ -4600,7 +4600,11 @@ static int vdbeRecordCompareString( nCmp = MIN( pPKey2->aMem[0].n, nStr ); res = memcmp(&aKey1[szHdr], pPKey2->aMem[0].z, nCmp); - if( res==0 ){ + if( res>0 ){ + res = pPKey2->r2; + }else if( res<0 ){ + res = pPKey2->r1; + }else{ res = nStr - pPKey2->aMem[0].n; if( res==0 ){ if( pPKey2->nField>1 ){ @@ -4614,10 +4618,6 @@ static int vdbeRecordCompareString( }else{ res = pPKey2->r1; } - }else if( res>0 ){ - res = pPKey2->r2; - }else{ - res = pPKey2->r1; } } From 01fd42beef1c16b8998ca8dba0bbc43e97efe9fd Mon Sep 17 00:00:00 2001 From: dan Date: Sat, 13 Jul 2019 09:55:33 +0000 Subject: [PATCH 8/9] Trivial performance improvement for balance(). FossilOrigin-Name: c7920f5569022f645ec4dbfce8c228880263785c708c2c0ac065e770c438b1d9 --- manifest | 12 ++++++------ manifest.uuid | 2 +- src/btree.c | 8 ++++---- 3 files changed, 11 insertions(+), 11 deletions(-) diff --git a/manifest b/manifest index 5625d1bf81..b5cdca5d5d 100644 --- a/manifest +++ b/manifest @@ -1,5 +1,5 @@ -C Minor\sperformance\simprovement\sin\svdbeRecordCompareString(). -D 2019-07-12T15:15:43.883 +C Trivial\sperformance\simprovement\sfor\sbalance(). +D 2019-07-13T09:55:33.596 F .fossil-settings/empty-dirs dbb81e8fc0401ac46a1491ab34a7f2c7c0452f2f06b54ebb845d024ca8283ef1 F .fossil-settings/ignore-glob 35175cdfcf539b2318cb04a9901442804be81cd677d8b889fcc9149c21f239ea F LICENSE.md df5091916dbb40e6e9686186587125e1b2ff51f022cc334e886c19a0e9982724 @@ -463,7 +463,7 @@ F src/auth.c 0fac71038875693a937e506bceb492c5f136dd7b1249fbd4ae70b4e8da14f9df F src/backup.c b1d37f6f7f5913944583733ed0f9e182f3ece0d42c27f46701142141a6e6fd33 F src/bitvec.c 17ea48eff8ba979f1f5b04cc484c7bb2be632f33 F src/btmutex.c 8acc2f464ee76324bf13310df5692a262b801808984c1b79defb2503bbafadb6 -F src/btree.c 1dbc191e5c1d9bca84a956fed5d73924c574ae5390855009f067bd0f6422e462 +F src/btree.c 0e30c98649796a91a889c2ab1c2f032da9918ac5244a3688ed97e8b5779b61fa F src/btree.h c11446f07ec0e9dc85af8041cb0855c52f5359c8b2a43e47e02a685282504d89 F src/btreeInt.h 6111c15868b90669f79081039d19e7ea8674013f907710baa3c814dc3f8bfd3f F src/build.c c5b97d3518c13081169027f291ff84b12661d526deb605031d413223ff2e0f03 @@ -1833,7 +1833,7 @@ F vsixtest/vsixtest.tcl 6a9a6ab600c25a91a7acc6293828957a386a8a93 F vsixtest/vsixtest.vcxproj.data 2ed517e100c66dc455b492e1a33350c1b20fbcdc F vsixtest/vsixtest.vcxproj.filters 37e51ffedcdb064aad6ff33b6148725226cd608e F vsixtest/vsixtest_TemporaryKey.pfx e5b1b036facdb453873e7084e1cae9102ccc67a0 -P be01b801fbc258f0ab9542130cec12a5ea5e2d0bf087684a9eda909ad459c211 -R 2db1f8ff35294de6464c20bf61e9dedc +P 777fec84a5db968e033883920586ec0afef65578be0c1eb3021fce525111edde +R 6059011e317373b890b2b02abcd3b80f U dan -Z ac0afc3f61428d63511d6d358e65ee50 +Z d0830bba5f797d712713acf5cb732357 diff --git a/manifest.uuid b/manifest.uuid index 2466c0473c..fcfc1efc2b 100644 --- a/manifest.uuid +++ b/manifest.uuid @@ -1 +1 @@ -777fec84a5db968e033883920586ec0afef65578be0c1eb3021fce525111edde \ No newline at end of file +c7920f5569022f645ec4dbfce8c228880263785c708c2c0ac065e770c438b1d9 \ No newline at end of file diff --git a/src/btree.c b/src/btree.c index 6c73a94223..5f82f1cc0a 100644 --- a/src/btree.c +++ b/src/btree.c @@ -8307,11 +8307,13 @@ static int balance(BtCursor *pCur){ VVA_ONLY( int balance_deeper_called = 0 ); do { - int iPage = pCur->iPage; + int iPage; MemPage *pPage = pCur->pPage; if( NEVER(pPage->nFree<0) && btreeComputeFreeSpace(pPage) ) break; - if( iPage==0 ){ + if( pPage->nOverflow==0 && pPage->nFree<=nMin ){ + break; + }else if( (iPage = pCur->iPage)==0 ){ if( pPage->nOverflow ){ /* The root page of the b-tree is overfull. In this case call the ** balance_deeper() function to create a new child for the root-page @@ -8332,8 +8334,6 @@ static int balance(BtCursor *pCur){ }else{ break; } - }else if( pPage->nOverflow==0 && pPage->nFree<=nMin ){ - break; }else{ MemPage * const pParent = pCur->apPage[iPage-1]; int const iIdx = pCur->aiIdx[iPage-1]; From 4f9adee289fcc8de83054332212c90f389609d4a Mon Sep 17 00:00:00 2001 From: dan Date: Sat, 13 Jul 2019 16:22:50 +0000 Subject: [PATCH 9/9] Rework the FILTER clause implementation to share more code with window functions. FossilOrigin-Name: 5dac8c38dfc3f41c5c8fb49ca35de7fd1b21f269d72e8ba6ba59ed0a4030a54d --- manifest | 26 ++++++++++++------------ manifest.uuid | 2 +- src/expr.c | 50 ++++++++++++++--------------------------------- src/parse.y | 41 +++++++++++++++++--------------------- src/resolve.c | 24 +++++++++++++---------- src/select.c | 16 ++++++++------- src/sqliteInt.h | 18 +++++++++++------ src/walker.c | 12 ++++-------- src/window.c | 28 ++++++++++++-------------- test/filter1.test | 18 ++++++++++++++++- 10 files changed, 116 insertions(+), 119 deletions(-) diff --git a/manifest b/manifest index 89b26fe6de..66070b23b5 100644 --- a/manifest +++ b/manifest @@ -1,5 +1,5 @@ -C Merge\slatest\strunk\schanges\sinto\sthis\sbranch. -D 2019-07-13T09:56:56.654 +C Rework\sthe\sFILTER\sclause\simplementation\sto\sshare\smore\scode\swith\swindow\sfunctions. +D 2019-07-13T16:22:50.675 F .fossil-settings/empty-dirs dbb81e8fc0401ac46a1491ab34a7f2c7c0452f2f06b54ebb845d024ca8283ef1 F .fossil-settings/ignore-glob 35175cdfcf539b2318cb04a9901442804be81cd677d8b889fcc9149c21f239ea F LICENSE.md df5091916dbb40e6e9686186587125e1b2ff51f022cc334e886c19a0e9982724 @@ -474,7 +474,7 @@ F src/date.c e1d8ac7102f3f283e63e13867acb0efa33861cf34f0faf4cdbaf9fa7a1eb7041 F src/dbpage.c 135eb3b5e74f9ef74bde5cec2571192c90c86984fa534c88bf4a055076fa19b7 F src/dbstat.c c12833de69cb655751487d2c5a59607e36be1c58ba1f4bd536609909ad47b319 F src/delete.c d08c9e01a2664afd12edcfa3a9c6578517e8ff8735f35509582693adbe0edeaf -F src/expr.c 01bbd69f29936bba85dba6188a409c1e21a0c260bf04b9fc78d5e9702a77acb5 +F src/expr.c 896c4576cd88a0dc5b5ae0abd236b507b778105b58744bd8974fac98fc432b4c F src/fault.c 460f3e55994363812d9d60844b2a6de88826e007 F src/fkey.c 0e14d4bef8eac2d87bbd517e492d9084c65008d117823f8922c5e7b2b599bd33 F src/func.c 905c0424900979fade3731b4a271592059425c988cb7d5b245032253712dd7c8 @@ -511,7 +511,7 @@ F src/os_win.c 85d9e532d0444ab6c16d7431490c2e279e282aa0917b0e988996b1ae0de5c5a0 F src/os_win.h 7b073010f1451abe501be30d12f6bc599824944a F src/pager.c 422fd8cfa59fb9173eff36a95878904a0eeb0dcc62ba49350acc8b1e51c4dc7b F src/pager.h 217921e81eb5fe455caa5cda96061959706bcdd29ddb57166198645ef7822ac3 -F src/parse.y 6f2e8ec7df1e4d4ab57c3c4f9e4815b13a78357685a917a512d6f3d949d8c435 +F src/parse.y ef494825aa4d633b601af10f936fe50a630bd4c4a6ceb4d941ecfb6756e06c56 F src/pcache.c fd2d0553b3222d6b9f7cb251079e5bca1299d1161da3027b525932d8bf46340a F src/pcache.h 4f87acd914cef5016fae3030343540d75f5b85a1877eed1a2a19b9f284248586 F src/pcache1.c e6373934cdafea272d94feb224344f9fd93ff94318221b64ccabd72d98b4672e @@ -520,14 +520,14 @@ F src/pragma.h 4a9fabff14db4487a734dfeeb4be984ce662bfdccfae16145b9c732327735e13 F src/prepare.c d633c3610c23dc98239ea445803c2a6188c2b8ab647e3df4243f369516e6c8d1 F src/printf.c 9be6945837c839ba57837b4bc3af349eba630920fa5532aa518816defe42a7d4 F src/random.c 80f5d666f23feb3e6665a6ce04c7197212a88384 -F src/resolve.c 138c9e9d8828f199709bd7c970d98aaff47a21d33c6c42ffb6e1e809620fcc9e +F src/resolve.c 6d96d5c1cfa48e49df160aee6032dbb1f79e1d04163c34f9be5625828ac9a2ec F src/rowset.c d977b011993aaea002cab3e0bb2ce50cf346000dff94e944d547b989f4b1fe93 -F src/select.c 98362f190fd6f70f8f612f38a898da4e30b086f13f4c5dab8c04c21ae5a61804 +F src/select.c 1ac359aed1a83239ce3434c0620e488ad9cf25dee829b86c89f7fe4b5cd366c5 F src/shell.c.in 82f8a473c01662f52233c6c75b9bf88d0d2cab276086d5d4ca6f2ff57a3e48eb F src/sqlite.h.in 83ebc8ab1a2e82d92214006ea2c15bf8a0604f3fac2c31dd9ce9021f568c71f2 F src/sqlite3.rc 5121c9e10c3964d5755191c80dd1180c122fc3a8 F src/sqlite3ext.h 9ecc93b8493bd20c0c07d52e2ac0ed8bab9b549c7f7955b59869597b650dd8b5 -F src/sqliteInt.h 13fa73fd67968a4e3c58c84fbd655770989a82e0902880b0446747cd6711ad74 +F src/sqliteInt.h e989d94d02e4389480e7abf4d86dbdd23d94f3e15db77c1424b67c75f8d97428 F src/sqliteLimit.h 1513bfb7b20378aa0041e7022d04acb73525de35b80b252f1b83fedb4de6a76b F src/status.c 46e7aec11f79dad50965a5ca5fa9de009f7d6bde08be2156f1538a0a296d4d0e F src/table.c b46ad567748f24a326d9de40e5b9659f96ffff34 @@ -607,12 +607,12 @@ F src/vtab.c 1fa256c6ddad7a81e2a4dc080d015d4b0a7135767717d311298e47f6fca64bb3 F src/vxworks.h d2988f4e5a61a4dfe82c6524dd3d6e4f2ce3cdb9 F src/wal.c b09a2a9cab50efa08451a8c81d47052120ad5da174048c6d0b08d405384abdf2 F src/wal.h 606292549f5a7be50b6227bd685fa76e3a4affad71bb8ac5ce4cb5c79f6a176a -F src/walker.c 588e7f5faf30d2e68aa9b176258bb3225d0553171b998b06b69ed74a0528e986 +F src/walker.c ccbe57a6eeedce5826510c509cebb547e0c877d53f14b3118698643057d4b597 F src/where.c 2f11eeb14335b7640f886b2fb441f54a94c35ab5cde8b53461a1074bfd587081 F src/whereInt.h 1b728f71654ebf8421a1715497a587f02d6f538e819af58dc826908f8577e810 F src/wherecode.c 37a1004237d630d785c47bba2290eac652a7a8b0047518eba3cb7c808b604c4a F src/whereexpr.c 5e559bdd24b06e3bc2e68f258bf751302954dc1e432daf71fdd8098a71462326 -F src/window.c 1d78310e655ce2a697a54cf28b713ae9dff9d1d6c5d370233499e0efbf96c699 +F src/window.c 6cfb1b3c821a4cb2c6f262c6a61cd400a2803b390370e3d45d7e59b9f38e61da F test/8_3_names.test ebbb5cd36741350040fd28b432ceadf495be25b2 F test/affinity2.test a6d901b436328bd67a79b41bb0ac2663918fe3bd F test/affinity3.test 6a101af2fc945ce2912f6fe54dd646018551710d @@ -856,7 +856,7 @@ F test/extraquick.test cb254400bd42bfb777ff675356aabf3287978f79 F test/fallocate.test 37a62e396a68eeede8f8d2ecf23573a80faceb630788d314d0a073d862616717 F test/filectrl.test 6e871c2d35dead1d9a88e176e8d2ca094fec6bb3 F test/filefmt.test f393e80c4b8d493b7a7f8f3809a8425bbf4292af1f5140f01cb1427798a2bbd4 -F test/filter1.test 8a0f541d2138a10b41c513a9b8193af09d5218ca4e670bc6fd9ed121db1b7bf3 +F test/filter1.test 5830d6214399abb874f2c0293d299b8cbd02d56b8e529e509befd322f1bc2003 F test/filter2.tcl 44e525497ce07382915f01bd29ffd0fa49dab3adb87253b5e5103ba8f93393e8 F test/filter2.test 485cf95d1f6d6ceee5632201ca52a71868599836f430cdee42e5f7f14666e30a F test/fkey1.test d11dbb8a93ead9b5c46ae5d02da016d61245d47662fb2d844c99214f6163f768 @@ -1834,7 +1834,7 @@ F vsixtest/vsixtest.tcl 6a9a6ab600c25a91a7acc6293828957a386a8a93 F vsixtest/vsixtest.vcxproj.data 2ed517e100c66dc455b492e1a33350c1b20fbcdc F vsixtest/vsixtest.vcxproj.filters 37e51ffedcdb064aad6ff33b6148725226cd608e F vsixtest/vsixtest_TemporaryKey.pfx e5b1b036facdb453873e7084e1cae9102ccc67a0 -P c7920f5569022f645ec4dbfce8c228880263785c708c2c0ac065e770c438b1d9 18bfb2179ce2c60cec8f5859a84b737731a5e53b28e35072cbb249f18b94262b -R 7c05f3663f5bc895dff508dc36c38a46 +P 86ab963cc5743867e0535b78a5776e18c13835aa44bf2009f77642df8407cb3f +R 7f5ccdaf1d791cabe4704b7e56f0cde3 U dan -Z b8ee1e32a67789f7b92523a517ccd572 +Z d18b0c3413514388f116d5ed56fe7aa4 diff --git a/manifest.uuid b/manifest.uuid index f67e4b02d9..a886df556a 100644 --- a/manifest.uuid +++ b/manifest.uuid @@ -1 +1 @@ -86ab963cc5743867e0535b78a5776e18c13835aa44bf2009f77642df8407cb3f \ No newline at end of file +5dac8c38dfc3f41c5c8fb49ca35de7fd1b21f269d72e8ba6ba59ed0a4030a54d \ No newline at end of file diff --git a/src/expr.c b/src/expr.c index 6fbeb827fb..9ba81a8455 100644 --- a/src/expr.c +++ b/src/expr.c @@ -1027,7 +1027,7 @@ static SQLITE_NOINLINE void sqlite3ExprDeleteNN(sqlite3 *db, Expr *p){ assert( !ExprHasProperty(p, EP_WinFunc) || p->y.pWin!=0 || db->mallocFailed ); assert( p->op!=TK_FUNCTION || ExprHasProperty(p, EP_TokenOnly|EP_Reduced) - || p->y.pWin==0 || ExprHasProperty(p, EP_WinFunc|EP_Filter) ); + || p->y.pWin==0 || ExprHasProperty(p, EP_WinFunc) ); #ifdef SQLITE_DEBUG if( ExprHasProperty(p, EP_Leaf) && !ExprHasProperty(p, EP_TokenOnly) ){ assert( p->pLeft==0 ); @@ -1040,22 +1040,16 @@ static SQLITE_NOINLINE void sqlite3ExprDeleteNN(sqlite3 *db, Expr *p){ assert( p->x.pList==0 || p->pRight==0 ); if( p->pLeft && p->op!=TK_SELECT_COLUMN ) sqlite3ExprDeleteNN(db, p->pLeft); if( p->pRight ){ - assert( !ExprHasProperty(p, (EP_WinFunc|EP_Filter)) ); + assert( !ExprHasProperty(p, EP_WinFunc) ); sqlite3ExprDeleteNN(db, p->pRight); }else if( ExprHasProperty(p, EP_xIsSelect) ){ - assert( !ExprHasProperty(p, (EP_WinFunc|EP_Filter)) ); + assert( !ExprHasProperty(p, EP_WinFunc) ); sqlite3SelectDelete(db, p->x.pSelect); }else{ sqlite3ExprListDelete(db, p->x.pList); #ifndef SQLITE_OMIT_WINDOWFUNC - if( ExprHasProperty(p, (EP_WinFunc|EP_Filter)) ){ - if( ExprHasProperty(p, EP_WinFunc) ){ - assert( p->op==TK_FUNCTION && !ExprHasProperty(p, EP_Filter) ); - sqlite3WindowDelete(db, p->y.pWin); - }else{ - assert( p->op==TK_FUNCTION || p->op==TK_AGG_FUNCTION ); - sqlite3ExprDeleteNN(db, p->y.pFilter); - } + if( ExprHasProperty(p, EP_WinFunc) ){ + sqlite3WindowDelete(db, p->y.pWin); } #endif } @@ -1273,7 +1267,7 @@ static Expr *exprDup(sqlite3 *db, Expr *p, int dupFlags, u8 **pzBuffer){ } /* Fill in pNew->pLeft and pNew->pRight. */ - if( ExprHasProperty(pNew, EP_Reduced|EP_TokenOnly|EP_WinFunc|EP_Filter) ){ + if( ExprHasProperty(pNew, EP_Reduced|EP_TokenOnly|EP_WinFunc) ){ zAlloc += dupedExprNodeSize(p, dupFlags); if( !ExprHasProperty(pNew, EP_TokenOnly|EP_Leaf) ){ pNew->pLeft = p->pLeft ? @@ -1286,10 +1280,6 @@ static Expr *exprDup(sqlite3 *db, Expr *p, int dupFlags, u8 **pzBuffer){ pNew->y.pWin = sqlite3WindowDup(db, pNew, p->y.pWin); assert( ExprHasProperty(pNew, EP_WinFunc) ); } - if( ExprHasProperty(p, EP_Filter) ){ - pNew->y.pFilter = sqlite3ExprDup(db, p->y.pFilter, 0); - assert( ExprHasProperty(pNew, EP_Filter) ); - } #endif /* SQLITE_OMIT_WINDOWFUNC */ if( pzBuffer ){ *pzBuffer = zAlloc; @@ -1347,6 +1337,7 @@ static With *withDup(sqlite3 *db, With *p){ static int gatherSelectWindowsCallback(Walker *pWalker, Expr *pExpr){ if( pExpr->op==TK_FUNCTION && ExprHasProperty(pExpr, EP_WinFunc) ){ assert( pExpr->y.pWin ); + assert( IsWindowFunc(pExpr) ); pExpr->y.pWin->pNextWin = pWalker->u.pSelect->pWin; pWalker->u.pSelect->pWin = pExpr->y.pWin; } @@ -4852,20 +4843,17 @@ int sqlite3ExprCompare(Parse *pParse, Expr *pA, Expr *pB, int iTab){ return 2; } if( pA->op!=TK_COLUMN && pA->op!=TK_AGG_COLUMN && pA->u.zToken ){ - if( pA->op==TK_FUNCTION ){ + if( pA->op==TK_FUNCTION || pA->op==TK_AGG_FUNCTION ){ if( sqlite3StrICmp(pA->u.zToken,pB->u.zToken)!=0 ) return 2; #ifndef SQLITE_OMIT_WINDOWFUNC - /* Justification for the assert(): - ** window functions have p->op==TK_FUNCTION but aggregate functions - ** have p->op==TK_AGG_FUNCTION. So any comparison between an aggregate - ** function and a window function should have failed before reaching - ** this point. And, it is not possible to have a window function and - ** a scalar function with the same name and number of arguments. So - ** if we reach this point, either A and B both window functions or - ** neither are a window functions. */ - assert( ExprHasProperty(pA,EP_WinFunc)==ExprHasProperty(pB,EP_WinFunc) ); + assert( pA->op==pB->op ); + if( ExprHasProperty(pA,EP_WinFunc)!=ExprHasProperty(pB,EP_WinFunc) ){ + return 2; + } if( ExprHasProperty(pA,EP_WinFunc) ){ - if( sqlite3WindowCompare(pParse,pA->y.pWin,pB->y.pWin)!=0 ) return 2; + if( sqlite3WindowCompare(pParse, pA->y.pWin, pB->y.pWin, 1)!=0 ){ + return 2; + } } #endif }else if( pA->op==TK_NULL ){ @@ -4875,14 +4863,6 @@ int sqlite3ExprCompare(Parse *pParse, Expr *pA, Expr *pB, int iTab){ }else if( ALWAYS(pB->u.zToken!=0) && strcmp(pA->u.zToken,pB->u.zToken)!=0 ){ return 2; } -#ifndef SQLITE_OMIT_WINDOWFUNC - else if( pA->op==TK_AGG_FUNCTION ){ - assert( ExprHasProperty(pA, EP_WinFunc)==0 ); - if( sqlite3ExprCompare(pParse, pA->y.pFilter, pB->y.pFilter, iTab) ){ - return 2; - } - } -#endif } if( (pA->flags & EP_Distinct)!=(pB->flags & EP_Distinct) ) return 2; if( (combinedFlags & EP_TokenOnly)==0 ){ diff --git a/src/parse.y b/src/parse.y index 3cef3e5e6b..7a3835a627 100644 --- a/src/parse.y +++ b/src/parse.y @@ -1044,23 +1044,13 @@ expr(A) ::= id(X) LP STAR RP. { } %ifndef SQLITE_OMIT_WINDOWFUNC -%type filter_over { - struct FunctionTail { - Window *pWin; - Expr *pFilter; - } -} -%destructor filter_over { - sqlite3WindowDelete(pParse->db, $$.pWin); - sqlite3ExprDelete(pParse->db, $$.pFilter); -} -expr(A) ::= id(X) LP distinct(D) exprlist(Y) RP filter_over(F). { +expr(A) ::= id(X) LP distinct(D) exprlist(Y) RP filter_over(Z). { A = sqlite3ExprFunction(pParse, Y, &X, D); - sqlite3WindowAttach(pParse, A, F.pFilter, F.pWin); + sqlite3WindowAttach(pParse, A, Z); } -expr(A) ::= id(X) LP STAR RP filter_over(F). { +expr(A) ::= id(X) LP STAR RP filter_over(Z). { A = sqlite3ExprFunction(pParse, 0, &X, 0); - sqlite3WindowAttach(pParse, A, F.pFilter, F.pWin); + sqlite3WindowAttach(pParse, A, Z); } %endif @@ -1673,6 +1663,9 @@ windowdefn(A) ::= nm(X) AS LP window(Y) RP. { %type over_clause {Window*} %destructor over_clause {sqlite3WindowDelete(pParse->db, $$);} +%type filter_over {Window*} +%destructor filter_over {sqlite3WindowDelete(pParse->db, $$);} + %type range_or_rows {int} %type frame_bound {struct FrameBound} @@ -1737,17 +1730,19 @@ frame_exclude(A) ::= GROUP|TIES(X). {A = @X; /*A-overwrites-X*/} %destructor window_clause {sqlite3WindowListDelete(pParse->db, $$);} window_clause(A) ::= WINDOW windowdefn_list(B). { A = B; } -filter_over(F) ::= filter_clause(A) over_clause(B). { - F.pFilter = A; - F.pWin = B; +filter_over(A) ::= filter_clause(F) over_clause(O). { + O->pFilter = F; + A = O; } -filter_over(F) ::= over_clause(B). { - F.pFilter = 0; - F.pWin = B; +filter_over(A) ::= over_clause(O). { + A = O; } -filter_over(F) ::= filter_clause(A). { - F.pFilter = A; - F.pWin = 0; +filter_over(A) ::= filter_clause(F). { + A = (Window*)sqlite3DbMallocZero(pParse->db, sizeof(Window)); + if( A ){ + A->eFrmType = TK_FILTER; + A->pFilter = F; + } } over_clause(A) ::= OVER LP window(Z) RP. { diff --git a/src/resolve.c b/src/resolve.c index 37aaa92606..872d04f57a 100644 --- a/src/resolve.c +++ b/src/resolve.c @@ -749,7 +749,9 @@ static int resolveExprStep(Walker *pWalker, Expr *pExpr){ FuncDef *pDef; /* Information about the function */ u8 enc = ENC(pParse->db); /* The database encoding */ int savedAllowFlags = (pNC->ncFlags & (NC_AllowAgg | NC_AllowWin)); - +#ifndef SQLITE_OMIT_WINDOWFUNC + Window *pWin = (IsWindowFunc(pExpr) ? pExpr->y.pWin : 0); +#endif assert( !ExprHasProperty(pExpr, EP_xIsSelect) ); zId = pExpr->u.zToken; nId = sqlite3Strlen30(zId); @@ -826,7 +828,6 @@ static int resolveExprStep(Walker *pWalker, Expr *pExpr){ if( 0==IN_RENAME_OBJECT ){ #ifndef SQLITE_OMIT_WINDOWFUNC - Window *pWin = (ExprHasProperty(pExpr, EP_WinFunc) ? pExpr->y.pWin : 0); assert( is_agg==0 || (pDef->funcFlags & SQLITE_FUNC_MINMAX) || (pDef->xValue==0 && pDef->xInverse==0) || (pDef->xValue && pDef->xInverse && pDef->xSFunc && pDef->xFinalize) @@ -850,7 +851,8 @@ static int resolveExprStep(Walker *pWalker, Expr *pExpr){ sqlite3ErrorMsg(pParse, "misuse of %s function %.*s()",zType,nId,zId); pNC->nErr++; is_agg = 0; - }else if( is_agg==0 && ExprHasProperty(pExpr, EP_Filter) ){ + }else if( is_agg==0 && ExprHasProperty(pExpr, EP_WinFunc) ){ + assert( !IsWindowFunc(pExpr) ); sqlite3ErrorMsg(pParse, "filter clause may not be used with non-aggregate %.*s()", nId, zId @@ -890,16 +892,16 @@ static int resolveExprStep(Walker *pWalker, Expr *pExpr){ sqlite3WalkExprList(pWalker, pList); if( is_agg ){ #ifndef SQLITE_OMIT_WINDOWFUNC - if( ExprHasProperty(pExpr, EP_WinFunc) ){ + if( pWin ){ Select *pSel = pNC->pWinSelect; if( IN_RENAME_OBJECT==0 ){ - sqlite3WindowUpdate(pParse, pSel->pWinDefn, pExpr->y.pWin, pDef); + sqlite3WindowUpdate(pParse, pSel->pWinDefn, pWin, pDef); } - sqlite3WalkExprList(pWalker, pExpr->y.pWin->pPartition); - sqlite3WalkExprList(pWalker, pExpr->y.pWin->pOrderBy); - sqlite3WalkExpr(pWalker, pExpr->y.pWin->pFilter); + sqlite3WalkExprList(pWalker, pWin->pPartition); + sqlite3WalkExprList(pWalker, pWin->pOrderBy); + sqlite3WalkExpr(pWalker, pWin->pFilter); if( 0==pSel->pWin - || 0==sqlite3WindowCompare(pParse, pSel->pWin, pExpr->y.pWin) + || 0==sqlite3WindowCompare(pParse, pSel->pWin, pWin, 0) ){ pExpr->y.pWin->pNextWin = pSel->pWin; pSel->pWin = pExpr->y.pWin; @@ -912,7 +914,9 @@ static int resolveExprStep(Walker *pWalker, Expr *pExpr){ pExpr->op = TK_AGG_FUNCTION; pExpr->op2 = 0; #ifndef SQLITE_OMIT_WINDOWFUNC - sqlite3WalkExpr(pWalker, pExpr->y.pFilter); + if( ExprHasProperty(pExpr, EP_WinFunc) ){ + sqlite3WalkExpr(pWalker, pExpr->y.pWin->pFilter); + } #endif while( pNC2 && !sqlite3FunctionUsesThisSrc(pExpr, pNC2->pSrcList) ){ pExpr->op2++; diff --git a/src/select.c b/src/select.c index 8b18e20d05..dc388b1f79 100644 --- a/src/select.c +++ b/src/select.c @@ -4403,7 +4403,8 @@ static u8 minMaxQuery(sqlite3 *db, Expr *pFunc, ExprList **ppMinMax){ assert( *ppMinMax==0 ); assert( pFunc->op==TK_AGG_FUNCTION ); - if( pEList==0 || pEList->nExpr!=1 || ExprHasProperty(pFunc, EP_Filter) ){ + assert( !IsWindowFunc(pFunc) ); + if( pEList==0 || pEList->nExpr!=1 || ExprHasProperty(pFunc, EP_WinFunc) ){ return eRet; } zFunc = pFunc->u.zToken; @@ -4452,7 +4453,7 @@ static Table *isSimpleCount(Select *p, AggInfo *pAggInfo){ if( pExpr->op!=TK_AGG_FUNCTION ) return 0; if( NEVER(pAggInfo->nFunc==0) ) return 0; if( (pAggInfo->aFunc[0].pFunc->funcFlags&SQLITE_FUNC_COUNT)==0 ) return 0; - if( ExprHasProperty(pExpr, EP_Distinct|EP_Filter) ) return 0; + if( ExprHasProperty(pExpr, EP_Distinct|EP_WinFunc) ) return 0; return pTab; } @@ -5332,8 +5333,9 @@ static void updateAccumulator(Parse *pParse, int regAcc, AggInfo *pAggInfo){ int regAgg; ExprList *pList = pF->pExpr->x.pList; assert( !ExprHasProperty(pF->pExpr, EP_xIsSelect) ); - if( ExprHasProperty(pF->pExpr, EP_Filter) ){ - Expr *pFilter = pF->pExpr->y.pFilter; + assert( !IsWindowFunc(pF->pExpr) ); + if( ExprHasProperty(pF->pExpr, EP_WinFunc) ){ + Expr *pFilter = pF->pExpr->y.pWin->pFilter; addrNext = sqlite3VdbeMakeLabel(pParse); sqlite3ExprIfFalse(pParse, pFilter, addrNext, SQLITE_JUMPIFNULL); } @@ -6236,9 +6238,9 @@ int sqlite3Select( sNC.ncFlags |= NC_InAggFunc; sqlite3ExprAnalyzeAggList(&sNC, pExpr->x.pList); #ifndef SQLITE_OMIT_WINDOWFUNC - assert( !ExprHasProperty(pExpr, EP_WinFunc) ); - if( pExpr->y.pFilter ){ - sqlite3ExprAnalyzeAggregates(&sNC, pExpr->y.pFilter); + assert( !IsWindowFunc(pExpr) ); + if( ExprHasProperty(pExpr, EP_WinFunc) ){ + sqlite3ExprAnalyzeAggregates(&sNC, pExpr->y.pWin->pFilter); } #endif sNC.ncFlags &= ~NC_InAggFunc; diff --git a/src/sqliteInt.h b/src/sqliteInt.h index c0f2e0a186..037d09e420 100644 --- a/src/sqliteInt.h +++ b/src/sqliteInt.h @@ -2491,8 +2491,7 @@ struct Expr { union { Table *pTab; /* TK_COLUMN: Table containing column. Can be NULL ** for a column of an index on an expression */ - Window *pWin; /* EP_WinFunc: Window definition for the func */ - Expr *pFilter; /* EP_Filter: Filter definition for the func */ + Window *pWin; /* EP_WinFunc: Window/Filter defn for a function */ struct { /* TK_IN, TK_SELECT, and TK_EXISTS */ int iAddr; /* Subroutine entry address */ int regReturn; /* Register used to hold return address */ @@ -2537,7 +2536,6 @@ struct Expr { #define EP_Static 0x8000000 /* Held in memory not obtained from malloc() */ #define EP_IsTrue 0x10000000 /* Always has boolean value of TRUE */ #define EP_IsFalse 0x20000000 /* Always has boolean value of FALSE */ -#define EP_Filter 0x40000000 /* TK_[AGG_]FUNCTION with Expr.y.pFilter set */ /* ** The EP_Propagate mask is a set of properties that automatically propagate @@ -2581,6 +2579,14 @@ struct Expr { */ #define EXPRDUP_REDUCE 0x0001 /* Used reduced-size Expr nodes */ +/* +** True if the expression passed as an argument was a function with +** an OVER() clause (a window function). +*/ +#define IsWindowFunc(p) ( \ + ExprHasProperty((p), EP_WinFunc) && p->y.pWin->eFrmType!=TK_FILTER \ +) + /* ** A list of expressions. Each expression may optionally have a ** name. An expr/name combination can be used in several ways, such @@ -3604,8 +3610,8 @@ struct Window { void sqlite3WindowDelete(sqlite3*, Window*); void sqlite3WindowListDelete(sqlite3 *db, Window *p); Window *sqlite3WindowAlloc(Parse*, int, int, Expr*, int , Expr*, u8); -void sqlite3WindowAttach(Parse*, Expr*, Expr*, Window*); -int sqlite3WindowCompare(Parse*, Window*, Window*); +void sqlite3WindowAttach(Parse*, Expr*, Window*); +int sqlite3WindowCompare(Parse*, Window*, Window*, int); void sqlite3WindowCodeInit(Parse*, Window*); void sqlite3WindowCodeStep(Parse*, Select*, WhereInfo*, int, int); int sqlite3WindowRewrite(Parse*, Select*); @@ -3619,7 +3625,7 @@ Window *sqlite3WindowAssemble(Parse*, Window*, ExprList*, ExprList*, Token*); #else # define sqlite3WindowDelete(a,b) # define sqlite3WindowFunctions() -# define sqlite3WindowAttach(a,b,c,d) +# define sqlite3WindowAttach(a,b,c) #endif /* diff --git a/src/walker.c b/src/walker.c index 44e3d18363..b976886c5f 100644 --- a/src/walker.c +++ b/src/walker.c @@ -63,23 +63,19 @@ static SQLITE_NOINLINE int walkExpr(Walker *pWalker, Expr *pExpr){ if( pExpr->pLeft && walkExpr(pWalker, pExpr->pLeft) ) return WRC_Abort; assert( pExpr->x.pList==0 || pExpr->pRight==0 ); if( pExpr->pRight ){ - assert( !ExprHasProperty(pExpr, EP_WinFunc|EP_Filter) ); + assert( !ExprHasProperty(pExpr, EP_WinFunc) ); pExpr = pExpr->pRight; continue; }else if( ExprHasProperty(pExpr, EP_xIsSelect) ){ - assert( !ExprHasProperty(pExpr, EP_WinFunc|EP_Filter) ); + assert( !ExprHasProperty(pExpr, EP_WinFunc) ); if( sqlite3WalkSelect(pWalker, pExpr->x.pSelect) ) return WRC_Abort; }else{ if( pExpr->x.pList ){ if( sqlite3WalkExprList(pWalker, pExpr->x.pList) ) return WRC_Abort; } #ifndef SQLITE_OMIT_WINDOWFUNC - if( ExprHasProperty(pExpr, EP_WinFunc|EP_Filter) ){ - if( ExprHasProperty(pExpr, EP_WinFunc) ){ - if( walkWindowList(pWalker, pExpr->y.pWin) ) return WRC_Abort; - }else if( ExprHasProperty(pExpr, EP_Filter) ){ - if( walkExpr(pWalker, pExpr->y.pFilter) ) return WRC_Abort; - } + if( ExprHasProperty(pExpr, EP_WinFunc) ){ + if( walkWindowList(pWalker, pExpr->y.pWin) ) return WRC_Abort; } #endif } diff --git a/src/window.c b/src/window.c index b91b0f0504..ed39107b92 100644 --- a/src/window.c +++ b/src/window.c @@ -1193,25 +1193,20 @@ void sqlite3WindowChain(Parse *pParse, Window *pWin, Window *pList){ /* ** Attach window object pWin to expression p. */ -void sqlite3WindowAttach(Parse *pParse, Expr *p, Expr *pFilter, Window *pWin){ +void sqlite3WindowAttach(Parse *pParse, Expr *p, Window *pWin){ if( p ){ assert( p->op==TK_FUNCTION ); - if( pWin ){ - p->y.pWin = pWin; - ExprSetProperty(p, EP_WinFunc); - pWin->pOwner = p; - if( p->flags & EP_Distinct ){ - sqlite3ErrorMsg(pParse, - "DISTINCT is not supported for window functions"); - } - pWin->pFilter = pFilter; - }else if( pFilter ){ - p->y.pFilter = pFilter; - ExprSetProperty(p, EP_Filter); + assert( pWin ); + p->y.pWin = pWin; + ExprSetProperty(p, EP_WinFunc); + pWin->pOwner = p; + if( (p->flags & EP_Distinct) && pWin->eFrmType!=TK_FILTER ){ + sqlite3ErrorMsg(pParse, + "DISTINCT is not supported for window functions" + ); } }else{ sqlite3WindowDelete(pParse->db, pWin); - sqlite3ExprDelete(pParse->db, pFilter); } } @@ -1219,7 +1214,7 @@ void sqlite3WindowAttach(Parse *pParse, Expr *p, Expr *pFilter, Window *pWin){ ** Return 0 if the two window objects are identical, or non-zero otherwise. ** Identical window objects can be processed in a single scan. */ -int sqlite3WindowCompare(Parse *pParse, Window *p1, Window *p2){ +int sqlite3WindowCompare(Parse *pParse, Window *p1, Window *p2, int bFilter){ if( p1->eFrmType!=p2->eFrmType ) return 1; if( p1->eStart!=p2->eStart ) return 1; if( p1->eEnd!=p2->eEnd ) return 1; @@ -1228,6 +1223,9 @@ int sqlite3WindowCompare(Parse *pParse, Window *p1, Window *p2){ if( sqlite3ExprCompare(pParse, p1->pEnd, p2->pEnd, -1) ) return 1; if( sqlite3ExprListCompare(p1->pPartition, p2->pPartition, -1) ) return 1; if( sqlite3ExprListCompare(p1->pOrderBy, p2->pOrderBy, -1) ) return 1; + if( bFilter ){ + if( sqlite3ExprCompare(pParse, p1->pFilter, p2->pFilter, -1) ) return 1; + } return 0; } diff --git a/test/filter1.test b/test/filter1.test index 8c0c14d87f..f0667c91cb 100644 --- a/test/filter1.test +++ b/test/filter1.test @@ -55,10 +55,26 @@ do_execsql_test 1.6 { SELECT count(*) FILTER (WHERE a!=5) FROM t1 } {8} -do_execsql_test 1.6 { +do_execsql_test 1.7 { SELECT min(a) FILTER (WHERE a>3) FROM t1 GROUP BY (a%2) ORDER BY 1; } {4 5} +do_execsql_test 1.8 { + CREATE VIEW vv AS + SELECT sum(a) FILTER( WHERE a>9 ), + sum(a) FILTER( WHERE a>8 ), + sum(a) FILTER( WHERE a>7 ), + sum(a) FILTER( WHERE a>6 ), + sum(a) FILTER( WHERE a>5 ), + sum(a) FILTER( WHERE a>4 ), + sum(a) FILTER( WHERE a>3 ), + sum(a) FILTER( WHERE a>2 ), + sum(a) FILTER( WHERE a>1 ), + sum(a) FILTER( WHERE a>0 ) + FROM t1; + SELECT * FROM vv; +} {{} 9 17 24 30 35 39 42 44 45} + #------------------------------------------------------------------------- # Test some errors: