diff options
Diffstat (limited to 'src')
-rw-r--r-- | src/func.c | 23 | ||||
-rw-r--r-- | src/resolve.c | 6 | ||||
-rw-r--r-- | src/test_window.c | 27 | ||||
-rw-r--r-- | src/window.c | 18 |
4 files changed, 63 insertions, 11 deletions
diff --git a/src/func.c b/src/func.c index c5dd6a58e..ff7c3049f 100644 --- a/src/func.c +++ b/src/func.c @@ -1572,6 +1572,9 @@ static void totalFinalize(sqlite3_context *context){ typedef struct CountCtx CountCtx; struct CountCtx { i64 n; +#ifdef SQLITE_DEBUG + int bInverse; /* True if xInverse() ever called */ +#endif }; /* @@ -1589,7 +1592,7 @@ static void countStep(sqlite3_context *context, int argc, sqlite3_value **argv){ ** sure it still operates correctly, verify that its count agrees with our ** internal count when using count(*) and when the total count can be ** expressed as a 32-bit integer. */ - assert( argc==1 || p==0 || p->n>0x7fffffff + assert( argc==1 || p==0 || p->n>0x7fffffff || p->bInverse || p->n==sqlite3_aggregate_count(context) ); #endif } @@ -1598,6 +1601,18 @@ static void countFinalize(sqlite3_context *context){ p = sqlite3_aggregate_context(context, 0); sqlite3_result_int64(context, p ? p->n : 0); } +#ifndef SQLITE_OMIT_WINDOWFUNC +static void countInverse(sqlite3_context *ctx, int argc, sqlite3_value **argv){ + CountCtx *p; + p = sqlite3_aggregate_context(ctx, sizeof(*p)); + if( (argc==0 || SQLITE_NULL!=sqlite3_value_type(argv[0])) && p ){ + p->n--; +#ifdef SQLITE_DEBUG + p->bInverse = 1; +#endif + } +} +#endif /* ** Routines to implement min() and max() aggregate functions. @@ -1943,8 +1958,10 @@ void sqlite3RegisterBuiltinFunctions(void){ WAGGREGATE(sum, 1,0,0, sumStep, sumFinalize, sumFinalize, sumInverse, 0), WAGGREGATE(total, 1,0,0, sumStep,totalFinalize,totalFinalize,sumInverse, 0), WAGGREGATE(avg, 1,0,0, sumStep, avgFinalize, avgFinalize, sumInverse, 0), - AGGREGATE2(count, 0,0,0, countStep, countFinalize, SQLITE_FUNC_COUNT ), - WAGGREGATE(count, 1,0,0, countStep, countFinalize, 0, 0, 0 ), + WAGGREGATE(count, 0,0,0, countStep, + countFinalize, countFinalize, countInverse, SQLITE_FUNC_COUNT ), + WAGGREGATE(count, 1,0,0, countStep, + countFinalize, countFinalize, countInverse, 0 ), WAGGREGATE(group_concat, 1, 0, 0, groupConcatStep, groupConcatFinalize, groupConcatValue, groupConcatInverse, 0), WAGGREGATE(group_concat, 2, 0, 0, groupConcatStep, diff --git a/src/resolve.c b/src/resolve.c index b6389eba6..7c1d4b162 100644 --- a/src/resolve.c +++ b/src/resolve.c @@ -758,7 +758,11 @@ static int resolveExprStep(Walker *pWalker, Expr *pExpr){ } #ifndef SQLITE_OMIT_WINDOWFUNC - if( is_agg==0 && pExpr->pWin ){ + 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 && pExpr->pWin ){ sqlite3ErrorMsg(pParse, "%.*s() may not be used as a window function", nId, zId ); diff --git a/src/test_window.c b/src/test_window.c index 576c58f6c..1b43c2717 100644 --- a/src/test_window.c +++ b/src/test_window.c @@ -302,6 +302,32 @@ static int SQLITE_TCLAPI test_create_sumint( return TCL_OK; } +static int SQLITE_TCLAPI test_override_sum( + void * clientData, + Tcl_Interp *interp, + int objc, + Tcl_Obj *CONST objv[] +){ + sqlite3 *db; + int rc; + + if( objc!=2 ){ + Tcl_WrongNumArgs(interp, 1, objv, "DB"); + return TCL_ERROR; + } + if( getDbPointer(interp, Tcl_GetString(objv[1]), &db) ) return TCL_ERROR; + + rc = sqlite3_create_function(db, "sum", -1, SQLITE_UTF8, 0, + 0, sumintStep, sumintFinal + ); + + if( rc!=SQLITE_OK ){ + Tcl_SetObjResult(interp, Tcl_NewStringObj(sqlite3ErrName(rc), -1)); + return TCL_ERROR; + } + return TCL_OK; +} + int Sqlitetest_window_Init(Tcl_Interp *interp){ static struct { char *zName; @@ -311,6 +337,7 @@ int Sqlitetest_window_Init(Tcl_Interp *interp){ { "sqlite3_create_window_function", test_create_window, 0 }, { "test_create_window_function_misuse", test_create_window_misuse, 0 }, { "test_create_sumint", test_create_sumint, 0 }, + { "test_override_sum", test_override_sum, 0 }, }; int i; for(i=0; i<sizeof(aObjCmd)/sizeof(aObjCmd[0]); i++){ diff --git a/src/window.c b/src/window.c index 744771933..6cc9b7edd 100644 --- a/src/window.c +++ b/src/window.c @@ -1594,17 +1594,21 @@ static void windowCodeRowExprStep( || pMWin->eStart==TK_PRECEDING || pMWin->eStart==TK_FOLLOWING ){ - int addrJumpHere = 0; + int lblSkipInverse = sqlite3VdbeMakeLabel(v);; if( pMWin->eStart==TK_PRECEDING ){ - addrJumpHere = sqlite3VdbeAddOp3(v, OP_IfPos, regStart, 0 , 1); + sqlite3VdbeAddOp3(v, OP_IfPos, regStart, lblSkipInverse, 1); VdbeCoverage(v); } - sqlite3VdbeAddOp2(v, OP_Next, csrStart, sqlite3VdbeCurrentAddr(v)+1); - VdbeCoverage(v); - windowAggStep(pParse, pMWin, csrStart, 1, regArg, regSize); - if( addrJumpHere ){ - sqlite3VdbeJumpHere(v, addrJumpHere); + if( pMWin->eStart==TK_FOLLOWING ){ + sqlite3VdbeAddOp2(v, OP_Next, csrStart, sqlite3VdbeCurrentAddr(v)+2); + VdbeCoverage(v); + sqlite3VdbeAddOp2(v, OP_Goto, 0, lblSkipInverse); + }else{ + sqlite3VdbeAddOp2(v, OP_Next, csrStart, sqlite3VdbeCurrentAddr(v)+1); + VdbeCoverage(v); } + windowAggStep(pParse, pMWin, csrStart, 1, regArg, regSize); + sqlite3VdbeResolveLabel(v, lblSkipInverse); } if( pMWin->eEnd==TK_FOLLOWING ){ sqlite3VdbeJumpHere(v, addrIfPos1); |