Rework the FILTER clause implementation to share more code with window functions.
FossilOrigin-Name: 5dac8c38dfc3f41c5c8fb49ca35de7fd1b21f269d72e8ba6ba59ed0a4030a54d
This commit is contained in:
parent
648d629953
commit
4f9adee289
26
manifest
26
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
|
||||
|
@ -1 +1 @@
|
||||
86ab963cc5743867e0535b78a5776e18c13835aa44bf2009f77642df8407cb3f
|
||||
5dac8c38dfc3f41c5c8fb49ca35de7fd1b21f269d72e8ba6ba59ed0a4030a54d
|
50
src/expr.c
50
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 ){
|
||||
|
41
src/parse.y
41
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. {
|
||||
|
@ -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++;
|
||||
|
16
src/select.c
16
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;
|
||||
|
@ -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
|
||||
|
||||
/*
|
||||
|
12
src/walker.c
12
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
|
||||
}
|
||||
|
28
src/window.c
28
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;
|
||||
}
|
||||
|
||||
|
@ -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:
|
||||
|
Loading…
x
Reference in New Issue
Block a user