Remove Window objects from the corresponding Select.pWin list when they are deleted, as they are, for example, when the ORDER BY clause is optimized out.

FossilOrigin-Name: fd7316cda0fdfe86985f41a97dff3beba188606ec9bc6fef38a686a6976a6e01
This commit is contained in:
dan 2019-07-22 16:33:02 +00:00
commit 7f6f12bdd3
8 changed files with 77 additions and 33 deletions

View File

@ -1,5 +1,5 @@
C Fix\sa\sproblem\swith\srenaming\stables\swhen\sthe\sschema\scontains\san\sinvocation\sof\sa\scurrently\sunregistered\saggregate\swith\sa\sFILTER\sclause.
D 2019-07-22T11:38:43.410
C Remove\sWindow\sobjects\sfrom\sthe\scorresponding\sSelect.pWin\slist\swhen\sthey\sare\sdeleted,\sas\sthey\sare,\sfor\sexample,\swhen\sthe\sORDER\sBY\sclause\sis\soptimized\sout.
D 2019-07-22T16:33:02.936
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 f1d87bc9f2d0f14df97b768d5a3d4e4e606fbe45372bc9a0cb6a2623d1933e6d
F src/expr.c 3dba8ac22b89b6af10cff7d672f8cab4a71c7ee75abb4cbc0a6a79d32f752a4a
F src/fault.c 460f3e55994363812d9d60844b2a6de88826e007
F src/fkey.c ab1971900477e93b6aaabf160d2d1c79c06edd9227441ccfaf0de7e7431d7f48
F src/func.c 905c0424900979fade3731b4a271592059425c988cb7d5b245032253712dd7c8
@ -520,14 +520,14 @@ F src/pragma.h 4a9fabff14db4487a734dfeeb4be984ce662bfdccfae16145b9c732327735e13
F src/prepare.c 1fdf68a8add154bd1de978dde5a4819478e2576d5701b93db2c65d78c3daf1c8
F src/printf.c 9be6945837c839ba57837b4bc3af349eba630920fa5532aa518816defe42a7d4
F src/random.c 80f5d666f23feb3e6665a6ce04c7197212a88384
F src/resolve.c 844e5d127f2a3f3e1992b3571b0c710cdbcdccdd7e216930f3cf2e685675e4fe
F src/resolve.c 63f353113a133810388e45c222ed512ea8d2371a4a9e9ec54001e84072a3df5c
F src/rowset.c d977b011993aaea002cab3e0bb2ce50cf346000dff94e944d547b989f4b1fe93
F src/select.c c4b4fe44df6bcf32b693267ae9ec68e1c25f9a29d4a17afebd287d4af2f5fb51
F src/select.c 9817d812b13c956ac28d27390602751a8add217f87224d2636fb3ef7e4d646a7
F src/shell.c.in a571d51dd7605b34796e9942dce0e895156d1860383faf6aaca3c88b08fe2a43
F src/sqlite.h.in 83ebc8ab1a2e82d92214006ea2c15bf8a0604f3fac2c31dd9ce9021f568c71f2
F src/sqlite3.rc 5121c9e10c3964d5755191c80dd1180c122fc3a8
F src/sqlite3ext.h 9ecc93b8493bd20c0c07d52e2ac0ed8bab9b549c7f7955b59869597b650dd8b5
F src/sqliteInt.h 30dc455dd9591fd9cb31a64ff8a35decfb20fd96a936812459b1cd4eef1748f7
F src/sqliteInt.h 01af0390cccdf0ea1c3057923afe7370a1f24a6e3363d802dd2e15fe66b0ea88
F src/sqliteLimit.h 1513bfb7b20378aa0041e7022d04acb73525de35b80b252f1b83fedb4de6a76b
F src/status.c 46e7aec11f79dad50965a5ca5fa9de009f7d6bde08be2156f1538a0a296d4d0e
F src/table.c b46ad567748f24a326d9de40e5b9659f96ffff34
@ -612,7 +612,7 @@ F src/where.c 2f508c9a6dd2a42ad2b692105e77eb92cdec201d32916962ccdeff71836acb52
F src/whereInt.h 1b728f71654ebf8421a1715497a587f02d6f538e819af58dc826908f8577e810
F src/wherecode.c 37a1004237d630d785c47bba2290eac652a7a8b0047518eba3cb7c808b604c4a
F src/whereexpr.c 5e559bdd24b06e3bc2e68f258bf751302954dc1e432daf71fdd8098a71462326
F src/window.c cdce0114be495ceb97b92abfb437e75fa4a56cfae84895579af1374e2025d0cd
F src/window.c 2c958b4bdccbd5a7450621541563d4263e50953a4454d97d44171aab2c549d30
F test/8_3_names.test ebbb5cd36741350040fd28b432ceadf495be25b2
F test/affinity2.test a6d901b436328bd67a79b41bb0ac2663918fe3bd
F test/affinity3.test 6a101af2fc945ce2912f6fe54dd646018551710d
@ -1713,7 +1713,7 @@ F test/window7.tcl 6a1210f05d40ec89c22960213a22cd3f98d4e2f2eb20646c83c8c30d4d761
F test/window7.test ce7f865241fdd1c5c4db869cd7bb2986c3be836bc2e73649a6846dd920f63e0f
F test/window8.tcl 9e9a82ae9eea90a4a83481d641a812b974980c38f9247f3b89a6e3c8bed45518
F test/window8.test df187dc19921f7be0ab709d531d681bd80ccaac96a913a89ecee8b272b91d43f
F test/window9.test 06495ac733843849b1fca8a79d13ba330d04aba02099703198af2ba7e231f90c
F test/window9.test 4329f463a151eb12ede2a73bbcf67abf0b9f781c8a1346f4138249f228a02b7e
F test/windowerr.tcl f5acd6fbc210d7b5546c0e879d157888455cd4a17a1d3f28f07c1c8a387019e0
F test/windowerr.test a8b752402109c15aa1c5efe1b93ccb0ce1ef84fa964ae1cd6684dd0b3cc1819b
F test/windowfault.test 403693d7d951c5473f052f7ecddb61ed15ac9d212f238b8904ea270ba90f83e5
@ -1837,7 +1837,8 @@ F vsixtest/vsixtest.tcl 6a9a6ab600c25a91a7acc6293828957a386a8a93
F vsixtest/vsixtest.vcxproj.data 2ed517e100c66dc455b492e1a33350c1b20fbcdc
F vsixtest/vsixtest.vcxproj.filters 37e51ffedcdb064aad6ff33b6148725226cd608e
F vsixtest/vsixtest_TemporaryKey.pfx e5b1b036facdb453873e7084e1cae9102ccc67a0
P 2bf5a4c16457562dc942bcc6ec06d9b4e795ef3ea8e31550e18857bbebd08a76
R e8003e9b2b1103083ec3c473a6e857e0
P bd37ce3fb8dee8d538f6afc0bfc13cdc3ebdd504e6461f0130c6ecc8af585f68 d23f33168222dfa40a67dc7de58057418151989e81429e4af47617e86db04667
R a453e99f9348decd797862fc4bdc9e7c
T +closed d23f33168222dfa40a67dc7de58057418151989e81429e4af47617e86db04667
U dan
Z 52b0d7cc4076423a4b21fd3826ccf54b
Z 969d210cbdfb355e385521e5fc73fac7

View File

@ -1 +1 @@
bd37ce3fb8dee8d538f6afc0bfc13cdc3ebdd504e6461f0130c6ecc8af585f68
fd7316cda0fdfe86985f41a97dff3beba188606ec9bc6fef38a686a6976a6e01

View File

@ -1326,10 +1326,17 @@ 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 );
Select *pSelect = pWalker->u.pSelect;
Window *pWin = pExpr->y.pWin;
assert( pWin );
assert( IsWindowFunc(pExpr) );
pExpr->y.pWin->pNextWin = pWalker->u.pSelect->pWin;
pWalker->u.pSelect->pWin = pExpr->y.pWin;
if( pSelect->pWin ){
*pSelect->pWin->ppThis = pSelect->pWin->pNextWin;
pSelect->pWin->ppThis = &pWin->pNextWin;
}
pWin->pNextWin = pSelect->pWin;
pWin->ppThis = &pSelect->pWin;
pSelect->pWin = pWin;
}
return WRC_Continue;
}

View File

@ -901,6 +901,7 @@ static int resolveExprStep(Walker *pWalker, Expr *pExpr){
#ifndef SQLITE_OMIT_WINDOWFUNC
if( pWin ){
Select *pSel = pNC->pWinSelect;
assert( pWin==pExpr->y.pWin );
if( IN_RENAME_OBJECT==0 ){
sqlite3WindowUpdate(pParse, pSel->pWinDefn, pWin, pDef);
}
@ -910,8 +911,12 @@ static int resolveExprStep(Walker *pWalker, Expr *pExpr){
if( 0==pSel->pWin
|| 0==sqlite3WindowCompare(pParse, pSel->pWin, pWin, 0)
){
pExpr->y.pWin->pNextWin = pSel->pWin;
pSel->pWin = pExpr->y.pWin;
pWin->pNextWin = pSel->pWin;
if( pSel->pWin ){
pSel->pWin->ppThis = &pWin->pNextWin;
}
pSel->pWin = pWin;
pWin->ppThis = &pSel->pWin;
}
pNC->ncFlags |= NC_HasWin;
}else
@ -1295,16 +1300,15 @@ int sqlite3ResolveOrderGroupBy(
#ifndef SQLITE_OMIT_WINDOWFUNC
/*
** Walker callback for resolveRemoveWindows().
** Walker callback for windowRemoveExprFromSelect().
*/
static int resolveRemoveWindowsCb(Walker *pWalker, Expr *pExpr){
if( ExprHasProperty(pExpr, EP_WinFunc) ){
Window **pp;
for(pp=&pWalker->u.pSelect->pWin; *pp; pp=&(*pp)->pNextWin){
if( *pp==pExpr->y.pWin ){
*pp = (*pp)->pNextWin;
break;
}
Window *pWin = pExpr->y.pWin;
if( pWin->ppThis ){
*pWin->ppThis = pWin->pNextWin;
if( pWin->pNextWin ) pWin->pNextWin->ppThis = pWin->ppThis;
pWin->ppThis = 0;
}
}
return WRC_Continue;
@ -1314,16 +1318,18 @@ static int resolveRemoveWindowsCb(Walker *pWalker, Expr *pExpr){
** Remove any Window objects owned by the expression pExpr from the
** Select.pWin list of Select object pSelect.
*/
static void resolveRemoveWindows(Select *pSelect, Expr *pExpr){
Walker sWalker;
memset(&sWalker, 0, sizeof(Walker));
sWalker.xExprCallback = resolveRemoveWindowsCb;
sWalker.u.pSelect = pSelect;
sqlite3WalkExpr(&sWalker, pExpr);
static void windowRemoveExprFromSelect(Select *pSelect, Expr *pExpr){
if( pSelect->pWin ){
Walker sWalker;
memset(&sWalker, 0, sizeof(Walker));
sWalker.xExprCallback = resolveRemoveWindowsCb;
sWalker.u.pSelect = pSelect;
sqlite3WalkExpr(&sWalker, pExpr);
}
}
#else
# define resolveRemoveWindows(x,y)
#endif
# define windowRemoveExprFromSelect(a, b)
#endif /* SQLITE_OMIT_WINDOWFUNC */
/*
** pOrderBy is an ORDER BY or GROUP BY clause in SELECT statement pSelect.
@ -1394,7 +1400,7 @@ static int resolveOrderGroupBy(
/* Since this expresion is being changed into a reference
** to an identical expression in the result set, remove all Window
** objects belonging to the expression from the Select.pWin list. */
resolveRemoveWindows(pSelect, pE);
windowRemoveExprFromSelect(pSelect, pE);
pItem->u.x.iOrderByCol = j+1;
}
}

View File

@ -102,6 +102,7 @@ static void clearSelect(sqlite3 *db, Select *p, int bFree){
}
#endif
if( OK_IF_ALWAYS_TRUE(p->pWith) ) sqlite3WithDelete(db, p->pWith);
assert( p->pWin==0 );
if( bFree ) sqlite3DbFreeNN(db, p);
p = pPrior;
bFree = 1;

View File

@ -3594,6 +3594,7 @@ struct Window {
u8 eExclude; /* TK_NO, TK_CURRENT, TK_TIES, TK_GROUP, or 0 */
Expr *pStart; /* Expression for "<expr> PRECEDING" */
Expr *pEnd; /* Expression for "<expr> FOLLOWING" */
Window **ppThis; /* Pointer to this object in Select.pWin list */
Window *pNextWin; /* Next window function belonging to this SELECT */
Expr *pFilter; /* The FILTER expression */
FuncDef *pFunc; /* The function */

View File

@ -1022,6 +1022,10 @@ int sqlite3WindowRewrite(Parse *pParse, Select *p){
*/
void sqlite3WindowDelete(sqlite3 *db, Window *p){
if( p ){
if( p->ppThis ){
*p->ppThis = p->pNextWin;
if( p->pNextWin ) p->pNextWin->ppThis = p->ppThis;
}
sqlite3ExprDelete(db, p->pFilter);
sqlite3ExprListDelete(db, p->pPartition);
sqlite3ExprListDelete(db, p->pOrderBy);

View File

@ -98,5 +98,29 @@ do_execsql_test 2.2.2 {
SELECT b=='2', rank() OVER (ORDER BY a) FROM t1
} {1 1 0 2}
#-------------------------------------------------------------------------
reset_db
do_execsql_test 3.0 {
CREATE TABLE t1(a);
CREATE TABLE t2(a,b,c);
}
do_execsql_test 3.1 {
SELECT EXISTS(SELECT 1 FROM t1 ORDER BY sum(a) OVER ()) FROM t1;
}
do_execsql_test 3.2 {
SELECT sum(a) OVER () FROM t2
ORDER BY EXISTS(SELECT 1 FROM t2 ORDER BY sum(a) OVER ());
}
do_catchsql_test 3.3 {
SELECT a, sum(a) OVER (ORDER BY a DESC) FROM t2
ORDER BY EXISTS(
SELECT 1 FROM t2 ORDER BY sum(a) OVER (ORDER BY a)
) OVER (ORDER BY a);
} {1 {near "OVER": syntax error}}
finish_test