Add support for window functions row_number(), rank(), dense_rank() and
percent_rank(). FossilOrigin-Name: 91c1cb7a217d0991a08256269f6c55ef185c25362d57b36bfbd2d85dab38e58f
This commit is contained in:
parent
f690b57206
commit
dfa552f45b
24
manifest
24
manifest
@ -1,5 +1,5 @@
|
||||
C Allow\san\sentire\spartition\sto\sbe\scached\sin\sa\stemp\stable\sfor\sall\stypes\sof\swindow\nframes.\sThis\sis\srequired\sby\snth_value()\sand\sothers.
|
||||
D 2018-06-01T21:00:08.284
|
||||
C Add\ssupport\sfor\swindow\sfunctions\srow_number(),\srank(),\sdense_rank()\sand\npercent_rank().
|
||||
D 2018-06-02T21:04:28.694
|
||||
F .fossil-settings/empty-dirs dbb81e8fc0401ac46a1491ab34a7f2c7c0452f2f06b54ebb845d024ca8283ef1
|
||||
F .fossil-settings/ignore-glob 35175cdfcf539b2318cb04a9901442804be81cd677d8b889fcc9149c21f239ea
|
||||
F Makefile.in bfc40f350586923e0419d2ea4b559c37ec10ee4b6e210e08c14401f8e340f0da
|
||||
@ -449,7 +449,7 @@ F src/delete.c 4c8c7604277a2041647f96b78f4b9a47858e9217e4fb333d35e7b5ab32c5b57f
|
||||
F src/expr.c bb57b0b5ba1351335091ce4ec43b40968746f03afd65c9e2920d7cbe4dc98133
|
||||
F src/fault.c 460f3e55994363812d9d60844b2a6de88826e007
|
||||
F src/fkey.c d617daf66b5515e2b42c1405b2b4984c30ca50fb705ab164271a9bf66c69e331
|
||||
F src/func.c eff9c15696cda3485df3ae52ce3663692a2cd506fba63a5f49f56cb204831021
|
||||
F src/func.c 4bee7609e4a736554d3d29db9ced9f4aacb1c81ab1c19e1d02451c647afa692b
|
||||
F src/global.c 9bf034fd560bdd514715170ed8460bb7f823cec113f0569ef3f18a20c7ccd128
|
||||
F src/hash.c a12580e143f10301ed5166ea4964ae2853d3905a511d4e0c44497245c7ce1f7a
|
||||
F src/hash.h ab34c5c54a9e9de2e790b24349ba5aab3dbb4fd4
|
||||
@ -492,14 +492,14 @@ F src/pragma.h bb83728944b42f6d409c77f5838a8edbdb0fe83046c5496ffc9602b40340a324
|
||||
F src/prepare.c 95a9dba7a5d032039a77775188cb3b6fb17f2fa1a0b7cd915b30b4b823383ffa
|
||||
F src/printf.c 1d1b4a568a58d0f32a5ff26c6b98db8a6e1883467f343a6406263cacd2e60c21
|
||||
F src/random.c 80f5d666f23feb3e6665a6ce04c7197212a88384
|
||||
F src/resolve.c 446f60b2e0d2440bb233d6a69a4ed0f2ad030a4e63ac4b3cfc0e98cf73d9c5a3
|
||||
F src/resolve.c 6b62d3427e642c9b275bf25b0239b7fb941bf00b059edb0e55b35f6c789c7259
|
||||
F src/rowset.c 7b7e7e479212e65b723bf40128c7b36dc5afdfac
|
||||
F src/select.c 8a7f842a049a3407079e0b0748de916dcd91c00377394b2e8b1aefc5972a0b2f
|
||||
F src/select.c 3ba9808f63c7e8e23903b85008f84df15d86da68730840521cbf04dec2f89883
|
||||
F src/shell.c.in 51c100206f4b7f86cd9affd80b764825e0edc36ca0190c442e4ca7994611bfe2
|
||||
F src/sqlite.h.in e379906b85cc3539c6a37ab972f2c28711b28854a457f54e137d20f75db5db7c
|
||||
F src/sqlite3.rc 5121c9e10c3964d5755191c80dd1180c122fc3a8
|
||||
F src/sqlite3ext.h 9887b27e69c01e79c2cbe74ef73bf01af5b5703d6a7f0a4371e386d7249cb1c7
|
||||
F src/sqliteInt.h daa86b28d3c73ddd8f73bbb24ee8067f7de901fca04ff44816e3a36f4f4a94f8
|
||||
F src/sqliteInt.h 777f048d4590801f421ff78e51d5b76ff4e9aa9de19dedfeccb309ff6a1559a0
|
||||
F src/sqliteLimit.h 1513bfb7b20378aa0041e7022d04acb73525de35b80b252f1b83fedb4de6a76b
|
||||
F src/status.c 46e7aec11f79dad50965a5ca5fa9de009f7d6bde08be2156f1538a0a296d4d0e
|
||||
F src/table.c b46ad567748f24a326d9de40e5b9659f96ffff34
|
||||
@ -582,7 +582,7 @@ F src/where.c 60ec752fcbe9f9e0271ac60548d159a540a1ee47a4f9fedc85e88a3d0e392dd1
|
||||
F src/whereInt.h cbae2bcd37cfebdb7812a8b188cdb19634ced2b9346470d1c270556b0c33ea53
|
||||
F src/wherecode.c 728c7f70731430ccdac807a79969873e1af6968bf1c4745dff3f9dd35f636cc8
|
||||
F src/whereexpr.c e90b2e76dcabc81edff56633bf281bc01d93b71e0c81482dc06925ce39f5844a
|
||||
F src/window.c 59f519e2090a930b73f0789e44e481bf5a27d256d22ed74dee100ea14ad3bb02
|
||||
F src/window.c 014c6c7ae0242f16ba9453d2c9272753b8d20af08d4f1c6d040a6f87dbc55055
|
||||
F test/8_3_names.test ebbb5cd36741350040fd28b432ceadf495be25b2
|
||||
F test/affinity2.test a6d901b436328bd67a79b41bb0ac2663918fe3bd
|
||||
F test/affinity3.test 6a101af2fc945ce2912f6fe54dd646018551710d
|
||||
@ -1618,8 +1618,8 @@ F test/win32nolock.test ac4f08811a562e45a5755e661f45ca85892bdbbc
|
||||
F test/window1.test 5705337783d220b47f6fb4432264543b7557a05be8013d772f57d71f2fded271
|
||||
F test/window2.tcl 0983de5eade5eeda49469244799d5331bfe3199fca3f6c6d2a836aa08f4fba1b
|
||||
F test/window2.test 79747b2edde4ad424e0752b27529aedc86e91f3d8d88846fa17ff0cb67f65086
|
||||
F test/window3.tcl d17eda13ef009ff447e3886c8b82d391d39a27bfc93bef5e447fb399e65e03a2
|
||||
F test/window3.test ce345cb0b9b7691d1b686e171ff00e8642ad710d5cd60029220a916b4858c27c
|
||||
F test/window3.tcl 27a34216e6e9b94307faa146285ef97e23bec57e05787064e17669fb8e276b29
|
||||
F test/window3.test d8975dfd7a3ac4d67f18c9d288ff7c46f6d0461fff4290414fcbd62027e8ac03
|
||||
F test/with1.test 58475190cd8caaeebea8cfeb2a264ec97a0c492b8ffe9ad20cefbb23df462f96
|
||||
F test/with2.test e0030e2f0267a910d6c0e4f46f2dfe941c1cc0d4f659ba69b3597728e7e8f1ab
|
||||
F test/with3.test 5e8ce2c585170bbbc0544e2a01a4941fa0be173ba5265e5c92eb588cd99a232d
|
||||
@ -1736,7 +1736,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 c16125a884a9131b707ac20033968c4c3177ea79625a15efb64d754568c6c7a0
|
||||
R 3ad4cd9edcd57b310020dff4b3e91b5b
|
||||
P b5b18f661341d8d450147e62d321791c706f16c0550bcd98eec3e0220c039189
|
||||
R 936d5c4f2319457074a03c8e7558fedb
|
||||
U dan
|
||||
Z 0764619e321410e8611ab0fa83a4bd79
|
||||
Z 37caf2da8db60907c2ea55a56e1b5065
|
||||
|
@ -1 +1 @@
|
||||
b5b18f661341d8d450147e62d321791c706f16c0550bcd98eec3e0220c039189
|
||||
91c1cb7a217d0991a08256269f6c55ef185c25362d57b36bfbd2d85dab38e58f
|
@ -1923,6 +1923,7 @@ void sqlite3RegisterBuiltinFunctions(void){
|
||||
#ifndef SQLITE_OMIT_ALTERTABLE
|
||||
sqlite3AlterFunctions();
|
||||
#endif
|
||||
sqlite3WindowFunctions();
|
||||
#if defined(SQLITE_ENABLE_STAT3) || defined(SQLITE_ENABLE_STAT4)
|
||||
sqlite3AnalyzeFunctions();
|
||||
#endif
|
||||
|
@ -776,6 +776,7 @@ static int resolveExprStep(Walker *pWalker, Expr *pExpr){
|
||||
sqlite3WalkExprList(pWalker, pList);
|
||||
if( is_agg ){
|
||||
if( pExpr->pWin ){
|
||||
sqlite3WindowUpdate(pParse, pExpr->pWin, pDef);
|
||||
if( 0==pNC->pWin
|
||||
|| 0==sqlite3WindowCompare(pParse, pNC->pWin, pExpr->pWin)
|
||||
){
|
||||
|
215
src/select.c
215
src/select.c
@ -4583,7 +4583,7 @@ static void selectPopWith(Walker *pWalker, Select *p){
|
||||
#define selectPopWith 0
|
||||
#endif
|
||||
|
||||
static int selectExpandSubquery(Parse *pParse, struct SrcList_item *pFrom){
|
||||
int sqlite3ExpandSubquery(Parse *pParse, struct SrcList_item *pFrom){
|
||||
Select *pSel = pFrom->pSelect;
|
||||
Table *pTab;
|
||||
|
||||
@ -4676,7 +4676,7 @@ static int selectExpander(Walker *pWalker, Select *p){
|
||||
assert( pSel!=0 );
|
||||
assert( pFrom->pTab==0 );
|
||||
if( sqlite3WalkSelect(pWalker, pSel) ) return WRC_Abort;
|
||||
if( selectExpandSubquery(pParse, pFrom) ) return WRC_Abort;
|
||||
if( sqlite3ExpandSubquery(pParse, pFrom) ) return WRC_Abort;
|
||||
#endif
|
||||
}else{
|
||||
/* An ordinary table or view name in the FROM clause */
|
||||
@ -5379,209 +5379,6 @@ static int countOfViewOptimization(Parse *pParse, Select *p){
|
||||
}
|
||||
#endif /* SQLITE_COUNTOFVIEW_OPTIMIZATION */
|
||||
|
||||
typedef struct WindowRewrite WindowRewrite;
|
||||
struct WindowRewrite {
|
||||
Window *pWin;
|
||||
ExprList *pSub;
|
||||
};
|
||||
|
||||
static int selectWindowRewriteSelectCb(Walker *pWalker, Select *pSelect){
|
||||
return WRC_Prune;
|
||||
}
|
||||
|
||||
static int selectWindowRewriteExprCb(Walker *pWalker, Expr *pExpr){
|
||||
struct WindowRewrite *p = pWalker->u.pRewrite;
|
||||
Parse *pParse = pWalker->pParse;
|
||||
|
||||
switch( pExpr->op ){
|
||||
|
||||
case TK_FUNCTION:
|
||||
if( pExpr->pWin==0 ){
|
||||
break;
|
||||
}else{
|
||||
Window *pWin;
|
||||
for(pWin=p->pWin; pWin; pWin=pWin->pNextWin){
|
||||
if( pExpr->pWin==pWin ){
|
||||
pExpr->pWin->pOwner = pExpr;
|
||||
return WRC_Prune;
|
||||
}
|
||||
}
|
||||
}
|
||||
/* Fall through. */
|
||||
|
||||
case TK_COLUMN: {
|
||||
Expr *pDup = sqlite3ExprDup(pParse->db, pExpr, 0);
|
||||
p->pSub = sqlite3ExprListAppend(pParse, p->pSub, pDup);
|
||||
if( p->pSub ){
|
||||
assert( ExprHasProperty(pExpr, EP_Static)==0 );
|
||||
ExprSetProperty(pExpr, EP_Static);
|
||||
sqlite3ExprDelete(pParse->db, pExpr);
|
||||
ExprClearProperty(pExpr, EP_Static);
|
||||
memset(pExpr, 0, sizeof(Expr));
|
||||
|
||||
pExpr->op = TK_COLUMN;
|
||||
pExpr->iColumn = p->pSub->nExpr-1;
|
||||
pExpr->iTable = p->pWin->iEphCsr;
|
||||
}
|
||||
|
||||
break;
|
||||
}
|
||||
|
||||
default: /* no-op */
|
||||
break;
|
||||
}
|
||||
|
||||
return WRC_Continue;
|
||||
}
|
||||
|
||||
static int selectWindowRewriteEList(
|
||||
Parse *pParse,
|
||||
Window *pWin,
|
||||
ExprList *pEList, /* Rewrite expressions in this list */
|
||||
ExprList **ppSub /* IN/OUT: Sub-select expression-list */
|
||||
){
|
||||
Walker sWalker;
|
||||
WindowRewrite sRewrite;
|
||||
int rc;
|
||||
|
||||
memset(&sWalker, 0, sizeof(Walker));
|
||||
memset(&sRewrite, 0, sizeof(WindowRewrite));
|
||||
|
||||
sRewrite.pSub = *ppSub;
|
||||
sRewrite.pWin = pWin;
|
||||
|
||||
sWalker.pParse = pParse;
|
||||
sWalker.xExprCallback = selectWindowRewriteExprCb;
|
||||
sWalker.xSelectCallback = selectWindowRewriteSelectCb;
|
||||
sWalker.u.pRewrite = &sRewrite;
|
||||
|
||||
rc = sqlite3WalkExprList(&sWalker, pEList);
|
||||
|
||||
*ppSub = sRewrite.pSub;
|
||||
return rc;
|
||||
}
|
||||
|
||||
static ExprList *exprListAppendList(
|
||||
Parse *pParse, /* Parsing context */
|
||||
ExprList *pList, /* List to which to append. Might be NULL */
|
||||
ExprList *pAppend /* List of values to append. Might be NULL */
|
||||
){
|
||||
if( pAppend ){
|
||||
int i;
|
||||
int nInit = pList ? pList->nExpr : 0;
|
||||
for(i=0; i<pAppend->nExpr; i++){
|
||||
Expr *pDup = sqlite3ExprDup(pParse->db, pAppend->a[i].pExpr, 0);
|
||||
pList = sqlite3ExprListAppend(pParse, pList, pDup);
|
||||
if( pList ) pList->a[nInit+i].sortOrder = pAppend->a[i].sortOrder;
|
||||
}
|
||||
}
|
||||
return pList;
|
||||
}
|
||||
|
||||
/*
|
||||
** If the SELECT statement passed as the second argument does not invoke
|
||||
** any SQL window functions, this function is a no-op. Otherwise, it
|
||||
** rewrites the SELECT statement so that window function xStep functions
|
||||
** are invoked in the correct order. The simplest version of the
|
||||
** transformation is:
|
||||
**
|
||||
** SELECT win(args...) OVER (<list1>) FROM <src> ORDER BY <list2>
|
||||
**
|
||||
** to
|
||||
**
|
||||
** SELECT win(args...) FROM (
|
||||
** SELECT args... FROM <src> ORDER BY <list1>
|
||||
** ) ORDER BY <list2>
|
||||
**
|
||||
** where <src> may contain WHERE, GROUP BY and HAVING clauses, and <list1>
|
||||
** is the concatenation of the PARTITION BY and ORDER BY clauses in the
|
||||
** OVER clause.
|
||||
**
|
||||
*/
|
||||
static int selectWindowRewrite(Parse *pParse, Select *p){
|
||||
int rc = SQLITE_OK;
|
||||
if( p->pWin ){
|
||||
Vdbe *v = sqlite3GetVdbe(pParse);
|
||||
int i;
|
||||
sqlite3 *db = pParse->db;
|
||||
Select *pSub = 0; /* The subquery */
|
||||
SrcList *pSrc = p->pSrc;
|
||||
Expr *pWhere = p->pWhere;
|
||||
ExprList *pGroupBy = p->pGroupBy;
|
||||
Expr *pHaving = p->pHaving;
|
||||
ExprList *pSort = 0;
|
||||
|
||||
ExprList *pSublist = 0; /* Expression list for sub-query */
|
||||
Window *pMWin = p->pWin; /* Master window object */
|
||||
Window *pWin; /* Window object iterator */
|
||||
|
||||
p->pSrc = 0;
|
||||
p->pWhere = 0;
|
||||
p->pGroupBy = 0;
|
||||
p->pHaving = 0;
|
||||
|
||||
/* Assign a cursor number for the ephemeral table used to buffer rows.
|
||||
** The OpenEphemeral instruction is coded later, after it is known how
|
||||
** many columns the table will have. */
|
||||
pMWin->iEphCsr = pParse->nTab++;
|
||||
|
||||
rc = selectWindowRewriteEList(pParse, pMWin, p->pEList, &pSublist);
|
||||
if( rc ) return rc;
|
||||
rc = selectWindowRewriteEList(pParse, pMWin, p->pOrderBy, &pSublist);
|
||||
if( rc ) return rc;
|
||||
pMWin->nBufferCol = (pSublist ? pSublist->nExpr : 0);
|
||||
|
||||
/* Create the ORDER BY clause for the sub-select. This is the concatenation
|
||||
** of the window PARTITION and ORDER BY clauses. Append the same
|
||||
** expressions to the sub-select expression list. They are required to
|
||||
** figure out where boundaries for partitions and sets of peer rows. */
|
||||
pSort = sqlite3ExprListDup(db, pMWin->pPartition, 0);
|
||||
if( pMWin->pOrderBy ){
|
||||
pSort = exprListAppendList(pParse, pSort, pMWin->pOrderBy);
|
||||
}
|
||||
pSublist = exprListAppendList(pParse, pSublist, pSort);
|
||||
|
||||
/* Append the arguments passed to each window function to the
|
||||
** sub-select expression list. Also allocate two registers for each
|
||||
** window function - one for the accumulator, another for interim
|
||||
** results. */
|
||||
for(pWin=pMWin; pWin; pWin=pWin->pNextWin){
|
||||
pWin->iArgCol = (pSublist ? pSublist->nExpr : 0);
|
||||
pSublist = exprListAppendList(pParse, pSublist, pWin->pOwner->x.pList);
|
||||
pWin->regAccum = ++pParse->nMem;
|
||||
pWin->regResult = ++pParse->nMem;
|
||||
sqlite3VdbeAddOp2(v, OP_Null, 0, pWin->regAccum);
|
||||
}
|
||||
|
||||
pSub = sqlite3SelectNew(
|
||||
pParse, pSublist, pSrc, pWhere, pGroupBy, pHaving, pSort, 0, 0
|
||||
);
|
||||
p->pSrc = sqlite3SrcListAppend(db, 0, 0, 0);
|
||||
if( p->pSrc ){
|
||||
int iTab;
|
||||
ExprList *pList = 0;
|
||||
p->pSrc->a[0].pSelect = pSub;
|
||||
sqlite3SrcListAssignCursors(pParse, p->pSrc);
|
||||
if( selectExpandSubquery(pParse, &p->pSrc->a[0]) ){
|
||||
rc = SQLITE_NOMEM;
|
||||
}else{
|
||||
pSub->selFlags |= SF_Expanded;
|
||||
}
|
||||
}
|
||||
|
||||
#if SELECTTRACE_ENABLED
|
||||
if( sqlite3SelectTrace & 0x108 ){
|
||||
SELECTTRACE(0x104,pParse,p, ("after window rewrite:\n"));
|
||||
sqlite3TreeViewSelect(0, p, 0);
|
||||
}
|
||||
#endif
|
||||
|
||||
sqlite3VdbeAddOp2(v, OP_OpenEphemeral, pMWin->iEphCsr, pSublist->nExpr);
|
||||
}
|
||||
|
||||
return rc;
|
||||
}
|
||||
|
||||
/*
|
||||
** Generate code for the SELECT statement given in the p argument.
|
||||
**
|
||||
@ -5666,9 +5463,15 @@ int sqlite3Select(
|
||||
generateColumnNames(pParse, p);
|
||||
}
|
||||
|
||||
if( (rc = selectWindowRewrite(pParse, p)) ){
|
||||
if( (rc = sqlite3WindowRewrite(pParse, p)) ){
|
||||
goto select_end;
|
||||
}
|
||||
#if SELECTTRACE_ENABLED
|
||||
if( sqlite3SelectTrace & 0x108 ){
|
||||
SELECTTRACE(0x104,pParse,p, ("after window rewrite:\n"));
|
||||
sqlite3TreeViewSelect(0, p, 0);
|
||||
}
|
||||
#endif
|
||||
pTabList = p->pSrc;
|
||||
|
||||
/* Try to various optimizations (flattening subqueries, and strength
|
||||
|
@ -1584,7 +1584,7 @@ struct sqlite3 {
|
||||
*/
|
||||
struct FuncDef {
|
||||
i8 nArg; /* Number of arguments. -1 means unlimited */
|
||||
u16 funcFlags; /* Some combination of SQLITE_FUNC_* */
|
||||
u32 funcFlags; /* Some combination of SQLITE_FUNC_* */
|
||||
void *pUserData; /* User data parameter */
|
||||
FuncDef *pNext; /* Next function with same name */
|
||||
void (*xSFunc)(sqlite3_context*,int,sqlite3_value**); /* func or agg-step */
|
||||
@ -1647,6 +1647,8 @@ struct FuncDestructor {
|
||||
** single query - might change over time */
|
||||
#define SQLITE_FUNC_AFFINITY 0x4000 /* Built-in affinity() function */
|
||||
#define SQLITE_FUNC_OFFSET 0x8000 /* Built-in sqlite_offset() function */
|
||||
#define SQLITE_FUNC_WINDOW 0x10000 /* Built-in window-only function */
|
||||
#define SQLITE_FUNC_WINDOW_SIZE 0x20000
|
||||
|
||||
/*
|
||||
** The following three macros, FUNCTION(), LIKEFUNC() and AGGREGATE() are
|
||||
@ -3501,6 +3503,9 @@ void sqlite3WindowAttach(Parse*, Expr*, Window*);
|
||||
int sqlite3WindowCompare(Parse*, Window*, Window*);
|
||||
void sqlite3WindowCodeInit(Parse*, Window*);
|
||||
void sqlite3WindowCodeStep(Parse*, Select*, WhereInfo*, int, int, int*);
|
||||
int sqlite3WindowRewrite(Parse*, Select*);
|
||||
int sqlite3ExpandSubquery(Parse*, struct SrcList_item*);
|
||||
void sqlite3WindowUpdate(Parse*, Window*, FuncDef*);
|
||||
|
||||
/*
|
||||
** Assuming zIn points to the first byte of a UTF-8 character,
|
||||
@ -4175,6 +4180,7 @@ extern sqlite3_uint64 sqlite3NProfileCnt;
|
||||
void sqlite3RootPageMoved(sqlite3*, int, int, int);
|
||||
void sqlite3Reindex(Parse*, Token*, Token*);
|
||||
void sqlite3AlterFunctions(void);
|
||||
void sqlite3WindowFunctions(void);
|
||||
void sqlite3AlterRenameTable(Parse*, SrcList*, Token*);
|
||||
int sqlite3GetToken(const unsigned char *, int *);
|
||||
void sqlite3NestedParse(Parse*, const char*, ...);
|
||||
|
440
src/window.c
440
src/window.c
@ -11,6 +11,400 @@
|
||||
*/
|
||||
#include "sqliteInt.h"
|
||||
|
||||
/*
|
||||
** Implementation of built-in window function row_number(). Assumes that the
|
||||
** window frame has been coerced to:
|
||||
**
|
||||
** ROWS BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW
|
||||
*/
|
||||
static void row_numberStepFunc(
|
||||
sqlite3_context *pCtx,
|
||||
int nArg,
|
||||
sqlite3_value **apArg
|
||||
){
|
||||
i64 *p = (i64*)sqlite3_aggregate_context(pCtx, sizeof(*p));
|
||||
if( p ) (*p)++;
|
||||
}
|
||||
static void row_numberInverseFunc(
|
||||
sqlite3_context *pCtx,
|
||||
int nArg,
|
||||
sqlite3_value **apArg
|
||||
){
|
||||
}
|
||||
static void row_numberValueFunc(sqlite3_context *pCtx){
|
||||
i64 *p = (i64*)sqlite3_aggregate_context(pCtx, sizeof(*p));
|
||||
sqlite3_result_int64(pCtx, (p ? *p : 0));
|
||||
}
|
||||
|
||||
/*
|
||||
** Context object type used by rank() and dense_rank().
|
||||
*/
|
||||
struct CallCount {
|
||||
i64 nValue;
|
||||
i64 nStep;
|
||||
i64 nTotal;
|
||||
};
|
||||
|
||||
/*
|
||||
** Implementation of built-in window function dense_rank().
|
||||
*/
|
||||
static void dense_rankStepFunc(
|
||||
sqlite3_context *pCtx,
|
||||
int nArg,
|
||||
sqlite3_value **apArg
|
||||
){
|
||||
struct CallCount *p;
|
||||
p = (struct CallCount*)sqlite3_aggregate_context(pCtx, sizeof(*p));
|
||||
if( p ) p->nStep = 1;
|
||||
}
|
||||
static void dense_rankInverseFunc(
|
||||
sqlite3_context *pCtx,
|
||||
int nArg,
|
||||
sqlite3_value **apArg
|
||||
){
|
||||
}
|
||||
static void dense_rankValueFunc(sqlite3_context *pCtx){
|
||||
struct CallCount *p;
|
||||
p = (struct CallCount*)sqlite3_aggregate_context(pCtx, sizeof(*p));
|
||||
if( p ){
|
||||
if( p->nStep ){
|
||||
p->nValue++;
|
||||
p->nStep = 0;
|
||||
}
|
||||
sqlite3_result_int64(pCtx, p->nValue);
|
||||
}
|
||||
}
|
||||
|
||||
/*
|
||||
** Implementation of built-in window function rank().
|
||||
*/
|
||||
static void rankStepFunc(
|
||||
sqlite3_context *pCtx,
|
||||
int nArg,
|
||||
sqlite3_value **apArg
|
||||
){
|
||||
struct CallCount *p;
|
||||
p = (struct CallCount*)sqlite3_aggregate_context(pCtx, sizeof(*p));
|
||||
if( p ){
|
||||
p->nStep++;
|
||||
if( p->nValue==0 ){
|
||||
p->nValue = p->nStep;
|
||||
}
|
||||
}
|
||||
}
|
||||
static void rankInverseFunc(
|
||||
sqlite3_context *pCtx,
|
||||
int nArg,
|
||||
sqlite3_value **apArg
|
||||
){
|
||||
}
|
||||
static void rankValueFunc(sqlite3_context *pCtx){
|
||||
struct CallCount *p;
|
||||
p = (struct CallCount*)sqlite3_aggregate_context(pCtx, sizeof(*p));
|
||||
if( p ){
|
||||
sqlite3_result_int64(pCtx, p->nValue);
|
||||
p->nValue = 0;
|
||||
}
|
||||
}
|
||||
|
||||
/*
|
||||
** Implementation of built-in window function percent_rank().
|
||||
*/
|
||||
static void percent_rankStepFunc(
|
||||
sqlite3_context *pCtx,
|
||||
int nArg,
|
||||
sqlite3_value **apArg
|
||||
){
|
||||
struct CallCount *p;
|
||||
assert( nArg==1 );
|
||||
|
||||
p = (struct CallCount*)sqlite3_aggregate_context(pCtx, sizeof(*p));
|
||||
if( p ){
|
||||
if( p->nTotal==0 ){
|
||||
p->nTotal = sqlite3_value_int64(apArg[0]);
|
||||
}
|
||||
p->nStep++;
|
||||
if( p->nValue==0 ){
|
||||
p->nValue = p->nStep;
|
||||
}
|
||||
}
|
||||
}
|
||||
static void percent_rankInverseFunc(
|
||||
sqlite3_context *pCtx,
|
||||
int nArg,
|
||||
sqlite3_value **apArg
|
||||
){
|
||||
}
|
||||
static void percent_rankValueFunc(sqlite3_context *pCtx){
|
||||
struct CallCount *p;
|
||||
p = (struct CallCount*)sqlite3_aggregate_context(pCtx, sizeof(*p));
|
||||
if( p ){
|
||||
if( p->nTotal>1 ){
|
||||
double r = (double)(p->nValue-1) / (double)(p->nTotal-1);
|
||||
sqlite3_result_double(pCtx, r);
|
||||
}else{
|
||||
sqlite3_result_double(pCtx, 100.0);
|
||||
}
|
||||
p->nValue = 0;
|
||||
}
|
||||
}
|
||||
|
||||
static void nth_valueStepFunc(
|
||||
sqlite3_context *pCtx,
|
||||
int nArg,
|
||||
sqlite3_value **apArg
|
||||
){
|
||||
}
|
||||
static void nth_valueInverseFunc(
|
||||
sqlite3_context *pCtx,
|
||||
int nArg,
|
||||
sqlite3_value **apArg
|
||||
){
|
||||
}
|
||||
static void nth_valueValueFunc(sqlite3_context *pCtx){
|
||||
}
|
||||
|
||||
#define WINDOWFUNC(name,nArg,extra) { \
|
||||
nArg, (SQLITE_UTF8|SQLITE_FUNC_WINDOW|extra), 0, 0, \
|
||||
name ## StepFunc, name ## ValueFunc, name ## ValueFunc, \
|
||||
name ## InverseFunc, #name \
|
||||
}
|
||||
|
||||
/*
|
||||
** Register those built-in window functions that are not also aggregates.
|
||||
*/
|
||||
void sqlite3WindowFunctions(void){
|
||||
static FuncDef aWindowFuncs[] = {
|
||||
WINDOWFUNC(row_number, 0, 0),
|
||||
WINDOWFUNC(dense_rank, 0, 0),
|
||||
WINDOWFUNC(rank, 0, 0),
|
||||
WINDOWFUNC(percent_rank, 0, SQLITE_FUNC_WINDOW_SIZE),
|
||||
WINDOWFUNC(nth_value, 2, 0),
|
||||
};
|
||||
sqlite3InsertBuiltinFuncs(aWindowFuncs, ArraySize(aWindowFuncs));
|
||||
}
|
||||
|
||||
void sqlite3WindowUpdate(Parse *pParse, Window *pWin, FuncDef *pFunc){
|
||||
if( pFunc->funcFlags & SQLITE_FUNC_WINDOW ){
|
||||
sqlite3 *db = pParse->db;
|
||||
if( pFunc->xSFunc==row_numberStepFunc ){
|
||||
sqlite3ExprDelete(db, pWin->pStart);
|
||||
sqlite3ExprDelete(db, pWin->pEnd);
|
||||
pWin->pStart = pWin->pEnd = 0;
|
||||
pWin->eType = TK_ROWS;
|
||||
pWin->eStart = TK_UNBOUNDED;
|
||||
pWin->eEnd = TK_CURRENT;
|
||||
}
|
||||
|
||||
if( pFunc->xSFunc==dense_rankStepFunc || pFunc->xSFunc==rankStepFunc
|
||||
|| pFunc->xSFunc==percent_rankStepFunc
|
||||
){
|
||||
sqlite3ExprDelete(db, pWin->pStart);
|
||||
sqlite3ExprDelete(db, pWin->pEnd);
|
||||
pWin->pStart = pWin->pEnd = 0;
|
||||
pWin->eType = TK_RANGE;
|
||||
pWin->eStart = TK_UNBOUNDED;
|
||||
pWin->eEnd = TK_CURRENT;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
typedef struct WindowRewrite WindowRewrite;
|
||||
struct WindowRewrite {
|
||||
Window *pWin;
|
||||
ExprList *pSub;
|
||||
};
|
||||
|
||||
static int selectWindowRewriteSelectCb(Walker *pWalker, Select *pSelect){
|
||||
return WRC_Prune;
|
||||
}
|
||||
|
||||
static int selectWindowRewriteExprCb(Walker *pWalker, Expr *pExpr){
|
||||
struct WindowRewrite *p = pWalker->u.pRewrite;
|
||||
Parse *pParse = pWalker->pParse;
|
||||
|
||||
switch( pExpr->op ){
|
||||
|
||||
case TK_FUNCTION:
|
||||
if( pExpr->pWin==0 ){
|
||||
break;
|
||||
}else{
|
||||
Window *pWin;
|
||||
for(pWin=p->pWin; pWin; pWin=pWin->pNextWin){
|
||||
if( pExpr->pWin==pWin ){
|
||||
pExpr->pWin->pOwner = pExpr;
|
||||
return WRC_Prune;
|
||||
}
|
||||
}
|
||||
}
|
||||
/* Fall through. */
|
||||
|
||||
case TK_COLUMN: {
|
||||
Expr *pDup = sqlite3ExprDup(pParse->db, pExpr, 0);
|
||||
p->pSub = sqlite3ExprListAppend(pParse, p->pSub, pDup);
|
||||
if( p->pSub ){
|
||||
assert( ExprHasProperty(pExpr, EP_Static)==0 );
|
||||
ExprSetProperty(pExpr, EP_Static);
|
||||
sqlite3ExprDelete(pParse->db, pExpr);
|
||||
ExprClearProperty(pExpr, EP_Static);
|
||||
memset(pExpr, 0, sizeof(Expr));
|
||||
|
||||
pExpr->op = TK_COLUMN;
|
||||
pExpr->iColumn = p->pSub->nExpr-1;
|
||||
pExpr->iTable = p->pWin->iEphCsr;
|
||||
}
|
||||
|
||||
break;
|
||||
}
|
||||
|
||||
default: /* no-op */
|
||||
break;
|
||||
}
|
||||
|
||||
return WRC_Continue;
|
||||
}
|
||||
|
||||
static int selectWindowRewriteEList(
|
||||
Parse *pParse,
|
||||
Window *pWin,
|
||||
ExprList *pEList, /* Rewrite expressions in this list */
|
||||
ExprList **ppSub /* IN/OUT: Sub-select expression-list */
|
||||
){
|
||||
Walker sWalker;
|
||||
WindowRewrite sRewrite;
|
||||
int rc;
|
||||
|
||||
memset(&sWalker, 0, sizeof(Walker));
|
||||
memset(&sRewrite, 0, sizeof(WindowRewrite));
|
||||
|
||||
sRewrite.pSub = *ppSub;
|
||||
sRewrite.pWin = pWin;
|
||||
|
||||
sWalker.pParse = pParse;
|
||||
sWalker.xExprCallback = selectWindowRewriteExprCb;
|
||||
sWalker.xSelectCallback = selectWindowRewriteSelectCb;
|
||||
sWalker.u.pRewrite = &sRewrite;
|
||||
|
||||
rc = sqlite3WalkExprList(&sWalker, pEList);
|
||||
|
||||
*ppSub = sRewrite.pSub;
|
||||
return rc;
|
||||
}
|
||||
|
||||
static ExprList *exprListAppendList(
|
||||
Parse *pParse, /* Parsing context */
|
||||
ExprList *pList, /* List to which to append. Might be NULL */
|
||||
ExprList *pAppend /* List of values to append. Might be NULL */
|
||||
){
|
||||
if( pAppend ){
|
||||
int i;
|
||||
int nInit = pList ? pList->nExpr : 0;
|
||||
for(i=0; i<pAppend->nExpr; i++){
|
||||
Expr *pDup = sqlite3ExprDup(pParse->db, pAppend->a[i].pExpr, 0);
|
||||
pList = sqlite3ExprListAppend(pParse, pList, pDup);
|
||||
if( pList ) pList->a[nInit+i].sortOrder = pAppend->a[i].sortOrder;
|
||||
}
|
||||
}
|
||||
return pList;
|
||||
}
|
||||
|
||||
/*
|
||||
** If the SELECT statement passed as the second argument does not invoke
|
||||
** any SQL window functions, this function is a no-op. Otherwise, it
|
||||
** rewrites the SELECT statement so that window function xStep functions
|
||||
** are invoked in the correct order. The simplest version of the
|
||||
** transformation is:
|
||||
**
|
||||
** SELECT win(args...) OVER (<list1>) FROM <src> ORDER BY <list2>
|
||||
**
|
||||
** to
|
||||
**
|
||||
** SELECT win(args...) FROM (
|
||||
** SELECT args... FROM <src> ORDER BY <list1>
|
||||
** ) ORDER BY <list2>
|
||||
**
|
||||
** where <src> may contain WHERE, GROUP BY and HAVING clauses, and <list1>
|
||||
** is the concatenation of the PARTITION BY and ORDER BY clauses in the
|
||||
** OVER clause.
|
||||
**
|
||||
*/
|
||||
int sqlite3WindowRewrite(Parse *pParse, Select *p){
|
||||
int rc = SQLITE_OK;
|
||||
if( p->pWin ){
|
||||
Vdbe *v = sqlite3GetVdbe(pParse);
|
||||
int i;
|
||||
sqlite3 *db = pParse->db;
|
||||
Select *pSub = 0; /* The subquery */
|
||||
SrcList *pSrc = p->pSrc;
|
||||
Expr *pWhere = p->pWhere;
|
||||
ExprList *pGroupBy = p->pGroupBy;
|
||||
Expr *pHaving = p->pHaving;
|
||||
ExprList *pSort = 0;
|
||||
|
||||
ExprList *pSublist = 0; /* Expression list for sub-query */
|
||||
Window *pMWin = p->pWin; /* Master window object */
|
||||
Window *pWin; /* Window object iterator */
|
||||
|
||||
p->pSrc = 0;
|
||||
p->pWhere = 0;
|
||||
p->pGroupBy = 0;
|
||||
p->pHaving = 0;
|
||||
|
||||
/* Assign a cursor number for the ephemeral table used to buffer rows.
|
||||
** The OpenEphemeral instruction is coded later, after it is known how
|
||||
** many columns the table will have. */
|
||||
pMWin->iEphCsr = pParse->nTab++;
|
||||
|
||||
rc = selectWindowRewriteEList(pParse, pMWin, p->pEList, &pSublist);
|
||||
if( rc ) return rc;
|
||||
rc = selectWindowRewriteEList(pParse, pMWin, p->pOrderBy, &pSublist);
|
||||
if( rc ) return rc;
|
||||
pMWin->nBufferCol = (pSublist ? pSublist->nExpr : 0);
|
||||
|
||||
/* Create the ORDER BY clause for the sub-select. This is the concatenation
|
||||
** of the window PARTITION and ORDER BY clauses. Append the same
|
||||
** expressions to the sub-select expression list. They are required to
|
||||
** figure out where boundaries for partitions and sets of peer rows. */
|
||||
pSort = sqlite3ExprListDup(db, pMWin->pPartition, 0);
|
||||
if( pMWin->pOrderBy ){
|
||||
pSort = exprListAppendList(pParse, pSort, pMWin->pOrderBy);
|
||||
}
|
||||
pSublist = exprListAppendList(pParse, pSublist, pSort);
|
||||
|
||||
/* Append the arguments passed to each window function to the
|
||||
** sub-select expression list. Also allocate two registers for each
|
||||
** window function - one for the accumulator, another for interim
|
||||
** results. */
|
||||
for(pWin=pMWin; pWin; pWin=pWin->pNextWin){
|
||||
pWin->iArgCol = (pSublist ? pSublist->nExpr : 0);
|
||||
pSublist = exprListAppendList(pParse, pSublist, pWin->pOwner->x.pList);
|
||||
pWin->regAccum = ++pParse->nMem;
|
||||
pWin->regResult = ++pParse->nMem;
|
||||
sqlite3VdbeAddOp2(v, OP_Null, 0, pWin->regAccum);
|
||||
}
|
||||
|
||||
pSub = sqlite3SelectNew(
|
||||
pParse, pSublist, pSrc, pWhere, pGroupBy, pHaving, pSort, 0, 0
|
||||
);
|
||||
p->pSrc = sqlite3SrcListAppend(db, 0, 0, 0);
|
||||
if( p->pSrc ){
|
||||
int iTab;
|
||||
ExprList *pList = 0;
|
||||
p->pSrc->a[0].pSelect = pSub;
|
||||
sqlite3SrcListAssignCursors(pParse, p->pSrc);
|
||||
if( sqlite3ExpandSubquery(pParse, &p->pSrc->a[0]) ){
|
||||
rc = SQLITE_NOMEM;
|
||||
}else{
|
||||
pSub->selFlags |= SF_Expanded;
|
||||
}
|
||||
}
|
||||
|
||||
sqlite3VdbeAddOp2(v, OP_OpenEphemeral, pMWin->iEphCsr, pSublist->nExpr);
|
||||
}
|
||||
|
||||
return rc;
|
||||
}
|
||||
|
||||
void sqlite3WindowDelete(sqlite3 *db, Window *p){
|
||||
if( p ){
|
||||
sqlite3ExprDelete(db, p->pFilter);
|
||||
@ -125,13 +519,20 @@ static void windowAggStep(
|
||||
Window *pMWin,
|
||||
int csr,
|
||||
int bInverse,
|
||||
int reg
|
||||
int reg,
|
||||
int regPartSize /* Register containing size of partition */
|
||||
){
|
||||
Vdbe *v = sqlite3GetVdbe(pParse);
|
||||
Window *pWin;
|
||||
for(pWin=pMWin; pWin; pWin=pWin->pNextWin){
|
||||
int flags = pWin->pFunc->funcFlags;
|
||||
int regArg;
|
||||
if( csr>=0 ){
|
||||
int nArg = pWin->nArg;
|
||||
|
||||
if( flags & SQLITE_FUNC_WINDOW_SIZE ){
|
||||
regArg = regPartSize;
|
||||
nArg++;
|
||||
}else if( csr>=0 ){
|
||||
int i;
|
||||
for(i=0; i<pWin->nArg; i++){
|
||||
sqlite3VdbeAddOp3(v, OP_Column, csr, pWin->iArgCol+i, reg+i);
|
||||
@ -160,7 +561,7 @@ static void windowAggStep(
|
||||
}
|
||||
sqlite3VdbeAddOp3(v, OP_AggStep0, bInverse, regArg, pWin->regAccum);
|
||||
sqlite3VdbeAppendP4(v, pWin->pFunc, P4_FUNCDEF);
|
||||
sqlite3VdbeChangeP5(v, (u8)pWin->nArg);
|
||||
sqlite3VdbeChangeP5(v, (u8)nArg);
|
||||
}
|
||||
}
|
||||
}
|
||||
@ -198,7 +599,8 @@ static void windowPartitionCache(
|
||||
Select *p,
|
||||
WhereInfo *pWInfo,
|
||||
int regFlushPart,
|
||||
int lblFlushPart
|
||||
int lblFlushPart,
|
||||
int *pRegSize
|
||||
){
|
||||
Window *pMWin = p->pWin;
|
||||
Vdbe *v = sqlite3GetVdbe(pParse);
|
||||
@ -211,6 +613,7 @@ static void windowPartitionCache(
|
||||
int regRecord = reg+nSub;
|
||||
int regRowid = regRecord+1;
|
||||
|
||||
*pRegSize = regRowid;
|
||||
pParse->nMem += nSub + 2;
|
||||
|
||||
/* Martial the row returned by the sub-select into an array of
|
||||
@ -405,6 +808,7 @@ static void windowCodeRowExprStep(
|
||||
int iPeer = 0; /* Column offset in eph-table of peer vals */
|
||||
int nPeerVal; /* Number of peer values */
|
||||
int bRange = 0;
|
||||
int regSize = 0;
|
||||
|
||||
assert( pMWin->eStart==TK_PRECEDING
|
||||
|| pMWin->eStart==TK_CURRENT
|
||||
@ -432,7 +836,7 @@ static void windowCodeRowExprStep(
|
||||
regStart = ++pParse->nMem;
|
||||
regEnd = ++pParse->nMem;
|
||||
|
||||
windowPartitionCache(pParse, p, pWInfo, regFlushPart, lblFlushPart);
|
||||
windowPartitionCache(pParse, p, pWInfo, regFlushPart, lblFlushPart, ®Size);
|
||||
|
||||
addrGoto = sqlite3VdbeAddOp0(v, OP_Goto);
|
||||
|
||||
@ -486,7 +890,7 @@ static void windowCodeRowExprStep(
|
||||
}
|
||||
sqlite3VdbeAddOp2(v, OP_Next, csrEnd, sqlite3VdbeCurrentAddr(v)+2);
|
||||
addr = sqlite3VdbeAddOp0(v, OP_Goto);
|
||||
windowAggStep(pParse, pMWin, csrEnd, 0, regArg);
|
||||
windowAggStep(pParse, pMWin, csrEnd, 0, regArg, regSize);
|
||||
if( pMWin->eEnd==TK_UNBOUNDED ){
|
||||
sqlite3VdbeAddOp2(v, OP_Goto, 0, addrTop);
|
||||
sqlite3VdbeJumpHere(v, addr);
|
||||
@ -552,7 +956,7 @@ static void windowCodeRowExprStep(
|
||||
addrJumpHere = sqlite3VdbeAddOp0(v, OP_Goto);
|
||||
}
|
||||
sqlite3VdbeAddOp2(v, OP_Next, csrStart, sqlite3VdbeCurrentAddr(v)+1);
|
||||
windowAggStep(pParse, pMWin, csrStart, 1, regArg);
|
||||
windowAggStep(pParse, pMWin, csrStart, 1, regArg, regSize);
|
||||
if( bRange ){
|
||||
sqlite3VdbeAddOp2(v, OP_Goto, 0, addrJumpHere-1);
|
||||
}
|
||||
@ -634,6 +1038,7 @@ static void windowCodeCacheStep(
|
||||
int csrLead;
|
||||
int regCtr;
|
||||
int regArg; /* Register array to martial function args */
|
||||
int regSize;
|
||||
int nArg;
|
||||
|
||||
assert( (pMWin->eStart==TK_UNBOUNDED && pMWin->eEnd==TK_CURRENT)
|
||||
@ -649,7 +1054,7 @@ static void windowCodeCacheStep(
|
||||
csrLead = pParse->nTab++;
|
||||
regCtr = ++pParse->nMem;
|
||||
|
||||
windowPartitionCache(pParse, p, pWInfo, regFlushPart, lblFlushPart);
|
||||
windowPartitionCache(pParse, p, pWInfo, regFlushPart, lblFlushPart, ®Size);
|
||||
addrGoto = sqlite3VdbeAddOp0(v, OP_Goto);
|
||||
|
||||
/* Start of "flush_partition" */
|
||||
@ -692,7 +1097,7 @@ static void windowCodeCacheStep(
|
||||
sqlite3VdbeJumpHere(v, addrJump);
|
||||
}
|
||||
|
||||
windowAggStep(pParse, pMWin, csrLead, 0, regArg);
|
||||
windowAggStep(pParse, pMWin, csrLead, 0, regArg, regSize);
|
||||
sqlite3VdbeAddOp2(v, OP_AddImm, regCtr, 1);
|
||||
|
||||
sqlite3VdbeAddOp2(v, OP_Next, csrLead, addrRewind+2);
|
||||
@ -836,7 +1241,7 @@ static void windowCodeDefaultStep(
|
||||
}
|
||||
|
||||
/* Invoke step function for window functions */
|
||||
windowAggStep(pParse, pMWin, -1, 0, reg);
|
||||
windowAggStep(pParse, pMWin, -1, 0, reg, 0);
|
||||
|
||||
/* Buffer the current row in the ephemeral table. */
|
||||
if( pMWin->nBufferCol>0 ){
|
||||
@ -919,6 +1324,7 @@ void sqlite3WindowCodeStep(
|
||||
int *pbLoop
|
||||
){
|
||||
Window *pMWin = p->pWin;
|
||||
Window *pWin;
|
||||
|
||||
*pbLoop = 0;
|
||||
if( (pMWin->eType==TK_ROWS
|
||||
@ -929,16 +1335,12 @@ void sqlite3WindowCodeStep(
|
||||
return;
|
||||
}
|
||||
|
||||
#if 0
|
||||
if( pMWin->eType==TK_RANGE
|
||||
&& pMWin->eStart==TK_UNBOUNDED
|
||||
&& pMWin->eEnd==TK_CURRENT
|
||||
&& pMWin->pOrderBy
|
||||
){
|
||||
windowCodeCacheStep(pParse, p, pWInfo, regGosub, addrGosub);
|
||||
return;
|
||||
for(pWin=pMWin; pWin; pWin=pWin->pNextWin){
|
||||
if( pWin->pFunc->funcFlags & SQLITE_FUNC_WINDOW_SIZE ){
|
||||
windowCodeCacheStep(pParse, p, pWInfo, regGosub, addrGosub);
|
||||
return;
|
||||
}
|
||||
}
|
||||
#endif
|
||||
|
||||
*pbLoop = 1;
|
||||
windowCodeDefaultStep(pParse, p, pWInfo, regGosub, addrGosub);
|
||||
|
@ -73,9 +73,95 @@ foreach {tn window} {
|
||||
16 "ROWS BETWEEN CURRENT ROW AND UNBOUNDED FOLLOWING"
|
||||
17 "ROWS BETWEEN 4 FOLLOWING AND UNBOUNDED FOLLOWING"
|
||||
} {
|
||||
execsql_test 1.2.1.$tn "SELECT max(b) OVER ( ORDER BY a $window ) FROM t2"
|
||||
execsql_test 1.2.2.$tn "SELECT min(b) OVER ( ORDER BY a $window ) FROM t2"
|
||||
execsql_test 1.$tn.2.1 "SELECT max(b) OVER ( ORDER BY a $window ) FROM t2"
|
||||
execsql_test 1.$tn.2.2 "SELECT min(b) OVER ( ORDER BY a $window ) FROM t2"
|
||||
|
||||
execsql_test 1.$tn.3.1 "
|
||||
SELECT row_number() OVER ( ORDER BY a $window ) FROM t2
|
||||
"
|
||||
execsql_test 1.$tn.3.2 "
|
||||
SELECT row_number() OVER ( PARTITION BY b%10 ORDER BY a $window ) FROM t2
|
||||
"
|
||||
|
||||
execsql_test 1.$tn.4.1 "
|
||||
SELECT dense_rank() OVER ( ORDER BY a $window ) FROM t2
|
||||
"
|
||||
execsql_test 1.$tn.4.2 "
|
||||
SELECT dense_rank() OVER ( PARTITION BY b%10 ORDER BY a $window ) FROM t2
|
||||
"
|
||||
execsql_test 1.$tn.4.3 "
|
||||
SELECT dense_rank() OVER ( ORDER BY b $window ) FROM t2
|
||||
"
|
||||
execsql_test 1.$tn.4.4 "
|
||||
SELECT dense_rank() OVER ( PARTITION BY b%10 ORDER BY b $window ) FROM t2
|
||||
"
|
||||
execsql_test 1.$tn.4.5 "
|
||||
SELECT dense_rank() OVER ( ORDER BY b%10 $window ) FROM t2
|
||||
"
|
||||
execsql_test 1.$tn.4.6 "
|
||||
SELECT dense_rank() OVER ( PARTITION BY b%2 ORDER BY b%10 $window ) FROM t2
|
||||
"
|
||||
|
||||
execsql_test 1.$tn.5.1 "
|
||||
SELECT rank() OVER ( ORDER BY a $window ) FROM t2
|
||||
"
|
||||
execsql_test 1.$tn.5.2 "
|
||||
SELECT rank() OVER ( PARTITION BY b%10 ORDER BY a $window ) FROM t2
|
||||
"
|
||||
execsql_test 1.$tn.5.3 "
|
||||
SELECT rank() OVER ( ORDER BY b $window ) FROM t2
|
||||
"
|
||||
execsql_test 1.$tn.5.4 "
|
||||
SELECT rank() OVER ( PARTITION BY b%10 ORDER BY b $window ) FROM t2
|
||||
"
|
||||
execsql_test 1.$tn.5.5 "
|
||||
SELECT rank() OVER ( ORDER BY b%10 $window ) FROM t2
|
||||
"
|
||||
execsql_test 1.$tn.5.6 "
|
||||
SELECT rank() OVER ( PARTITION BY b%2 ORDER BY b%10 $window ) FROM t2
|
||||
"
|
||||
|
||||
execsql_test 1.$tn.6.1 "
|
||||
SELECT
|
||||
row_number() OVER ( PARTITION BY b%2 ORDER BY b%10 $window ),
|
||||
rank() OVER ( PARTITION BY b%2 ORDER BY b%10 $window ),
|
||||
dense_rank() OVER ( PARTITION BY b%2 ORDER BY b%10 $window )
|
||||
FROM t2
|
||||
"
|
||||
|
||||
execsql_test 1.$tn.7.1 "
|
||||
SELECT CAST( round( 100 *
|
||||
percent_rank() OVER ( ORDER BY a $window )
|
||||
) AS integer) FROM t2"
|
||||
|
||||
#execsql_test 1.$tn.7.2 "
|
||||
#SELECT CAST( round( 100 *
|
||||
#percent_rank() OVER ( PARTITION BY b%10 ORDER BY a $window )
|
||||
#) AS integer) FROM t2"
|
||||
|
||||
execsql_test 1.$tn.7.3 "
|
||||
SELECT CAST( round( 100 *
|
||||
percent_rank() OVER ( ORDER BY b $window )
|
||||
) AS integer) FROM t2"
|
||||
|
||||
#execsql_test 1.$tn.7.4 "
|
||||
#SELECT CAST( round( 100 *
|
||||
#percent_rank() OVER ( PARTITION BY b%10 ORDER BY b $window )
|
||||
#) AS integer) FROM t2"
|
||||
|
||||
execsql_test 1.$tn.7.5 "
|
||||
SELECT CAST( round( 100 *
|
||||
percent_rank() OVER ( ORDER BY b%10 $window )
|
||||
) AS integer) FROM t2"
|
||||
|
||||
execsql_test 1.$tn.7.6 "
|
||||
SELECT CAST( round( 100 *
|
||||
percent_rank() OVER ( PARTITION BY b%2 ORDER BY b%10 $window )
|
||||
) AS integer) FROM t2"
|
||||
|
||||
|
||||
}
|
||||
|
||||
|
||||
finish_test
|
||||
|
||||
|
1562
test/window3.test
1562
test/window3.test
File diff suppressed because it is too large
Load Diff
Loading…
Reference in New Issue
Block a user