blob: 00f3f44c8b57b824594e671cf91ce769b03cf41d [file] [log] [blame]
dan86fb6e12018-05-16 20:58:07 +00001/*
dan26522d12018-06-11 18:16:51 +00002** 2018 May 08
dan86fb6e12018-05-16 20:58:07 +00003**
4** The author disclaims copyright to this source code. In place of
5** a legal notice, here is a blessing:
6**
7** May you do good and not evil.
8** May you find forgiveness for yourself and forgive others.
9** May you share freely, never taking more than you give.
10**
11*************************************************************************
12*/
13#include "sqliteInt.h"
14
dandfa552f2018-06-02 21:04:28 +000015/*
dan73925692018-06-12 18:40:17 +000016** SELECT REWRITING
17**
18** Any SELECT statement that contains one or more window functions in
19** either the select list or ORDER BY clause (the only two places window
20** functions may be used) is transformed by function sqlite3WindowRewrite()
21** in order to support window function processing. For example, with the
22** schema:
23**
24** CREATE TABLE t1(a, b, c, d, e, f, g);
25**
26** the statement:
27**
28** SELECT a+1, max(b) OVER (PARTITION BY c ORDER BY d) FROM t1 ORDER BY e;
29**
30** is transformed to:
31**
32** SELECT a+1, max(b) OVER (PARTITION BY c ORDER BY d) FROM (
33** SELECT a, e, c, d, b FROM t1 ORDER BY c, d
34** ) ORDER BY e;
35**
36** The flattening optimization is disabled when processing this transformed
37** SELECT statement. This allows the implementation of the window function
38** (in this case max()) to process rows sorted in order of (c, d), which
39** makes things easier for obvious reasons. More generally:
40**
41** * FROM, WHERE, GROUP BY and HAVING clauses are all moved to
42** the sub-query.
43**
44** * ORDER BY, LIMIT and OFFSET remain part of the parent query.
45**
46** * Terminals from each of the expression trees that make up the
47** select-list and ORDER BY expressions in the parent query are
48** selected by the sub-query. For the purposes of the transformation,
49** terminals are column references and aggregate functions.
50**
51** If there is more than one window function in the SELECT that uses
52** the same window declaration (the OVER bit), then a single scan may
53** be used to process more than one window function. For example:
54**
55** SELECT max(b) OVER (PARTITION BY c ORDER BY d),
56** min(e) OVER (PARTITION BY c ORDER BY d)
57** FROM t1;
58**
59** is transformed in the same way as the example above. However:
60**
61** SELECT max(b) OVER (PARTITION BY c ORDER BY d),
62** min(e) OVER (PARTITION BY a ORDER BY b)
63** FROM t1;
64**
65** Must be transformed to:
66**
67** SELECT max(b) OVER (PARTITION BY c ORDER BY d) FROM (
68** SELECT e, min(e) OVER (PARTITION BY a ORDER BY b), c, d, b FROM
69** SELECT a, e, c, d, b FROM t1 ORDER BY a, b
70** ) ORDER BY c, d
71** ) ORDER BY e;
72**
73** so that both min() and max() may process rows in the order defined by
74** their respective window declarations.
75**
76** INTERFACE WITH SELECT.C
77**
78** When processing the rewritten SELECT statement, code in select.c calls
79** sqlite3WhereBegin() to begin iterating through the results of the
80** sub-query, which is always implemented as a co-routine. It then calls
81** sqlite3WindowCodeStep() to process rows and finish the scan by calling
82** sqlite3WhereEnd().
83**
84** sqlite3WindowCodeStep() generates VM code so that, for each row returned
85** by the sub-query a sub-routine (OP_Gosub) coded by select.c is invoked.
86** When the sub-routine is invoked:
87**
88** * The results of all window-functions for the row are stored
89** in the associated Window.regResult registers.
90**
91** * The required terminal values are stored in the current row of
92** temp table Window.iEphCsr.
93**
94** In some cases, depending on the window frame and the specific window
95** functions invoked, sqlite3WindowCodeStep() caches each entire partition
96** in a temp table before returning any rows. In other cases it does not.
97** This detail is encapsulated within this file, the code generated by
98** select.c is the same in either case.
99**
100** BUILT-IN WINDOW FUNCTIONS
101**
102** This implementation features the following built-in window functions:
103**
104** row_number()
105** rank()
106** dense_rank()
107** percent_rank()
108** cume_dist()
109** ntile(N)
110** lead(expr [, offset [, default]])
111** lag(expr [, offset [, default]])
112** first_value(expr)
113** last_value(expr)
114** nth_value(expr, N)
115**
116** These are the same built-in window functions supported by Postgres.
117** Although the behaviour of aggregate window functions (functions that
118** can be used as either aggregates or window funtions) allows them to
119** be implemented using an API, built-in window functions are much more
120** esoteric. Additionally, some window functions (e.g. nth_value())
121** may only be implemented by caching the entire partition in memory.
122** As such, some built-in window functions use the same API as aggregate
123** window functions and some are implemented directly using VDBE
124** instructions. Additionally, for those functions that use the API, the
125** window frame is sometimes modified before the SELECT statement is
126** rewritten. For example, regardless of the specified window frame, the
127** row_number() function always uses:
128**
129** ROWS BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW
130**
131** See sqlite3WindowUpdate() for details.
danc0bb4452018-06-12 20:53:38 +0000132**
133** As well as some of the built-in window functions, aggregate window
134** functions min() and max() are implemented using VDBE instructions if
135** the start of the window frame is declared as anything other than
136** UNBOUNDED PRECEDING.
dan73925692018-06-12 18:40:17 +0000137*/
138
139/*
dandfa552f2018-06-02 21:04:28 +0000140** Implementation of built-in window function row_number(). Assumes that the
141** window frame has been coerced to:
142**
143** ROWS BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW
144*/
145static void row_numberStepFunc(
146 sqlite3_context *pCtx,
147 int nArg,
148 sqlite3_value **apArg
149){
150 i64 *p = (i64*)sqlite3_aggregate_context(pCtx, sizeof(*p));
151 if( p ) (*p)++;
152}
dan2a11bb22018-06-11 20:50:25 +0000153static void row_numberInvFunc(
dandfa552f2018-06-02 21:04:28 +0000154 sqlite3_context *pCtx,
155 int nArg,
156 sqlite3_value **apArg
157){
158}
159static void row_numberValueFunc(sqlite3_context *pCtx){
160 i64 *p = (i64*)sqlite3_aggregate_context(pCtx, sizeof(*p));
161 sqlite3_result_int64(pCtx, (p ? *p : 0));
162}
163
164/*
dan2a11bb22018-06-11 20:50:25 +0000165** Context object type used by rank(), dense_rank(), percent_rank() and
166** cume_dist().
dandfa552f2018-06-02 21:04:28 +0000167*/
168struct CallCount {
169 i64 nValue;
170 i64 nStep;
171 i64 nTotal;
172};
173
174/*
175** Implementation of built-in window function dense_rank().
176*/
177static void dense_rankStepFunc(
178 sqlite3_context *pCtx,
179 int nArg,
180 sqlite3_value **apArg
181){
182 struct CallCount *p;
183 p = (struct CallCount*)sqlite3_aggregate_context(pCtx, sizeof(*p));
184 if( p ) p->nStep = 1;
185}
dan2a11bb22018-06-11 20:50:25 +0000186static void dense_rankInvFunc(
dandfa552f2018-06-02 21:04:28 +0000187 sqlite3_context *pCtx,
188 int nArg,
189 sqlite3_value **apArg
190){
191}
192static void dense_rankValueFunc(sqlite3_context *pCtx){
193 struct CallCount *p;
194 p = (struct CallCount*)sqlite3_aggregate_context(pCtx, sizeof(*p));
195 if( p ){
196 if( p->nStep ){
197 p->nValue++;
198 p->nStep = 0;
199 }
200 sqlite3_result_int64(pCtx, p->nValue);
201 }
202}
203
204/*
205** Implementation of built-in window function rank().
206*/
207static void rankStepFunc(
208 sqlite3_context *pCtx,
209 int nArg,
210 sqlite3_value **apArg
211){
212 struct CallCount *p;
213 p = (struct CallCount*)sqlite3_aggregate_context(pCtx, sizeof(*p));
214 if( p ){
215 p->nStep++;
216 if( p->nValue==0 ){
217 p->nValue = p->nStep;
218 }
219 }
220}
dan2a11bb22018-06-11 20:50:25 +0000221static void rankInvFunc(
dandfa552f2018-06-02 21:04:28 +0000222 sqlite3_context *pCtx,
223 int nArg,
224 sqlite3_value **apArg
225){
226}
227static void rankValueFunc(sqlite3_context *pCtx){
228 struct CallCount *p;
229 p = (struct CallCount*)sqlite3_aggregate_context(pCtx, sizeof(*p));
230 if( p ){
231 sqlite3_result_int64(pCtx, p->nValue);
232 p->nValue = 0;
233 }
234}
235
236/*
237** Implementation of built-in window function percent_rank().
238*/
239static void percent_rankStepFunc(
240 sqlite3_context *pCtx,
241 int nArg,
242 sqlite3_value **apArg
243){
244 struct CallCount *p;
245 assert( nArg==1 );
246
247 p = (struct CallCount*)sqlite3_aggregate_context(pCtx, sizeof(*p));
248 if( p ){
249 if( p->nTotal==0 ){
250 p->nTotal = sqlite3_value_int64(apArg[0]);
251 }
252 p->nStep++;
253 if( p->nValue==0 ){
254 p->nValue = p->nStep;
255 }
256 }
257}
dan2a11bb22018-06-11 20:50:25 +0000258static void percent_rankInvFunc(
dandfa552f2018-06-02 21:04:28 +0000259 sqlite3_context *pCtx,
260 int nArg,
261 sqlite3_value **apArg
262){
263}
264static void percent_rankValueFunc(sqlite3_context *pCtx){
265 struct CallCount *p;
266 p = (struct CallCount*)sqlite3_aggregate_context(pCtx, sizeof(*p));
267 if( p ){
268 if( p->nTotal>1 ){
269 double r = (double)(p->nValue-1) / (double)(p->nTotal-1);
270 sqlite3_result_double(pCtx, r);
271 }else{
272 sqlite3_result_double(pCtx, 100.0);
273 }
274 p->nValue = 0;
275 }
276}
277
danf1abe362018-06-04 08:22:09 +0000278static void cume_distStepFunc(
279 sqlite3_context *pCtx,
280 int nArg,
281 sqlite3_value **apArg
282){
283 struct CallCount *p;
284 assert( nArg==1 );
285
286 p = (struct CallCount*)sqlite3_aggregate_context(pCtx, sizeof(*p));
287 if( p ){
288 if( p->nTotal==0 ){
289 p->nTotal = sqlite3_value_int64(apArg[0]);
290 }
291 p->nStep++;
292 }
293}
dan2a11bb22018-06-11 20:50:25 +0000294static void cume_distInvFunc(
danf1abe362018-06-04 08:22:09 +0000295 sqlite3_context *pCtx,
296 int nArg,
297 sqlite3_value **apArg
298){
299}
300static void cume_distValueFunc(sqlite3_context *pCtx){
301 struct CallCount *p;
302 p = (struct CallCount*)sqlite3_aggregate_context(pCtx, sizeof(*p));
303 if( p ){
304 double r = (double)(p->nStep) / (double)(p->nTotal);
305 sqlite3_result_double(pCtx, r);
306 }
307}
308
dan2a11bb22018-06-11 20:50:25 +0000309/*
310** Context object for ntile() window function.
311*/
dan6bc5c9e2018-06-04 18:55:11 +0000312struct NtileCtx {
313 i64 nTotal; /* Total rows in partition */
314 i64 nParam; /* Parameter passed to ntile(N) */
315 i64 iRow; /* Current row */
316};
317
318/*
319** Implementation of ntile(). This assumes that the window frame has
320** been coerced to:
321**
322** ROWS UNBOUNDED PRECEDING AND CURRENT ROW
dan6bc5c9e2018-06-04 18:55:11 +0000323*/
324static void ntileStepFunc(
325 sqlite3_context *pCtx,
326 int nArg,
327 sqlite3_value **apArg
328){
329 struct NtileCtx *p;
330 assert( nArg==2 );
331 p = (struct NtileCtx*)sqlite3_aggregate_context(pCtx, sizeof(*p));
332 if( p ){
333 if( p->nTotal==0 ){
334 p->nParam = sqlite3_value_int64(apArg[0]);
335 p->nTotal = sqlite3_value_int64(apArg[1]);
336 if( p->nParam<=0 ){
337 sqlite3_result_error(
338 pCtx, "argument of ntile must be a positive integer", -1
339 );
340 }
341 }
342 p->iRow++;
343 }
344}
dan2a11bb22018-06-11 20:50:25 +0000345static void ntileInvFunc(
dan6bc5c9e2018-06-04 18:55:11 +0000346 sqlite3_context *pCtx,
347 int nArg,
348 sqlite3_value **apArg
349){
350}
351static void ntileValueFunc(sqlite3_context *pCtx){
352 struct NtileCtx *p;
353 p = (struct NtileCtx*)sqlite3_aggregate_context(pCtx, sizeof(*p));
354 if( p && p->nParam>0 ){
355 int nSize = (p->nTotal / p->nParam);
356 if( nSize==0 ){
357 sqlite3_result_int64(pCtx, p->iRow);
358 }else{
359 i64 nLarge = p->nTotal - p->nParam*nSize;
360 i64 iSmall = nLarge*(nSize+1);
361 i64 iRow = p->iRow-1;
362
363 assert( (nLarge*(nSize+1) + (p->nParam-nLarge)*nSize)==p->nTotal );
364
365 if( iRow<iSmall ){
366 sqlite3_result_int64(pCtx, 1 + iRow/(nSize+1));
367 }else{
368 sqlite3_result_int64(pCtx, 1 + nLarge + (iRow-iSmall)/nSize);
369 }
370 }
371 }
372}
373
dan2a11bb22018-06-11 20:50:25 +0000374/*
375** Context object for last_value() window function.
376*/
dan1c5ed622018-06-05 16:16:17 +0000377struct LastValueCtx {
378 sqlite3_value *pVal;
379 int nVal;
380};
381
382/*
383** Implementation of last_value().
384*/
385static void last_valueStepFunc(
386 sqlite3_context *pCtx,
387 int nArg,
388 sqlite3_value **apArg
389){
390 struct LastValueCtx *p;
391 p = (struct LastValueCtx *)sqlite3_aggregate_context(pCtx, sizeof(*p));
392 if( p ){
393 sqlite3_value_free(p->pVal);
394 p->pVal = sqlite3_value_dup(apArg[0]);
dan6fde1792018-06-15 19:01:35 +0000395 if( p->pVal==0 ){
396 sqlite3_result_error_nomem(pCtx);
397 }else{
398 p->nVal++;
399 }
dan1c5ed622018-06-05 16:16:17 +0000400 }
401}
dan2a11bb22018-06-11 20:50:25 +0000402static void last_valueInvFunc(
dan1c5ed622018-06-05 16:16:17 +0000403 sqlite3_context *pCtx,
404 int nArg,
405 sqlite3_value **apArg
406){
407 struct LastValueCtx *p;
408 p = (struct LastValueCtx *)sqlite3_aggregate_context(pCtx, sizeof(*p));
409 if( p ){
410 p->nVal--;
411 if( p->nVal==0 ){
412 sqlite3_value_free(p->pVal);
413 p->pVal = 0;
414 }
415 }
416}
417static void last_valueValueFunc(sqlite3_context *pCtx){
418 struct LastValueCtx *p;
419 p = (struct LastValueCtx *)sqlite3_aggregate_context(pCtx, sizeof(*p));
420 if( p && p->pVal ){
421 sqlite3_result_value(pCtx, p->pVal);
422 }
423}
424static void last_valueFinalizeFunc(sqlite3_context *pCtx){
425 struct LastValueCtx *p;
426 p = (struct LastValueCtx *)sqlite3_aggregate_context(pCtx, sizeof(*p));
427 if( p && p->pVal ){
428 sqlite3_result_value(pCtx, p->pVal);
429 sqlite3_value_free(p->pVal);
430 p->pVal = 0;
431 }
432}
433
dan2a11bb22018-06-11 20:50:25 +0000434/*
435** No-op implementations of nth_value(), first_value(), lead() and lag().
436** These are all implemented inline using VDBE instructions.
437*/
438static void nth_valueStepFunc(sqlite3_context *pCtx, int n, sqlite3_value **a){}
439static void nth_valueInvFunc(sqlite3_context *pCtx, int n, sqlite3_value **ap){}
440static void nth_valueValueFunc(sqlite3_context *pCtx){}
441static void first_valueStepFunc(sqlite3_context *p, int n, sqlite3_value **ap){}
442static void first_valueInvFunc(sqlite3_context *p, int n, sqlite3_value **ap){}
443static void first_valueValueFunc(sqlite3_context *pCtx){}
444static void leadStepFunc(sqlite3_context *pCtx, int n, sqlite3_value **ap){}
445static void leadInvFunc(sqlite3_context *pCtx, int n, sqlite3_value **ap){}
446static void leadValueFunc(sqlite3_context *pCtx){}
447static void lagStepFunc(sqlite3_context *pCtx, int n, sqlite3_value **ap){}
448static void lagInvFunc(sqlite3_context *pCtx, int n, sqlite3_value **ap){}
449static void lagValueFunc(sqlite3_context *pCtx){}
danfe4e25a2018-06-07 20:08:59 +0000450
dandfa552f2018-06-02 21:04:28 +0000451#define WINDOWFUNC(name,nArg,extra) { \
452 nArg, (SQLITE_UTF8|SQLITE_FUNC_WINDOW|extra), 0, 0, \
453 name ## StepFunc, name ## ValueFunc, name ## ValueFunc, \
dan2a11bb22018-06-11 20:50:25 +0000454 name ## InvFunc, #name \
dandfa552f2018-06-02 21:04:28 +0000455}
456
dan1c5ed622018-06-05 16:16:17 +0000457#define WINDOWFUNCF(name,nArg,extra) { \
458 nArg, (SQLITE_UTF8|SQLITE_FUNC_WINDOW|extra), 0, 0, \
459 name ## StepFunc, name ## FinalizeFunc, name ## ValueFunc, \
dan2a11bb22018-06-11 20:50:25 +0000460 name ## InvFunc, #name \
dan1c5ed622018-06-05 16:16:17 +0000461}
462
dandfa552f2018-06-02 21:04:28 +0000463/*
464** Register those built-in window functions that are not also aggregates.
465*/
466void sqlite3WindowFunctions(void){
467 static FuncDef aWindowFuncs[] = {
468 WINDOWFUNC(row_number, 0, 0),
469 WINDOWFUNC(dense_rank, 0, 0),
470 WINDOWFUNC(rank, 0, 0),
471 WINDOWFUNC(percent_rank, 0, SQLITE_FUNC_WINDOW_SIZE),
danf1abe362018-06-04 08:22:09 +0000472 WINDOWFUNC(cume_dist, 0, SQLITE_FUNC_WINDOW_SIZE),
dan6bc5c9e2018-06-04 18:55:11 +0000473 WINDOWFUNC(ntile, 1, SQLITE_FUNC_WINDOW_SIZE),
dan1c5ed622018-06-05 16:16:17 +0000474 WINDOWFUNCF(last_value, 1, 0),
dandfa552f2018-06-02 21:04:28 +0000475 WINDOWFUNC(nth_value, 2, 0),
dan7095c002018-06-07 17:45:22 +0000476 WINDOWFUNC(first_value, 1, 0),
danfe4e25a2018-06-07 20:08:59 +0000477 WINDOWFUNC(lead, 1, 0), WINDOWFUNC(lead, 2, 0), WINDOWFUNC(lead, 3, 0),
478 WINDOWFUNC(lag, 1, 0), WINDOWFUNC(lag, 2, 0), WINDOWFUNC(lag, 3, 0),
dandfa552f2018-06-02 21:04:28 +0000479 };
480 sqlite3InsertBuiltinFuncs(aWindowFuncs, ArraySize(aWindowFuncs));
481}
482
danc0bb4452018-06-12 20:53:38 +0000483/*
484** This function is called immediately after resolving the function name
485** for a window function within a SELECT statement. Argument pList is a
486** linked list of WINDOW definitions for the current SELECT statement.
487** Argument pFunc is the function definition just resolved and pWin
488** is the Window object representing the associated OVER clause. This
489** function updates the contents of pWin as follows:
490**
491** * If the OVER clause refered to a named window (as in "max(x) OVER win"),
492** search list pList for a matching WINDOW definition, and update pWin
493** accordingly. If no such WINDOW clause can be found, leave an error
494** in pParse.
495**
496** * If the function is a built-in window function that requires the
497** window to be coerced (see "BUILT-IN WINDOW FUNCTIONS" at the top
498** of this file), pWin is updated here.
499*/
dane3bf6322018-06-08 20:58:27 +0000500void sqlite3WindowUpdate(
501 Parse *pParse,
danc0bb4452018-06-12 20:53:38 +0000502 Window *pList, /* List of named windows for this SELECT */
503 Window *pWin, /* Window frame to update */
504 FuncDef *pFunc /* Window function definition */
dane3bf6322018-06-08 20:58:27 +0000505){
506 if( pWin->zName ){
507 Window *p;
508 for(p=pList; p; p=p->pNextWin){
509 if( sqlite3StrICmp(p->zName, pWin->zName)==0 ) break;
510 }
511 if( p==0 ){
512 sqlite3ErrorMsg(pParse, "no such window: %s", pWin->zName);
513 return;
514 }
515 pWin->pPartition = sqlite3ExprListDup(pParse->db, p->pPartition, 0);
516 pWin->pOrderBy = sqlite3ExprListDup(pParse->db, p->pOrderBy, 0);
517 pWin->pStart = sqlite3ExprDup(pParse->db, p->pStart, 0);
518 pWin->pEnd = sqlite3ExprDup(pParse->db, p->pEnd, 0);
519 pWin->eStart = p->eStart;
520 pWin->eEnd = p->eEnd;
521 }
dandfa552f2018-06-02 21:04:28 +0000522 if( pFunc->funcFlags & SQLITE_FUNC_WINDOW ){
523 sqlite3 *db = pParse->db;
dan8b985602018-06-09 17:43:45 +0000524 if( pWin->pFilter ){
525 sqlite3ErrorMsg(pParse,
526 "FILTER clause may only be used with aggregate window functions"
527 );
528 }else
dan6bc5c9e2018-06-04 18:55:11 +0000529 if( pFunc->xSFunc==row_numberStepFunc || pFunc->xSFunc==ntileStepFunc ){
dandfa552f2018-06-02 21:04:28 +0000530 sqlite3ExprDelete(db, pWin->pStart);
531 sqlite3ExprDelete(db, pWin->pEnd);
532 pWin->pStart = pWin->pEnd = 0;
533 pWin->eType = TK_ROWS;
534 pWin->eStart = TK_UNBOUNDED;
535 pWin->eEnd = TK_CURRENT;
dan8b985602018-06-09 17:43:45 +0000536 }else
dandfa552f2018-06-02 21:04:28 +0000537
538 if( pFunc->xSFunc==dense_rankStepFunc || pFunc->xSFunc==rankStepFunc
danf1abe362018-06-04 08:22:09 +0000539 || pFunc->xSFunc==percent_rankStepFunc || pFunc->xSFunc==cume_distStepFunc
dandfa552f2018-06-02 21:04:28 +0000540 ){
541 sqlite3ExprDelete(db, pWin->pStart);
542 sqlite3ExprDelete(db, pWin->pEnd);
543 pWin->pStart = pWin->pEnd = 0;
544 pWin->eType = TK_RANGE;
545 pWin->eStart = TK_UNBOUNDED;
546 pWin->eEnd = TK_CURRENT;
547 }
548 }
dan2a11bb22018-06-11 20:50:25 +0000549 pWin->pFunc = pFunc;
dandfa552f2018-06-02 21:04:28 +0000550}
551
danc0bb4452018-06-12 20:53:38 +0000552/*
553** Context object passed through sqlite3WalkExprList() to
554** selectWindowRewriteExprCb() by selectWindowRewriteEList().
555*/
dandfa552f2018-06-02 21:04:28 +0000556typedef struct WindowRewrite WindowRewrite;
557struct WindowRewrite {
558 Window *pWin;
559 ExprList *pSub;
560};
561
danc0bb4452018-06-12 20:53:38 +0000562/*
563** Callback function used by selectWindowRewriteEList(). If necessary,
564** this function appends to the output expression-list and updates
565** expression (*ppExpr) in place.
566*/
dandfa552f2018-06-02 21:04:28 +0000567static int selectWindowRewriteExprCb(Walker *pWalker, Expr *pExpr){
568 struct WindowRewrite *p = pWalker->u.pRewrite;
569 Parse *pParse = pWalker->pParse;
570
571 switch( pExpr->op ){
572
573 case TK_FUNCTION:
574 if( pExpr->pWin==0 ){
575 break;
576 }else{
577 Window *pWin;
578 for(pWin=p->pWin; pWin; pWin=pWin->pNextWin){
579 if( pExpr->pWin==pWin ){
dan2a11bb22018-06-11 20:50:25 +0000580 assert( pWin->pOwner==pExpr );
dandfa552f2018-06-02 21:04:28 +0000581 return WRC_Prune;
582 }
583 }
584 }
585 /* Fall through. */
586
dan73925692018-06-12 18:40:17 +0000587 case TK_AGG_FUNCTION:
dandfa552f2018-06-02 21:04:28 +0000588 case TK_COLUMN: {
589 Expr *pDup = sqlite3ExprDup(pParse->db, pExpr, 0);
590 p->pSub = sqlite3ExprListAppend(pParse, p->pSub, pDup);
591 if( p->pSub ){
592 assert( ExprHasProperty(pExpr, EP_Static)==0 );
593 ExprSetProperty(pExpr, EP_Static);
594 sqlite3ExprDelete(pParse->db, pExpr);
595 ExprClearProperty(pExpr, EP_Static);
596 memset(pExpr, 0, sizeof(Expr));
597
598 pExpr->op = TK_COLUMN;
599 pExpr->iColumn = p->pSub->nExpr-1;
600 pExpr->iTable = p->pWin->iEphCsr;
601 }
602
603 break;
604 }
605
606 default: /* no-op */
607 break;
608 }
609
610 return WRC_Continue;
611}
danc0bb4452018-06-12 20:53:38 +0000612static int selectWindowRewriteSelectCb(Walker *pWalker, Select *pSelect){
613 return WRC_Prune;
614}
dandfa552f2018-06-02 21:04:28 +0000615
danc0bb4452018-06-12 20:53:38 +0000616
617/*
618** Iterate through each expression in expression-list pEList. For each:
619**
620** * TK_COLUMN,
621** * aggregate function, or
622** * window function with a Window object that is not a member of the
623** linked list passed as the second argument (pWin)
624**
625** Append the node to output expression-list (*ppSub). And replace it
626** with a TK_COLUMN that reads the (N-1)th element of table
627** pWin->iEphCsr, where N is the number of elements in (*ppSub) after
628** appending the new one.
629*/
dan13b08bb2018-06-15 20:46:12 +0000630static void selectWindowRewriteEList(
dandfa552f2018-06-02 21:04:28 +0000631 Parse *pParse,
632 Window *pWin,
633 ExprList *pEList, /* Rewrite expressions in this list */
634 ExprList **ppSub /* IN/OUT: Sub-select expression-list */
635){
636 Walker sWalker;
637 WindowRewrite sRewrite;
dandfa552f2018-06-02 21:04:28 +0000638
639 memset(&sWalker, 0, sizeof(Walker));
640 memset(&sRewrite, 0, sizeof(WindowRewrite));
641
642 sRewrite.pSub = *ppSub;
643 sRewrite.pWin = pWin;
644
645 sWalker.pParse = pParse;
646 sWalker.xExprCallback = selectWindowRewriteExprCb;
647 sWalker.xSelectCallback = selectWindowRewriteSelectCb;
648 sWalker.u.pRewrite = &sRewrite;
649
dan13b08bb2018-06-15 20:46:12 +0000650 (void)sqlite3WalkExprList(&sWalker, pEList);
dandfa552f2018-06-02 21:04:28 +0000651
652 *ppSub = sRewrite.pSub;
dandfa552f2018-06-02 21:04:28 +0000653}
654
danc0bb4452018-06-12 20:53:38 +0000655/*
656** Append a copy of each expression in expression-list pAppend to
657** expression list pList. Return a pointer to the result list.
658*/
dandfa552f2018-06-02 21:04:28 +0000659static ExprList *exprListAppendList(
660 Parse *pParse, /* Parsing context */
661 ExprList *pList, /* List to which to append. Might be NULL */
662 ExprList *pAppend /* List of values to append. Might be NULL */
663){
664 if( pAppend ){
665 int i;
666 int nInit = pList ? pList->nExpr : 0;
667 for(i=0; i<pAppend->nExpr; i++){
668 Expr *pDup = sqlite3ExprDup(pParse->db, pAppend->a[i].pExpr, 0);
669 pList = sqlite3ExprListAppend(pParse, pList, pDup);
670 if( pList ) pList->a[nInit+i].sortOrder = pAppend->a[i].sortOrder;
671 }
672 }
673 return pList;
674}
675
676/*
677** If the SELECT statement passed as the second argument does not invoke
678** any SQL window functions, this function is a no-op. Otherwise, it
679** rewrites the SELECT statement so that window function xStep functions
danc0bb4452018-06-12 20:53:38 +0000680** are invoked in the correct order as described under "SELECT REWRITING"
681** at the top of this file.
dandfa552f2018-06-02 21:04:28 +0000682*/
683int sqlite3WindowRewrite(Parse *pParse, Select *p){
684 int rc = SQLITE_OK;
685 if( p->pWin ){
686 Vdbe *v = sqlite3GetVdbe(pParse);
687 int i;
688 sqlite3 *db = pParse->db;
689 Select *pSub = 0; /* The subquery */
690 SrcList *pSrc = p->pSrc;
691 Expr *pWhere = p->pWhere;
692 ExprList *pGroupBy = p->pGroupBy;
693 Expr *pHaving = p->pHaving;
694 ExprList *pSort = 0;
695
696 ExprList *pSublist = 0; /* Expression list for sub-query */
697 Window *pMWin = p->pWin; /* Master window object */
698 Window *pWin; /* Window object iterator */
699
700 p->pSrc = 0;
701 p->pWhere = 0;
702 p->pGroupBy = 0;
703 p->pHaving = 0;
704
705 /* Assign a cursor number for the ephemeral table used to buffer rows.
706 ** The OpenEphemeral instruction is coded later, after it is known how
707 ** many columns the table will have. */
708 pMWin->iEphCsr = pParse->nTab++;
709
dan13b08bb2018-06-15 20:46:12 +0000710 selectWindowRewriteEList(pParse, pMWin, p->pEList, &pSublist);
711 selectWindowRewriteEList(pParse, pMWin, p->pOrderBy, &pSublist);
dandfa552f2018-06-02 21:04:28 +0000712 pMWin->nBufferCol = (pSublist ? pSublist->nExpr : 0);
713
714 /* Create the ORDER BY clause for the sub-select. This is the concatenation
715 ** of the window PARTITION and ORDER BY clauses. Append the same
716 ** expressions to the sub-select expression list. They are required to
717 ** figure out where boundaries for partitions and sets of peer rows. */
718 pSort = sqlite3ExprListDup(db, pMWin->pPartition, 0);
719 if( pMWin->pOrderBy ){
720 pSort = exprListAppendList(pParse, pSort, pMWin->pOrderBy);
721 }
722 pSublist = exprListAppendList(pParse, pSublist, pSort);
723
724 /* Append the arguments passed to each window function to the
725 ** sub-select expression list. Also allocate two registers for each
726 ** window function - one for the accumulator, another for interim
727 ** results. */
728 for(pWin=pMWin; pWin; pWin=pWin->pNextWin){
729 pWin->iArgCol = (pSublist ? pSublist->nExpr : 0);
730 pSublist = exprListAppendList(pParse, pSublist, pWin->pOwner->x.pList);
dan8b985602018-06-09 17:43:45 +0000731 if( pWin->pFilter ){
732 Expr *pFilter = sqlite3ExprDup(db, pWin->pFilter, 0);
733 pSublist = sqlite3ExprListAppend(pParse, pSublist, pFilter);
734 }
dandfa552f2018-06-02 21:04:28 +0000735 pWin->regAccum = ++pParse->nMem;
736 pWin->regResult = ++pParse->nMem;
737 sqlite3VdbeAddOp2(v, OP_Null, 0, pWin->regAccum);
738 }
739
740 pSub = sqlite3SelectNew(
741 pParse, pSublist, pSrc, pWhere, pGroupBy, pHaving, pSort, 0, 0
742 );
743 p->pSrc = sqlite3SrcListAppend(db, 0, 0, 0);
dan6fde1792018-06-15 19:01:35 +0000744 assert( p->pSrc || db->mallocFailed );
dandfa552f2018-06-02 21:04:28 +0000745 if( p->pSrc ){
746 int iTab;
747 ExprList *pList = 0;
748 p->pSrc->a[0].pSelect = pSub;
749 sqlite3SrcListAssignCursors(pParse, p->pSrc);
750 if( sqlite3ExpandSubquery(pParse, &p->pSrc->a[0]) ){
751 rc = SQLITE_NOMEM;
752 }else{
753 pSub->selFlags |= SF_Expanded;
dan73925692018-06-12 18:40:17 +0000754 p->selFlags &= ~SF_Aggregate;
755 sqlite3SelectPrep(pParse, pSub, 0);
dandfa552f2018-06-02 21:04:28 +0000756 }
dandfa552f2018-06-02 21:04:28 +0000757
dan6fde1792018-06-15 19:01:35 +0000758 sqlite3VdbeAddOp2(v, OP_OpenEphemeral, pMWin->iEphCsr, pSublist->nExpr);
759 }else{
760 sqlite3SelectDelete(db, pSub);
761 }
762 if( db->mallocFailed ) rc = SQLITE_NOMEM;
dandfa552f2018-06-02 21:04:28 +0000763 }
764
765 return rc;
766}
767
danc0bb4452018-06-12 20:53:38 +0000768/*
769** Free the Window object passed as the second argument.
770*/
dan86fb6e12018-05-16 20:58:07 +0000771void sqlite3WindowDelete(sqlite3 *db, Window *p){
772 if( p ){
773 sqlite3ExprDelete(db, p->pFilter);
774 sqlite3ExprListDelete(db, p->pPartition);
775 sqlite3ExprListDelete(db, p->pOrderBy);
776 sqlite3ExprDelete(db, p->pEnd);
777 sqlite3ExprDelete(db, p->pStart);
dane3bf6322018-06-08 20:58:27 +0000778 sqlite3DbFree(db, p->zName);
dan86fb6e12018-05-16 20:58:07 +0000779 sqlite3DbFree(db, p);
780 }
781}
782
danc0bb4452018-06-12 20:53:38 +0000783/*
784** Free the linked list of Window objects starting at the second argument.
785*/
dane3bf6322018-06-08 20:58:27 +0000786void sqlite3WindowListDelete(sqlite3 *db, Window *p){
787 while( p ){
788 Window *pNext = p->pNextWin;
789 sqlite3WindowDelete(db, p);
790 p = pNext;
791 }
792}
793
danc0bb4452018-06-12 20:53:38 +0000794/*
795** Allocate and return a new Window object.
796*/
dan86fb6e12018-05-16 20:58:07 +0000797Window *sqlite3WindowAlloc(
798 Parse *pParse,
799 int eType,
danc3a20c12018-05-23 20:55:37 +0000800 int eStart, Expr *pStart,
801 int eEnd, Expr *pEnd
dan86fb6e12018-05-16 20:58:07 +0000802){
803 Window *pWin = (Window*)sqlite3DbMallocZero(pParse->db, sizeof(Window));
804
805 if( pWin ){
806 pWin->eType = eType;
807 pWin->eStart = eStart;
808 pWin->eEnd = eEnd;
809 pWin->pEnd = pEnd;
810 pWin->pStart = pStart;
811 }else{
812 sqlite3ExprDelete(pParse->db, pEnd);
813 sqlite3ExprDelete(pParse->db, pStart);
814 }
815
816 return pWin;
817}
818
danc0bb4452018-06-12 20:53:38 +0000819/*
820** Attach window object pWin to expression p.
821*/
dan86fb6e12018-05-16 20:58:07 +0000822void sqlite3WindowAttach(Parse *pParse, Expr *p, Window *pWin){
823 if( p ){
824 p->pWin = pWin;
dan2a11bb22018-06-11 20:50:25 +0000825 if( pWin ) pWin->pOwner = p;
dan86fb6e12018-05-16 20:58:07 +0000826 }else{
827 sqlite3WindowDelete(pParse->db, pWin);
828 }
829}
dane2f781b2018-05-17 19:24:08 +0000830
831/*
832** Return 0 if the two window objects are identical, or non-zero otherwise.
dan13078ca2018-06-13 20:29:38 +0000833** Identical window objects can be processed in a single scan.
dane2f781b2018-05-17 19:24:08 +0000834*/
835int sqlite3WindowCompare(Parse *pParse, Window *p1, Window *p2){
836 if( p1->eType!=p2->eType ) return 1;
837 if( p1->eStart!=p2->eStart ) return 1;
838 if( p1->eEnd!=p2->eEnd ) return 1;
839 if( sqlite3ExprCompare(pParse, p1->pStart, p2->pStart, -1) ) return 1;
840 if( sqlite3ExprCompare(pParse, p1->pEnd, p2->pEnd, -1) ) return 1;
841 if( sqlite3ExprListCompare(p1->pPartition, p2->pPartition, -1) ) return 1;
842 if( sqlite3ExprListCompare(p1->pOrderBy, p2->pOrderBy, -1) ) return 1;
843 return 0;
844}
845
dan13078ca2018-06-13 20:29:38 +0000846
847/*
848** This is called by code in select.c before it calls sqlite3WhereBegin()
849** to begin iterating through the sub-query results. It is used to allocate
850** and initialize registers and cursors used by sqlite3WindowCodeStep().
851*/
852void sqlite3WindowCodeInit(Parse *pParse, Window *pMWin){
danc9a86682018-05-30 20:44:58 +0000853 Window *pWin;
dan13078ca2018-06-13 20:29:38 +0000854 Vdbe *v = sqlite3GetVdbe(pParse);
855 int nPart = (pMWin->pPartition ? pMWin->pPartition->nExpr : 0);
856 nPart += (pMWin->pOrderBy ? pMWin->pOrderBy->nExpr : 0);
857 if( nPart ){
858 pMWin->regPart = pParse->nMem+1;
859 pParse->nMem += nPart;
860 sqlite3VdbeAddOp3(v, OP_Null, 0, pMWin->regPart, pMWin->regPart+nPart-1);
861 }
862
danc9a86682018-05-30 20:44:58 +0000863 for(pWin=pMWin; pWin; pWin=pWin->pNextWin){
danec891fd2018-06-06 20:51:02 +0000864 FuncDef *p = pWin->pFunc;
865 if( (p->funcFlags & SQLITE_FUNC_MINMAX) && pWin->eStart!=TK_UNBOUNDED ){
dan9a947222018-06-14 19:06:36 +0000866 /* The inline versions of min() and max() require a single ephemeral
867 ** table and 3 registers. The registers are used as follows:
868 **
869 ** regApp+0: slot to copy min()/max() argument to for MakeRecord
870 ** regApp+1: integer value used to ensure keys are unique
871 ** regApp+2: output of MakeRecord
872 */
danc9a86682018-05-30 20:44:58 +0000873 ExprList *pList = pWin->pOwner->x.pList;
874 KeyInfo *pKeyInfo = sqlite3KeyInfoFromExprList(pParse, pList, 0, 0);
danc9a86682018-05-30 20:44:58 +0000875 pWin->csrApp = pParse->nTab++;
876 pWin->regApp = pParse->nMem+1;
877 pParse->nMem += 3;
878 if( pKeyInfo && pWin->pFunc->zName[1]=='i' ){
879 assert( pKeyInfo->aSortOrder[0]==0 );
880 pKeyInfo->aSortOrder[0] = 1;
881 }
882 sqlite3VdbeAddOp2(v, OP_OpenEphemeral, pWin->csrApp, 2);
883 sqlite3VdbeAppendP4(v, pKeyInfo, P4_KEYINFO);
884 sqlite3VdbeAddOp2(v, OP_Integer, 0, pWin->regApp+1);
885 }
dan7095c002018-06-07 17:45:22 +0000886 else if( p->xSFunc==nth_valueStepFunc || p->xSFunc==first_valueStepFunc ){
danec891fd2018-06-06 20:51:02 +0000887 /* Allocate two registers at pWin->regApp. These will be used to
888 ** store the start and end index of the current frame. */
889 assert( pMWin->iEphCsr );
890 pWin->regApp = pParse->nMem+1;
891 pWin->csrApp = pParse->nTab++;
892 pParse->nMem += 2;
danec891fd2018-06-06 20:51:02 +0000893 sqlite3VdbeAddOp2(v, OP_OpenDup, pWin->csrApp, pMWin->iEphCsr);
894 }
danfe4e25a2018-06-07 20:08:59 +0000895 else if( p->xSFunc==leadStepFunc || p->xSFunc==lagStepFunc ){
896 assert( pMWin->iEphCsr );
897 pWin->csrApp = pParse->nTab++;
898 sqlite3VdbeAddOp2(v, OP_OpenDup, pWin->csrApp, pMWin->iEphCsr);
899 }
danc9a86682018-05-30 20:44:58 +0000900 }
901}
902
dan13078ca2018-06-13 20:29:38 +0000903/*
904** A "PRECEDING <expr>" (bEnd==0) or "FOLLOWING <expr>" (bEnd==1) has just
905** been evaluated and the result left in register reg. This function generates
906** VM code to check that the value is a non-negative integer and throws
907** an exception if it is not.
908*/
danc3a20c12018-05-23 20:55:37 +0000909static void windowCheckFrameValue(Parse *pParse, int reg, int bEnd){
910 static const char *azErr[] = {
911 "frame starting offset must be a non-negative integer",
912 "frame ending offset must be a non-negative integer"
913 };
914 Vdbe *v = sqlite3GetVdbe(pParse);
dan13078ca2018-06-13 20:29:38 +0000915 int regZero = sqlite3GetTempReg(pParse);
danc3a20c12018-05-23 20:55:37 +0000916 sqlite3VdbeAddOp2(v, OP_Integer, 0, regZero);
917 sqlite3VdbeAddOp2(v, OP_MustBeInt, reg, sqlite3VdbeCurrentAddr(v)+2);
918 sqlite3VdbeAddOp3(v, OP_Ge, regZero, sqlite3VdbeCurrentAddr(v)+2, reg);
919 sqlite3VdbeAddOp2(v, OP_Halt, SQLITE_ERROR, OE_Abort);
920 sqlite3VdbeAppendP4(v, (void*)azErr[bEnd], P4_STATIC);
dan13078ca2018-06-13 20:29:38 +0000921 sqlite3ReleaseTempReg(pParse, regZero);
danc3a20c12018-05-23 20:55:37 +0000922}
923
dan13078ca2018-06-13 20:29:38 +0000924/*
925** Return the number of arguments passed to the window-function associated
926** with the object passed as the only argument to this function.
927*/
dan2a11bb22018-06-11 20:50:25 +0000928static int windowArgCount(Window *pWin){
929 ExprList *pList = pWin->pOwner->x.pList;
930 return (pList ? pList->nExpr : 0);
931}
932
danc9a86682018-05-30 20:44:58 +0000933/*
934** Generate VM code to invoke either xStep() (if bInverse is 0) or
935** xInverse (if bInverse is non-zero) for each window function in the
dan13078ca2018-06-13 20:29:38 +0000936** linked list starting at pMWin. Or, for built-in window functions
937** that do not use the standard function API, generate the required
938** inline VM code.
939**
940** If argument csr is greater than or equal to 0, then argument reg is
941** the first register in an array of registers guaranteed to be large
942** enough to hold the array of arguments for each function. In this case
943** the arguments are extracted from the current row of csr into the
944** array of registers before invoking OP_AggStep.
945**
946** Or, if csr is less than zero, then the array of registers at reg is
947** already populated with all columns from the current row of the sub-query.
948**
949** If argument regPartSize is non-zero, then it is a register containing the
950** number of rows in the current partition.
danc9a86682018-05-30 20:44:58 +0000951*/
dan31f56392018-05-24 21:10:57 +0000952static void windowAggStep(
953 Parse *pParse,
dan13078ca2018-06-13 20:29:38 +0000954 Window *pMWin, /* Linked list of window functions */
955 int csr, /* Read arguments from this cursor */
956 int bInverse, /* True to invoke xInverse instead of xStep */
957 int reg, /* Array of registers */
dandfa552f2018-06-02 21:04:28 +0000958 int regPartSize /* Register containing size of partition */
dan31f56392018-05-24 21:10:57 +0000959){
960 Vdbe *v = sqlite3GetVdbe(pParse);
961 Window *pWin;
962 for(pWin=pMWin; pWin; pWin=pWin->pNextWin){
dandfa552f2018-06-02 21:04:28 +0000963 int flags = pWin->pFunc->funcFlags;
danc9a86682018-05-30 20:44:58 +0000964 int regArg;
dan2a11bb22018-06-11 20:50:25 +0000965 int nArg = windowArgCount(pWin);
dandfa552f2018-06-02 21:04:28 +0000966
dan6bc5c9e2018-06-04 18:55:11 +0000967 if( csr>=0 ){
danc9a86682018-05-30 20:44:58 +0000968 int i;
dan2a11bb22018-06-11 20:50:25 +0000969 for(i=0; i<nArg; i++){
danc9a86682018-05-30 20:44:58 +0000970 sqlite3VdbeAddOp3(v, OP_Column, csr, pWin->iArgCol+i, reg+i);
971 }
972 regArg = reg;
dan6bc5c9e2018-06-04 18:55:11 +0000973 if( flags & SQLITE_FUNC_WINDOW_SIZE ){
974 if( nArg==0 ){
975 regArg = regPartSize;
976 }else{
977 sqlite3VdbeAddOp2(v, OP_SCopy, regPartSize, reg+nArg);
978 }
979 nArg++;
980 }
danc9a86682018-05-30 20:44:58 +0000981 }else{
dan6bc5c9e2018-06-04 18:55:11 +0000982 assert( !(flags & SQLITE_FUNC_WINDOW_SIZE) );
danc9a86682018-05-30 20:44:58 +0000983 regArg = reg + pWin->iArgCol;
dan31f56392018-05-24 21:10:57 +0000984 }
danc9a86682018-05-30 20:44:58 +0000985
danec891fd2018-06-06 20:51:02 +0000986 if( (pWin->pFunc->funcFlags & SQLITE_FUNC_MINMAX)
987 && pWin->eStart!=TK_UNBOUNDED
988 ){
danc9a86682018-05-30 20:44:58 +0000989 if( bInverse==0 ){
990 sqlite3VdbeAddOp2(v, OP_AddImm, pWin->regApp+1, 1);
991 sqlite3VdbeAddOp2(v, OP_SCopy, regArg, pWin->regApp);
992 sqlite3VdbeAddOp3(v, OP_MakeRecord, pWin->regApp, 2, pWin->regApp+2);
993 sqlite3VdbeAddOp2(v, OP_IdxInsert, pWin->csrApp, pWin->regApp+2);
994 }else{
995 sqlite3VdbeAddOp4Int(v, OP_SeekGE, pWin->csrApp, 0, regArg, 1);
996 sqlite3VdbeAddOp1(v, OP_Delete, pWin->csrApp);
997 sqlite3VdbeJumpHere(v, sqlite3VdbeCurrentAddr(v)-2);
998 }
danec891fd2018-06-06 20:51:02 +0000999 }else if( pWin->regApp ){
dan7095c002018-06-07 17:45:22 +00001000 assert( pWin->pFunc->xSFunc==nth_valueStepFunc
1001 || pWin->pFunc->xSFunc==first_valueStepFunc
1002 );
danec891fd2018-06-06 20:51:02 +00001003 assert( bInverse==0 || bInverse==1 );
1004 sqlite3VdbeAddOp2(v, OP_AddImm, pWin->regApp+1-bInverse, 1);
danfe4e25a2018-06-07 20:08:59 +00001005 }else if( pWin->pFunc->xSFunc==leadStepFunc
1006 || pWin->pFunc->xSFunc==lagStepFunc
1007 ){
1008 /* no-op */
danc9a86682018-05-30 20:44:58 +00001009 }else{
dan8b985602018-06-09 17:43:45 +00001010 int addrIf = 0;
1011 if( pWin->pFilter ){
1012 int regTmp;
dan2a11bb22018-06-11 20:50:25 +00001013 assert( nArg==pWin->pOwner->x.pList->nExpr );
dan8b985602018-06-09 17:43:45 +00001014 if( csr>0 ){
1015 regTmp = sqlite3GetTempReg(pParse);
dan2a11bb22018-06-11 20:50:25 +00001016 sqlite3VdbeAddOp3(v, OP_Column, csr, pWin->iArgCol+nArg,regTmp);
dan8b985602018-06-09 17:43:45 +00001017 }else{
dan2a11bb22018-06-11 20:50:25 +00001018 regTmp = regArg + nArg;
dan8b985602018-06-09 17:43:45 +00001019 }
1020 addrIf = sqlite3VdbeAddOp3(v, OP_IfNot, regTmp, 0, 1);
1021 if( csr>0 ){
1022 sqlite3ReleaseTempReg(pParse, regTmp);
1023 }
1024 }
danc9a86682018-05-30 20:44:58 +00001025 if( pWin->pFunc->funcFlags & SQLITE_FUNC_NEEDCOLL ){
1026 CollSeq *pColl;
dan8b985602018-06-09 17:43:45 +00001027 pColl = sqlite3ExprNNCollSeq(pParse, pWin->pOwner->x.pList->a[0].pExpr);
danc9a86682018-05-30 20:44:58 +00001028 sqlite3VdbeAddOp4(v, OP_CollSeq, 0,0,0, (const char*)pColl, P4_COLLSEQ);
1029 }
1030 sqlite3VdbeAddOp3(v, OP_AggStep0, bInverse, regArg, pWin->regAccum);
1031 sqlite3VdbeAppendP4(v, pWin->pFunc, P4_FUNCDEF);
dandfa552f2018-06-02 21:04:28 +00001032 sqlite3VdbeChangeP5(v, (u8)nArg);
dan8b985602018-06-09 17:43:45 +00001033 if( addrIf ) sqlite3VdbeJumpHere(v, addrIf);
danc9a86682018-05-30 20:44:58 +00001034 }
dan31f56392018-05-24 21:10:57 +00001035 }
1036}
1037
dan13078ca2018-06-13 20:29:38 +00001038/*
1039** Generate VM code to invoke either xValue() (bFinal==0) or xFinalize()
1040** (bFinal==1) for each window function in the linked list starting at
1041** pMWin. Or, for built-in window-functions that do not use the standard
1042** API, generate the equivalent VM code.
1043*/
dand6f784e2018-05-28 18:30:45 +00001044static void windowAggFinal(Parse *pParse, Window *pMWin, int bFinal){
1045 Vdbe *v = sqlite3GetVdbe(pParse);
1046 Window *pWin;
1047
1048 for(pWin=pMWin; pWin; pWin=pWin->pNextWin){
danec891fd2018-06-06 20:51:02 +00001049 if( (pWin->pFunc->funcFlags & SQLITE_FUNC_MINMAX)
1050 && pWin->eStart!=TK_UNBOUNDED
1051 ){
dand6f784e2018-05-28 18:30:45 +00001052 sqlite3VdbeAddOp2(v, OP_Null, 0, pWin->regResult);
danc9a86682018-05-30 20:44:58 +00001053 sqlite3VdbeAddOp1(v, OP_Last, pWin->csrApp);
1054 sqlite3VdbeAddOp3(v, OP_Column, pWin->csrApp, 0, pWin->regResult);
1055 sqlite3VdbeJumpHere(v, sqlite3VdbeCurrentAddr(v)-2);
1056 if( bFinal ){
1057 sqlite3VdbeAddOp1(v, OP_ResetSorter, pWin->csrApp);
1058 }
danec891fd2018-06-06 20:51:02 +00001059 }else if( pWin->regApp ){
dand6f784e2018-05-28 18:30:45 +00001060 }else{
danc9a86682018-05-30 20:44:58 +00001061 if( bFinal==0 ){
1062 sqlite3VdbeAddOp2(v, OP_Null, 0, pWin->regResult);
1063 }
dan2a11bb22018-06-11 20:50:25 +00001064 sqlite3VdbeAddOp2(v, OP_AggFinal, pWin->regAccum, windowArgCount(pWin));
danc9a86682018-05-30 20:44:58 +00001065 sqlite3VdbeAppendP4(v, pWin->pFunc, P4_FUNCDEF);
1066 if( bFinal ){
1067 sqlite3VdbeAddOp2(v, OP_Copy, pWin->regAccum, pWin->regResult);
dan8b985602018-06-09 17:43:45 +00001068 sqlite3VdbeAddOp2(v, OP_Null, 0, pWin->regAccum);
danc9a86682018-05-30 20:44:58 +00001069 }else{
1070 sqlite3VdbeChangeP3(v, -1, pWin->regResult);
1071 }
dand6f784e2018-05-28 18:30:45 +00001072 }
1073 }
1074}
1075
dan13078ca2018-06-13 20:29:38 +00001076/*
1077** This function generates VM code to invoke the sub-routine at address
1078** lblFlushPart once for each partition with the entire partition cached in
1079** the Window.iEphCsr temp table.
1080*/
danf690b572018-06-01 21:00:08 +00001081static void windowPartitionCache(
1082 Parse *pParse,
dan13078ca2018-06-13 20:29:38 +00001083 Select *p, /* The rewritten SELECT statement */
1084 WhereInfo *pWInfo, /* WhereInfo to call WhereEnd() on */
1085 int regFlushPart, /* Register to use with Gosub lblFlushPart */
1086 int lblFlushPart, /* Subroutine to Gosub to */
1087 int *pRegSize /* OUT: Register containing partition size */
danf690b572018-06-01 21:00:08 +00001088){
1089 Window *pMWin = p->pWin;
1090 Vdbe *v = sqlite3GetVdbe(pParse);
1091 Window *pWin;
1092 int iSubCsr = p->pSrc->a[0].iCursor;
1093 int nSub = p->pSrc->a[0].pTab->nCol;
1094 int k;
1095
1096 int reg = pParse->nMem+1;
1097 int regRecord = reg+nSub;
1098 int regRowid = regRecord+1;
1099
dandfa552f2018-06-02 21:04:28 +00001100 *pRegSize = regRowid;
danf690b572018-06-01 21:00:08 +00001101 pParse->nMem += nSub + 2;
1102
1103 /* Martial the row returned by the sub-select into an array of
1104 ** registers. */
1105 for(k=0; k<nSub; k++){
1106 sqlite3VdbeAddOp3(v, OP_Column, iSubCsr, k, reg+k);
1107 }
1108 sqlite3VdbeAddOp3(v, OP_MakeRecord, reg, nSub, regRecord);
1109
1110 /* Check if this is the start of a new partition. If so, call the
1111 ** flush_partition sub-routine. */
1112 if( pMWin->pPartition ){
1113 int addr;
1114 ExprList *pPart = pMWin->pPartition;
dane0a5e202018-06-15 16:10:44 +00001115 int nPart = pPart->nExpr;
danf690b572018-06-01 21:00:08 +00001116 int regNewPart = reg + pMWin->nBufferCol;
1117 KeyInfo *pKeyInfo = sqlite3KeyInfoFromExprList(pParse, pPart, 0, 0);
1118
1119 addr = sqlite3VdbeAddOp3(v, OP_Compare, regNewPart, pMWin->regPart,nPart);
1120 sqlite3VdbeAppendP4(v, (void*)pKeyInfo, P4_KEYINFO);
1121 sqlite3VdbeAddOp3(v, OP_Jump, addr+2, addr+4, addr+2);
1122 sqlite3VdbeAddOp3(v, OP_Copy, regNewPart, pMWin->regPart, nPart-1);
1123 sqlite3VdbeAddOp2(v, OP_Gosub, regFlushPart, lblFlushPart);
1124 }
1125
1126 /* Buffer the current row in the ephemeral table. */
1127 sqlite3VdbeAddOp2(v, OP_NewRowid, pMWin->iEphCsr, regRowid);
1128 sqlite3VdbeAddOp3(v, OP_Insert, pMWin->iEphCsr, regRecord, regRowid);
1129
1130 /* End of the input loop */
1131 sqlite3WhereEnd(pWInfo);
1132
1133 /* Invoke "flush_partition" to deal with the final (or only) partition */
1134 sqlite3VdbeAddOp2(v, OP_Gosub, regFlushPart, lblFlushPart);
1135}
dand6f784e2018-05-28 18:30:45 +00001136
dan13078ca2018-06-13 20:29:38 +00001137/*
1138** Invoke the sub-routine at regGosub (generated by code in select.c) to
1139** return the current row of Window.iEphCsr. If all window functions are
1140** aggregate window functions that use the standard API, a single
1141** OP_Gosub instruction is all that this routine generates. Extra VM code
1142** for per-row processing is only generated for the following built-in window
1143** functions:
1144**
1145** nth_value()
1146** first_value()
1147** lag()
1148** lead()
1149*/
danec891fd2018-06-06 20:51:02 +00001150static void windowReturnOneRow(
1151 Parse *pParse,
1152 Window *pMWin,
1153 int regGosub,
1154 int addrGosub
1155){
1156 Vdbe *v = sqlite3GetVdbe(pParse);
1157 Window *pWin;
1158 for(pWin=pMWin; pWin; pWin=pWin->pNextWin){
1159 FuncDef *pFunc = pWin->pFunc;
dan7095c002018-06-07 17:45:22 +00001160 if( pFunc->xSFunc==nth_valueStepFunc
1161 || pFunc->xSFunc==first_valueStepFunc
1162 ){
danec891fd2018-06-06 20:51:02 +00001163 int csr = pWin->csrApp;
1164 int lbl = sqlite3VdbeMakeLabel(v);
1165 int tmpReg = sqlite3GetTempReg(pParse);
1166 sqlite3VdbeAddOp2(v, OP_Null, 0, pWin->regResult);
dan7095c002018-06-07 17:45:22 +00001167
1168 if( pFunc->xSFunc==nth_valueStepFunc ){
dan6fde1792018-06-15 19:01:35 +00001169 sqlite3VdbeAddOp3(v, OP_Column, pMWin->iEphCsr, pWin->iArgCol+1,tmpReg);
dan7095c002018-06-07 17:45:22 +00001170 }else{
1171 sqlite3VdbeAddOp2(v, OP_Integer, 1, tmpReg);
1172 }
danec891fd2018-06-06 20:51:02 +00001173 sqlite3VdbeAddOp3(v, OP_Add, tmpReg, pWin->regApp, tmpReg);
1174 sqlite3VdbeAddOp3(v, OP_Gt, pWin->regApp+1, lbl, tmpReg);
1175 sqlite3VdbeAddOp3(v, OP_SeekRowid, csr, lbl, tmpReg);
1176 sqlite3VdbeAddOp3(v, OP_Column, csr, pWin->iArgCol, pWin->regResult);
1177 sqlite3VdbeResolveLabel(v, lbl);
1178 sqlite3ReleaseTempReg(pParse, tmpReg);
1179 }
danfe4e25a2018-06-07 20:08:59 +00001180 else if( pFunc->xSFunc==leadStepFunc || pFunc->xSFunc==lagStepFunc ){
dan2a11bb22018-06-11 20:50:25 +00001181 int nArg = pWin->pOwner->x.pList->nExpr;
dane0a5e202018-06-15 16:10:44 +00001182 int iEph = pMWin->iEphCsr;
danfe4e25a2018-06-07 20:08:59 +00001183 int csr = pWin->csrApp;
1184 int lbl = sqlite3VdbeMakeLabel(v);
1185 int tmpReg = sqlite3GetTempReg(pParse);
1186
dan2a11bb22018-06-11 20:50:25 +00001187 if( nArg<3 ){
danfe4e25a2018-06-07 20:08:59 +00001188 sqlite3VdbeAddOp2(v, OP_Null, 0, pWin->regResult);
1189 }else{
1190 sqlite3VdbeAddOp3(v, OP_Column, iEph, pWin->iArgCol+2, pWin->regResult);
1191 }
1192 sqlite3VdbeAddOp2(v, OP_Rowid, iEph, tmpReg);
dan2a11bb22018-06-11 20:50:25 +00001193 if( nArg<2 ){
danfe4e25a2018-06-07 20:08:59 +00001194 int val = (pFunc->xSFunc==leadStepFunc ? 1 : -1);
1195 sqlite3VdbeAddOp2(v, OP_AddImm, tmpReg, val);
1196 }else{
1197 int op = (pFunc->xSFunc==leadStepFunc ? OP_Add : OP_Subtract);
1198 int tmpReg2 = sqlite3GetTempReg(pParse);
1199 sqlite3VdbeAddOp3(v, OP_Column, iEph, pWin->iArgCol+1, tmpReg2);
1200 sqlite3VdbeAddOp3(v, op, tmpReg2, tmpReg, tmpReg);
1201 sqlite3ReleaseTempReg(pParse, tmpReg2);
1202 }
1203
1204 sqlite3VdbeAddOp3(v, OP_SeekRowid, csr, lbl, tmpReg);
1205 sqlite3VdbeAddOp3(v, OP_Column, csr, pWin->iArgCol, pWin->regResult);
1206 sqlite3VdbeResolveLabel(v, lbl);
1207 sqlite3ReleaseTempReg(pParse, tmpReg);
1208 }
danec891fd2018-06-06 20:51:02 +00001209 }
1210 sqlite3VdbeAddOp2(v, OP_Gosub, regGosub, addrGosub);
1211}
1212
dan13078ca2018-06-13 20:29:38 +00001213/*
1214** Invoke the code generated by windowReturnOneRow() and, optionally, the
1215** xInverse() function for each window function, for one or more rows
1216** from the Window.iEphCsr temp table. This routine generates VM code
1217** similar to:
1218**
1219** while( regCtr>0 ){
1220** regCtr--;
1221** windowReturnOneRow()
1222** if( bInverse ){
1223** AggStep (xInverse)
1224** }
1225** Next (Window.iEphCsr)
1226** }
1227*/
danec891fd2018-06-06 20:51:02 +00001228static void windowReturnRows(
1229 Parse *pParse,
dan13078ca2018-06-13 20:29:38 +00001230 Window *pMWin, /* List of window functions */
1231 int regCtr, /* Register containing number of rows */
1232 int regGosub, /* Register for Gosub addrGosub */
1233 int addrGosub, /* Address of sub-routine for ReturnOneRow */
1234 int regInvArg, /* Array of registers for xInverse args */
1235 int regInvSize /* Register containing size of partition */
danec891fd2018-06-06 20:51:02 +00001236){
1237 int addr;
1238 Vdbe *v = sqlite3GetVdbe(pParse);
1239 windowAggFinal(pParse, pMWin, 0);
1240 addr = sqlite3VdbeAddOp3(v, OP_IfPos, regCtr, sqlite3VdbeCurrentAddr(v)+2 ,1);
1241 sqlite3VdbeAddOp2(v, OP_Goto, 0, 0);
1242 windowReturnOneRow(pParse, pMWin, regGosub, addrGosub);
1243 if( regInvArg ){
1244 windowAggStep(pParse, pMWin, pMWin->iEphCsr, 1, regInvArg, regInvSize);
1245 }
1246 sqlite3VdbeAddOp2(v, OP_Next, pMWin->iEphCsr, addr);
1247 sqlite3VdbeJumpHere(v, addr+1); /* The OP_Goto */
1248}
1249
dan54a9ab32018-06-14 14:27:05 +00001250/*
1251** Generate code to set the accumulator register for each window function
1252** in the linked list passed as the second argument to NULL. And perform
1253** any equivalent initialization required by any built-in window functions
1254** in the list.
1255*/
dan2e605682018-06-07 15:54:26 +00001256static int windowInitAccum(Parse *pParse, Window *pMWin){
1257 Vdbe *v = sqlite3GetVdbe(pParse);
1258 int regArg;
1259 int nArg = 0;
1260 Window *pWin;
1261 for(pWin=pMWin; pWin; pWin=pWin->pNextWin){
dan9a947222018-06-14 19:06:36 +00001262 FuncDef *pFunc = pWin->pFunc;
dan2e605682018-06-07 15:54:26 +00001263 sqlite3VdbeAddOp2(v, OP_Null, 0, pWin->regAccum);
dan2a11bb22018-06-11 20:50:25 +00001264 nArg = MAX(nArg, windowArgCount(pWin));
dan9a947222018-06-14 19:06:36 +00001265 if( pFunc->xSFunc==nth_valueStepFunc
1266 || pFunc->xSFunc==first_valueStepFunc
dan7095c002018-06-07 17:45:22 +00001267 ){
dan2e605682018-06-07 15:54:26 +00001268 sqlite3VdbeAddOp2(v, OP_Integer, 0, pWin->regApp);
1269 sqlite3VdbeAddOp2(v, OP_Integer, 0, pWin->regApp+1);
1270 }
dan9a947222018-06-14 19:06:36 +00001271
1272 if( (pFunc->funcFlags & SQLITE_FUNC_MINMAX) && pWin->csrApp ){
1273 assert( pWin->eStart!=TK_UNBOUNDED );
1274 sqlite3VdbeAddOp1(v, OP_ResetSorter, pWin->csrApp);
1275 sqlite3VdbeAddOp2(v, OP_Integer, 0, pWin->regApp+1);
1276 }
dan2e605682018-06-07 15:54:26 +00001277 }
1278 regArg = pParse->nMem+1;
1279 pParse->nMem += nArg;
1280 return regArg;
1281}
1282
1283
dan99652dd2018-05-24 17:49:14 +00001284/*
dan54a9ab32018-06-14 14:27:05 +00001285** This function does the work of sqlite3WindowCodeStep() for all "ROWS"
1286** window frame types except for "BETWEEN UNBOUNDED PRECEDING AND CURRENT
1287** ROW". Pseudo-code for each follows.
1288**
dan09590aa2018-05-25 20:30:17 +00001289** ROWS BETWEEN <expr1> PRECEDING AND <expr2> FOLLOWING
dan09590aa2018-05-25 20:30:17 +00001290**
1291** ...
1292** if( new partition ){
1293** Gosub flush_partition
1294** }
1295** Insert (record in eph-table)
1296** sqlite3WhereEnd()
1297** Gosub flush_partition
1298**
1299** flush_partition:
1300** Once {
1301** OpenDup (iEphCsr -> csrStart)
1302** OpenDup (iEphCsr -> csrEnd)
dan99652dd2018-05-24 17:49:14 +00001303** }
dan09590aa2018-05-25 20:30:17 +00001304** regStart = <expr1> // PRECEDING expression
1305** regEnd = <expr2> // FOLLOWING expression
1306** if( regStart<0 || regEnd<0 ){ error! }
1307** Rewind (csr,csrStart,csrEnd) // if EOF goto flush_partition_done
1308** Next(csrEnd) // if EOF skip Aggstep
1309** Aggstep (csrEnd)
1310** if( (regEnd--)<=0 ){
1311** AggFinal (xValue)
1312** Gosub addrGosub
1313** Next(csr) // if EOF goto flush_partition_done
1314** if( (regStart--)<=0 ){
1315** AggStep (csrStart, xInverse)
1316** Next(csrStart)
1317** }
1318** }
1319** flush_partition_done:
1320** ResetSorter (csr)
1321** Return
dan99652dd2018-05-24 17:49:14 +00001322**
dan09590aa2018-05-25 20:30:17 +00001323** ROWS BETWEEN <expr> PRECEDING AND CURRENT ROW
1324** ROWS BETWEEN CURRENT ROW AND <expr> FOLLOWING
1325** ROWS BETWEEN UNBOUNDED PRECEDING AND <expr> FOLLOWING
1326**
1327** These are similar to the above. For "CURRENT ROW", intialize the
1328** register to 0. For "UNBOUNDED PRECEDING" to infinity.
1329**
1330** ROWS BETWEEN <expr> PRECEDING AND UNBOUNDED FOLLOWING
1331** ROWS BETWEEN CURRENT ROW AND UNBOUNDED FOLLOWING
1332**
1333** Rewind (csr,csrStart,csrEnd) // if EOF goto flush_partition_done
1334** while( 1 ){
1335** Next(csrEnd) // Exit while(1) at EOF
1336** Aggstep (csrEnd)
1337** }
1338** while( 1 ){
dan99652dd2018-05-24 17:49:14 +00001339** AggFinal (xValue)
1340** Gosub addrGosub
dan09590aa2018-05-25 20:30:17 +00001341** Next(csr) // if EOF goto flush_partition_done
dan31f56392018-05-24 21:10:57 +00001342** if( (regStart--)<=0 ){
1343** AggStep (csrStart, xInverse)
1344** Next(csrStart)
dan99652dd2018-05-24 17:49:14 +00001345** }
1346** }
dan99652dd2018-05-24 17:49:14 +00001347**
dan09590aa2018-05-25 20:30:17 +00001348** For the "CURRENT ROW AND UNBOUNDED FOLLOWING" case, the final if()
1349** condition is always true (as if regStart were initialized to 0).
dan99652dd2018-05-24 17:49:14 +00001350**
dan09590aa2018-05-25 20:30:17 +00001351** RANGE BETWEEN CURRENT ROW AND UNBOUNDED FOLLOWING
1352**
1353** This is the only RANGE case handled by this routine. It modifies the
1354** second while( 1 ) loop in "ROWS BETWEEN CURRENT ... UNBOUNDED..." to
1355** be:
1356**
1357** while( 1 ){
1358** AggFinal (xValue)
1359** while( 1 ){
1360** regPeer++
1361** Gosub addrGosub
1362** Next(csr) // if EOF goto flush_partition_done
1363** if( new peer ) break;
1364** }
1365** while( (regPeer--)>0 ){
1366** AggStep (csrStart, xInverse)
1367** Next(csrStart)
1368** }
1369** }
dan99652dd2018-05-24 17:49:14 +00001370**
dan31f56392018-05-24 21:10:57 +00001371** ROWS BETWEEN <expr> FOLLOWING AND <expr> FOLLOWING
1372**
1373** regEnd = regEnd - regStart
1374** Rewind (csr,csrStart,csrEnd) // if EOF goto flush_partition_done
1375** Aggstep (csrEnd)
1376** Next(csrEnd) // if EOF fall-through
1377** if( (regEnd--)<=0 ){
dan31f56392018-05-24 21:10:57 +00001378** if( (regStart--)<=0 ){
1379** AggFinal (xValue)
1380** Gosub addrGosub
1381** Next(csr) // if EOF goto flush_partition_done
1382** }
dane105dd72018-05-25 09:29:11 +00001383** AggStep (csrStart, xInverse)
1384** Next (csrStart)
dan31f56392018-05-24 21:10:57 +00001385** }
1386**
1387** ROWS BETWEEN <expr> PRECEDING AND <expr> PRECEDING
1388**
1389** Replace the bit after "Rewind" in the above with:
1390**
1391** if( (regEnd--)<=0 ){
1392** AggStep (csrEnd)
1393** Next (csrEnd)
1394** }
1395** AggFinal (xValue)
1396** Gosub addrGosub
1397** Next(csr) // if EOF goto flush_partition_done
1398** if( (regStart--)<=0 ){
1399** AggStep (csr2, xInverse)
1400** Next (csr2)
1401** }
1402**
dan99652dd2018-05-24 17:49:14 +00001403*/
danc3a20c12018-05-23 20:55:37 +00001404static void windowCodeRowExprStep(
1405 Parse *pParse,
1406 Select *p,
1407 WhereInfo *pWInfo,
1408 int regGosub,
1409 int addrGosub
1410){
1411 Window *pMWin = p->pWin;
1412 Vdbe *v = sqlite3GetVdbe(pParse);
1413 Window *pWin;
1414 int k;
danc3a20c12018-05-23 20:55:37 +00001415 int nSub = p->pSrc->a[0].pTab->nCol;
1416 int regFlushPart; /* Register for "Gosub flush_partition" */
dan31f56392018-05-24 21:10:57 +00001417 int lblFlushPart; /* Label for "Gosub flush_partition" */
1418 int lblFlushDone; /* Label for "Gosub flush_partition_done" */
danc3a20c12018-05-23 20:55:37 +00001419
danf690b572018-06-01 21:00:08 +00001420 int regArg;
1421 int nArg;
danc3a20c12018-05-23 20:55:37 +00001422 int addr;
dan31f56392018-05-24 21:10:57 +00001423 int csrStart = pParse->nTab++;
1424 int csrEnd = pParse->nTab++;
1425 int regStart; /* Value of <expr> PRECEDING */
1426 int regEnd; /* Value of <expr> FOLLOWING */
danc3a20c12018-05-23 20:55:37 +00001427 int addrNext;
1428 int addrGoto;
dan31f56392018-05-24 21:10:57 +00001429 int addrTop;
danc3a20c12018-05-23 20:55:37 +00001430 int addrIfPos1;
1431 int addrIfPos2;
1432
dan09590aa2018-05-25 20:30:17 +00001433 int regPeer = 0; /* Number of peers in current group */
1434 int regPeerVal = 0; /* Array of values identifying peer group */
1435 int iPeer = 0; /* Column offset in eph-table of peer vals */
1436 int nPeerVal; /* Number of peer values */
dandfa552f2018-06-02 21:04:28 +00001437 int regSize = 0;
dan09590aa2018-05-25 20:30:17 +00001438
dan99652dd2018-05-24 17:49:14 +00001439 assert( pMWin->eStart==TK_PRECEDING
1440 || pMWin->eStart==TK_CURRENT
dane105dd72018-05-25 09:29:11 +00001441 || pMWin->eStart==TK_FOLLOWING
dan99652dd2018-05-24 17:49:14 +00001442 || pMWin->eStart==TK_UNBOUNDED
1443 );
1444 assert( pMWin->eEnd==TK_FOLLOWING
1445 || pMWin->eEnd==TK_CURRENT
1446 || pMWin->eEnd==TK_UNBOUNDED
dan31f56392018-05-24 21:10:57 +00001447 || pMWin->eEnd==TK_PRECEDING
dan99652dd2018-05-24 17:49:14 +00001448 );
1449
danc3a20c12018-05-23 20:55:37 +00001450 /* Allocate register and label for the "flush_partition" sub-routine. */
1451 regFlushPart = ++pParse->nMem;
dan31f56392018-05-24 21:10:57 +00001452 lblFlushPart = sqlite3VdbeMakeLabel(v);
1453 lblFlushDone = sqlite3VdbeMakeLabel(v);
danc3a20c12018-05-23 20:55:37 +00001454
dan31f56392018-05-24 21:10:57 +00001455 regStart = ++pParse->nMem;
1456 regEnd = ++pParse->nMem;
danc3a20c12018-05-23 20:55:37 +00001457
dandfa552f2018-06-02 21:04:28 +00001458 windowPartitionCache(pParse, p, pWInfo, regFlushPart, lblFlushPart, &regSize);
danc3a20c12018-05-23 20:55:37 +00001459
danc3a20c12018-05-23 20:55:37 +00001460 addrGoto = sqlite3VdbeAddOp0(v, OP_Goto);
1461
danc9a86682018-05-30 20:44:58 +00001462 /* Start of "flush_partition" */
dan31f56392018-05-24 21:10:57 +00001463 sqlite3VdbeResolveLabel(v, lblFlushPart);
danc3a20c12018-05-23 20:55:37 +00001464 sqlite3VdbeAddOp2(v, OP_Once, 0, sqlite3VdbeCurrentAddr(v)+3);
dan31f56392018-05-24 21:10:57 +00001465 sqlite3VdbeAddOp2(v, OP_OpenDup, csrStart, pMWin->iEphCsr);
1466 sqlite3VdbeAddOp2(v, OP_OpenDup, csrEnd, pMWin->iEphCsr);
danc3a20c12018-05-23 20:55:37 +00001467
dan31f56392018-05-24 21:10:57 +00001468 /* If either regStart or regEnd are not non-negative integers, throw
dan99652dd2018-05-24 17:49:14 +00001469 ** an exception. */
1470 if( pMWin->pStart ){
dan31f56392018-05-24 21:10:57 +00001471 sqlite3ExprCode(pParse, pMWin->pStart, regStart);
1472 windowCheckFrameValue(pParse, regStart, 0);
dan99652dd2018-05-24 17:49:14 +00001473 }
1474 if( pMWin->pEnd ){
dan31f56392018-05-24 21:10:57 +00001475 sqlite3ExprCode(pParse, pMWin->pEnd, regEnd);
1476 windowCheckFrameValue(pParse, regEnd, 1);
dan99652dd2018-05-24 17:49:14 +00001477 }
danc3a20c12018-05-23 20:55:37 +00001478
danc9a86682018-05-30 20:44:58 +00001479 /* If this is "ROWS <expr1> FOLLOWING AND ROWS <expr2> FOLLOWING", do:
1480 **
dan26522d12018-06-11 18:16:51 +00001481 ** if( regEnd<regStart ){
1482 ** // The frame always consists of 0 rows
1483 ** regStart = regSize;
1484 ** }
danc9a86682018-05-30 20:44:58 +00001485 ** regEnd = regEnd - regStart;
1486 */
1487 if( pMWin->pEnd && pMWin->pStart && pMWin->eStart==TK_FOLLOWING ){
1488 assert( pMWin->eEnd==TK_FOLLOWING );
dan26522d12018-06-11 18:16:51 +00001489 sqlite3VdbeAddOp3(v, OP_Ge, regStart, sqlite3VdbeCurrentAddr(v)+2, regEnd);
1490 sqlite3VdbeAddOp2(v, OP_Copy, regSize, regStart);
danc9a86682018-05-30 20:44:58 +00001491 sqlite3VdbeAddOp3(v, OP_Subtract, regStart, regEnd, regEnd);
1492 }
1493
dan26522d12018-06-11 18:16:51 +00001494 if( pMWin->pEnd && pMWin->pStart && pMWin->eEnd==TK_PRECEDING ){
1495 assert( pMWin->eStart==TK_PRECEDING );
1496 sqlite3VdbeAddOp3(v, OP_Le, regStart, sqlite3VdbeCurrentAddr(v)+3, regEnd);
1497 sqlite3VdbeAddOp2(v, OP_Copy, regSize, regStart);
1498 sqlite3VdbeAddOp2(v, OP_Copy, regSize, regEnd);
1499 }
1500
danc9a86682018-05-30 20:44:58 +00001501 /* Initialize the accumulator register for each window function to NULL */
dan2e605682018-06-07 15:54:26 +00001502 regArg = windowInitAccum(pParse, pMWin);
danc3a20c12018-05-23 20:55:37 +00001503
dan31f56392018-05-24 21:10:57 +00001504 sqlite3VdbeAddOp2(v, OP_Rewind, pMWin->iEphCsr, lblFlushDone);
1505 sqlite3VdbeAddOp2(v, OP_Rewind, csrStart, lblFlushDone);
danc3a20c12018-05-23 20:55:37 +00001506 sqlite3VdbeChangeP5(v, 1);
dan31f56392018-05-24 21:10:57 +00001507 sqlite3VdbeAddOp2(v, OP_Rewind, csrEnd, lblFlushDone);
danc3a20c12018-05-23 20:55:37 +00001508 sqlite3VdbeChangeP5(v, 1);
1509
1510 /* Invoke AggStep function for each window function using the row that
dan31f56392018-05-24 21:10:57 +00001511 ** csrEnd currently points to. Or, if csrEnd is already at EOF,
danc3a20c12018-05-23 20:55:37 +00001512 ** do nothing. */
dan31f56392018-05-24 21:10:57 +00001513 addrTop = sqlite3VdbeCurrentAddr(v);
1514 if( pMWin->eEnd==TK_PRECEDING ){
1515 addrIfPos1 = sqlite3VdbeAddOp3(v, OP_IfPos, regEnd, 0 , 1);
danc3a20c12018-05-23 20:55:37 +00001516 }
dan31f56392018-05-24 21:10:57 +00001517 sqlite3VdbeAddOp2(v, OP_Next, csrEnd, sqlite3VdbeCurrentAddr(v)+2);
1518 addr = sqlite3VdbeAddOp0(v, OP_Goto);
dandfa552f2018-06-02 21:04:28 +00001519 windowAggStep(pParse, pMWin, csrEnd, 0, regArg, regSize);
dan99652dd2018-05-24 17:49:14 +00001520 if( pMWin->eEnd==TK_UNBOUNDED ){
dan31f56392018-05-24 21:10:57 +00001521 sqlite3VdbeAddOp2(v, OP_Goto, 0, addrTop);
1522 sqlite3VdbeJumpHere(v, addr);
1523 addrTop = sqlite3VdbeCurrentAddr(v);
dan99652dd2018-05-24 17:49:14 +00001524 }else{
dan31f56392018-05-24 21:10:57 +00001525 sqlite3VdbeJumpHere(v, addr);
1526 if( pMWin->eEnd==TK_PRECEDING ){
1527 sqlite3VdbeJumpHere(v, addrIfPos1);
1528 }
dan99652dd2018-05-24 17:49:14 +00001529 }
danc3a20c12018-05-23 20:55:37 +00001530
dan99652dd2018-05-24 17:49:14 +00001531 if( pMWin->eEnd==TK_FOLLOWING ){
dan31f56392018-05-24 21:10:57 +00001532 addrIfPos1 = sqlite3VdbeAddOp3(v, OP_IfPos, regEnd, 0 , 1);
dan99652dd2018-05-24 17:49:14 +00001533 }
dane105dd72018-05-25 09:29:11 +00001534 if( pMWin->eStart==TK_FOLLOWING ){
1535 addrIfPos2 = sqlite3VdbeAddOp3(v, OP_IfPos, regStart, 0 , 1);
1536 }
dand6f784e2018-05-28 18:30:45 +00001537 windowAggFinal(pParse, pMWin, 0);
danec891fd2018-06-06 20:51:02 +00001538 windowReturnOneRow(pParse, pMWin, regGosub, addrGosub);
danc3a20c12018-05-23 20:55:37 +00001539 sqlite3VdbeAddOp2(v, OP_Next, pMWin->iEphCsr, sqlite3VdbeCurrentAddr(v)+2);
dan31f56392018-05-24 21:10:57 +00001540 sqlite3VdbeAddOp2(v, OP_Goto, 0, lblFlushDone);
dane105dd72018-05-25 09:29:11 +00001541 if( pMWin->eStart==TK_FOLLOWING ){
1542 sqlite3VdbeJumpHere(v, addrIfPos2);
1543 }
danc3a20c12018-05-23 20:55:37 +00001544
dane105dd72018-05-25 09:29:11 +00001545 if( pMWin->eStart==TK_CURRENT
1546 || pMWin->eStart==TK_PRECEDING
1547 || pMWin->eStart==TK_FOLLOWING
1548 ){
dan09590aa2018-05-25 20:30:17 +00001549 int addrJumpHere = 0;
dan99652dd2018-05-24 17:49:14 +00001550 if( pMWin->eStart==TK_PRECEDING ){
dan09590aa2018-05-25 20:30:17 +00001551 addrJumpHere = sqlite3VdbeAddOp3(v, OP_IfPos, regStart, 0 , 1);
1552 }
dan31f56392018-05-24 21:10:57 +00001553 sqlite3VdbeAddOp2(v, OP_Next, csrStart, sqlite3VdbeCurrentAddr(v)+1);
dandfa552f2018-06-02 21:04:28 +00001554 windowAggStep(pParse, pMWin, csrStart, 1, regArg, regSize);
dan09590aa2018-05-25 20:30:17 +00001555 if( addrJumpHere ){
1556 sqlite3VdbeJumpHere(v, addrJumpHere);
dan99652dd2018-05-24 17:49:14 +00001557 }
danc3a20c12018-05-23 20:55:37 +00001558 }
dan99652dd2018-05-24 17:49:14 +00001559 if( pMWin->eEnd==TK_FOLLOWING ){
1560 sqlite3VdbeJumpHere(v, addrIfPos1);
1561 }
dan31f56392018-05-24 21:10:57 +00001562 sqlite3VdbeAddOp2(v, OP_Goto, 0, addrTop);
danc3a20c12018-05-23 20:55:37 +00001563
1564 /* flush_partition_done: */
dan31f56392018-05-24 21:10:57 +00001565 sqlite3VdbeResolveLabel(v, lblFlushDone);
danc3a20c12018-05-23 20:55:37 +00001566 sqlite3VdbeAddOp1(v, OP_ResetSorter, pMWin->iEphCsr);
1567 sqlite3VdbeAddOp1(v, OP_Return, regFlushPart);
1568
1569 /* Jump to here to skip over flush_partition */
1570 sqlite3VdbeJumpHere(v, addrGoto);
1571}
1572
dan79d45442018-05-26 21:17:29 +00001573/*
dan54a9ab32018-06-14 14:27:05 +00001574** This function does the work of sqlite3WindowCodeStep() for cases that
1575** would normally be handled by windowCodeDefaultStep() when there are
1576** one or more built-in window-functions that require the entire partition
1577** to be cached in a temp table before any rows can be returned. Additionally.
1578** "RANGE BETWEEN CURRENT ROW AND UNBOUNDED FOLLOWING" is always handled by
1579** this function.
1580**
1581** Pseudo-code corresponding to the VM code generated by this function
1582** for each type of window follows.
1583**
dan79d45442018-05-26 21:17:29 +00001584** RANGE BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW
1585**
danf690b572018-06-01 21:00:08 +00001586** flush_partition:
1587** Once {
1588** OpenDup (iEphCsr -> csrLead)
1589** }
1590** Integer ctr 0
1591** foreach row (csrLead){
1592** if( new peer ){
1593** AggFinal (xValue)
1594** for(i=0; i<ctr; i++){
1595** Gosub addrGosub
1596** Next iEphCsr
1597** }
1598** Integer ctr 0
1599** }
1600** AggStep (csrLead)
1601** Incr ctr
1602** }
1603**
1604** AggFinal (xFinalize)
1605** for(i=0; i<ctr; i++){
1606** Gosub addrGosub
1607** Next iEphCsr
1608** }
1609**
1610** ResetSorter (csr)
1611** Return
1612**
1613** ROWS BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW
dan54a9ab32018-06-14 14:27:05 +00001614**
1615** As above, except that the "if( new peer )" branch is always taken.
1616**
danf690b572018-06-01 21:00:08 +00001617** RANGE BETWEEN CURRENT ROW AND CURRENT ROW
dan54a9ab32018-06-14 14:27:05 +00001618**
1619** As above, except that each of the for() loops becomes:
1620**
1621** for(i=0; i<ctr; i++){
1622** Gosub addrGosub
1623** AggStep (xInverse, iEphCsr)
1624** Next iEphCsr
1625** }
1626**
1627** RANGE BETWEEN UNBOUNDED PRECEDING AND UNBOUNDED FOLLOWING
1628**
1629** flush_partition:
1630** Once {
1631** OpenDup (iEphCsr -> csrLead)
1632** }
1633** foreach row (csrLead) {
1634** AggStep (csrLead)
1635** }
1636** foreach row (iEphCsr) {
1637** Gosub addrGosub
1638** }
1639**
1640** RANGE BETWEEN CURRENT ROW AND UNBOUNDED FOLLOWING
1641**
1642** flush_partition:
1643** Once {
1644** OpenDup (iEphCsr -> csrLead)
1645** }
1646** foreach row (csrLead){
1647** AggStep (csrLead)
1648** }
1649** Rewind (csrLead)
1650** Integer ctr 0
1651** foreach row (csrLead){
1652** if( new peer ){
1653** AggFinal (xValue)
1654** for(i=0; i<ctr; i++){
1655** Gosub addrGosub
1656** AggStep (xInverse, iEphCsr)
1657** Next iEphCsr
1658** }
1659** Integer ctr 0
1660** }
1661** Incr ctr
1662** }
1663**
1664** AggFinal (xFinalize)
1665** for(i=0; i<ctr; i++){
1666** Gosub addrGosub
1667** Next iEphCsr
1668** }
1669**
1670** ResetSorter (csr)
1671** Return
danf690b572018-06-01 21:00:08 +00001672*/
1673static void windowCodeCacheStep(
1674 Parse *pParse,
1675 Select *p,
1676 WhereInfo *pWInfo,
1677 int regGosub,
1678 int addrGosub
1679){
1680 Window *pMWin = p->pWin;
1681 Vdbe *v = sqlite3GetVdbe(pParse);
1682 Window *pWin;
1683 int k;
1684 int addr;
1685 ExprList *pPart = pMWin->pPartition;
1686 ExprList *pOrderBy = pMWin->pOrderBy;
dan54a9ab32018-06-14 14:27:05 +00001687 int nPeer = pOrderBy ? pOrderBy->nExpr : 0;
danf690b572018-06-01 21:00:08 +00001688 int regNewPeer;
1689
1690 int addrGoto; /* Address of Goto used to jump flush_par.. */
dan13078ca2018-06-13 20:29:38 +00001691 int addrNext; /* Jump here for next iteration of loop */
danf690b572018-06-01 21:00:08 +00001692 int regFlushPart;
1693 int lblFlushPart;
1694 int csrLead;
1695 int regCtr;
1696 int regArg; /* Register array to martial function args */
dandfa552f2018-06-02 21:04:28 +00001697 int regSize;
danf690b572018-06-01 21:00:08 +00001698 int nArg;
dan13078ca2018-06-13 20:29:38 +00001699 int lblEmpty;
dan54a9ab32018-06-14 14:27:05 +00001700 int bReverse = pMWin->pOrderBy && pMWin->eStart==TK_CURRENT
1701 && pMWin->eEnd==TK_UNBOUNDED;
danf690b572018-06-01 21:00:08 +00001702
1703 assert( (pMWin->eStart==TK_UNBOUNDED && pMWin->eEnd==TK_CURRENT)
danec891fd2018-06-06 20:51:02 +00001704 || (pMWin->eStart==TK_UNBOUNDED && pMWin->eEnd==TK_UNBOUNDED)
1705 || (pMWin->eStart==TK_CURRENT && pMWin->eEnd==TK_CURRENT)
dan13078ca2018-06-13 20:29:38 +00001706 || (pMWin->eStart==TK_CURRENT && pMWin->eEnd==TK_UNBOUNDED)
danf690b572018-06-01 21:00:08 +00001707 );
1708
dan13078ca2018-06-13 20:29:38 +00001709 lblEmpty = sqlite3VdbeMakeLabel(v);
danf690b572018-06-01 21:00:08 +00001710 regNewPeer = pParse->nMem+1;
1711 pParse->nMem += nPeer;
1712
1713 /* Allocate register and label for the "flush_partition" sub-routine. */
1714 regFlushPart = ++pParse->nMem;
1715 lblFlushPart = sqlite3VdbeMakeLabel(v);
1716
1717 csrLead = pParse->nTab++;
1718 regCtr = ++pParse->nMem;
1719
dandfa552f2018-06-02 21:04:28 +00001720 windowPartitionCache(pParse, p, pWInfo, regFlushPart, lblFlushPart, &regSize);
danf690b572018-06-01 21:00:08 +00001721 addrGoto = sqlite3VdbeAddOp0(v, OP_Goto);
1722
1723 /* Start of "flush_partition" */
1724 sqlite3VdbeResolveLabel(v, lblFlushPart);
1725 sqlite3VdbeAddOp2(v, OP_Once, 0, sqlite3VdbeCurrentAddr(v)+2);
1726 sqlite3VdbeAddOp2(v, OP_OpenDup, csrLead, pMWin->iEphCsr);
1727
1728 /* Initialize the accumulator register for each window function to NULL */
dan2e605682018-06-07 15:54:26 +00001729 regArg = windowInitAccum(pParse, pMWin);
danf690b572018-06-01 21:00:08 +00001730
1731 sqlite3VdbeAddOp2(v, OP_Integer, 0, regCtr);
dan13078ca2018-06-13 20:29:38 +00001732 sqlite3VdbeAddOp2(v, OP_Rewind, csrLead, lblEmpty);
1733 sqlite3VdbeAddOp2(v, OP_Rewind, pMWin->iEphCsr, lblEmpty);
danf690b572018-06-01 21:00:08 +00001734
dan13078ca2018-06-13 20:29:38 +00001735 if( bReverse ){
1736 int addr = sqlite3VdbeCurrentAddr(v);
1737 windowAggStep(pParse, pMWin, csrLead, 0, regArg, regSize);
1738 sqlite3VdbeAddOp2(v, OP_Next, csrLead, addr);
1739 sqlite3VdbeAddOp2(v, OP_Rewind, csrLead, lblEmpty);
1740 }
1741 addrNext = sqlite3VdbeCurrentAddr(v);
1742
1743 if( pOrderBy && (pMWin->eEnd==TK_CURRENT || pMWin->eStart==TK_CURRENT) ){
1744 int bCurrent = (pMWin->eStart==TK_CURRENT);
danec891fd2018-06-06 20:51:02 +00001745 int addrJump = 0; /* Address of OP_Jump below */
1746 if( pMWin->eType==TK_RANGE ){
1747 int iOff = pMWin->nBufferCol + (pPart ? pPart->nExpr : 0);
1748 int regPeer = pMWin->regPart + (pPart ? pPart->nExpr : 0);
1749 KeyInfo *pKeyInfo = sqlite3KeyInfoFromExprList(pParse, pOrderBy, 0, 0);
1750 for(k=0; k<nPeer; k++){
1751 sqlite3VdbeAddOp3(v, OP_Column, csrLead, iOff+k, regNewPeer+k);
1752 }
1753 addr = sqlite3VdbeAddOp3(v, OP_Compare, regNewPeer, regPeer, nPeer);
1754 sqlite3VdbeAppendP4(v, (void*)pKeyInfo, P4_KEYINFO);
1755 addrJump = sqlite3VdbeAddOp3(v, OP_Jump, addr+2, 0, addr+2);
1756 sqlite3VdbeAddOp3(v, OP_Copy, regNewPeer, regPeer, nPeer-1);
danf690b572018-06-01 21:00:08 +00001757 }
1758
dan13078ca2018-06-13 20:29:38 +00001759 windowReturnRows(pParse, pMWin, regCtr, regGosub, addrGosub,
danec891fd2018-06-06 20:51:02 +00001760 (bCurrent ? regArg : 0), (bCurrent ? regSize : 0)
1761 );
1762 if( addrJump ) sqlite3VdbeJumpHere(v, addrJump);
danf690b572018-06-01 21:00:08 +00001763 }
1764
dan13078ca2018-06-13 20:29:38 +00001765 if( bReverse==0 ){
1766 windowAggStep(pParse, pMWin, csrLead, 0, regArg, regSize);
1767 }
danf690b572018-06-01 21:00:08 +00001768 sqlite3VdbeAddOp2(v, OP_AddImm, regCtr, 1);
dan13078ca2018-06-13 20:29:38 +00001769 sqlite3VdbeAddOp2(v, OP_Next, csrLead, addrNext);
danf690b572018-06-01 21:00:08 +00001770
dan13078ca2018-06-13 20:29:38 +00001771 windowReturnRows(pParse, pMWin, regCtr, regGosub, addrGosub, 0, 0);
danf690b572018-06-01 21:00:08 +00001772
dan13078ca2018-06-13 20:29:38 +00001773 sqlite3VdbeResolveLabel(v, lblEmpty);
danf690b572018-06-01 21:00:08 +00001774 sqlite3VdbeAddOp1(v, OP_ResetSorter, pMWin->iEphCsr);
1775 sqlite3VdbeAddOp1(v, OP_Return, regFlushPart);
1776
1777 /* Jump to here to skip over flush_partition */
1778 sqlite3VdbeJumpHere(v, addrGoto);
1779}
1780
1781
1782/*
1783** RANGE BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW
1784**
dan79d45442018-05-26 21:17:29 +00001785** ...
1786** if( new partition ){
1787** AggFinal (xFinalize)
1788** Gosub addrGosub
1789** ResetSorter eph-table
1790** }
1791** else if( new peer ){
1792** AggFinal (xValue)
1793** Gosub addrGosub
1794** ResetSorter eph-table
1795** }
1796** AggStep
1797** Insert (record into eph-table)
1798** sqlite3WhereEnd()
1799** AggFinal (xFinalize)
1800** Gosub addrGosub
danf690b572018-06-01 21:00:08 +00001801**
1802** RANGE BETWEEN UNBOUNDED PRECEDING AND UNBOUNDED FOLLOWING
1803**
1804** As above, except take no action for a "new peer". Invoke
1805** the sub-routine once only for each partition.
1806**
1807** RANGE BETWEEN CURRENT ROW AND CURRENT ROW
1808**
1809** As above, except that the "new peer" condition is handled in the
1810** same way as "new partition" (so there is no "else if" block).
1811**
1812** ROWS BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW
1813**
1814** As above, except assume every row is a "new peer".
dan79d45442018-05-26 21:17:29 +00001815*/
danc3a20c12018-05-23 20:55:37 +00001816static void windowCodeDefaultStep(
1817 Parse *pParse,
1818 Select *p,
1819 WhereInfo *pWInfo,
1820 int regGosub,
1821 int addrGosub
1822){
1823 Window *pMWin = p->pWin;
1824 Vdbe *v = sqlite3GetVdbe(pParse);
1825 Window *pWin;
1826 int k;
1827 int iSubCsr = p->pSrc->a[0].iCursor;
1828 int nSub = p->pSrc->a[0].pTab->nCol;
1829 int reg = pParse->nMem+1;
1830 int regRecord = reg+nSub;
1831 int regRowid = regRecord+1;
1832 int addr;
dand6f784e2018-05-28 18:30:45 +00001833 ExprList *pPart = pMWin->pPartition;
1834 ExprList *pOrderBy = pMWin->pOrderBy;
danc3a20c12018-05-23 20:55:37 +00001835
dan79d45442018-05-26 21:17:29 +00001836 assert( pMWin->eType==TK_RANGE
1837 || (pMWin->eStart==TK_UNBOUNDED && pMWin->eEnd==TK_CURRENT)
1838 );
1839
dand6f784e2018-05-28 18:30:45 +00001840 assert( (pMWin->eStart==TK_UNBOUNDED && pMWin->eEnd==TK_CURRENT)
1841 || (pMWin->eStart==TK_UNBOUNDED && pMWin->eEnd==TK_UNBOUNDED)
1842 || (pMWin->eStart==TK_CURRENT && pMWin->eEnd==TK_CURRENT)
1843 || (pMWin->eStart==TK_CURRENT && pMWin->eEnd==TK_UNBOUNDED && !pOrderBy)
1844 );
1845
1846 if( pMWin->eEnd==TK_UNBOUNDED ){
1847 pOrderBy = 0;
1848 }
1849
danc3a20c12018-05-23 20:55:37 +00001850 pParse->nMem += nSub + 2;
1851
1852 /* Martial the row returned by the sub-select into an array of
1853 ** registers. */
1854 for(k=0; k<nSub; k++){
1855 sqlite3VdbeAddOp3(v, OP_Column, iSubCsr, k, reg+k);
1856 }
1857
1858 /* Check if this is the start of a new partition or peer group. */
dand6f784e2018-05-28 18:30:45 +00001859 if( pPart || pOrderBy ){
danc3a20c12018-05-23 20:55:37 +00001860 int nPart = (pPart ? pPart->nExpr : 0);
danc3a20c12018-05-23 20:55:37 +00001861 int addrGoto = 0;
1862 int addrJump = 0;
dand6f784e2018-05-28 18:30:45 +00001863 int nPeer = (pOrderBy ? pOrderBy->nExpr : 0);
danc3a20c12018-05-23 20:55:37 +00001864
1865 if( pPart ){
1866 int regNewPart = reg + pMWin->nBufferCol;
1867 KeyInfo *pKeyInfo = sqlite3KeyInfoFromExprList(pParse, pPart, 0, 0);
1868 addr = sqlite3VdbeAddOp3(v, OP_Compare, regNewPart, pMWin->regPart,nPart);
1869 sqlite3VdbeAppendP4(v, (void*)pKeyInfo, P4_KEYINFO);
1870 addrJump = sqlite3VdbeAddOp3(v, OP_Jump, addr+2, 0, addr+2);
dand6f784e2018-05-28 18:30:45 +00001871 windowAggFinal(pParse, pMWin, 1);
danc3a20c12018-05-23 20:55:37 +00001872 if( pOrderBy ){
1873 addrGoto = sqlite3VdbeAddOp0(v, OP_Goto);
1874 }
1875 }
1876
1877 if( pOrderBy ){
1878 int regNewPeer = reg + pMWin->nBufferCol + nPart;
1879 int regPeer = pMWin->regPart + nPart;
1880
danc3a20c12018-05-23 20:55:37 +00001881 if( addrJump ) sqlite3VdbeJumpHere(v, addrJump);
dan79d45442018-05-26 21:17:29 +00001882 if( pMWin->eType==TK_RANGE ){
1883 KeyInfo *pKeyInfo = sqlite3KeyInfoFromExprList(pParse, pOrderBy, 0, 0);
1884 addr = sqlite3VdbeAddOp3(v, OP_Compare, regNewPeer, regPeer, nPeer);
1885 sqlite3VdbeAppendP4(v, (void*)pKeyInfo, P4_KEYINFO);
1886 addrJump = sqlite3VdbeAddOp3(v, OP_Jump, addr+2, 0, addr+2);
1887 }else{
1888 addrJump = 0;
1889 }
dand6f784e2018-05-28 18:30:45 +00001890 windowAggFinal(pParse, pMWin, pMWin->eStart==TK_CURRENT);
danc3a20c12018-05-23 20:55:37 +00001891 if( addrGoto ) sqlite3VdbeJumpHere(v, addrGoto);
1892 }
1893
dandacf1de2018-06-08 16:11:55 +00001894 sqlite3VdbeAddOp2(v, OP_Rewind, pMWin->iEphCsr,sqlite3VdbeCurrentAddr(v)+3);
danc3a20c12018-05-23 20:55:37 +00001895 sqlite3VdbeAddOp2(v, OP_Gosub, regGosub, addrGosub);
dandacf1de2018-06-08 16:11:55 +00001896 sqlite3VdbeAddOp2(v, OP_Next, pMWin->iEphCsr, sqlite3VdbeCurrentAddr(v)-1);
1897
danc3a20c12018-05-23 20:55:37 +00001898 sqlite3VdbeAddOp1(v, OP_ResetSorter, pMWin->iEphCsr);
1899 sqlite3VdbeAddOp3(
1900 v, OP_Copy, reg+pMWin->nBufferCol, pMWin->regPart, nPart+nPeer-1
1901 );
1902
dan79d45442018-05-26 21:17:29 +00001903 if( addrJump ) sqlite3VdbeJumpHere(v, addrJump);
danc3a20c12018-05-23 20:55:37 +00001904 }
1905
1906 /* Invoke step function for window functions */
dandfa552f2018-06-02 21:04:28 +00001907 windowAggStep(pParse, pMWin, -1, 0, reg, 0);
danc3a20c12018-05-23 20:55:37 +00001908
1909 /* Buffer the current row in the ephemeral table. */
1910 if( pMWin->nBufferCol>0 ){
1911 sqlite3VdbeAddOp3(v, OP_MakeRecord, reg, pMWin->nBufferCol, regRecord);
1912 }else{
1913 sqlite3VdbeAddOp2(v, OP_Blob, 0, regRecord);
1914 sqlite3VdbeAppendP4(v, (void*)"", 0);
1915 }
1916 sqlite3VdbeAddOp2(v, OP_NewRowid, pMWin->iEphCsr, regRowid);
1917 sqlite3VdbeAddOp3(v, OP_Insert, pMWin->iEphCsr, regRecord, regRowid);
1918
1919 /* End the database scan loop. */
1920 sqlite3WhereEnd(pWInfo);
1921
dand6f784e2018-05-28 18:30:45 +00001922 windowAggFinal(pParse, pMWin, 1);
dandacf1de2018-06-08 16:11:55 +00001923 sqlite3VdbeAddOp2(v, OP_Rewind, pMWin->iEphCsr,sqlite3VdbeCurrentAddr(v)+3);
danc3a20c12018-05-23 20:55:37 +00001924 sqlite3VdbeAddOp2(v, OP_Gosub, regGosub, addrGosub);
dandacf1de2018-06-08 16:11:55 +00001925 sqlite3VdbeAddOp2(v, OP_Next, pMWin->iEphCsr, sqlite3VdbeCurrentAddr(v)-1);
danc3a20c12018-05-23 20:55:37 +00001926}
1927
dan13078ca2018-06-13 20:29:38 +00001928/*
1929** Allocate and return a duplicate of the Window object indicated by the
1930** third argument. Set the Window.pOwner field of the new object to
1931** pOwner.
1932*/
dan2a11bb22018-06-11 20:50:25 +00001933Window *sqlite3WindowDup(sqlite3 *db, Expr *pOwner, Window *p){
dandacf1de2018-06-08 16:11:55 +00001934 Window *pNew = 0;
1935 if( p ){
1936 pNew = sqlite3DbMallocZero(db, sizeof(Window));
1937 if( pNew ){
1938 pNew->pFilter = sqlite3ExprDup(db, p->pFilter, 0);
1939 pNew->pPartition = sqlite3ExprListDup(db, p->pPartition, 0);
1940 pNew->pOrderBy = sqlite3ExprListDup(db, p->pOrderBy, 0);
1941 pNew->eType = p->eType;
1942 pNew->eEnd = p->eEnd;
1943 pNew->eStart = p->eStart;
dan303451a2018-06-14 20:52:08 +00001944 pNew->pStart = sqlite3ExprDup(db, p->pStart, 0);
1945 pNew->pEnd = sqlite3ExprDup(db, p->pEnd, 0);
dan2a11bb22018-06-11 20:50:25 +00001946 pNew->pOwner = pOwner;
dandacf1de2018-06-08 16:11:55 +00001947 }
1948 }
1949 return pNew;
1950}
danc3a20c12018-05-23 20:55:37 +00001951
danf9eae182018-05-21 19:45:11 +00001952/*
dan2a11bb22018-06-11 20:50:25 +00001953** sqlite3WhereBegin() has already been called for the SELECT statement
1954** passed as the second argument when this function is invoked. It generates
1955** code to populate the Window.regResult register for each window function and
1956** invoke the sub-routine at instruction addrGosub once for each row.
1957** This function calls sqlite3WhereEnd() before returning.
danf9eae182018-05-21 19:45:11 +00001958*/
1959void sqlite3WindowCodeStep(
dan2a11bb22018-06-11 20:50:25 +00001960 Parse *pParse, /* Parse context */
1961 Select *p, /* Rewritten SELECT statement */
1962 WhereInfo *pWInfo, /* Context returned by sqlite3WhereBegin() */
1963 int regGosub, /* Register for OP_Gosub */
1964 int addrGosub /* OP_Gosub here to return each row */
danf9eae182018-05-21 19:45:11 +00001965){
danf9eae182018-05-21 19:45:11 +00001966 Window *pMWin = p->pWin;
danf9eae182018-05-21 19:45:11 +00001967
dan54a9ab32018-06-14 14:27:05 +00001968 /* There are three different functions that may be used to do the work
1969 ** of this one, depending on the window frame and the specific built-in
1970 ** window functions used (if any).
1971 **
1972 ** windowCodeRowExprStep() handles all "ROWS" window frames, except for:
dan26522d12018-06-11 18:16:51 +00001973 **
dan13078ca2018-06-13 20:29:38 +00001974 ** ROWS BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW
dan54a9ab32018-06-14 14:27:05 +00001975 **
1976 ** The exception is because windowCodeRowExprStep() implements all window
1977 ** frame types by caching the entire partition in a temp table, and
1978 ** "ROWS BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW" is easy enough to
1979 ** implement without such a cache.
1980 **
1981 ** windowCodeCacheStep() is used for:
1982 **
1983 ** RANGE BETWEEN CURRENT ROW AND UNBOUNDED FOLLOWING
1984 **
1985 ** It is also used for anything not handled by windowCodeRowExprStep()
1986 ** that invokes a built-in window function that requires the entire
1987 ** partition to be cached in a temp table before any rows are returned
1988 ** (e.g. nth_value() or percent_rank()).
1989 **
1990 ** Finally, assuming there is no built-in window function that requires
1991 ** the partition to be cached, windowCodeDefaultStep() is used for:
1992 **
1993 ** RANGE BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW
1994 ** RANGE BETWEEN UNBOUNDED PRECEDING AND UNBOUNDED FOLLOWING
1995 ** RANGE BETWEEN CURRENT ROW AND CURRENT ROW
1996 ** ROWS BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW
1997 **
1998 ** windowCodeDefaultStep() is the only one of the three functions that
1999 ** does not cache each partition in a temp table before beginning to
2000 ** return rows.
dan26522d12018-06-11 18:16:51 +00002001 */
dan54a9ab32018-06-14 14:27:05 +00002002 if( pMWin->eType==TK_ROWS
2003 && (pMWin->eStart!=TK_UNBOUNDED||pMWin->eEnd!=TK_CURRENT||!pMWin->pOrderBy)
dan09590aa2018-05-25 20:30:17 +00002004 ){
danc3a20c12018-05-23 20:55:37 +00002005 windowCodeRowExprStep(pParse, p, pWInfo, regGosub, addrGosub);
dan13078ca2018-06-13 20:29:38 +00002006 }else{
2007 Window *pWin;
dan54a9ab32018-06-14 14:27:05 +00002008 int bCache = 0; /* True to use CacheStep() */
danf9eae182018-05-21 19:45:11 +00002009
dan54a9ab32018-06-14 14:27:05 +00002010 if( pMWin->eStart==TK_CURRENT && pMWin->eEnd==TK_UNBOUNDED ){
dan13078ca2018-06-13 20:29:38 +00002011 bCache = 1;
2012 }else{
dan13078ca2018-06-13 20:29:38 +00002013 for(pWin=pMWin; pWin; pWin=pWin->pNextWin){
2014 FuncDef *pFunc = pWin->pFunc;
2015 if( (pFunc->funcFlags & SQLITE_FUNC_WINDOW_SIZE)
dan54a9ab32018-06-14 14:27:05 +00002016 || (pFunc->xSFunc==nth_valueStepFunc)
2017 || (pFunc->xSFunc==first_valueStepFunc)
2018 || (pFunc->xSFunc==leadStepFunc)
2019 || (pFunc->xSFunc==lagStepFunc)
2020 ){
dan13078ca2018-06-13 20:29:38 +00002021 bCache = 1;
2022 break;
2023 }
2024 }
2025 }
2026
2027 /* Otherwise, call windowCodeDefaultStep(). */
2028 if( bCache ){
dandfa552f2018-06-02 21:04:28 +00002029 windowCodeCacheStep(pParse, p, pWInfo, regGosub, addrGosub);
dan13078ca2018-06-13 20:29:38 +00002030 }else{
2031 windowCodeDefaultStep(pParse, p, pWInfo, regGosub, addrGosub);
dandfa552f2018-06-02 21:04:28 +00002032 }
danf690b572018-06-01 21:00:08 +00002033 }
danf9eae182018-05-21 19:45:11 +00002034}
2035