blob: 909decba36e811ab144b8da68f8f638e6d0031c3 [file] [log] [blame]
dan86fb6e12018-05-16 20:58:07 +00001/*
2**
3** The author disclaims copyright to this source code. In place of
4** a legal notice, here is a blessing:
5**
6** May you do good and not evil.
7** May you find forgiveness for yourself and forgive others.
8** May you share freely, never taking more than you give.
9**
10*************************************************************************
11*/
12#include "sqliteInt.h"
13
dandfa552f2018-06-02 21:04:28 +000014/*
15** Implementation of built-in window function row_number(). Assumes that the
16** window frame has been coerced to:
17**
18** ROWS BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW
19*/
20static void row_numberStepFunc(
21 sqlite3_context *pCtx,
22 int nArg,
23 sqlite3_value **apArg
24){
25 i64 *p = (i64*)sqlite3_aggregate_context(pCtx, sizeof(*p));
26 if( p ) (*p)++;
27}
28static void row_numberInverseFunc(
29 sqlite3_context *pCtx,
30 int nArg,
31 sqlite3_value **apArg
32){
33}
34static void row_numberValueFunc(sqlite3_context *pCtx){
35 i64 *p = (i64*)sqlite3_aggregate_context(pCtx, sizeof(*p));
36 sqlite3_result_int64(pCtx, (p ? *p : 0));
37}
38
39/*
40** Context object type used by rank() and dense_rank().
41*/
42struct CallCount {
43 i64 nValue;
44 i64 nStep;
45 i64 nTotal;
46};
47
48/*
49** Implementation of built-in window function dense_rank().
50*/
51static void dense_rankStepFunc(
52 sqlite3_context *pCtx,
53 int nArg,
54 sqlite3_value **apArg
55){
56 struct CallCount *p;
57 p = (struct CallCount*)sqlite3_aggregate_context(pCtx, sizeof(*p));
58 if( p ) p->nStep = 1;
59}
60static void dense_rankInverseFunc(
61 sqlite3_context *pCtx,
62 int nArg,
63 sqlite3_value **apArg
64){
65}
66static void dense_rankValueFunc(sqlite3_context *pCtx){
67 struct CallCount *p;
68 p = (struct CallCount*)sqlite3_aggregate_context(pCtx, sizeof(*p));
69 if( p ){
70 if( p->nStep ){
71 p->nValue++;
72 p->nStep = 0;
73 }
74 sqlite3_result_int64(pCtx, p->nValue);
75 }
76}
77
78/*
79** Implementation of built-in window function rank().
80*/
81static void rankStepFunc(
82 sqlite3_context *pCtx,
83 int nArg,
84 sqlite3_value **apArg
85){
86 struct CallCount *p;
87 p = (struct CallCount*)sqlite3_aggregate_context(pCtx, sizeof(*p));
88 if( p ){
89 p->nStep++;
90 if( p->nValue==0 ){
91 p->nValue = p->nStep;
92 }
93 }
94}
95static void rankInverseFunc(
96 sqlite3_context *pCtx,
97 int nArg,
98 sqlite3_value **apArg
99){
100}
101static void rankValueFunc(sqlite3_context *pCtx){
102 struct CallCount *p;
103 p = (struct CallCount*)sqlite3_aggregate_context(pCtx, sizeof(*p));
104 if( p ){
105 sqlite3_result_int64(pCtx, p->nValue);
106 p->nValue = 0;
107 }
108}
109
110/*
111** Implementation of built-in window function percent_rank().
112*/
113static void percent_rankStepFunc(
114 sqlite3_context *pCtx,
115 int nArg,
116 sqlite3_value **apArg
117){
118 struct CallCount *p;
119 assert( nArg==1 );
120
121 p = (struct CallCount*)sqlite3_aggregate_context(pCtx, sizeof(*p));
122 if( p ){
123 if( p->nTotal==0 ){
124 p->nTotal = sqlite3_value_int64(apArg[0]);
125 }
126 p->nStep++;
127 if( p->nValue==0 ){
128 p->nValue = p->nStep;
129 }
130 }
131}
132static void percent_rankInverseFunc(
133 sqlite3_context *pCtx,
134 int nArg,
135 sqlite3_value **apArg
136){
137}
138static void percent_rankValueFunc(sqlite3_context *pCtx){
139 struct CallCount *p;
140 p = (struct CallCount*)sqlite3_aggregate_context(pCtx, sizeof(*p));
141 if( p ){
142 if( p->nTotal>1 ){
143 double r = (double)(p->nValue-1) / (double)(p->nTotal-1);
144 sqlite3_result_double(pCtx, r);
145 }else{
146 sqlite3_result_double(pCtx, 100.0);
147 }
148 p->nValue = 0;
149 }
150}
151
danf1abe362018-06-04 08:22:09 +0000152static void cume_distStepFunc(
153 sqlite3_context *pCtx,
154 int nArg,
155 sqlite3_value **apArg
156){
157 struct CallCount *p;
158 assert( nArg==1 );
159
160 p = (struct CallCount*)sqlite3_aggregate_context(pCtx, sizeof(*p));
161 if( p ){
162 if( p->nTotal==0 ){
163 p->nTotal = sqlite3_value_int64(apArg[0]);
164 }
165 p->nStep++;
166 }
167}
168static void cume_distInverseFunc(
169 sqlite3_context *pCtx,
170 int nArg,
171 sqlite3_value **apArg
172){
173}
174static void cume_distValueFunc(sqlite3_context *pCtx){
175 struct CallCount *p;
176 p = (struct CallCount*)sqlite3_aggregate_context(pCtx, sizeof(*p));
177 if( p ){
178 double r = (double)(p->nStep) / (double)(p->nTotal);
179 sqlite3_result_double(pCtx, r);
180 }
181}
182
dan6bc5c9e2018-06-04 18:55:11 +0000183struct NtileCtx {
184 i64 nTotal; /* Total rows in partition */
185 i64 nParam; /* Parameter passed to ntile(N) */
186 i64 iRow; /* Current row */
187};
188
189/*
190** Implementation of ntile(). This assumes that the window frame has
191** been coerced to:
192**
193** ROWS UNBOUNDED PRECEDING AND CURRENT ROW
dan6bc5c9e2018-06-04 18:55:11 +0000194*/
195static void ntileStepFunc(
196 sqlite3_context *pCtx,
197 int nArg,
198 sqlite3_value **apArg
199){
200 struct NtileCtx *p;
201 assert( nArg==2 );
202 p = (struct NtileCtx*)sqlite3_aggregate_context(pCtx, sizeof(*p));
203 if( p ){
204 if( p->nTotal==0 ){
205 p->nParam = sqlite3_value_int64(apArg[0]);
206 p->nTotal = sqlite3_value_int64(apArg[1]);
207 if( p->nParam<=0 ){
208 sqlite3_result_error(
209 pCtx, "argument of ntile must be a positive integer", -1
210 );
211 }
212 }
213 p->iRow++;
214 }
215}
216static void ntileInverseFunc(
217 sqlite3_context *pCtx,
218 int nArg,
219 sqlite3_value **apArg
220){
221}
222static void ntileValueFunc(sqlite3_context *pCtx){
223 struct NtileCtx *p;
224 p = (struct NtileCtx*)sqlite3_aggregate_context(pCtx, sizeof(*p));
225 if( p && p->nParam>0 ){
226 int nSize = (p->nTotal / p->nParam);
227 if( nSize==0 ){
228 sqlite3_result_int64(pCtx, p->iRow);
229 }else{
230 i64 nLarge = p->nTotal - p->nParam*nSize;
231 i64 iSmall = nLarge*(nSize+1);
232 i64 iRow = p->iRow-1;
233
234 assert( (nLarge*(nSize+1) + (p->nParam-nLarge)*nSize)==p->nTotal );
235
236 if( iRow<iSmall ){
237 sqlite3_result_int64(pCtx, 1 + iRow/(nSize+1));
238 }else{
239 sqlite3_result_int64(pCtx, 1 + nLarge + (iRow-iSmall)/nSize);
240 }
241 }
242 }
243}
244
dan1c5ed622018-06-05 16:16:17 +0000245struct LastValueCtx {
246 sqlite3_value *pVal;
247 int nVal;
248};
249
250/*
251** Implementation of last_value().
252*/
253static void last_valueStepFunc(
254 sqlite3_context *pCtx,
255 int nArg,
256 sqlite3_value **apArg
257){
258 struct LastValueCtx *p;
259 p = (struct LastValueCtx *)sqlite3_aggregate_context(pCtx, sizeof(*p));
260 if( p ){
261 sqlite3_value_free(p->pVal);
262 p->pVal = sqlite3_value_dup(apArg[0]);
263 p->nVal++;
264 }
265}
266static void last_valueInverseFunc(
267 sqlite3_context *pCtx,
268 int nArg,
269 sqlite3_value **apArg
270){
271 struct LastValueCtx *p;
272 p = (struct LastValueCtx *)sqlite3_aggregate_context(pCtx, sizeof(*p));
273 if( p ){
274 p->nVal--;
275 if( p->nVal==0 ){
276 sqlite3_value_free(p->pVal);
277 p->pVal = 0;
278 }
279 }
280}
281static void last_valueValueFunc(sqlite3_context *pCtx){
282 struct LastValueCtx *p;
283 p = (struct LastValueCtx *)sqlite3_aggregate_context(pCtx, sizeof(*p));
284 if( p && p->pVal ){
285 sqlite3_result_value(pCtx, p->pVal);
286 }
287}
288static void last_valueFinalizeFunc(sqlite3_context *pCtx){
289 struct LastValueCtx *p;
290 p = (struct LastValueCtx *)sqlite3_aggregate_context(pCtx, sizeof(*p));
291 if( p && p->pVal ){
292 sqlite3_result_value(pCtx, p->pVal);
293 sqlite3_value_free(p->pVal);
294 p->pVal = 0;
295 }
296}
297
dandfa552f2018-06-02 21:04:28 +0000298static void nth_valueStepFunc(
299 sqlite3_context *pCtx,
300 int nArg,
301 sqlite3_value **apArg
302){
303}
304static void nth_valueInverseFunc(
305 sqlite3_context *pCtx,
306 int nArg,
307 sqlite3_value **apArg
308){
309}
310static void nth_valueValueFunc(sqlite3_context *pCtx){
311}
312
313#define WINDOWFUNC(name,nArg,extra) { \
314 nArg, (SQLITE_UTF8|SQLITE_FUNC_WINDOW|extra), 0, 0, \
315 name ## StepFunc, name ## ValueFunc, name ## ValueFunc, \
316 name ## InverseFunc, #name \
317}
318
dan1c5ed622018-06-05 16:16:17 +0000319#define WINDOWFUNCF(name,nArg,extra) { \
320 nArg, (SQLITE_UTF8|SQLITE_FUNC_WINDOW|extra), 0, 0, \
321 name ## StepFunc, name ## FinalizeFunc, name ## ValueFunc, \
322 name ## InverseFunc, #name \
323}
324
dandfa552f2018-06-02 21:04:28 +0000325/*
326** Register those built-in window functions that are not also aggregates.
327*/
328void sqlite3WindowFunctions(void){
329 static FuncDef aWindowFuncs[] = {
330 WINDOWFUNC(row_number, 0, 0),
331 WINDOWFUNC(dense_rank, 0, 0),
332 WINDOWFUNC(rank, 0, 0),
333 WINDOWFUNC(percent_rank, 0, SQLITE_FUNC_WINDOW_SIZE),
danf1abe362018-06-04 08:22:09 +0000334 WINDOWFUNC(cume_dist, 0, SQLITE_FUNC_WINDOW_SIZE),
dan6bc5c9e2018-06-04 18:55:11 +0000335 WINDOWFUNC(ntile, 1, SQLITE_FUNC_WINDOW_SIZE),
dan1c5ed622018-06-05 16:16:17 +0000336 WINDOWFUNCF(last_value, 1, 0),
dandfa552f2018-06-02 21:04:28 +0000337 WINDOWFUNC(nth_value, 2, 0),
338 };
339 sqlite3InsertBuiltinFuncs(aWindowFuncs, ArraySize(aWindowFuncs));
340}
341
342void sqlite3WindowUpdate(Parse *pParse, Window *pWin, FuncDef *pFunc){
343 if( pFunc->funcFlags & SQLITE_FUNC_WINDOW ){
344 sqlite3 *db = pParse->db;
dan6bc5c9e2018-06-04 18:55:11 +0000345 if( pFunc->xSFunc==row_numberStepFunc || pFunc->xSFunc==ntileStepFunc ){
dandfa552f2018-06-02 21:04:28 +0000346 sqlite3ExprDelete(db, pWin->pStart);
347 sqlite3ExprDelete(db, pWin->pEnd);
348 pWin->pStart = pWin->pEnd = 0;
349 pWin->eType = TK_ROWS;
350 pWin->eStart = TK_UNBOUNDED;
351 pWin->eEnd = TK_CURRENT;
352 }
353
354 if( pFunc->xSFunc==dense_rankStepFunc || pFunc->xSFunc==rankStepFunc
danf1abe362018-06-04 08:22:09 +0000355 || pFunc->xSFunc==percent_rankStepFunc || pFunc->xSFunc==cume_distStepFunc
dandfa552f2018-06-02 21:04:28 +0000356 ){
357 sqlite3ExprDelete(db, pWin->pStart);
358 sqlite3ExprDelete(db, pWin->pEnd);
359 pWin->pStart = pWin->pEnd = 0;
360 pWin->eType = TK_RANGE;
361 pWin->eStart = TK_UNBOUNDED;
362 pWin->eEnd = TK_CURRENT;
363 }
364 }
365}
366
367typedef struct WindowRewrite WindowRewrite;
368struct WindowRewrite {
369 Window *pWin;
370 ExprList *pSub;
371};
372
373static int selectWindowRewriteSelectCb(Walker *pWalker, Select *pSelect){
374 return WRC_Prune;
375}
376
377static int selectWindowRewriteExprCb(Walker *pWalker, Expr *pExpr){
378 struct WindowRewrite *p = pWalker->u.pRewrite;
379 Parse *pParse = pWalker->pParse;
380
381 switch( pExpr->op ){
382
383 case TK_FUNCTION:
384 if( pExpr->pWin==0 ){
385 break;
386 }else{
387 Window *pWin;
388 for(pWin=p->pWin; pWin; pWin=pWin->pNextWin){
389 if( pExpr->pWin==pWin ){
390 pExpr->pWin->pOwner = pExpr;
391 return WRC_Prune;
392 }
393 }
394 }
395 /* Fall through. */
396
397 case TK_COLUMN: {
398 Expr *pDup = sqlite3ExprDup(pParse->db, pExpr, 0);
399 p->pSub = sqlite3ExprListAppend(pParse, p->pSub, pDup);
400 if( p->pSub ){
401 assert( ExprHasProperty(pExpr, EP_Static)==0 );
402 ExprSetProperty(pExpr, EP_Static);
403 sqlite3ExprDelete(pParse->db, pExpr);
404 ExprClearProperty(pExpr, EP_Static);
405 memset(pExpr, 0, sizeof(Expr));
406
407 pExpr->op = TK_COLUMN;
408 pExpr->iColumn = p->pSub->nExpr-1;
409 pExpr->iTable = p->pWin->iEphCsr;
410 }
411
412 break;
413 }
414
415 default: /* no-op */
416 break;
417 }
418
419 return WRC_Continue;
420}
421
422static int selectWindowRewriteEList(
423 Parse *pParse,
424 Window *pWin,
425 ExprList *pEList, /* Rewrite expressions in this list */
426 ExprList **ppSub /* IN/OUT: Sub-select expression-list */
427){
428 Walker sWalker;
429 WindowRewrite sRewrite;
430 int rc;
431
432 memset(&sWalker, 0, sizeof(Walker));
433 memset(&sRewrite, 0, sizeof(WindowRewrite));
434
435 sRewrite.pSub = *ppSub;
436 sRewrite.pWin = pWin;
437
438 sWalker.pParse = pParse;
439 sWalker.xExprCallback = selectWindowRewriteExprCb;
440 sWalker.xSelectCallback = selectWindowRewriteSelectCb;
441 sWalker.u.pRewrite = &sRewrite;
442
443 rc = sqlite3WalkExprList(&sWalker, pEList);
444
445 *ppSub = sRewrite.pSub;
446 return rc;
447}
448
449static ExprList *exprListAppendList(
450 Parse *pParse, /* Parsing context */
451 ExprList *pList, /* List to which to append. Might be NULL */
452 ExprList *pAppend /* List of values to append. Might be NULL */
453){
454 if( pAppend ){
455 int i;
456 int nInit = pList ? pList->nExpr : 0;
457 for(i=0; i<pAppend->nExpr; i++){
458 Expr *pDup = sqlite3ExprDup(pParse->db, pAppend->a[i].pExpr, 0);
459 pList = sqlite3ExprListAppend(pParse, pList, pDup);
460 if( pList ) pList->a[nInit+i].sortOrder = pAppend->a[i].sortOrder;
461 }
462 }
463 return pList;
464}
465
466/*
467** If the SELECT statement passed as the second argument does not invoke
468** any SQL window functions, this function is a no-op. Otherwise, it
469** rewrites the SELECT statement so that window function xStep functions
470** are invoked in the correct order. The simplest version of the
471** transformation is:
472**
473** SELECT win(args...) OVER (<list1>) FROM <src> ORDER BY <list2>
474**
475** to
476**
477** SELECT win(args...) FROM (
478** SELECT args... FROM <src> ORDER BY <list1>
479** ) ORDER BY <list2>
480**
481** where <src> may contain WHERE, GROUP BY and HAVING clauses, and <list1>
482** is the concatenation of the PARTITION BY and ORDER BY clauses in the
483** OVER clause.
484**
485*/
486int sqlite3WindowRewrite(Parse *pParse, Select *p){
487 int rc = SQLITE_OK;
488 if( p->pWin ){
489 Vdbe *v = sqlite3GetVdbe(pParse);
490 int i;
491 sqlite3 *db = pParse->db;
492 Select *pSub = 0; /* The subquery */
493 SrcList *pSrc = p->pSrc;
494 Expr *pWhere = p->pWhere;
495 ExprList *pGroupBy = p->pGroupBy;
496 Expr *pHaving = p->pHaving;
497 ExprList *pSort = 0;
498
499 ExprList *pSublist = 0; /* Expression list for sub-query */
500 Window *pMWin = p->pWin; /* Master window object */
501 Window *pWin; /* Window object iterator */
502
503 p->pSrc = 0;
504 p->pWhere = 0;
505 p->pGroupBy = 0;
506 p->pHaving = 0;
507
508 /* Assign a cursor number for the ephemeral table used to buffer rows.
509 ** The OpenEphemeral instruction is coded later, after it is known how
510 ** many columns the table will have. */
511 pMWin->iEphCsr = pParse->nTab++;
512
513 rc = selectWindowRewriteEList(pParse, pMWin, p->pEList, &pSublist);
514 if( rc ) return rc;
515 rc = selectWindowRewriteEList(pParse, pMWin, p->pOrderBy, &pSublist);
516 if( rc ) return rc;
517 pMWin->nBufferCol = (pSublist ? pSublist->nExpr : 0);
518
519 /* Create the ORDER BY clause for the sub-select. This is the concatenation
520 ** of the window PARTITION and ORDER BY clauses. Append the same
521 ** expressions to the sub-select expression list. They are required to
522 ** figure out where boundaries for partitions and sets of peer rows. */
523 pSort = sqlite3ExprListDup(db, pMWin->pPartition, 0);
524 if( pMWin->pOrderBy ){
525 pSort = exprListAppendList(pParse, pSort, pMWin->pOrderBy);
526 }
527 pSublist = exprListAppendList(pParse, pSublist, pSort);
528
529 /* Append the arguments passed to each window function to the
530 ** sub-select expression list. Also allocate two registers for each
531 ** window function - one for the accumulator, another for interim
532 ** results. */
533 for(pWin=pMWin; pWin; pWin=pWin->pNextWin){
534 pWin->iArgCol = (pSublist ? pSublist->nExpr : 0);
535 pSublist = exprListAppendList(pParse, pSublist, pWin->pOwner->x.pList);
536 pWin->regAccum = ++pParse->nMem;
537 pWin->regResult = ++pParse->nMem;
538 sqlite3VdbeAddOp2(v, OP_Null, 0, pWin->regAccum);
539 }
540
541 pSub = sqlite3SelectNew(
542 pParse, pSublist, pSrc, pWhere, pGroupBy, pHaving, pSort, 0, 0
543 );
544 p->pSrc = sqlite3SrcListAppend(db, 0, 0, 0);
545 if( p->pSrc ){
546 int iTab;
547 ExprList *pList = 0;
548 p->pSrc->a[0].pSelect = pSub;
549 sqlite3SrcListAssignCursors(pParse, p->pSrc);
550 if( sqlite3ExpandSubquery(pParse, &p->pSrc->a[0]) ){
551 rc = SQLITE_NOMEM;
552 }else{
553 pSub->selFlags |= SF_Expanded;
554 }
555 }
556
557 sqlite3VdbeAddOp2(v, OP_OpenEphemeral, pMWin->iEphCsr, pSublist->nExpr);
558 }
559
560 return rc;
561}
562
dan86fb6e12018-05-16 20:58:07 +0000563void sqlite3WindowDelete(sqlite3 *db, Window *p){
564 if( p ){
565 sqlite3ExprDelete(db, p->pFilter);
566 sqlite3ExprListDelete(db, p->pPartition);
567 sqlite3ExprListDelete(db, p->pOrderBy);
568 sqlite3ExprDelete(db, p->pEnd);
569 sqlite3ExprDelete(db, p->pStart);
570 sqlite3DbFree(db, p);
571 }
572}
573
574Window *sqlite3WindowAlloc(
575 Parse *pParse,
576 int eType,
danc3a20c12018-05-23 20:55:37 +0000577 int eStart, Expr *pStart,
578 int eEnd, Expr *pEnd
dan86fb6e12018-05-16 20:58:07 +0000579){
580 Window *pWin = (Window*)sqlite3DbMallocZero(pParse->db, sizeof(Window));
581
582 if( pWin ){
583 pWin->eType = eType;
584 pWin->eStart = eStart;
585 pWin->eEnd = eEnd;
586 pWin->pEnd = pEnd;
587 pWin->pStart = pStart;
588 }else{
589 sqlite3ExprDelete(pParse->db, pEnd);
590 sqlite3ExprDelete(pParse->db, pStart);
591 }
592
593 return pWin;
594}
595
596void sqlite3WindowAttach(Parse *pParse, Expr *p, Window *pWin){
597 if( p ){
598 p->pWin = pWin;
599 }else{
600 sqlite3WindowDelete(pParse->db, pWin);
601 }
602}
dane2f781b2018-05-17 19:24:08 +0000603
604/*
605** Return 0 if the two window objects are identical, or non-zero otherwise.
606*/
607int sqlite3WindowCompare(Parse *pParse, Window *p1, Window *p2){
608 if( p1->eType!=p2->eType ) return 1;
609 if( p1->eStart!=p2->eStart ) return 1;
610 if( p1->eEnd!=p2->eEnd ) return 1;
611 if( sqlite3ExprCompare(pParse, p1->pStart, p2->pStart, -1) ) return 1;
612 if( sqlite3ExprCompare(pParse, p1->pEnd, p2->pEnd, -1) ) return 1;
613 if( sqlite3ExprListCompare(p1->pPartition, p2->pPartition, -1) ) return 1;
614 if( sqlite3ExprListCompare(p1->pOrderBy, p2->pOrderBy, -1) ) return 1;
615 return 0;
616}
617
danc9a86682018-05-30 20:44:58 +0000618static void windowAggInit(Parse *pParse, Window *pMWin){
619 Window *pWin;
620 for(pWin=pMWin; pWin; pWin=pWin->pNextWin){
danec891fd2018-06-06 20:51:02 +0000621 Vdbe *v = sqlite3GetVdbe(pParse);
622 FuncDef *p = pWin->pFunc;
623 if( (p->funcFlags & SQLITE_FUNC_MINMAX) && pWin->eStart!=TK_UNBOUNDED ){
danc9a86682018-05-30 20:44:58 +0000624 ExprList *pList = pWin->pOwner->x.pList;
625 KeyInfo *pKeyInfo = sqlite3KeyInfoFromExprList(pParse, pList, 0, 0);
danc9a86682018-05-30 20:44:58 +0000626 pWin->csrApp = pParse->nTab++;
627 pWin->regApp = pParse->nMem+1;
628 pParse->nMem += 3;
629 if( pKeyInfo && pWin->pFunc->zName[1]=='i' ){
630 assert( pKeyInfo->aSortOrder[0]==0 );
631 pKeyInfo->aSortOrder[0] = 1;
632 }
633 sqlite3VdbeAddOp2(v, OP_OpenEphemeral, pWin->csrApp, 2);
634 sqlite3VdbeAppendP4(v, pKeyInfo, P4_KEYINFO);
635 sqlite3VdbeAddOp2(v, OP_Integer, 0, pWin->regApp+1);
636 }
danec891fd2018-06-06 20:51:02 +0000637 else if( p->xSFunc==nth_valueStepFunc ){
638 /* Allocate two registers at pWin->regApp. These will be used to
639 ** store the start and end index of the current frame. */
640 assert( pMWin->iEphCsr );
641 pWin->regApp = pParse->nMem+1;
642 pWin->csrApp = pParse->nTab++;
643 pParse->nMem += 2;
danec891fd2018-06-06 20:51:02 +0000644 sqlite3VdbeAddOp2(v, OP_OpenDup, pWin->csrApp, pMWin->iEphCsr);
645 }
danc9a86682018-05-30 20:44:58 +0000646 }
647}
648
danf9eae182018-05-21 19:45:11 +0000649void sqlite3WindowCodeInit(Parse *pParse, Window *pWin){
650 Vdbe *v = sqlite3GetVdbe(pParse);
651 int nPart = (pWin->pPartition ? pWin->pPartition->nExpr : 0);
652 nPart += (pWin->pOrderBy ? pWin->pOrderBy->nExpr : 0);
653 if( nPart ){
654 pWin->regPart = pParse->nMem+1;
655 pParse->nMem += nPart;
656 sqlite3VdbeAddOp3(v, OP_Null, 0, pWin->regPart, pWin->regPart+nPart-1);
657 }
danc9a86682018-05-30 20:44:58 +0000658 windowAggInit(pParse, pWin);
danf9eae182018-05-21 19:45:11 +0000659}
660
danc3a20c12018-05-23 20:55:37 +0000661static void windowCheckFrameValue(Parse *pParse, int reg, int bEnd){
662 static const char *azErr[] = {
663 "frame starting offset must be a non-negative integer",
664 "frame ending offset must be a non-negative integer"
665 };
666 Vdbe *v = sqlite3GetVdbe(pParse);
667 int regZero = ++pParse->nMem;
668
danc3a20c12018-05-23 20:55:37 +0000669 sqlite3VdbeAddOp2(v, OP_Integer, 0, regZero);
670 sqlite3VdbeAddOp2(v, OP_MustBeInt, reg, sqlite3VdbeCurrentAddr(v)+2);
671 sqlite3VdbeAddOp3(v, OP_Ge, regZero, sqlite3VdbeCurrentAddr(v)+2, reg);
672 sqlite3VdbeAddOp2(v, OP_Halt, SQLITE_ERROR, OE_Abort);
673 sqlite3VdbeAppendP4(v, (void*)azErr[bEnd], P4_STATIC);
674}
675
danc9a86682018-05-30 20:44:58 +0000676/*
677** Generate VM code to invoke either xStep() (if bInverse is 0) or
678** xInverse (if bInverse is non-zero) for each window function in the
679** linked list starting at pMWin.
680*/
dan31f56392018-05-24 21:10:57 +0000681static void windowAggStep(
682 Parse *pParse,
683 Window *pMWin,
684 int csr,
685 int bInverse,
dandfa552f2018-06-02 21:04:28 +0000686 int reg,
687 int regPartSize /* Register containing size of partition */
dan31f56392018-05-24 21:10:57 +0000688){
689 Vdbe *v = sqlite3GetVdbe(pParse);
690 Window *pWin;
691 for(pWin=pMWin; pWin; pWin=pWin->pNextWin){
dandfa552f2018-06-02 21:04:28 +0000692 int flags = pWin->pFunc->funcFlags;
danc9a86682018-05-30 20:44:58 +0000693 int regArg;
dandfa552f2018-06-02 21:04:28 +0000694 int nArg = pWin->nArg;
695
dan6bc5c9e2018-06-04 18:55:11 +0000696 if( csr>=0 ){
danc9a86682018-05-30 20:44:58 +0000697 int i;
698 for(i=0; i<pWin->nArg; i++){
699 sqlite3VdbeAddOp3(v, OP_Column, csr, pWin->iArgCol+i, reg+i);
700 }
701 regArg = reg;
dan6bc5c9e2018-06-04 18:55:11 +0000702 if( flags & SQLITE_FUNC_WINDOW_SIZE ){
703 if( nArg==0 ){
704 regArg = regPartSize;
705 }else{
706 sqlite3VdbeAddOp2(v, OP_SCopy, regPartSize, reg+nArg);
707 }
708 nArg++;
709 }
danc9a86682018-05-30 20:44:58 +0000710 }else{
dan6bc5c9e2018-06-04 18:55:11 +0000711 assert( !(flags & SQLITE_FUNC_WINDOW_SIZE) );
danc9a86682018-05-30 20:44:58 +0000712 regArg = reg + pWin->iArgCol;
dan31f56392018-05-24 21:10:57 +0000713 }
danc9a86682018-05-30 20:44:58 +0000714
danec891fd2018-06-06 20:51:02 +0000715 if( (pWin->pFunc->funcFlags & SQLITE_FUNC_MINMAX)
716 && pWin->eStart!=TK_UNBOUNDED
717 ){
danc9a86682018-05-30 20:44:58 +0000718 if( bInverse==0 ){
719 sqlite3VdbeAddOp2(v, OP_AddImm, pWin->regApp+1, 1);
720 sqlite3VdbeAddOp2(v, OP_SCopy, regArg, pWin->regApp);
721 sqlite3VdbeAddOp3(v, OP_MakeRecord, pWin->regApp, 2, pWin->regApp+2);
722 sqlite3VdbeAddOp2(v, OP_IdxInsert, pWin->csrApp, pWin->regApp+2);
723 }else{
724 sqlite3VdbeAddOp4Int(v, OP_SeekGE, pWin->csrApp, 0, regArg, 1);
725 sqlite3VdbeAddOp1(v, OP_Delete, pWin->csrApp);
726 sqlite3VdbeJumpHere(v, sqlite3VdbeCurrentAddr(v)-2);
727 }
danec891fd2018-06-06 20:51:02 +0000728 }else if( pWin->regApp ){
729 assert( pWin->pFunc->xSFunc==nth_valueStepFunc );
730 assert( bInverse==0 || bInverse==1 );
731 sqlite3VdbeAddOp2(v, OP_AddImm, pWin->regApp+1-bInverse, 1);
danc9a86682018-05-30 20:44:58 +0000732 }else{
733 if( pWin->pFunc->funcFlags & SQLITE_FUNC_NEEDCOLL ){
734 CollSeq *pColl;
735 pColl = sqlite3ExprCollSeq(pParse, pWin->pOwner->x.pList->a[0].pExpr);
736 sqlite3VdbeAddOp4(v, OP_CollSeq, 0,0,0, (const char*)pColl, P4_COLLSEQ);
737 }
738 sqlite3VdbeAddOp3(v, OP_AggStep0, bInverse, regArg, pWin->regAccum);
739 sqlite3VdbeAppendP4(v, pWin->pFunc, P4_FUNCDEF);
dandfa552f2018-06-02 21:04:28 +0000740 sqlite3VdbeChangeP5(v, (u8)nArg);
danc9a86682018-05-30 20:44:58 +0000741 }
dan31f56392018-05-24 21:10:57 +0000742 }
743}
744
dand6f784e2018-05-28 18:30:45 +0000745static void windowAggFinal(Parse *pParse, Window *pMWin, int bFinal){
746 Vdbe *v = sqlite3GetVdbe(pParse);
747 Window *pWin;
748
749 for(pWin=pMWin; pWin; pWin=pWin->pNextWin){
danec891fd2018-06-06 20:51:02 +0000750 if( (pWin->pFunc->funcFlags & SQLITE_FUNC_MINMAX)
751 && pWin->eStart!=TK_UNBOUNDED
752 ){
dand6f784e2018-05-28 18:30:45 +0000753 sqlite3VdbeAddOp2(v, OP_Null, 0, pWin->regResult);
danc9a86682018-05-30 20:44:58 +0000754 sqlite3VdbeAddOp1(v, OP_Last, pWin->csrApp);
755 sqlite3VdbeAddOp3(v, OP_Column, pWin->csrApp, 0, pWin->regResult);
756 sqlite3VdbeJumpHere(v, sqlite3VdbeCurrentAddr(v)-2);
757 if( bFinal ){
758 sqlite3VdbeAddOp1(v, OP_ResetSorter, pWin->csrApp);
759 }
danec891fd2018-06-06 20:51:02 +0000760 }else if( pWin->regApp ){
dand6f784e2018-05-28 18:30:45 +0000761 }else{
danc9a86682018-05-30 20:44:58 +0000762 if( bFinal==0 ){
763 sqlite3VdbeAddOp2(v, OP_Null, 0, pWin->regResult);
764 }
765 sqlite3VdbeAddOp2(v, OP_AggFinal, pWin->regAccum, pWin->nArg);
766 sqlite3VdbeAppendP4(v, pWin->pFunc, P4_FUNCDEF);
767 if( bFinal ){
768 sqlite3VdbeAddOp2(v, OP_Copy, pWin->regAccum, pWin->regResult);
769 }else{
770 sqlite3VdbeChangeP3(v, -1, pWin->regResult);
771 }
dand6f784e2018-05-28 18:30:45 +0000772 }
773 }
774}
775
danf690b572018-06-01 21:00:08 +0000776static void windowPartitionCache(
777 Parse *pParse,
778 Select *p,
779 WhereInfo *pWInfo,
780 int regFlushPart,
dandfa552f2018-06-02 21:04:28 +0000781 int lblFlushPart,
782 int *pRegSize
danf690b572018-06-01 21:00:08 +0000783){
784 Window *pMWin = p->pWin;
785 Vdbe *v = sqlite3GetVdbe(pParse);
786 Window *pWin;
787 int iSubCsr = p->pSrc->a[0].iCursor;
788 int nSub = p->pSrc->a[0].pTab->nCol;
789 int k;
790
791 int reg = pParse->nMem+1;
792 int regRecord = reg+nSub;
793 int regRowid = regRecord+1;
794
dandfa552f2018-06-02 21:04:28 +0000795 *pRegSize = regRowid;
danf690b572018-06-01 21:00:08 +0000796 pParse->nMem += nSub + 2;
797
798 /* Martial the row returned by the sub-select into an array of
799 ** registers. */
800 for(k=0; k<nSub; k++){
801 sqlite3VdbeAddOp3(v, OP_Column, iSubCsr, k, reg+k);
802 }
803 sqlite3VdbeAddOp3(v, OP_MakeRecord, reg, nSub, regRecord);
804
805 /* Check if this is the start of a new partition. If so, call the
806 ** flush_partition sub-routine. */
807 if( pMWin->pPartition ){
808 int addr;
809 ExprList *pPart = pMWin->pPartition;
810 int nPart = (pPart ? pPart->nExpr : 0);
811 int regNewPart = reg + pMWin->nBufferCol;
812 KeyInfo *pKeyInfo = sqlite3KeyInfoFromExprList(pParse, pPart, 0, 0);
813
814 addr = sqlite3VdbeAddOp3(v, OP_Compare, regNewPart, pMWin->regPart,nPart);
815 sqlite3VdbeAppendP4(v, (void*)pKeyInfo, P4_KEYINFO);
816 sqlite3VdbeAddOp3(v, OP_Jump, addr+2, addr+4, addr+2);
817 sqlite3VdbeAddOp3(v, OP_Copy, regNewPart, pMWin->regPart, nPart-1);
818 sqlite3VdbeAddOp2(v, OP_Gosub, regFlushPart, lblFlushPart);
819 }
820
821 /* Buffer the current row in the ephemeral table. */
822 sqlite3VdbeAddOp2(v, OP_NewRowid, pMWin->iEphCsr, regRowid);
823 sqlite3VdbeAddOp3(v, OP_Insert, pMWin->iEphCsr, regRecord, regRowid);
824
825 /* End of the input loop */
826 sqlite3WhereEnd(pWInfo);
827
828 /* Invoke "flush_partition" to deal with the final (or only) partition */
829 sqlite3VdbeAddOp2(v, OP_Gosub, regFlushPart, lblFlushPart);
830}
dand6f784e2018-05-28 18:30:45 +0000831
danec891fd2018-06-06 20:51:02 +0000832static void windowReturnOneRow(
833 Parse *pParse,
834 Window *pMWin,
835 int regGosub,
836 int addrGosub
837){
838 Vdbe *v = sqlite3GetVdbe(pParse);
839 Window *pWin;
840 for(pWin=pMWin; pWin; pWin=pWin->pNextWin){
841 FuncDef *pFunc = pWin->pFunc;
842 if( pFunc->xSFunc==nth_valueStepFunc ){
843 int csr = pWin->csrApp;
844 int lbl = sqlite3VdbeMakeLabel(v);
845 int tmpReg = sqlite3GetTempReg(pParse);
846 sqlite3VdbeAddOp2(v, OP_Null, 0, pWin->regResult);
847 sqlite3VdbeAddOp3(v, OP_Column, pWin->iEphCsr, pWin->iArgCol+1, tmpReg);
848 sqlite3VdbeAddOp3(v, OP_Add, tmpReg, pWin->regApp, tmpReg);
849 sqlite3VdbeAddOp3(v, OP_Gt, pWin->regApp+1, lbl, tmpReg);
850 sqlite3VdbeAddOp3(v, OP_SeekRowid, csr, lbl, tmpReg);
851 sqlite3VdbeAddOp3(v, OP_Column, csr, pWin->iArgCol, pWin->regResult);
852 sqlite3VdbeResolveLabel(v, lbl);
853 sqlite3ReleaseTempReg(pParse, tmpReg);
854 }
855 }
856 sqlite3VdbeAddOp2(v, OP_Gosub, regGosub, addrGosub);
857}
858
859static void windowReturnRows(
860 Parse *pParse,
861 Window *pMWin,
862 int regCtr,
863 int bFinal,
864 int regGosub,
865 int addrGosub,
866 int regInvArg,
867 int regInvSize
868){
869 int addr;
870 Vdbe *v = sqlite3GetVdbe(pParse);
871 windowAggFinal(pParse, pMWin, 0);
872 addr = sqlite3VdbeAddOp3(v, OP_IfPos, regCtr, sqlite3VdbeCurrentAddr(v)+2 ,1);
873 sqlite3VdbeAddOp2(v, OP_Goto, 0, 0);
874 windowReturnOneRow(pParse, pMWin, regGosub, addrGosub);
875 if( regInvArg ){
876 windowAggStep(pParse, pMWin, pMWin->iEphCsr, 1, regInvArg, regInvSize);
877 }
878 sqlite3VdbeAddOp2(v, OP_Next, pMWin->iEphCsr, addr);
879 sqlite3VdbeJumpHere(v, addr+1); /* The OP_Goto */
880}
881
dan2e605682018-06-07 15:54:26 +0000882static int windowInitAccum(Parse *pParse, Window *pMWin){
883 Vdbe *v = sqlite3GetVdbe(pParse);
884 int regArg;
885 int nArg = 0;
886 Window *pWin;
887 for(pWin=pMWin; pWin; pWin=pWin->pNextWin){
888 sqlite3VdbeAddOp2(v, OP_Null, 0, pWin->regAccum);
889 nArg = MAX(nArg, pWin->nArg);
890 if( pWin->pFunc->xSFunc==nth_valueStepFunc ){
891 sqlite3VdbeAddOp2(v, OP_Integer, 0, pWin->regApp);
892 sqlite3VdbeAddOp2(v, OP_Integer, 0, pWin->regApp+1);
893 }
894 }
895 regArg = pParse->nMem+1;
896 pParse->nMem += nArg;
897 return regArg;
898}
899
900
dan99652dd2018-05-24 17:49:14 +0000901/*
dan09590aa2018-05-25 20:30:17 +0000902** ROWS BETWEEN <expr1> PRECEDING AND <expr2> FOLLOWING
903** ----------------------------------------------------
dan99652dd2018-05-24 17:49:14 +0000904**
dan09590aa2018-05-25 20:30:17 +0000905** Pseudo-code for the implementation of this window frame type is as
906** follows. sqlite3WhereBegin() has already been called to generate the
907** top of the main loop when this function is called.
908**
909** Each time the sub-routine at addrGosub is invoked, a single output
910** row is generated based on the current row indicated by Window.iEphCsr.
911**
912** ...
913** if( new partition ){
914** Gosub flush_partition
915** }
916** Insert (record in eph-table)
917** sqlite3WhereEnd()
918** Gosub flush_partition
919**
920** flush_partition:
921** Once {
922** OpenDup (iEphCsr -> csrStart)
923** OpenDup (iEphCsr -> csrEnd)
dan99652dd2018-05-24 17:49:14 +0000924** }
dan09590aa2018-05-25 20:30:17 +0000925** regStart = <expr1> // PRECEDING expression
926** regEnd = <expr2> // FOLLOWING expression
927** if( regStart<0 || regEnd<0 ){ error! }
928** Rewind (csr,csrStart,csrEnd) // if EOF goto flush_partition_done
929** Next(csrEnd) // if EOF skip Aggstep
930** Aggstep (csrEnd)
931** if( (regEnd--)<=0 ){
932** AggFinal (xValue)
933** Gosub addrGosub
934** Next(csr) // if EOF goto flush_partition_done
935** if( (regStart--)<=0 ){
936** AggStep (csrStart, xInverse)
937** Next(csrStart)
938** }
939** }
940** flush_partition_done:
941** ResetSorter (csr)
942** Return
dan99652dd2018-05-24 17:49:14 +0000943**
dan09590aa2018-05-25 20:30:17 +0000944** ROWS BETWEEN <expr> PRECEDING AND CURRENT ROW
945** ROWS BETWEEN CURRENT ROW AND <expr> FOLLOWING
946** ROWS BETWEEN UNBOUNDED PRECEDING AND <expr> FOLLOWING
947**
948** These are similar to the above. For "CURRENT ROW", intialize the
949** register to 0. For "UNBOUNDED PRECEDING" to infinity.
950**
951** ROWS BETWEEN <expr> PRECEDING AND UNBOUNDED FOLLOWING
952** ROWS BETWEEN CURRENT ROW AND UNBOUNDED FOLLOWING
953**
954** Rewind (csr,csrStart,csrEnd) // if EOF goto flush_partition_done
955** while( 1 ){
956** Next(csrEnd) // Exit while(1) at EOF
957** Aggstep (csrEnd)
958** }
959** while( 1 ){
dan99652dd2018-05-24 17:49:14 +0000960** AggFinal (xValue)
961** Gosub addrGosub
dan09590aa2018-05-25 20:30:17 +0000962** Next(csr) // if EOF goto flush_partition_done
dan31f56392018-05-24 21:10:57 +0000963** if( (regStart--)<=0 ){
964** AggStep (csrStart, xInverse)
965** Next(csrStart)
dan99652dd2018-05-24 17:49:14 +0000966** }
967** }
dan99652dd2018-05-24 17:49:14 +0000968**
dan09590aa2018-05-25 20:30:17 +0000969** For the "CURRENT ROW AND UNBOUNDED FOLLOWING" case, the final if()
970** condition is always true (as if regStart were initialized to 0).
dan99652dd2018-05-24 17:49:14 +0000971**
dan09590aa2018-05-25 20:30:17 +0000972** RANGE BETWEEN CURRENT ROW AND UNBOUNDED FOLLOWING
973**
974** This is the only RANGE case handled by this routine. It modifies the
975** second while( 1 ) loop in "ROWS BETWEEN CURRENT ... UNBOUNDED..." to
976** be:
977**
978** while( 1 ){
979** AggFinal (xValue)
980** while( 1 ){
981** regPeer++
982** Gosub addrGosub
983** Next(csr) // if EOF goto flush_partition_done
984** if( new peer ) break;
985** }
986** while( (regPeer--)>0 ){
987** AggStep (csrStart, xInverse)
988** Next(csrStart)
989** }
990** }
dan99652dd2018-05-24 17:49:14 +0000991**
dan31f56392018-05-24 21:10:57 +0000992** ROWS BETWEEN <expr> FOLLOWING AND <expr> FOLLOWING
993**
994** regEnd = regEnd - regStart
995** Rewind (csr,csrStart,csrEnd) // if EOF goto flush_partition_done
996** Aggstep (csrEnd)
997** Next(csrEnd) // if EOF fall-through
998** if( (regEnd--)<=0 ){
dan31f56392018-05-24 21:10:57 +0000999** if( (regStart--)<=0 ){
1000** AggFinal (xValue)
1001** Gosub addrGosub
1002** Next(csr) // if EOF goto flush_partition_done
1003** }
dane105dd72018-05-25 09:29:11 +00001004** AggStep (csrStart, xInverse)
1005** Next (csrStart)
dan31f56392018-05-24 21:10:57 +00001006** }
1007**
1008** ROWS BETWEEN <expr> PRECEDING AND <expr> PRECEDING
1009**
1010** Replace the bit after "Rewind" in the above with:
1011**
1012** if( (regEnd--)<=0 ){
1013** AggStep (csrEnd)
1014** Next (csrEnd)
1015** }
1016** AggFinal (xValue)
1017** Gosub addrGosub
1018** Next(csr) // if EOF goto flush_partition_done
1019** if( (regStart--)<=0 ){
1020** AggStep (csr2, xInverse)
1021** Next (csr2)
1022** }
1023**
dan99652dd2018-05-24 17:49:14 +00001024*/
danc3a20c12018-05-23 20:55:37 +00001025static void windowCodeRowExprStep(
1026 Parse *pParse,
1027 Select *p,
1028 WhereInfo *pWInfo,
1029 int regGosub,
1030 int addrGosub
1031){
1032 Window *pMWin = p->pWin;
1033 Vdbe *v = sqlite3GetVdbe(pParse);
1034 Window *pWin;
1035 int k;
danc3a20c12018-05-23 20:55:37 +00001036 int nSub = p->pSrc->a[0].pTab->nCol;
1037 int regFlushPart; /* Register for "Gosub flush_partition" */
dan31f56392018-05-24 21:10:57 +00001038 int lblFlushPart; /* Label for "Gosub flush_partition" */
1039 int lblFlushDone; /* Label for "Gosub flush_partition_done" */
danc3a20c12018-05-23 20:55:37 +00001040
danf690b572018-06-01 21:00:08 +00001041 int regArg;
1042 int nArg;
danc3a20c12018-05-23 20:55:37 +00001043 int addr;
dan31f56392018-05-24 21:10:57 +00001044 int csrStart = pParse->nTab++;
1045 int csrEnd = pParse->nTab++;
1046 int regStart; /* Value of <expr> PRECEDING */
1047 int regEnd; /* Value of <expr> FOLLOWING */
danc3a20c12018-05-23 20:55:37 +00001048 int addrNext;
1049 int addrGoto;
dan31f56392018-05-24 21:10:57 +00001050 int addrTop;
danc3a20c12018-05-23 20:55:37 +00001051 int addrIfPos1;
1052 int addrIfPos2;
1053
dan09590aa2018-05-25 20:30:17 +00001054 int regPeer = 0; /* Number of peers in current group */
1055 int regPeerVal = 0; /* Array of values identifying peer group */
1056 int iPeer = 0; /* Column offset in eph-table of peer vals */
1057 int nPeerVal; /* Number of peer values */
dand6f784e2018-05-28 18:30:45 +00001058 int bRange = 0;
dandfa552f2018-06-02 21:04:28 +00001059 int regSize = 0;
dan09590aa2018-05-25 20:30:17 +00001060
dan99652dd2018-05-24 17:49:14 +00001061 assert( pMWin->eStart==TK_PRECEDING
1062 || pMWin->eStart==TK_CURRENT
dane105dd72018-05-25 09:29:11 +00001063 || pMWin->eStart==TK_FOLLOWING
dan99652dd2018-05-24 17:49:14 +00001064 || pMWin->eStart==TK_UNBOUNDED
1065 );
1066 assert( pMWin->eEnd==TK_FOLLOWING
1067 || pMWin->eEnd==TK_CURRENT
1068 || pMWin->eEnd==TK_UNBOUNDED
dan31f56392018-05-24 21:10:57 +00001069 || pMWin->eEnd==TK_PRECEDING
dan99652dd2018-05-24 17:49:14 +00001070 );
1071
dand6f784e2018-05-28 18:30:45 +00001072 if( pMWin->eType==TK_RANGE
1073 && pMWin->eStart==TK_CURRENT
1074 && pMWin->eEnd==TK_UNBOUNDED
1075 ){
1076 bRange = 1;
1077 }
1078
danc3a20c12018-05-23 20:55:37 +00001079 /* Allocate register and label for the "flush_partition" sub-routine. */
1080 regFlushPart = ++pParse->nMem;
dan31f56392018-05-24 21:10:57 +00001081 lblFlushPart = sqlite3VdbeMakeLabel(v);
1082 lblFlushDone = sqlite3VdbeMakeLabel(v);
danc3a20c12018-05-23 20:55:37 +00001083
dan31f56392018-05-24 21:10:57 +00001084 regStart = ++pParse->nMem;
1085 regEnd = ++pParse->nMem;
danc3a20c12018-05-23 20:55:37 +00001086
dandfa552f2018-06-02 21:04:28 +00001087 windowPartitionCache(pParse, p, pWInfo, regFlushPart, lblFlushPart, &regSize);
danc3a20c12018-05-23 20:55:37 +00001088
danc3a20c12018-05-23 20:55:37 +00001089 addrGoto = sqlite3VdbeAddOp0(v, OP_Goto);
1090
danc9a86682018-05-30 20:44:58 +00001091 /* Start of "flush_partition" */
dan31f56392018-05-24 21:10:57 +00001092 sqlite3VdbeResolveLabel(v, lblFlushPart);
danc3a20c12018-05-23 20:55:37 +00001093 sqlite3VdbeAddOp2(v, OP_Once, 0, sqlite3VdbeCurrentAddr(v)+3);
dan31f56392018-05-24 21:10:57 +00001094 sqlite3VdbeAddOp2(v, OP_OpenDup, csrStart, pMWin->iEphCsr);
1095 sqlite3VdbeAddOp2(v, OP_OpenDup, csrEnd, pMWin->iEphCsr);
danc3a20c12018-05-23 20:55:37 +00001096
dan31f56392018-05-24 21:10:57 +00001097 /* If either regStart or regEnd are not non-negative integers, throw
dan99652dd2018-05-24 17:49:14 +00001098 ** an exception. */
1099 if( pMWin->pStart ){
dan31f56392018-05-24 21:10:57 +00001100 sqlite3ExprCode(pParse, pMWin->pStart, regStart);
1101 windowCheckFrameValue(pParse, regStart, 0);
dan99652dd2018-05-24 17:49:14 +00001102 }
1103 if( pMWin->pEnd ){
dan31f56392018-05-24 21:10:57 +00001104 sqlite3ExprCode(pParse, pMWin->pEnd, regEnd);
1105 windowCheckFrameValue(pParse, regEnd, 1);
dan99652dd2018-05-24 17:49:14 +00001106 }
danc3a20c12018-05-23 20:55:37 +00001107
danc9a86682018-05-30 20:44:58 +00001108 /* If this is "ROWS <expr1> FOLLOWING AND ROWS <expr2> FOLLOWING", do:
1109 **
1110 ** regEnd = regEnd - regStart;
1111 */
1112 if( pMWin->pEnd && pMWin->pStart && pMWin->eStart==TK_FOLLOWING ){
1113 assert( pMWin->eEnd==TK_FOLLOWING );
1114 sqlite3VdbeAddOp3(v, OP_Subtract, regStart, regEnd, regEnd);
1115 }
1116
1117 /* Initialize the accumulator register for each window function to NULL */
dan2e605682018-06-07 15:54:26 +00001118 regArg = windowInitAccum(pParse, pMWin);
danc3a20c12018-05-23 20:55:37 +00001119
dan31f56392018-05-24 21:10:57 +00001120 sqlite3VdbeAddOp2(v, OP_Rewind, pMWin->iEphCsr, lblFlushDone);
1121 sqlite3VdbeAddOp2(v, OP_Rewind, csrStart, lblFlushDone);
danc3a20c12018-05-23 20:55:37 +00001122 sqlite3VdbeChangeP5(v, 1);
dan31f56392018-05-24 21:10:57 +00001123 sqlite3VdbeAddOp2(v, OP_Rewind, csrEnd, lblFlushDone);
danc3a20c12018-05-23 20:55:37 +00001124 sqlite3VdbeChangeP5(v, 1);
1125
1126 /* Invoke AggStep function for each window function using the row that
dan31f56392018-05-24 21:10:57 +00001127 ** csrEnd currently points to. Or, if csrEnd is already at EOF,
danc3a20c12018-05-23 20:55:37 +00001128 ** do nothing. */
dan31f56392018-05-24 21:10:57 +00001129 addrTop = sqlite3VdbeCurrentAddr(v);
1130 if( pMWin->eEnd==TK_PRECEDING ){
1131 addrIfPos1 = sqlite3VdbeAddOp3(v, OP_IfPos, regEnd, 0 , 1);
danc3a20c12018-05-23 20:55:37 +00001132 }
dan31f56392018-05-24 21:10:57 +00001133 sqlite3VdbeAddOp2(v, OP_Next, csrEnd, sqlite3VdbeCurrentAddr(v)+2);
1134 addr = sqlite3VdbeAddOp0(v, OP_Goto);
dandfa552f2018-06-02 21:04:28 +00001135 windowAggStep(pParse, pMWin, csrEnd, 0, regArg, regSize);
dan99652dd2018-05-24 17:49:14 +00001136 if( pMWin->eEnd==TK_UNBOUNDED ){
dan31f56392018-05-24 21:10:57 +00001137 sqlite3VdbeAddOp2(v, OP_Goto, 0, addrTop);
1138 sqlite3VdbeJumpHere(v, addr);
1139 addrTop = sqlite3VdbeCurrentAddr(v);
dan99652dd2018-05-24 17:49:14 +00001140 }else{
dan31f56392018-05-24 21:10:57 +00001141 sqlite3VdbeJumpHere(v, addr);
1142 if( pMWin->eEnd==TK_PRECEDING ){
1143 sqlite3VdbeJumpHere(v, addrIfPos1);
1144 }
dan99652dd2018-05-24 17:49:14 +00001145 }
danc3a20c12018-05-23 20:55:37 +00001146
dan99652dd2018-05-24 17:49:14 +00001147 if( pMWin->eEnd==TK_FOLLOWING ){
dan31f56392018-05-24 21:10:57 +00001148 addrIfPos1 = sqlite3VdbeAddOp3(v, OP_IfPos, regEnd, 0 , 1);
dan99652dd2018-05-24 17:49:14 +00001149 }
dane105dd72018-05-25 09:29:11 +00001150 if( pMWin->eStart==TK_FOLLOWING ){
1151 addrIfPos2 = sqlite3VdbeAddOp3(v, OP_IfPos, regStart, 0 , 1);
1152 }
dand6f784e2018-05-28 18:30:45 +00001153 if( bRange ){
dan09590aa2018-05-25 20:30:17 +00001154 assert( pMWin->eStart==TK_CURRENT && pMWin->pOrderBy );
1155 regPeer = ++pParse->nMem;
1156 regPeerVal = pParse->nMem+1;
1157 iPeer = pMWin->nBufferCol + (pMWin->pPartition?pMWin->pPartition->nExpr:0);
1158 nPeerVal = pMWin->pOrderBy->nExpr;
1159 pParse->nMem += (2 * nPeerVal);
1160 for(k=0; k<nPeerVal; k++){
1161 sqlite3VdbeAddOp3(v, OP_Column, pMWin->iEphCsr, iPeer+k, regPeerVal+k);
1162 }
1163 sqlite3VdbeAddOp2(v, OP_Integer, 0, regPeer);
1164 }
danec891fd2018-06-06 20:51:02 +00001165
dand6f784e2018-05-28 18:30:45 +00001166 windowAggFinal(pParse, pMWin, 0);
1167 if( bRange ){
dan09590aa2018-05-25 20:30:17 +00001168 sqlite3VdbeAddOp2(v, OP_AddImm, regPeer, 1);
1169 }
danec891fd2018-06-06 20:51:02 +00001170 windowReturnOneRow(pParse, pMWin, regGosub, addrGosub);
danc3a20c12018-05-23 20:55:37 +00001171 sqlite3VdbeAddOp2(v, OP_Next, pMWin->iEphCsr, sqlite3VdbeCurrentAddr(v)+2);
dan31f56392018-05-24 21:10:57 +00001172 sqlite3VdbeAddOp2(v, OP_Goto, 0, lblFlushDone);
dand6f784e2018-05-28 18:30:45 +00001173 if( bRange ){
dan09590aa2018-05-25 20:30:17 +00001174 KeyInfo *pKeyInfo = sqlite3KeyInfoFromExprList(pParse, pMWin->pOrderBy,0,0);
1175 int addrJump = sqlite3VdbeCurrentAddr(v)-4;
1176 for(k=0; k<nPeerVal; k++){
1177 int iOut = regPeerVal + nPeerVal + k;
1178 sqlite3VdbeAddOp3(v, OP_Column, pMWin->iEphCsr, iPeer+k, iOut);
1179 }
1180 sqlite3VdbeAddOp3(v, OP_Compare, regPeerVal, regPeerVal+nPeerVal, nPeerVal);
1181 sqlite3VdbeAppendP4(v, (void*)pKeyInfo, P4_KEYINFO);
1182 addr = sqlite3VdbeCurrentAddr(v)+1;
1183 sqlite3VdbeAddOp3(v, OP_Jump, addr, addrJump, addr);
1184 }
dane105dd72018-05-25 09:29:11 +00001185 if( pMWin->eStart==TK_FOLLOWING ){
1186 sqlite3VdbeJumpHere(v, addrIfPos2);
1187 }
danc3a20c12018-05-23 20:55:37 +00001188
dane105dd72018-05-25 09:29:11 +00001189 if( pMWin->eStart==TK_CURRENT
1190 || pMWin->eStart==TK_PRECEDING
1191 || pMWin->eStart==TK_FOLLOWING
1192 ){
dan09590aa2018-05-25 20:30:17 +00001193 int addrJumpHere = 0;
dan99652dd2018-05-24 17:49:14 +00001194 if( pMWin->eStart==TK_PRECEDING ){
dan09590aa2018-05-25 20:30:17 +00001195 addrJumpHere = sqlite3VdbeAddOp3(v, OP_IfPos, regStart, 0 , 1);
1196 }
dand6f784e2018-05-28 18:30:45 +00001197 if( bRange ){
dan09590aa2018-05-25 20:30:17 +00001198 sqlite3VdbeAddOp3(v, OP_IfPos, regPeer, sqlite3VdbeCurrentAddr(v)+2, 1);
1199 addrJumpHere = sqlite3VdbeAddOp0(v, OP_Goto);
danc3a20c12018-05-23 20:55:37 +00001200 }
dan31f56392018-05-24 21:10:57 +00001201 sqlite3VdbeAddOp2(v, OP_Next, csrStart, sqlite3VdbeCurrentAddr(v)+1);
dandfa552f2018-06-02 21:04:28 +00001202 windowAggStep(pParse, pMWin, csrStart, 1, regArg, regSize);
dand6f784e2018-05-28 18:30:45 +00001203 if( bRange ){
dan09590aa2018-05-25 20:30:17 +00001204 sqlite3VdbeAddOp2(v, OP_Goto, 0, addrJumpHere-1);
1205 }
1206 if( addrJumpHere ){
1207 sqlite3VdbeJumpHere(v, addrJumpHere);
dan99652dd2018-05-24 17:49:14 +00001208 }
danc3a20c12018-05-23 20:55:37 +00001209 }
dan99652dd2018-05-24 17:49:14 +00001210 if( pMWin->eEnd==TK_FOLLOWING ){
1211 sqlite3VdbeJumpHere(v, addrIfPos1);
1212 }
dan31f56392018-05-24 21:10:57 +00001213 sqlite3VdbeAddOp2(v, OP_Goto, 0, addrTop);
danc3a20c12018-05-23 20:55:37 +00001214
1215 /* flush_partition_done: */
dan31f56392018-05-24 21:10:57 +00001216 sqlite3VdbeResolveLabel(v, lblFlushDone);
danc3a20c12018-05-23 20:55:37 +00001217 sqlite3VdbeAddOp1(v, OP_ResetSorter, pMWin->iEphCsr);
1218 sqlite3VdbeAddOp1(v, OP_Return, regFlushPart);
1219
1220 /* Jump to here to skip over flush_partition */
1221 sqlite3VdbeJumpHere(v, addrGoto);
1222}
1223
dan79d45442018-05-26 21:17:29 +00001224/*
1225** RANGE BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW
1226**
danf690b572018-06-01 21:00:08 +00001227** flush_partition:
1228** Once {
1229** OpenDup (iEphCsr -> csrLead)
1230** }
1231** Integer ctr 0
1232** foreach row (csrLead){
1233** if( new peer ){
1234** AggFinal (xValue)
1235** for(i=0; i<ctr; i++){
1236** Gosub addrGosub
1237** Next iEphCsr
1238** }
1239** Integer ctr 0
1240** }
1241** AggStep (csrLead)
1242** Incr ctr
1243** }
1244**
1245** AggFinal (xFinalize)
1246** for(i=0; i<ctr; i++){
1247** Gosub addrGosub
1248** Next iEphCsr
1249** }
1250**
1251** ResetSorter (csr)
1252** Return
1253**
1254** ROWS BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW
1255** RANGE BETWEEN UNBOUNDED PRECEDING AND UNBOUNDED FOLLOWING
1256** RANGE BETWEEN CURRENT ROW AND CURRENT ROW
1257**
1258** TODO.
1259*/
1260static void windowCodeCacheStep(
1261 Parse *pParse,
1262 Select *p,
1263 WhereInfo *pWInfo,
1264 int regGosub,
1265 int addrGosub
1266){
1267 Window *pMWin = p->pWin;
1268 Vdbe *v = sqlite3GetVdbe(pParse);
1269 Window *pWin;
1270 int k;
1271 int addr;
1272 ExprList *pPart = pMWin->pPartition;
1273 ExprList *pOrderBy = pMWin->pOrderBy;
1274 int nPeer = pOrderBy->nExpr;
1275 int regNewPeer;
1276
1277 int addrGoto; /* Address of Goto used to jump flush_par.. */
1278 int addrRewind; /* Address of Rewind that starts loop */
1279 int regFlushPart;
1280 int lblFlushPart;
1281 int csrLead;
1282 int regCtr;
1283 int regArg; /* Register array to martial function args */
dandfa552f2018-06-02 21:04:28 +00001284 int regSize;
danf690b572018-06-01 21:00:08 +00001285 int nArg;
1286
1287 assert( (pMWin->eStart==TK_UNBOUNDED && pMWin->eEnd==TK_CURRENT)
danec891fd2018-06-06 20:51:02 +00001288 || (pMWin->eStart==TK_UNBOUNDED && pMWin->eEnd==TK_UNBOUNDED)
1289 || (pMWin->eStart==TK_CURRENT && pMWin->eEnd==TK_CURRENT)
danf690b572018-06-01 21:00:08 +00001290 );
1291
1292 regNewPeer = pParse->nMem+1;
1293 pParse->nMem += nPeer;
1294
1295 /* Allocate register and label for the "flush_partition" sub-routine. */
1296 regFlushPart = ++pParse->nMem;
1297 lblFlushPart = sqlite3VdbeMakeLabel(v);
1298
1299 csrLead = pParse->nTab++;
1300 regCtr = ++pParse->nMem;
1301
dandfa552f2018-06-02 21:04:28 +00001302 windowPartitionCache(pParse, p, pWInfo, regFlushPart, lblFlushPart, &regSize);
danf690b572018-06-01 21:00:08 +00001303 addrGoto = sqlite3VdbeAddOp0(v, OP_Goto);
1304
1305 /* Start of "flush_partition" */
1306 sqlite3VdbeResolveLabel(v, lblFlushPart);
1307 sqlite3VdbeAddOp2(v, OP_Once, 0, sqlite3VdbeCurrentAddr(v)+2);
1308 sqlite3VdbeAddOp2(v, OP_OpenDup, csrLead, pMWin->iEphCsr);
1309
1310 /* Initialize the accumulator register for each window function to NULL */
dan2e605682018-06-07 15:54:26 +00001311 regArg = windowInitAccum(pParse, pMWin);
danf690b572018-06-01 21:00:08 +00001312
1313 sqlite3VdbeAddOp2(v, OP_Integer, 0, regCtr);
1314 addrRewind = sqlite3VdbeAddOp1(v, OP_Rewind, csrLead);
1315 sqlite3VdbeAddOp1(v, OP_Rewind, pMWin->iEphCsr);
1316
danec891fd2018-06-06 20:51:02 +00001317 if( pOrderBy && pMWin->eEnd==TK_CURRENT ){
1318 int bCurrent = (pMWin->eEnd==TK_CURRENT && pMWin->eStart==TK_CURRENT);
1319 int addrJump = 0; /* Address of OP_Jump below */
1320 if( pMWin->eType==TK_RANGE ){
1321 int iOff = pMWin->nBufferCol + (pPart ? pPart->nExpr : 0);
1322 int regPeer = pMWin->regPart + (pPart ? pPart->nExpr : 0);
1323 KeyInfo *pKeyInfo = sqlite3KeyInfoFromExprList(pParse, pOrderBy, 0, 0);
1324 for(k=0; k<nPeer; k++){
1325 sqlite3VdbeAddOp3(v, OP_Column, csrLead, iOff+k, regNewPeer+k);
1326 }
1327 addr = sqlite3VdbeAddOp3(v, OP_Compare, regNewPeer, regPeer, nPeer);
1328 sqlite3VdbeAppendP4(v, (void*)pKeyInfo, P4_KEYINFO);
1329 addrJump = sqlite3VdbeAddOp3(v, OP_Jump, addr+2, 0, addr+2);
1330 sqlite3VdbeAddOp3(v, OP_Copy, regNewPeer, regPeer, nPeer-1);
danf690b572018-06-01 21:00:08 +00001331 }
1332
danec891fd2018-06-06 20:51:02 +00001333 windowReturnRows(pParse, pMWin, regCtr, 0, regGosub, addrGosub,
1334 (bCurrent ? regArg : 0), (bCurrent ? regSize : 0)
1335 );
1336 if( addrJump ) sqlite3VdbeJumpHere(v, addrJump);
danf690b572018-06-01 21:00:08 +00001337 }
1338
dandfa552f2018-06-02 21:04:28 +00001339 windowAggStep(pParse, pMWin, csrLead, 0, regArg, regSize);
danf690b572018-06-01 21:00:08 +00001340 sqlite3VdbeAddOp2(v, OP_AddImm, regCtr, 1);
danf690b572018-06-01 21:00:08 +00001341 sqlite3VdbeAddOp2(v, OP_Next, csrLead, addrRewind+2);
1342
danec891fd2018-06-06 20:51:02 +00001343 windowReturnRows(pParse, pMWin, regCtr, 1, regGosub, addrGosub, 0, 0);
danf690b572018-06-01 21:00:08 +00001344
1345 sqlite3VdbeJumpHere(v, addrRewind);
1346 sqlite3VdbeJumpHere(v, addrRewind+1);
1347 sqlite3VdbeAddOp1(v, OP_ResetSorter, pMWin->iEphCsr);
1348 sqlite3VdbeAddOp1(v, OP_Return, regFlushPart);
1349
1350 /* Jump to here to skip over flush_partition */
1351 sqlite3VdbeJumpHere(v, addrGoto);
1352}
1353
1354
1355/*
1356** RANGE BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW
1357**
dan79d45442018-05-26 21:17:29 +00001358** ...
1359** if( new partition ){
1360** AggFinal (xFinalize)
1361** Gosub addrGosub
1362** ResetSorter eph-table
1363** }
1364** else if( new peer ){
1365** AggFinal (xValue)
1366** Gosub addrGosub
1367** ResetSorter eph-table
1368** }
1369** AggStep
1370** Insert (record into eph-table)
1371** sqlite3WhereEnd()
1372** AggFinal (xFinalize)
1373** Gosub addrGosub
danf690b572018-06-01 21:00:08 +00001374**
1375** RANGE BETWEEN UNBOUNDED PRECEDING AND UNBOUNDED FOLLOWING
1376**
1377** As above, except take no action for a "new peer". Invoke
1378** the sub-routine once only for each partition.
1379**
1380** RANGE BETWEEN CURRENT ROW AND CURRENT ROW
1381**
1382** As above, except that the "new peer" condition is handled in the
1383** same way as "new partition" (so there is no "else if" block).
1384**
1385** ROWS BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW
1386**
1387** As above, except assume every row is a "new peer".
dan79d45442018-05-26 21:17:29 +00001388*/
danc3a20c12018-05-23 20:55:37 +00001389static void windowCodeDefaultStep(
1390 Parse *pParse,
1391 Select *p,
1392 WhereInfo *pWInfo,
1393 int regGosub,
1394 int addrGosub
1395){
1396 Window *pMWin = p->pWin;
1397 Vdbe *v = sqlite3GetVdbe(pParse);
1398 Window *pWin;
1399 int k;
1400 int iSubCsr = p->pSrc->a[0].iCursor;
1401 int nSub = p->pSrc->a[0].pTab->nCol;
1402 int reg = pParse->nMem+1;
1403 int regRecord = reg+nSub;
1404 int regRowid = regRecord+1;
1405 int addr;
dand6f784e2018-05-28 18:30:45 +00001406 ExprList *pPart = pMWin->pPartition;
1407 ExprList *pOrderBy = pMWin->pOrderBy;
danc3a20c12018-05-23 20:55:37 +00001408
dan79d45442018-05-26 21:17:29 +00001409 assert( pMWin->eType==TK_RANGE
1410 || (pMWin->eStart==TK_UNBOUNDED && pMWin->eEnd==TK_CURRENT)
1411 );
1412
dand6f784e2018-05-28 18:30:45 +00001413 assert( (pMWin->eStart==TK_UNBOUNDED && pMWin->eEnd==TK_CURRENT)
1414 || (pMWin->eStart==TK_UNBOUNDED && pMWin->eEnd==TK_UNBOUNDED)
1415 || (pMWin->eStart==TK_CURRENT && pMWin->eEnd==TK_CURRENT)
1416 || (pMWin->eStart==TK_CURRENT && pMWin->eEnd==TK_UNBOUNDED && !pOrderBy)
1417 );
1418
1419 if( pMWin->eEnd==TK_UNBOUNDED ){
1420 pOrderBy = 0;
1421 }
1422
danc3a20c12018-05-23 20:55:37 +00001423 pParse->nMem += nSub + 2;
1424
1425 /* Martial the row returned by the sub-select into an array of
1426 ** registers. */
1427 for(k=0; k<nSub; k++){
1428 sqlite3VdbeAddOp3(v, OP_Column, iSubCsr, k, reg+k);
1429 }
1430
1431 /* Check if this is the start of a new partition or peer group. */
dand6f784e2018-05-28 18:30:45 +00001432 if( pPart || pOrderBy ){
danc3a20c12018-05-23 20:55:37 +00001433 int nPart = (pPart ? pPart->nExpr : 0);
danc3a20c12018-05-23 20:55:37 +00001434 int addrGoto = 0;
1435 int addrJump = 0;
dand6f784e2018-05-28 18:30:45 +00001436 int nPeer = (pOrderBy ? pOrderBy->nExpr : 0);
danc3a20c12018-05-23 20:55:37 +00001437
1438 if( pPart ){
1439 int regNewPart = reg + pMWin->nBufferCol;
1440 KeyInfo *pKeyInfo = sqlite3KeyInfoFromExprList(pParse, pPart, 0, 0);
1441 addr = sqlite3VdbeAddOp3(v, OP_Compare, regNewPart, pMWin->regPart,nPart);
1442 sqlite3VdbeAppendP4(v, (void*)pKeyInfo, P4_KEYINFO);
1443 addrJump = sqlite3VdbeAddOp3(v, OP_Jump, addr+2, 0, addr+2);
dand6f784e2018-05-28 18:30:45 +00001444 windowAggFinal(pParse, pMWin, 1);
danc3a20c12018-05-23 20:55:37 +00001445 if( pOrderBy ){
1446 addrGoto = sqlite3VdbeAddOp0(v, OP_Goto);
1447 }
1448 }
1449
1450 if( pOrderBy ){
1451 int regNewPeer = reg + pMWin->nBufferCol + nPart;
1452 int regPeer = pMWin->regPart + nPart;
1453
danc3a20c12018-05-23 20:55:37 +00001454 if( addrJump ) sqlite3VdbeJumpHere(v, addrJump);
dan79d45442018-05-26 21:17:29 +00001455 if( pMWin->eType==TK_RANGE ){
1456 KeyInfo *pKeyInfo = sqlite3KeyInfoFromExprList(pParse, pOrderBy, 0, 0);
1457 addr = sqlite3VdbeAddOp3(v, OP_Compare, regNewPeer, regPeer, nPeer);
1458 sqlite3VdbeAppendP4(v, (void*)pKeyInfo, P4_KEYINFO);
1459 addrJump = sqlite3VdbeAddOp3(v, OP_Jump, addr+2, 0, addr+2);
1460 }else{
1461 addrJump = 0;
1462 }
dand6f784e2018-05-28 18:30:45 +00001463 windowAggFinal(pParse, pMWin, pMWin->eStart==TK_CURRENT);
danc3a20c12018-05-23 20:55:37 +00001464 if( addrGoto ) sqlite3VdbeJumpHere(v, addrGoto);
1465 }
1466
1467 sqlite3VdbeAddOp2(v, OP_Gosub, regGosub, addrGosub);
1468 sqlite3VdbeAddOp1(v, OP_ResetSorter, pMWin->iEphCsr);
1469 sqlite3VdbeAddOp3(
1470 v, OP_Copy, reg+pMWin->nBufferCol, pMWin->regPart, nPart+nPeer-1
1471 );
1472
dan79d45442018-05-26 21:17:29 +00001473 if( addrJump ) sqlite3VdbeJumpHere(v, addrJump);
danc3a20c12018-05-23 20:55:37 +00001474 }
1475
1476 /* Invoke step function for window functions */
dandfa552f2018-06-02 21:04:28 +00001477 windowAggStep(pParse, pMWin, -1, 0, reg, 0);
danc3a20c12018-05-23 20:55:37 +00001478
1479 /* Buffer the current row in the ephemeral table. */
1480 if( pMWin->nBufferCol>0 ){
1481 sqlite3VdbeAddOp3(v, OP_MakeRecord, reg, pMWin->nBufferCol, regRecord);
1482 }else{
1483 sqlite3VdbeAddOp2(v, OP_Blob, 0, regRecord);
1484 sqlite3VdbeAppendP4(v, (void*)"", 0);
1485 }
1486 sqlite3VdbeAddOp2(v, OP_NewRowid, pMWin->iEphCsr, regRowid);
1487 sqlite3VdbeAddOp3(v, OP_Insert, pMWin->iEphCsr, regRecord, regRowid);
1488
1489 /* End the database scan loop. */
1490 sqlite3WhereEnd(pWInfo);
1491
dand6f784e2018-05-28 18:30:45 +00001492 windowAggFinal(pParse, pMWin, 1);
danc3a20c12018-05-23 20:55:37 +00001493 sqlite3VdbeAddOp2(v, OP_Gosub, regGosub, addrGosub);
1494}
1495
1496
danf9eae182018-05-21 19:45:11 +00001497/*
danf9eae182018-05-21 19:45:11 +00001498** RANGE BETWEEN UNBOUNDED PRECEDING AND UNBOUNDED FOLLOWING
1499**
1500** As above, except take no action for a "new peer". Invoke
1501** the sub-routine once only for each partition.
1502**
1503** RANGE BETWEEN CURRENT ROW AND CURRENT ROW
1504**
1505** As above, except that the "new peer" condition is handled in the
1506** same way as "new partition" (so there is no "else if" block).
1507**
1508** RANGE BETWEEN CURRENT ROW AND UNBOUNDED FOLLOWING
1509**
1510** One way is to just reverse the sort order and do as for BETWEEN
1511** UNBOUNDED PRECEDING AND CURRENT ROW. But that is not quite the same for
1512** things like group_concat(). And perhaps other user defined aggregates
1513** as well.
1514**
1515** ...
1516** if( new partition ){
1517** Gosub flush_partition;
1518** ResetSorter eph-table
1519** }
1520** AggStep
1521** Insert (record into eph-table)
1522** sqlite3WhereEnd()
1523** Gosub flush_partition
1524**
1525** flush_partition:
1526** OpenDup (csr -> csr2)
1527** foreach (record in eph-table) {
1528** if( new peer ){
1529** while( csr2!=csr ){
1530** AggStep (xInverse)
1531** Next (csr2)
1532** }
1533** }
1534** AggFinal (xValue)
1535** Gosub addrGosub
1536** }
1537**
1538**========================================================================
1539**
1540** ROWS BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW
1541** ...
1542** if( new partition ){
1543** AggFinal (xFinalize)
1544** }
1545** AggStep
1546** AggFinal (xValue)
1547** Gosub addrGosub
1548** sqlite3WhereEnd()
1549**
danf9eae182018-05-21 19:45:11 +00001550*/
1551void sqlite3WindowCodeStep(
1552 Parse *pParse,
1553 Select *p,
1554 WhereInfo *pWInfo,
1555 int regGosub,
danc3a20c12018-05-23 20:55:37 +00001556 int addrGosub,
1557 int *pbLoop
danf9eae182018-05-21 19:45:11 +00001558){
danf9eae182018-05-21 19:45:11 +00001559 Window *pMWin = p->pWin;
dandfa552f2018-06-02 21:04:28 +00001560 Window *pWin;
danf9eae182018-05-21 19:45:11 +00001561
danf690b572018-06-01 21:00:08 +00001562 *pbLoop = 0;
dan09590aa2018-05-25 20:30:17 +00001563 if( (pMWin->eType==TK_ROWS
dand6f784e2018-05-28 18:30:45 +00001564 && (pMWin->eStart!=TK_UNBOUNDED||pMWin->eEnd!=TK_CURRENT||!pMWin->pOrderBy))
1565 || (pMWin->eStart==TK_CURRENT&&pMWin->eEnd==TK_UNBOUNDED&&pMWin->pOrderBy)
dan09590aa2018-05-25 20:30:17 +00001566 ){
danc3a20c12018-05-23 20:55:37 +00001567 windowCodeRowExprStep(pParse, p, pWInfo, regGosub, addrGosub);
1568 return;
danf9eae182018-05-21 19:45:11 +00001569 }
1570
dandfa552f2018-06-02 21:04:28 +00001571 for(pWin=pMWin; pWin; pWin=pWin->pNextWin){
danec891fd2018-06-06 20:51:02 +00001572 FuncDef *pFunc = pWin->pFunc;
1573 if( (pFunc->funcFlags & SQLITE_FUNC_WINDOW_SIZE)
1574 || (pFunc->xSFunc==nth_valueStepFunc)
1575 ){
dandfa552f2018-06-02 21:04:28 +00001576 windowCodeCacheStep(pParse, p, pWInfo, regGosub, addrGosub);
1577 return;
1578 }
danf690b572018-06-01 21:00:08 +00001579 }
danf690b572018-06-01 21:00:08 +00001580
danc3a20c12018-05-23 20:55:37 +00001581 *pbLoop = 1;
1582 windowCodeDefaultStep(pParse, p, pWInfo, regGosub, addrGosub);
danf9eae182018-05-21 19:45:11 +00001583}
1584