Omit all window-function related code when building with SQLITE_OMIT_WINDOWFUNC.

FossilOrigin-Name: 5f04b016467342b5a796bf702ed25b621eb86f2961c1e703d276c93f2cb6aa89
diff --git a/src/btree.c b/src/btree.c
index 35e1347..d2c580d 100644
--- a/src/btree.c
+++ b/src/btree.c
@@ -5190,12 +5190,14 @@
 ** Otherwise, if pCur is valid, configure it so that the next call to
 ** sqlite3BtreeNext() is a no-op.
 */
+#ifndef SQLITE_OMIT_WINDOWFUNC
 void sqlite3BtreeSkipNext(BtCursor *pCur){
   if( pCur->eState==CURSOR_VALID ){
     pCur->eState = CURSOR_SKIPNEXT;
     pCur->skipNext = 1;
   }
 }
+#endif /* SQLITE_OMIT_WINDOWFUNC */
 
 /* Move the cursor to the last entry in the table.  Return SQLITE_OK
 ** on success.  Set *pRes to 0 if the cursor actually points to something
diff --git a/src/btree.h b/src/btree.h
index 0b97849..f6cf55b 100644
--- a/src/btree.h
+++ b/src/btree.h
@@ -301,7 +301,9 @@
 int sqlite3BtreeInsert(BtCursor*, const BtreePayload *pPayload,
                        int flags, int seekResult);
 int sqlite3BtreeFirst(BtCursor*, int *pRes);
+#ifndef SQLITE_OMIT_WINDOWFUNC
 void sqlite3BtreeSkipNext(BtCursor*);
+#endif
 int sqlite3BtreeLast(BtCursor*, int *pRes);
 int sqlite3BtreeNext(BtCursor*, int flags);
 int sqlite3BtreeEof(BtCursor*);
diff --git a/src/expr.c b/src/expr.c
index 1969426..24d1cb4 100644
--- a/src/expr.c
+++ b/src/expr.c
@@ -772,7 +772,6 @@
     memset(pNew, 0, sizeof(Expr));
     pNew->op = (u8)op;
     pNew->iAgg = -1;
-    pNew->pWin = 0;
     if( pToken ){
       if( nExtra==0 ){
         pNew->flags |= EP_IntValue|EP_Leaf;
@@ -862,7 +861,6 @@
       memset(p, 0, sizeof(Expr));
       p->op = op & TKFLG_MASK;
       p->iAgg = -1;
-      p->pWin = 0;
     }
     sqlite3ExprAttachSubtrees(pParse->db, p, pLeft, pRight);
   }
@@ -1128,7 +1126,11 @@
   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 || p->pWin ){
+  if( 0==flags || p->op==TK_SELECT_COLUMN 
+#ifndef SQLITE_OMIT_WINDOWFUNC
+   || p->pWin 
+#endif
+  ){
     nSize = EXPR_FULLSIZE;
   }else{
     assert( !ExprHasProperty(p, EP_TokenOnly|EP_Reduced) );
@@ -1268,11 +1270,13 @@
         *pzBuffer = zAlloc;
       }
     }else{
+#ifndef SQLITE_OMIT_WINDOWFUNC
       if( ExprHasProperty(p, EP_Reduced|EP_TokenOnly) ){
         pNew->pWin = 0;
       }else{
         pNew->pWin = sqlite3WindowDup(db, pNew, p->pWin);
       }
+#endif /* SQLITE_OMIT_WINDOWFUNC */
       if( !ExprHasProperty(p, EP_TokenOnly|EP_Leaf) ){
         if( pNew->op==TK_SELECT_COLUMN ){
           pNew->pLeft = p->pLeft;
@@ -1479,8 +1483,10 @@
     pNew->addrOpenEphm[1] = -1;
     pNew->nSelectRow = p->nSelectRow;
     pNew->pWith = withDup(db, p->pWith);
+#ifndef SQLITE_OMIT_WINDOWFUNC
     pNew->pWin = 0;
     pNew->pWinDefn = sqlite3WindowListDup(db, p->pWinDefn);
+#endif
     sqlite3SelectSetName(pNew, p->zSelName);
     *pp = pNew;
     pp = &pNew->pPrior;
@@ -3800,9 +3806,11 @@
       u8 enc = ENC(db);      /* The text encoding used by this database */
       CollSeq *pColl = 0;    /* A collating sequence */
 
+#ifndef SQLITE_OMIT_WINDOWFUNC
       if( !ExprHasProperty(pExpr, EP_TokenOnly|EP_Reduced) && pExpr->pWin ){
         return pExpr->pWin->regResult;
       }
+#endif
 
       if( ConstFactorOk(pParse) && sqlite3ExprIsConstantNotJoin(pExpr) ){
         /* SQL functions can be expensive. So try to move constant functions
diff --git a/src/func.c b/src/func.c
index 57a1f0f..c5dd6a5 100644
--- a/src/func.c
+++ b/src/func.c
@@ -1513,6 +1513,7 @@
     }
   }
 }
+#ifndef SQLITE_OMIT_WINDOWFUNC
 static void sumInverse(sqlite3_context *context, int argc, sqlite3_value**argv){
   SumCtx *p;
   int type;
@@ -1534,6 +1535,9 @@
     }
   }
 }
+#else
+# define sumInverse 0
+#endif /* SQLITE_OMIT_WINDOWFUNC */
 static void sumFinalize(sqlite3_context *context){
   SumCtx *p;
   p = sqlite3_aggregate_context(context, 0);
@@ -1646,9 +1650,13 @@
     if( bValue==0 ) sqlite3VdbeMemRelease(pRes);
   }
 }
+#ifndef SQLITE_OMIT_WINDOWFUNC
 static void minMaxValue(sqlite3_context *context){
   return minMaxValueFinalize(context, 1);
 }
+#else
+# define minMaxValue 0
+#endif /* SQLITE_OMIT_WINDOWFUNC */
 static void minMaxFinalize(sqlite3_context *context){
   return minMaxValueFinalize(context, 0);
 }
@@ -1688,6 +1696,7 @@
     if( zVal ) sqlite3_str_append(pAccum, zVal, nVal);
   }
 }
+#ifndef SQLITE_OMIT_WINDOWFUNC
 static void groupConcatInverse(
   sqlite3_context *context,
   int argc,
@@ -1712,6 +1721,9 @@
     if( pAccum->nChar==0 ) pAccum->mxAlloc = 0;
   }
 }
+#else
+# define groupConcatInverse 0
+#endif /* SQLITE_OMIT_WINDOWFUNC */
 static void groupConcatFinalize(sqlite3_context *context){
   StrAccum *pAccum;
   pAccum = sqlite3_aggregate_context(context, 0);
@@ -1726,6 +1738,7 @@
     }
   }
 }
+#ifndef SQLITE_OMIT_WINDOWFUNC
 static void groupConcatValue(sqlite3_context *context){
   sqlite3_str *pAccum;
   pAccum = (sqlite3_str*)sqlite3_aggregate_context(context, 0);
@@ -1740,6 +1753,9 @@
     }
   }
 }
+#else
+# define groupConcatValue 0
+#endif /* SQLITE_OMIT_WINDOWFUNC */
 
 /*
 ** This routine does per-connection function registration.  Most
diff --git a/src/parse.y b/src/parse.y
index d28ffc3..b253480 100644
--- a/src/parse.y
+++ b/src/parse.y
@@ -529,17 +529,22 @@
 multiselect_op(A) ::= EXCEPT|INTERSECT(OP).  {A = @OP; /*A-overwrites-OP*/}
 %endif SQLITE_OMIT_COMPOUND_SELECT
 oneselect(A) ::= SELECT(S) distinct(D) selcollist(W) from(X) where_opt(Y)
-                 groupby_opt(P) having_opt(Q) windowdefn_opt(R)
+                 groupby_opt(P) having_opt(Q) 
+%ifndef SQLITE_OMIT_WINDOWFUNC
+                 windowdefn_opt(R)
+%endif
                  orderby_opt(Z) limit_opt(L). {
 #if SELECTTRACE_ENABLED
   Token s = S; /*A-overwrites-S*/
 #endif
   A = sqlite3SelectNew(pParse,W,X,Y,P,Q,Z,D,L);
+#ifndef SQLITE_OMIT_WINDOWFUNC
   if( A ){
     A->pWinDefn = R;
   }else{
     sqlite3WindowListDelete(pParse->db, R);
   }
+#endif // SQLITE_OMIT_WINDOWFUNC
 #if SELECTTRACE_ENABLED
   /* Populate the Select.zSelName[] string that is used to help with
   ** query planner debugging, to differentiate between multiple Select
@@ -1011,7 +1016,11 @@
   sqlite3ExprAttachSubtrees(pParse->db, A, E, 0);
 }
 %endif  SQLITE_OMIT_CAST
-expr(A) ::= id(X) LP distinct(D) exprlist(Y) RP over_opt(Z). {
+expr(A) ::= id(X) LP distinct(D) exprlist(Y) RP 
+%ifndef SQLITE_OMIT_WINDOWFUNC
+  over_opt(Z)
+%endif
+. {
   if( Y && Y->nExpr>pParse->db->aLimit[SQLITE_LIMIT_FUNCTION_ARG] ){
     sqlite3ErrorMsg(pParse, "too many arguments on function %T", &X);
   }
@@ -1021,7 +1030,11 @@
     A->flags |= EP_Distinct;
   }
 }
-expr(A) ::= id(X) LP STAR RP over_opt(Z). {
+expr(A) ::= id(X) LP STAR RP
+%ifndef SQLITE_OMIT_WINDOWFUNC
+  over_opt(Z)
+%endif
+. {
   A = sqlite3ExprFunction(pParse, 0, &X);
   sqlite3WindowAttach(pParse, A, Z);
 }
@@ -1029,6 +1042,8 @@
   A = sqlite3ExprFunction(pParse, 0, &OP);
 }
 
+%ifndef SQLITE_OMIT_WINDOWFUNC
+
 %type windowdefn_opt {Window*}
 %destructor windowdefn_opt {sqlite3WindowDelete(pParse->db, $$);}
 windowdefn_opt(A) ::= . { A = 0; }
@@ -1121,6 +1136,7 @@
 frame_bound(A) ::= expr(X) FOLLOWING.   { A.eType = TK_FOLLOWING; A.pExpr = X; }
 frame_bound(A) ::= UNBOUNDED FOLLOWING. { A.eType = TK_UNBOUNDED; A.pExpr = 0; }
 
+%endif // SQLITE_OMIT_WINDOWFUNC
 
 expr(A) ::= LP nexprlist(X) COMMA expr(Y) RP. {
   ExprList *pList = sqlite3ExprListAppend(pParse, X, Y);
diff --git a/src/resolve.c b/src/resolve.c
index 221564f..84ec659 100644
--- a/src/resolve.c
+++ b/src/resolve.c
@@ -757,6 +757,7 @@
         }
       }
 
+#ifndef SQLITE_OMIT_WINDOWFUNC
       if( is_agg==0 && pExpr->pWin ){
         sqlite3ErrorMsg(pParse, 
             "%.*s() may not be used as a window function", nId, zId
@@ -773,6 +774,10 @@
         }else{
           zType = "aggregate";
         }
+#else
+      if( (is_agg && (pNC->ncFlags & NC_AllowAgg)==0) ){
+        const char *zType = "aggregate";
+#endif
         sqlite3ErrorMsg(pParse, "misuse of %s function %.*s()", zType, nId,zId);
         pNC->nErr++;
         is_agg = 0;
@@ -791,6 +796,7 @@
       if( is_agg ) pNC->ncFlags &= ~NC_AllowAgg;
       sqlite3WalkExprList(pWalker, pList);
       if( is_agg ){
+#ifndef SQLITE_OMIT_WINDOWFUNC
         if( pExpr->pWin ){
           Select *pSel = pNC->pWinSelect;
           sqlite3WindowUpdate(pParse, pSel->pWinDefn, pExpr->pWin, pDef);
@@ -800,7 +806,9 @@
             pExpr->pWin->pNextWin = pSel->pWin;
             pSel->pWin = pExpr->pWin;
           }
-        }else{
+        }else
+#endif /* SQLITE_OMIT_WINDOWFUNC */
+        {
           NameContext *pNC2 = pNC;
           pExpr->op = TK_AGG_FUNCTION;
           pExpr->op2 = 0;
@@ -1264,7 +1272,6 @@
   nCompound = 0;
   pLeftmost = p;
   while( p ){
-    assert( p->pWin==0 );
     assert( (p->selFlags & SF_Expanded)!=0 );
     assert( (p->selFlags & SF_Resolved)==0 );
     p->selFlags |= SF_Resolved;
diff --git a/src/select.c b/src/select.c
index 2c07504..43228b6 100644
--- a/src/select.c
+++ b/src/select.c
@@ -96,9 +96,11 @@
     sqlite3ExprDelete(db, p->pHaving);
     sqlite3ExprListDelete(db, p->pOrderBy);
     sqlite3ExprDelete(db, p->pLimit);
+#ifndef SQLITE_OMIT_WINDOWFUNC
     if( OK_IF_ALWAYS_TRUE(p->pWinDefn) ){
       sqlite3WindowListDelete(db, p->pWinDefn);
     }
+#endif
     if( OK_IF_ALWAYS_TRUE(p->pWith) ) sqlite3WithDelete(db, p->pWith);
     if( bFree ) sqlite3DbFreeNN(db, p);
     p = pPrior;
@@ -165,8 +167,10 @@
   pNew->pNext = 0;
   pNew->pLimit = pLimit;
   pNew->pWith = 0;
+#ifndef SQLITE_OMIT_WINDOWFUNC
   pNew->pWin = 0;
   pNew->pWinDefn = 0;
+#endif
   if( pParse->db->mallocFailed ) {
     clearSelect(pParse->db, pNew, pNew!=&standin);
     pNew = 0;
@@ -3720,7 +3724,9 @@
   pSub = pSubitem->pSelect;
   assert( pSub!=0 );
 
+#ifndef SQLITE_OMIT_WINDOWFUNC
   if( p->pWin || pSub->pWin ) return 0;                  /* Restriction (25) */
+#endif
 
   pSubSrc = pSub->pSrc;
   assert( pSubSrc );
@@ -5487,7 +5493,8 @@
     generateColumnNames(pParse, p);
   }
 
-  if( (rc = sqlite3WindowRewrite(pParse, p)) ){
+#ifndef SQLITE_OMIT_WINDOWFUNC
+  if( sqlite3WindowRewrite(pParse, p) ){
     goto select_end;
   }
 #if SELECTTRACE_ENABLED
@@ -5496,6 +5503,7 @@
     sqlite3TreeViewSelect(0, p, 0);
   }
 #endif
+#endif /* SQLITE_OMIT_WINDOWFUNC */
   pTabList = p->pSrc;
   isAgg = (p->selFlags & SF_Aggregate)!=0;
 
@@ -5873,16 +5881,17 @@
   }
 
   if( !isAgg && pGroupBy==0 ){
-    Window *pWin = p->pWin;      /* Master window object (or NULL) */
-
     /* No aggregate functions and no GROUP BY clause */
-    u16 wctrlFlags = (sDistinct.isTnct ? WHERE_WANT_DISTINCT : 0);
-    assert( WHERE_USE_LIMIT==SF_FixedLimit );
-    wctrlFlags |= p->selFlags & SF_FixedLimit;
-
+    u16 wctrlFlags = (sDistinct.isTnct ? WHERE_WANT_DISTINCT : 0)
+                   | (p->selFlags & SF_FixedLimit);
+#ifndef SQLITE_OMIT_WINDOWFUNC
+    Window *pWin = p->pWin;      /* Master window object (or NULL) */
     if( pWin ){
       sqlite3WindowCodeInit(pParse, pWin);
     }
+#endif
+    assert( WHERE_USE_LIMIT==SF_FixedLimit );
+
 
     /* Begin the database scan. */
     SELECTTRACE(1,pParse,p,("WhereBegin\n"));
@@ -5912,6 +5921,7 @@
     }
 
     assert( p->pEList==pEList );
+#ifndef SQLITE_OMIT_WINDOWFUNC
     if( pWin ){
       int addrGosub = sqlite3VdbeMakeLabel(v);
       int iCont = sqlite3VdbeMakeLabel(v);
@@ -5927,7 +5937,9 @@
       sqlite3VdbeAddOp1(v, OP_Return, regGosub);
       sqlite3VdbeJumpHere(v, addr);
 
-    }else{
+    }else
+#endif /* SQLITE_OMIT_WINDOWFUNC */
+    {
       /* Use the standard inner loop. */
       selectInnerLoop(pParse, p, -1, &sSort, &sDistinct, pDest,
           sqlite3WhereContinueLabel(pWInfo),
diff --git a/src/sqliteInt.h b/src/sqliteInt.h
index 294aaef..9fab8f5 100644
--- a/src/sqliteInt.h
+++ b/src/sqliteInt.h
@@ -2466,7 +2466,9 @@
   AggInfo *pAggInfo;     /* Used by TK_AGG_COLUMN and TK_AGG_FUNCTION */
   Table *pTab;           /* Table for TK_COLUMN expressions.  Can be NULL
                          ** for a column of an index on an expression */
+#ifndef SQLITE_OMIT_WINDOWFUNC
   Window *pWin;          /* Window definition for window functions */
+#endif
 };
 
 /*
@@ -2819,8 +2821,10 @@
   Select *pNext;         /* Next select to the left in a compound */
   Expr *pLimit;          /* LIMIT expression. NULL means not used. */
   With *pWith;           /* WITH clause attached to this select. Or NULL. */
+#ifndef SQLITE_OMIT_WINDOWFUNC
   Window *pWin;          /* List of window functions */
   Window *pWinDefn;      /* List of named window definitions */
+#endif
 };
 
 /*
@@ -3517,6 +3521,7 @@
   int iArgCol;            /* Offset of first argument for this function */
 };
 
+#ifndef SQLITE_OMIT_WINDOWFUNC
 void sqlite3WindowDelete(sqlite3*, Window*);
 void sqlite3WindowListDelete(sqlite3 *db, Window *p);
 Window *sqlite3WindowAlloc(Parse*, int, int, Expr*, int , Expr*);
@@ -3530,6 +3535,11 @@
 Window *sqlite3WindowDup(sqlite3 *db, Expr *pOwner, Window *p);
 Window *sqlite3WindowListDup(sqlite3 *db, Window *p);
 void sqlite3WindowFunctions(void);
+#else
+# define sqlite3WindowDelete(a,b)
+# define sqlite3WindowFunctions()
+# define sqlite3WindowAttach(a,b,c)
+#endif
 
 /*
 ** Assuming zIn points to the first byte of a UTF-8 character,
diff --git a/src/test_config.c b/src/test_config.c
index aa0626a..d1837d4 100644
--- a/src/test_config.c
+++ b/src/test_config.c
@@ -762,6 +762,12 @@
   Tcl_SetVar2(interp, "sqlite_options", "uri_00_error", "0", TCL_GLOBAL_ONLY);
 #endif
 
+#ifdef SQLITE_OMIT_WINDOWFUNC
+  Tcl_SetVar2(interp, "sqlite_options", "windowfunc", "0", TCL_GLOBAL_ONLY);
+#else
+  Tcl_SetVar2(interp, "sqlite_options", "windowfunc", "1", TCL_GLOBAL_ONLY);
+#endif
+
 #define LINKVAR(x) { \
     static const int cv_ ## x = SQLITE_ ## x; \
     Tcl_LinkVar(interp, "SQLITE_" #x, (char *)&(cv_ ## x), \
diff --git a/src/vdbe.c b/src/vdbe.c
index 2c8920f..6eb5f95 100644
--- a/src/vdbe.c
+++ b/src/vdbe.c
@@ -5118,7 +5118,9 @@
     pCrsr = pC->uc.pCursor;
     assert( pCrsr );
     rc = sqlite3BtreeFirst(pCrsr, &res);
+#ifndef SQLITE_OMIT_WINDOWFUNC
     if( pOp->p5 ) sqlite3BtreeSkipNext(pCrsr);
+#endif
     pC->deferredMoveto = 0;
     pC->cacheStatus = CACHE_STALE;
   }
@@ -6369,8 +6371,13 @@
   assert( pCtx->pOut->flags==MEM_Null );
   assert( pCtx->isError==0 );
   assert( pCtx->skipFlag==0 );
-  (pOp->p1 ? (pCtx->pFunc->xInverse) : (pCtx->pFunc->xSFunc))
-    (pCtx,pCtx->argc,pCtx->argv); /* IMP: R-24505-23230 */
+#ifndef SQLITE_OMIT_WINDOWFUNC
+  if( pOp->p1 ){
+    (pCtx->pFunc->xInverse)(pCtx,pCtx->argc,pCtx->argv);
+  }else
+#endif
+  (pCtx->pFunc->xSFunc)(pCtx,pCtx->argc,pCtx->argv); /* IMP: R-24505-23230 */
+
   if( pCtx->isError ){
     if( pCtx->isError>0 ){
       sqlite3VdbeError(p, "%s", sqlite3_value_text(pCtx->pOut));
@@ -6412,12 +6419,14 @@
   assert( pOp->p1>0 && pOp->p1<=(p->nMem+1 - p->nCursor) );
   pMem = &aMem[pOp->p1];
   assert( (pMem->flags & ~(MEM_Null|MEM_Agg))==0 );
+#ifndef SQLITE_OMIT_WINDOWFUNC
   if( pOp->p3 ){
     rc = sqlite3VdbeMemAggValue(pMem, &aMem[pOp->p3], pOp->p4.pFunc);
     pMem = &aMem[pOp->p3];
-  }else{
-    rc = sqlite3VdbeMemFinalize(pMem, pOp->p4.pFunc);
-  }
+  }else
+#endif
+  rc = sqlite3VdbeMemFinalize(pMem, pOp->p4.pFunc);
+  
   if( rc ){
     sqlite3VdbeError(p, "%s", sqlite3_value_text(pMem));
     goto abort_due_to_error;
diff --git a/src/vdbeInt.h b/src/vdbeInt.h
index b0008c3..d6c5660 100644
--- a/src/vdbeInt.h
+++ b/src/vdbeInt.h
@@ -497,7 +497,9 @@
 int sqlite3VdbeMemFromBtree(BtCursor*,u32,u32,Mem*);
 void sqlite3VdbeMemRelease(Mem *p);
 int sqlite3VdbeMemFinalize(Mem*, FuncDef*);
+#ifndef SQLITE_OMIT_WINDOWFUNC
 int sqlite3VdbeMemAggValue(Mem*, Mem*, FuncDef*);
+#endif
 const char *sqlite3OpcodeName(int);
 int sqlite3VdbeMemGrow(Mem *pMem, int n, int preserve);
 int sqlite3VdbeMemClearAndResize(Mem *pMem, int n);
diff --git a/src/vdbemem.c b/src/vdbemem.c
index 6e0f8d6..dc0a3f9 100644
--- a/src/vdbemem.c
+++ b/src/vdbemem.c
@@ -423,6 +423,7 @@
 ** SQLITE_ERROR is returned if xValue() reports an error. SQLITE_OK 
 ** otherwise.
 */
+#ifndef SQLITE_OMIT_WINDOWFUNC
 int sqlite3VdbeMemAggValue(Mem *pAccum, Mem *pOut, FuncDef *pFunc){
   sqlite3_context ctx;
   Mem t;
@@ -440,6 +441,7 @@
   pFunc->xValue(&ctx);
   return ctx.isError;
 }
+#endif /* SQLITE_OMIT_WINDOWFUNC */
 
 /*
 ** If the memory cell contains a value that must be freed by
diff --git a/src/window.c b/src/window.c
index ff65d15..5aa8800 100644
--- a/src/window.c
+++ b/src/window.c
@@ -12,6 +12,8 @@
 */
 #include "sqliteInt.h"
 
+#ifndef SQLITE_OMIT_WINDOWFUNC
+
 /*
 ** SELECT REWRITING
 **
@@ -2066,3 +2068,4 @@
   }
 }
 
+#endif /* SQLITE_OMIT_WINDOWFUNC */