Index: src/expr.c ================================================================== --- src/expr.c +++ src/expr.c @@ -1114,11 +1114,11 @@ ** return value with EP_Reduced|EP_TokenOnly. ** ** Note that with flags==EXPRDUP_REDUCE, this routines works on full-size ** (unreduced) Expr objects as they or originally constructed by the parser. ** During expression analysis, extra information is computed and moved into -** later parts of teh Expr object and that extra information might get chopped +** later parts of the Expr object and that extra information might get chopped ** off if the expression is reduced. Note also that it does not work to ** make an EXPRDUP_REDUCE copy of a reduced expression. It is only legal ** to reduce a pristine expression tree from the parser. The implementation ** of dupedExprStructSize() contain multiple assert() statements that attempt ** to enforce this constraint. @@ -1126,11 +1126,11 @@ static int dupedExprStructSize(Expr *p, int flags){ int nSize; assert( flags==EXPRDUP_REDUCE || flags==0 ); /* Only one flag value allowed */ assert( EXPR_FULLSIZE<=0xfff ); assert( (0xfff & (EP_Reduced|EP_TokenOnly))==0 ); - if( 0==flags || p->op==TK_SELECT_COLUMN ){ + if( 0==flags || p->op==TK_SELECT_COLUMN || p->pWin ){ nSize = EXPR_FULLSIZE; }else{ assert( !ExprHasProperty(p, EP_TokenOnly|EP_Reduced) ); assert( !ExprHasProperty(p, EP_FromJoin) ); assert( !ExprHasProperty(p, EP_MemToken) ); @@ -1478,11 +1478,11 @@ pNew->addrOpenEphm[0] = -1; pNew->addrOpenEphm[1] = -1; pNew->nSelectRow = p->nSelectRow; pNew->pWith = withDup(db, p->pWith); pNew->pWin = 0; - pNew->pWinDefn = 0; /* TODO!! */ + pNew->pWinDefn = sqlite3WindowListDup(db, p->pWinDefn); sqlite3SelectSetName(pNew, p->zSelName); *pp = pNew; pp = &pNew->pPrior; pNext = pNew; } Index: src/sqliteInt.h ================================================================== --- src/sqliteInt.h +++ src/sqliteInt.h @@ -3526,10 +3526,11 @@ void sqlite3WindowCodeStep(Parse*, Select*, WhereInfo*, int, int); int sqlite3WindowRewrite(Parse*, Select*); int sqlite3ExpandSubquery(Parse*, struct SrcList_item*); void sqlite3WindowUpdate(Parse*, Window*, Window*, FuncDef*); Window *sqlite3WindowDup(sqlite3 *db, Expr *pOwner, Window *p); +Window *sqlite3WindowListDup(sqlite3 *db, Window *p); void sqlite3WindowFunctions(void); /* ** Assuming zIn points to the first byte of a UTF-8 character, ** advance zIn to point to the first byte of the next UTF-8 character. Index: src/window.c ================================================================== --- src/window.c +++ src/window.c @@ -501,11 +501,11 @@ Parse *pParse, Window *pList, /* List of named windows for this SELECT */ Window *pWin, /* Window frame to update */ FuncDef *pFunc /* Window function definition */ ){ - if( pWin->zName ){ + if( pWin->zName && pWin->eType==0 ){ Window *p; for(p=pList; p; p=p->pNextWin){ if( sqlite3StrICmp(p->zName, pWin->zName)==0 ) break; } if( p==0 ){ @@ -516,10 +516,11 @@ pWin->pOrderBy = sqlite3ExprListDup(pParse->db, p->pOrderBy, 0); pWin->pStart = sqlite3ExprDup(pParse->db, p->pStart, 0); pWin->pEnd = sqlite3ExprDup(pParse->db, p->pEnd, 0); pWin->eStart = p->eStart; pWin->eEnd = p->eEnd; + pWin->eType = p->eType; } if( pFunc->funcFlags & SQLITE_FUNC_WINDOW ){ sqlite3 *db = pParse->db; if( pWin->pFilter ){ sqlite3ErrorMsg(pParse, @@ -798,10 +799,11 @@ int eEnd, Expr *pEnd ){ Window *pWin = (Window*)sqlite3DbMallocZero(pParse->db, sizeof(Window)); if( pWin ){ + assert( eType ); pWin->eType = eType; pWin->eStart = eStart; pWin->eEnd = eEnd; pWin->pEnd = pEnd; pWin->pStart = pStart; @@ -1916,10 +1918,11 @@ Window *sqlite3WindowDup(sqlite3 *db, Expr *pOwner, Window *p){ Window *pNew = 0; if( p ){ pNew = sqlite3DbMallocZero(db, sizeof(Window)); if( pNew ){ + pNew->zName = sqlite3DbStrDup(db, p->zName); pNew->pFilter = sqlite3ExprDup(db, p->pFilter, 0); pNew->pPartition = sqlite3ExprListDup(db, p->pPartition, 0); pNew->pOrderBy = sqlite3ExprListDup(db, p->pOrderBy, 0); pNew->eType = p->eType; pNew->eEnd = p->eEnd; @@ -1929,10 +1932,28 @@ pNew->pOwner = pOwner; } } return pNew; } + +/* +** Return a copy of the linked list of Window objects passed as the +** second argument. +*/ +Window *sqlite3WindowListDup(sqlite3 *db, Window *p){ + Window *pWin; + Window *pRet = 0; + Window **pp = &pRet; + + for(pWin=p; pWin; pWin=pWin->pNextWin){ + *pp = sqlite3WindowDup(db, 0, pWin); + if( *pp==0 ) break; + pp = &((*pp)->pNextWin); + } + + return pRet; +} /* ** sqlite3WhereBegin() has already been called for the SELECT statement ** passed as the second argument when this function is invoked. It generates ** code to populate the Window.regResult register for each window function and Index: test/window1.test ================================================================== --- test/window1.test +++ test/window1.test @@ -267,15 +267,57 @@ do_execsql_test 7.3 { SELECT row_number() OVER (ORDER BY x) FROM t1 } {1 2 3 4 5} +breakpoint do_execsql_test 7.4 { SELECT row_number() OVER win, lead(x) OVER win FROM t1 WINDOW win AS (ORDER BY x ROWS BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW) } {1 3 2 5 3 7 4 9 5 {}} + +#------------------------------------------------------------------------- +# Attempt to use a window function in a view. +# +do_execsql_test 8.0 { + CREATE TABLE t3(a, b, c); + + WITH s(i) AS ( VALUES(1) UNION ALL SELECT i+1 FROM s WHERE i<6 ) + INSERT INTO t3 SELECT i, i, i FROM s; + + CREATE VIEW v1 AS SELECT + sum(b) OVER (ORDER BY c), + min(b) OVER (ORDER BY c), + max(b) OVER (ORDER BY c) + FROM t3; + + CREATE VIEW v2 AS SELECT + sum(b) OVER win, + min(b) OVER win, + max(b) OVER win + FROM t3 + WINDOW win AS (ORDER BY c); +} + +do_execsql_test 8.1.1 { + SELECT * FROM v1 +} {1 1 1 3 1 2 6 1 3 10 1 4 15 1 5 21 1 6} +do_execsql_test 8.1.2 { + SELECT * FROM v2 +} {1 1 1 3 1 2 6 1 3 10 1 4 15 1 5 21 1 6} + +db close +sqlite3 db test.db +do_execsql_test 8.2.1 { + SELECT * FROM v1 +} {1 1 1 3 1 2 6 1 3 10 1 4 15 1 5 21 1 6} +do_execsql_test 8.2.2 { + SELECT * FROM v2 +} {1 1 1 3 1 2 6 1 3 10 1 4 15 1 5 21 1 6} + + finish_test