blob: 9d5cadcdd1db78b0ad27f93305ef4bc7769f9270 [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));
dan660af932018-06-18 16:55:22 +0000303 if( p && p->nTotal ){
danf1abe362018-06-04 08:22:09 +0000304 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){
danc95f38d2018-06-18 20:34:43 +0000506 if( pWin->zName && pWin->eType==0 ){
dane3bf6322018-06-08 20:58:27 +0000507 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;
danc95f38d2018-06-18 20:34:43 +0000521 pWin->eType = p->eType;
dane3bf6322018-06-08 20:58:27 +0000522 }
dandfa552f2018-06-02 21:04:28 +0000523 if( pFunc->funcFlags & SQLITE_FUNC_WINDOW ){
524 sqlite3 *db = pParse->db;
dan8b985602018-06-09 17:43:45 +0000525 if( pWin->pFilter ){
526 sqlite3ErrorMsg(pParse,
527 "FILTER clause may only be used with aggregate window functions"
528 );
529 }else
dan6bc5c9e2018-06-04 18:55:11 +0000530 if( pFunc->xSFunc==row_numberStepFunc || pFunc->xSFunc==ntileStepFunc ){
dandfa552f2018-06-02 21:04:28 +0000531 sqlite3ExprDelete(db, pWin->pStart);
532 sqlite3ExprDelete(db, pWin->pEnd);
533 pWin->pStart = pWin->pEnd = 0;
534 pWin->eType = TK_ROWS;
535 pWin->eStart = TK_UNBOUNDED;
536 pWin->eEnd = TK_CURRENT;
dan8b985602018-06-09 17:43:45 +0000537 }else
dandfa552f2018-06-02 21:04:28 +0000538
539 if( pFunc->xSFunc==dense_rankStepFunc || pFunc->xSFunc==rankStepFunc
danf1abe362018-06-04 08:22:09 +0000540 || pFunc->xSFunc==percent_rankStepFunc || pFunc->xSFunc==cume_distStepFunc
dandfa552f2018-06-02 21:04:28 +0000541 ){
542 sqlite3ExprDelete(db, pWin->pStart);
543 sqlite3ExprDelete(db, pWin->pEnd);
544 pWin->pStart = pWin->pEnd = 0;
545 pWin->eType = TK_RANGE;
546 pWin->eStart = TK_UNBOUNDED;
547 pWin->eEnd = TK_CURRENT;
548 }
549 }
dan2a11bb22018-06-11 20:50:25 +0000550 pWin->pFunc = pFunc;
dandfa552f2018-06-02 21:04:28 +0000551}
552
danc0bb4452018-06-12 20:53:38 +0000553/*
554** Context object passed through sqlite3WalkExprList() to
555** selectWindowRewriteExprCb() by selectWindowRewriteEList().
556*/
dandfa552f2018-06-02 21:04:28 +0000557typedef struct WindowRewrite WindowRewrite;
558struct WindowRewrite {
559 Window *pWin;
560 ExprList *pSub;
561};
562
danc0bb4452018-06-12 20:53:38 +0000563/*
564** Callback function used by selectWindowRewriteEList(). If necessary,
565** this function appends to the output expression-list and updates
566** expression (*ppExpr) in place.
567*/
dandfa552f2018-06-02 21:04:28 +0000568static int selectWindowRewriteExprCb(Walker *pWalker, Expr *pExpr){
569 struct WindowRewrite *p = pWalker->u.pRewrite;
570 Parse *pParse = pWalker->pParse;
571
572 switch( pExpr->op ){
573
574 case TK_FUNCTION:
575 if( pExpr->pWin==0 ){
576 break;
577 }else{
578 Window *pWin;
579 for(pWin=p->pWin; pWin; pWin=pWin->pNextWin){
580 if( pExpr->pWin==pWin ){
dan2a11bb22018-06-11 20:50:25 +0000581 assert( pWin->pOwner==pExpr );
dandfa552f2018-06-02 21:04:28 +0000582 return WRC_Prune;
583 }
584 }
585 }
586 /* Fall through. */
587
dan73925692018-06-12 18:40:17 +0000588 case TK_AGG_FUNCTION:
dandfa552f2018-06-02 21:04:28 +0000589 case TK_COLUMN: {
590 Expr *pDup = sqlite3ExprDup(pParse->db, pExpr, 0);
591 p->pSub = sqlite3ExprListAppend(pParse, p->pSub, pDup);
592 if( p->pSub ){
593 assert( ExprHasProperty(pExpr, EP_Static)==0 );
594 ExprSetProperty(pExpr, EP_Static);
595 sqlite3ExprDelete(pParse->db, pExpr);
596 ExprClearProperty(pExpr, EP_Static);
597 memset(pExpr, 0, sizeof(Expr));
598
599 pExpr->op = TK_COLUMN;
600 pExpr->iColumn = p->pSub->nExpr-1;
601 pExpr->iTable = p->pWin->iEphCsr;
602 }
603
604 break;
605 }
606
607 default: /* no-op */
608 break;
609 }
610
611 return WRC_Continue;
612}
danc0bb4452018-06-12 20:53:38 +0000613static int selectWindowRewriteSelectCb(Walker *pWalker, Select *pSelect){
614 return WRC_Prune;
615}
dandfa552f2018-06-02 21:04:28 +0000616
danc0bb4452018-06-12 20:53:38 +0000617
618/*
619** Iterate through each expression in expression-list pEList. For each:
620**
621** * TK_COLUMN,
622** * aggregate function, or
623** * window function with a Window object that is not a member of the
624** linked list passed as the second argument (pWin)
625**
626** Append the node to output expression-list (*ppSub). And replace it
627** with a TK_COLUMN that reads the (N-1)th element of table
628** pWin->iEphCsr, where N is the number of elements in (*ppSub) after
629** appending the new one.
630*/
dan13b08bb2018-06-15 20:46:12 +0000631static void selectWindowRewriteEList(
dandfa552f2018-06-02 21:04:28 +0000632 Parse *pParse,
633 Window *pWin,
634 ExprList *pEList, /* Rewrite expressions in this list */
635 ExprList **ppSub /* IN/OUT: Sub-select expression-list */
636){
637 Walker sWalker;
638 WindowRewrite sRewrite;
dandfa552f2018-06-02 21:04:28 +0000639
640 memset(&sWalker, 0, sizeof(Walker));
641 memset(&sRewrite, 0, sizeof(WindowRewrite));
642
643 sRewrite.pSub = *ppSub;
644 sRewrite.pWin = pWin;
645
646 sWalker.pParse = pParse;
647 sWalker.xExprCallback = selectWindowRewriteExprCb;
648 sWalker.xSelectCallback = selectWindowRewriteSelectCb;
649 sWalker.u.pRewrite = &sRewrite;
650
dan13b08bb2018-06-15 20:46:12 +0000651 (void)sqlite3WalkExprList(&sWalker, pEList);
dandfa552f2018-06-02 21:04:28 +0000652
653 *ppSub = sRewrite.pSub;
dandfa552f2018-06-02 21:04:28 +0000654}
655
danc0bb4452018-06-12 20:53:38 +0000656/*
657** Append a copy of each expression in expression-list pAppend to
658** expression list pList. Return a pointer to the result list.
659*/
dandfa552f2018-06-02 21:04:28 +0000660static ExprList *exprListAppendList(
661 Parse *pParse, /* Parsing context */
662 ExprList *pList, /* List to which to append. Might be NULL */
663 ExprList *pAppend /* List of values to append. Might be NULL */
664){
665 if( pAppend ){
666 int i;
667 int nInit = pList ? pList->nExpr : 0;
668 for(i=0; i<pAppend->nExpr; i++){
669 Expr *pDup = sqlite3ExprDup(pParse->db, pAppend->a[i].pExpr, 0);
670 pList = sqlite3ExprListAppend(pParse, pList, pDup);
671 if( pList ) pList->a[nInit+i].sortOrder = pAppend->a[i].sortOrder;
672 }
673 }
674 return pList;
675}
676
677/*
678** If the SELECT statement passed as the second argument does not invoke
679** any SQL window functions, this function is a no-op. Otherwise, it
680** rewrites the SELECT statement so that window function xStep functions
danc0bb4452018-06-12 20:53:38 +0000681** are invoked in the correct order as described under "SELECT REWRITING"
682** at the top of this file.
dandfa552f2018-06-02 21:04:28 +0000683*/
684int sqlite3WindowRewrite(Parse *pParse, Select *p){
685 int rc = SQLITE_OK;
686 if( p->pWin ){
687 Vdbe *v = sqlite3GetVdbe(pParse);
dandfa552f2018-06-02 21:04:28 +0000688 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 ){
dandfa552f2018-06-02 21:04:28 +0000746 p->pSrc->a[0].pSelect = pSub;
747 sqlite3SrcListAssignCursors(pParse, p->pSrc);
748 if( sqlite3ExpandSubquery(pParse, &p->pSrc->a[0]) ){
749 rc = SQLITE_NOMEM;
750 }else{
751 pSub->selFlags |= SF_Expanded;
dan73925692018-06-12 18:40:17 +0000752 p->selFlags &= ~SF_Aggregate;
753 sqlite3SelectPrep(pParse, pSub, 0);
dandfa552f2018-06-02 21:04:28 +0000754 }
dandfa552f2018-06-02 21:04:28 +0000755
dan6fde1792018-06-15 19:01:35 +0000756 sqlite3VdbeAddOp2(v, OP_OpenEphemeral, pMWin->iEphCsr, pSublist->nExpr);
757 }else{
758 sqlite3SelectDelete(db, pSub);
759 }
760 if( db->mallocFailed ) rc = SQLITE_NOMEM;
dandfa552f2018-06-02 21:04:28 +0000761 }
762
763 return rc;
764}
765
danc0bb4452018-06-12 20:53:38 +0000766/*
767** Free the Window object passed as the second argument.
768*/
dan86fb6e12018-05-16 20:58:07 +0000769void sqlite3WindowDelete(sqlite3 *db, Window *p){
770 if( p ){
771 sqlite3ExprDelete(db, p->pFilter);
772 sqlite3ExprListDelete(db, p->pPartition);
773 sqlite3ExprListDelete(db, p->pOrderBy);
774 sqlite3ExprDelete(db, p->pEnd);
775 sqlite3ExprDelete(db, p->pStart);
dane3bf6322018-06-08 20:58:27 +0000776 sqlite3DbFree(db, p->zName);
dan86fb6e12018-05-16 20:58:07 +0000777 sqlite3DbFree(db, p);
778 }
779}
780
danc0bb4452018-06-12 20:53:38 +0000781/*
782** Free the linked list of Window objects starting at the second argument.
783*/
dane3bf6322018-06-08 20:58:27 +0000784void sqlite3WindowListDelete(sqlite3 *db, Window *p){
785 while( p ){
786 Window *pNext = p->pNextWin;
787 sqlite3WindowDelete(db, p);
788 p = pNext;
789 }
790}
791
danc0bb4452018-06-12 20:53:38 +0000792/*
793** Allocate and return a new Window object.
794*/
dan86fb6e12018-05-16 20:58:07 +0000795Window *sqlite3WindowAlloc(
796 Parse *pParse,
797 int eType,
danc3a20c12018-05-23 20:55:37 +0000798 int eStart, Expr *pStart,
799 int eEnd, Expr *pEnd
dan86fb6e12018-05-16 20:58:07 +0000800){
801 Window *pWin = (Window*)sqlite3DbMallocZero(pParse->db, sizeof(Window));
802
803 if( pWin ){
danc95f38d2018-06-18 20:34:43 +0000804 assert( eType );
dan86fb6e12018-05-16 20:58:07 +0000805 pWin->eType = eType;
806 pWin->eStart = eStart;
807 pWin->eEnd = eEnd;
808 pWin->pEnd = pEnd;
809 pWin->pStart = pStart;
810 }else{
811 sqlite3ExprDelete(pParse->db, pEnd);
812 sqlite3ExprDelete(pParse->db, pStart);
813 }
814
815 return pWin;
816}
817
danc0bb4452018-06-12 20:53:38 +0000818/*
819** Attach window object pWin to expression p.
820*/
dan86fb6e12018-05-16 20:58:07 +0000821void sqlite3WindowAttach(Parse *pParse, Expr *p, Window *pWin){
822 if( p ){
823 p->pWin = pWin;
dan2a11bb22018-06-11 20:50:25 +0000824 if( pWin ) pWin->pOwner = p;
dan86fb6e12018-05-16 20:58:07 +0000825 }else{
826 sqlite3WindowDelete(pParse->db, pWin);
827 }
828}
dane2f781b2018-05-17 19:24:08 +0000829
830/*
831** Return 0 if the two window objects are identical, or non-zero otherwise.
dan13078ca2018-06-13 20:29:38 +0000832** Identical window objects can be processed in a single scan.
dane2f781b2018-05-17 19:24:08 +0000833*/
834int sqlite3WindowCompare(Parse *pParse, Window *p1, Window *p2){
835 if( p1->eType!=p2->eType ) return 1;
836 if( p1->eStart!=p2->eStart ) return 1;
837 if( p1->eEnd!=p2->eEnd ) return 1;
838 if( sqlite3ExprCompare(pParse, p1->pStart, p2->pStart, -1) ) return 1;
839 if( sqlite3ExprCompare(pParse, p1->pEnd, p2->pEnd, -1) ) return 1;
840 if( sqlite3ExprListCompare(p1->pPartition, p2->pPartition, -1) ) return 1;
841 if( sqlite3ExprListCompare(p1->pOrderBy, p2->pOrderBy, -1) ) return 1;
842 return 0;
843}
844
dan13078ca2018-06-13 20:29:38 +0000845
846/*
847** This is called by code in select.c before it calls sqlite3WhereBegin()
848** to begin iterating through the sub-query results. It is used to allocate
849** and initialize registers and cursors used by sqlite3WindowCodeStep().
850*/
851void sqlite3WindowCodeInit(Parse *pParse, Window *pMWin){
danc9a86682018-05-30 20:44:58 +0000852 Window *pWin;
dan13078ca2018-06-13 20:29:38 +0000853 Vdbe *v = sqlite3GetVdbe(pParse);
854 int nPart = (pMWin->pPartition ? pMWin->pPartition->nExpr : 0);
855 nPart += (pMWin->pOrderBy ? pMWin->pOrderBy->nExpr : 0);
856 if( nPart ){
857 pMWin->regPart = pParse->nMem+1;
858 pParse->nMem += nPart;
859 sqlite3VdbeAddOp3(v, OP_Null, 0, pMWin->regPart, pMWin->regPart+nPart-1);
860 }
861
danc9a86682018-05-30 20:44:58 +0000862 for(pWin=pMWin; pWin; pWin=pWin->pNextWin){
danec891fd2018-06-06 20:51:02 +0000863 FuncDef *p = pWin->pFunc;
864 if( (p->funcFlags & SQLITE_FUNC_MINMAX) && pWin->eStart!=TK_UNBOUNDED ){
dan9a947222018-06-14 19:06:36 +0000865 /* The inline versions of min() and max() require a single ephemeral
866 ** table and 3 registers. The registers are used as follows:
867 **
868 ** regApp+0: slot to copy min()/max() argument to for MakeRecord
869 ** regApp+1: integer value used to ensure keys are unique
870 ** regApp+2: output of MakeRecord
871 */
danc9a86682018-05-30 20:44:58 +0000872 ExprList *pList = pWin->pOwner->x.pList;
873 KeyInfo *pKeyInfo = sqlite3KeyInfoFromExprList(pParse, pList, 0, 0);
danc9a86682018-05-30 20:44:58 +0000874 pWin->csrApp = pParse->nTab++;
875 pWin->regApp = pParse->nMem+1;
876 pParse->nMem += 3;
877 if( pKeyInfo && pWin->pFunc->zName[1]=='i' ){
878 assert( pKeyInfo->aSortOrder[0]==0 );
879 pKeyInfo->aSortOrder[0] = 1;
880 }
881 sqlite3VdbeAddOp2(v, OP_OpenEphemeral, pWin->csrApp, 2);
882 sqlite3VdbeAppendP4(v, pKeyInfo, P4_KEYINFO);
883 sqlite3VdbeAddOp2(v, OP_Integer, 0, pWin->regApp+1);
884 }
dan7095c002018-06-07 17:45:22 +0000885 else if( p->xSFunc==nth_valueStepFunc || p->xSFunc==first_valueStepFunc ){
danec891fd2018-06-06 20:51:02 +0000886 /* Allocate two registers at pWin->regApp. These will be used to
887 ** store the start and end index of the current frame. */
888 assert( pMWin->iEphCsr );
889 pWin->regApp = pParse->nMem+1;
890 pWin->csrApp = pParse->nTab++;
891 pParse->nMem += 2;
danec891fd2018-06-06 20:51:02 +0000892 sqlite3VdbeAddOp2(v, OP_OpenDup, pWin->csrApp, pMWin->iEphCsr);
893 }
danfe4e25a2018-06-07 20:08:59 +0000894 else if( p->xSFunc==leadStepFunc || p->xSFunc==lagStepFunc ){
895 assert( pMWin->iEphCsr );
896 pWin->csrApp = pParse->nTab++;
897 sqlite3VdbeAddOp2(v, OP_OpenDup, pWin->csrApp, pMWin->iEphCsr);
898 }
danc9a86682018-05-30 20:44:58 +0000899 }
900}
901
dan13078ca2018-06-13 20:29:38 +0000902/*
903** A "PRECEDING <expr>" (bEnd==0) or "FOLLOWING <expr>" (bEnd==1) has just
904** been evaluated and the result left in register reg. This function generates
905** VM code to check that the value is a non-negative integer and throws
906** an exception if it is not.
907*/
danc3a20c12018-05-23 20:55:37 +0000908static void windowCheckFrameValue(Parse *pParse, int reg, int bEnd){
909 static const char *azErr[] = {
910 "frame starting offset must be a non-negative integer",
911 "frame ending offset must be a non-negative integer"
912 };
913 Vdbe *v = sqlite3GetVdbe(pParse);
dan13078ca2018-06-13 20:29:38 +0000914 int regZero = sqlite3GetTempReg(pParse);
danc3a20c12018-05-23 20:55:37 +0000915 sqlite3VdbeAddOp2(v, OP_Integer, 0, regZero);
916 sqlite3VdbeAddOp2(v, OP_MustBeInt, reg, sqlite3VdbeCurrentAddr(v)+2);
917 sqlite3VdbeAddOp3(v, OP_Ge, regZero, sqlite3VdbeCurrentAddr(v)+2, reg);
918 sqlite3VdbeAddOp2(v, OP_Halt, SQLITE_ERROR, OE_Abort);
919 sqlite3VdbeAppendP4(v, (void*)azErr[bEnd], P4_STATIC);
dan13078ca2018-06-13 20:29:38 +0000920 sqlite3ReleaseTempReg(pParse, regZero);
danc3a20c12018-05-23 20:55:37 +0000921}
922
dan13078ca2018-06-13 20:29:38 +0000923/*
924** Return the number of arguments passed to the window-function associated
925** with the object passed as the only argument to this function.
926*/
dan2a11bb22018-06-11 20:50:25 +0000927static int windowArgCount(Window *pWin){
928 ExprList *pList = pWin->pOwner->x.pList;
929 return (pList ? pList->nExpr : 0);
930}
931
danc9a86682018-05-30 20:44:58 +0000932/*
933** Generate VM code to invoke either xStep() (if bInverse is 0) or
934** xInverse (if bInverse is non-zero) for each window function in the
dan13078ca2018-06-13 20:29:38 +0000935** linked list starting at pMWin. Or, for built-in window functions
936** that do not use the standard function API, generate the required
937** inline VM code.
938**
939** If argument csr is greater than or equal to 0, then argument reg is
940** the first register in an array of registers guaranteed to be large
941** enough to hold the array of arguments for each function. In this case
942** the arguments are extracted from the current row of csr into the
943** array of registers before invoking OP_AggStep.
944**
945** Or, if csr is less than zero, then the array of registers at reg is
946** already populated with all columns from the current row of the sub-query.
947**
948** If argument regPartSize is non-zero, then it is a register containing the
949** number of rows in the current partition.
danc9a86682018-05-30 20:44:58 +0000950*/
dan31f56392018-05-24 21:10:57 +0000951static void windowAggStep(
952 Parse *pParse,
dan13078ca2018-06-13 20:29:38 +0000953 Window *pMWin, /* Linked list of window functions */
954 int csr, /* Read arguments from this cursor */
955 int bInverse, /* True to invoke xInverse instead of xStep */
956 int reg, /* Array of registers */
dandfa552f2018-06-02 21:04:28 +0000957 int regPartSize /* Register containing size of partition */
dan31f56392018-05-24 21:10:57 +0000958){
959 Vdbe *v = sqlite3GetVdbe(pParse);
960 Window *pWin;
961 for(pWin=pMWin; pWin; pWin=pWin->pNextWin){
dandfa552f2018-06-02 21:04:28 +0000962 int flags = pWin->pFunc->funcFlags;
danc9a86682018-05-30 20:44:58 +0000963 int regArg;
dan2a11bb22018-06-11 20:50:25 +0000964 int nArg = windowArgCount(pWin);
dandfa552f2018-06-02 21:04:28 +0000965
dan6bc5c9e2018-06-04 18:55:11 +0000966 if( csr>=0 ){
danc9a86682018-05-30 20:44:58 +0000967 int i;
dan2a11bb22018-06-11 20:50:25 +0000968 for(i=0; i<nArg; i++){
danc9a86682018-05-30 20:44:58 +0000969 sqlite3VdbeAddOp3(v, OP_Column, csr, pWin->iArgCol+i, reg+i);
970 }
971 regArg = reg;
dan6bc5c9e2018-06-04 18:55:11 +0000972 if( flags & SQLITE_FUNC_WINDOW_SIZE ){
973 if( nArg==0 ){
974 regArg = regPartSize;
975 }else{
976 sqlite3VdbeAddOp2(v, OP_SCopy, regPartSize, reg+nArg);
977 }
978 nArg++;
979 }
danc9a86682018-05-30 20:44:58 +0000980 }else{
dan6bc5c9e2018-06-04 18:55:11 +0000981 assert( !(flags & SQLITE_FUNC_WINDOW_SIZE) );
danc9a86682018-05-30 20:44:58 +0000982 regArg = reg + pWin->iArgCol;
dan31f56392018-05-24 21:10:57 +0000983 }
danc9a86682018-05-30 20:44:58 +0000984
danec891fd2018-06-06 20:51:02 +0000985 if( (pWin->pFunc->funcFlags & SQLITE_FUNC_MINMAX)
986 && pWin->eStart!=TK_UNBOUNDED
987 ){
danc9a86682018-05-30 20:44:58 +0000988 if( bInverse==0 ){
989 sqlite3VdbeAddOp2(v, OP_AddImm, pWin->regApp+1, 1);
990 sqlite3VdbeAddOp2(v, OP_SCopy, regArg, pWin->regApp);
991 sqlite3VdbeAddOp3(v, OP_MakeRecord, pWin->regApp, 2, pWin->regApp+2);
992 sqlite3VdbeAddOp2(v, OP_IdxInsert, pWin->csrApp, pWin->regApp+2);
993 }else{
994 sqlite3VdbeAddOp4Int(v, OP_SeekGE, pWin->csrApp, 0, regArg, 1);
995 sqlite3VdbeAddOp1(v, OP_Delete, pWin->csrApp);
996 sqlite3VdbeJumpHere(v, sqlite3VdbeCurrentAddr(v)-2);
997 }
danec891fd2018-06-06 20:51:02 +0000998 }else if( pWin->regApp ){
dan7095c002018-06-07 17:45:22 +0000999 assert( pWin->pFunc->xSFunc==nth_valueStepFunc
1000 || pWin->pFunc->xSFunc==first_valueStepFunc
1001 );
danec891fd2018-06-06 20:51:02 +00001002 assert( bInverse==0 || bInverse==1 );
1003 sqlite3VdbeAddOp2(v, OP_AddImm, pWin->regApp+1-bInverse, 1);
danfe4e25a2018-06-07 20:08:59 +00001004 }else if( pWin->pFunc->xSFunc==leadStepFunc
1005 || pWin->pFunc->xSFunc==lagStepFunc
1006 ){
1007 /* no-op */
danc9a86682018-05-30 20:44:58 +00001008 }else{
dan8b985602018-06-09 17:43:45 +00001009 int addrIf = 0;
1010 if( pWin->pFilter ){
1011 int regTmp;
dan2a11bb22018-06-11 20:50:25 +00001012 assert( nArg==pWin->pOwner->x.pList->nExpr );
dan8b985602018-06-09 17:43:45 +00001013 if( csr>0 ){
1014 regTmp = sqlite3GetTempReg(pParse);
dan2a11bb22018-06-11 20:50:25 +00001015 sqlite3VdbeAddOp3(v, OP_Column, csr, pWin->iArgCol+nArg,regTmp);
dan8b985602018-06-09 17:43:45 +00001016 }else{
dan2a11bb22018-06-11 20:50:25 +00001017 regTmp = regArg + nArg;
dan8b985602018-06-09 17:43:45 +00001018 }
1019 addrIf = sqlite3VdbeAddOp3(v, OP_IfNot, regTmp, 0, 1);
1020 if( csr>0 ){
1021 sqlite3ReleaseTempReg(pParse, regTmp);
1022 }
1023 }
danc9a86682018-05-30 20:44:58 +00001024 if( pWin->pFunc->funcFlags & SQLITE_FUNC_NEEDCOLL ){
1025 CollSeq *pColl;
dan8b985602018-06-09 17:43:45 +00001026 pColl = sqlite3ExprNNCollSeq(pParse, pWin->pOwner->x.pList->a[0].pExpr);
danc9a86682018-05-30 20:44:58 +00001027 sqlite3VdbeAddOp4(v, OP_CollSeq, 0,0,0, (const char*)pColl, P4_COLLSEQ);
1028 }
1029 sqlite3VdbeAddOp3(v, OP_AggStep0, bInverse, regArg, pWin->regAccum);
1030 sqlite3VdbeAppendP4(v, pWin->pFunc, P4_FUNCDEF);
dandfa552f2018-06-02 21:04:28 +00001031 sqlite3VdbeChangeP5(v, (u8)nArg);
dan8b985602018-06-09 17:43:45 +00001032 if( addrIf ) sqlite3VdbeJumpHere(v, addrIf);
danc9a86682018-05-30 20:44:58 +00001033 }
dan31f56392018-05-24 21:10:57 +00001034 }
1035}
1036
dan13078ca2018-06-13 20:29:38 +00001037/*
1038** Generate VM code to invoke either xValue() (bFinal==0) or xFinalize()
1039** (bFinal==1) for each window function in the linked list starting at
1040** pMWin. Or, for built-in window-functions that do not use the standard
1041** API, generate the equivalent VM code.
1042*/
dand6f784e2018-05-28 18:30:45 +00001043static void windowAggFinal(Parse *pParse, Window *pMWin, int bFinal){
1044 Vdbe *v = sqlite3GetVdbe(pParse);
1045 Window *pWin;
1046
1047 for(pWin=pMWin; pWin; pWin=pWin->pNextWin){
danec891fd2018-06-06 20:51:02 +00001048 if( (pWin->pFunc->funcFlags & SQLITE_FUNC_MINMAX)
1049 && pWin->eStart!=TK_UNBOUNDED
1050 ){
dand6f784e2018-05-28 18:30:45 +00001051 sqlite3VdbeAddOp2(v, OP_Null, 0, pWin->regResult);
danc9a86682018-05-30 20:44:58 +00001052 sqlite3VdbeAddOp1(v, OP_Last, pWin->csrApp);
1053 sqlite3VdbeAddOp3(v, OP_Column, pWin->csrApp, 0, pWin->regResult);
1054 sqlite3VdbeJumpHere(v, sqlite3VdbeCurrentAddr(v)-2);
1055 if( bFinal ){
1056 sqlite3VdbeAddOp1(v, OP_ResetSorter, pWin->csrApp);
1057 }
danec891fd2018-06-06 20:51:02 +00001058 }else if( pWin->regApp ){
dand6f784e2018-05-28 18:30:45 +00001059 }else{
danc9a86682018-05-30 20:44:58 +00001060 if( bFinal==0 ){
1061 sqlite3VdbeAddOp2(v, OP_Null, 0, pWin->regResult);
1062 }
dan2a11bb22018-06-11 20:50:25 +00001063 sqlite3VdbeAddOp2(v, OP_AggFinal, pWin->regAccum, windowArgCount(pWin));
danc9a86682018-05-30 20:44:58 +00001064 sqlite3VdbeAppendP4(v, pWin->pFunc, P4_FUNCDEF);
1065 if( bFinal ){
1066 sqlite3VdbeAddOp2(v, OP_Copy, pWin->regAccum, pWin->regResult);
dan8b985602018-06-09 17:43:45 +00001067 sqlite3VdbeAddOp2(v, OP_Null, 0, pWin->regAccum);
danc9a86682018-05-30 20:44:58 +00001068 }else{
1069 sqlite3VdbeChangeP3(v, -1, pWin->regResult);
1070 }
dand6f784e2018-05-28 18:30:45 +00001071 }
1072 }
1073}
1074
dan13078ca2018-06-13 20:29:38 +00001075/*
1076** This function generates VM code to invoke the sub-routine at address
1077** lblFlushPart once for each partition with the entire partition cached in
1078** the Window.iEphCsr temp table.
1079*/
danf690b572018-06-01 21:00:08 +00001080static void windowPartitionCache(
1081 Parse *pParse,
dan13078ca2018-06-13 20:29:38 +00001082 Select *p, /* The rewritten SELECT statement */
1083 WhereInfo *pWInfo, /* WhereInfo to call WhereEnd() on */
1084 int regFlushPart, /* Register to use with Gosub lblFlushPart */
1085 int lblFlushPart, /* Subroutine to Gosub to */
1086 int *pRegSize /* OUT: Register containing partition size */
danf690b572018-06-01 21:00:08 +00001087){
1088 Window *pMWin = p->pWin;
1089 Vdbe *v = sqlite3GetVdbe(pParse);
danf690b572018-06-01 21:00:08 +00001090 int iSubCsr = p->pSrc->a[0].iCursor;
1091 int nSub = p->pSrc->a[0].pTab->nCol;
1092 int k;
1093
1094 int reg = pParse->nMem+1;
1095 int regRecord = reg+nSub;
1096 int regRowid = regRecord+1;
1097
dandfa552f2018-06-02 21:04:28 +00001098 *pRegSize = regRowid;
danf690b572018-06-01 21:00:08 +00001099 pParse->nMem += nSub + 2;
1100
1101 /* Martial the row returned by the sub-select into an array of
1102 ** registers. */
1103 for(k=0; k<nSub; k++){
1104 sqlite3VdbeAddOp3(v, OP_Column, iSubCsr, k, reg+k);
1105 }
1106 sqlite3VdbeAddOp3(v, OP_MakeRecord, reg, nSub, regRecord);
1107
1108 /* Check if this is the start of a new partition. If so, call the
1109 ** flush_partition sub-routine. */
1110 if( pMWin->pPartition ){
1111 int addr;
1112 ExprList *pPart = pMWin->pPartition;
dane0a5e202018-06-15 16:10:44 +00001113 int nPart = pPart->nExpr;
danf690b572018-06-01 21:00:08 +00001114 int regNewPart = reg + pMWin->nBufferCol;
1115 KeyInfo *pKeyInfo = sqlite3KeyInfoFromExprList(pParse, pPart, 0, 0);
1116
1117 addr = sqlite3VdbeAddOp3(v, OP_Compare, regNewPart, pMWin->regPart,nPart);
1118 sqlite3VdbeAppendP4(v, (void*)pKeyInfo, P4_KEYINFO);
1119 sqlite3VdbeAddOp3(v, OP_Jump, addr+2, addr+4, addr+2);
1120 sqlite3VdbeAddOp3(v, OP_Copy, regNewPart, pMWin->regPart, nPart-1);
1121 sqlite3VdbeAddOp2(v, OP_Gosub, regFlushPart, lblFlushPart);
1122 }
1123
1124 /* Buffer the current row in the ephemeral table. */
1125 sqlite3VdbeAddOp2(v, OP_NewRowid, pMWin->iEphCsr, regRowid);
1126 sqlite3VdbeAddOp3(v, OP_Insert, pMWin->iEphCsr, regRecord, regRowid);
1127
1128 /* End of the input loop */
1129 sqlite3WhereEnd(pWInfo);
1130
1131 /* Invoke "flush_partition" to deal with the final (or only) partition */
1132 sqlite3VdbeAddOp2(v, OP_Gosub, regFlushPart, lblFlushPart);
1133}
dand6f784e2018-05-28 18:30:45 +00001134
dan13078ca2018-06-13 20:29:38 +00001135/*
1136** Invoke the sub-routine at regGosub (generated by code in select.c) to
1137** return the current row of Window.iEphCsr. If all window functions are
1138** aggregate window functions that use the standard API, a single
1139** OP_Gosub instruction is all that this routine generates. Extra VM code
1140** for per-row processing is only generated for the following built-in window
1141** functions:
1142**
1143** nth_value()
1144** first_value()
1145** lag()
1146** lead()
1147*/
danec891fd2018-06-06 20:51:02 +00001148static void windowReturnOneRow(
1149 Parse *pParse,
1150 Window *pMWin,
1151 int regGosub,
1152 int addrGosub
1153){
1154 Vdbe *v = sqlite3GetVdbe(pParse);
1155 Window *pWin;
1156 for(pWin=pMWin; pWin; pWin=pWin->pNextWin){
1157 FuncDef *pFunc = pWin->pFunc;
dan7095c002018-06-07 17:45:22 +00001158 if( pFunc->xSFunc==nth_valueStepFunc
1159 || pFunc->xSFunc==first_valueStepFunc
1160 ){
danec891fd2018-06-06 20:51:02 +00001161 int csr = pWin->csrApp;
1162 int lbl = sqlite3VdbeMakeLabel(v);
1163 int tmpReg = sqlite3GetTempReg(pParse);
1164 sqlite3VdbeAddOp2(v, OP_Null, 0, pWin->regResult);
dan7095c002018-06-07 17:45:22 +00001165
1166 if( pFunc->xSFunc==nth_valueStepFunc ){
dan6fde1792018-06-15 19:01:35 +00001167 sqlite3VdbeAddOp3(v, OP_Column, pMWin->iEphCsr, pWin->iArgCol+1,tmpReg);
dan7095c002018-06-07 17:45:22 +00001168 }else{
1169 sqlite3VdbeAddOp2(v, OP_Integer, 1, tmpReg);
1170 }
danec891fd2018-06-06 20:51:02 +00001171 sqlite3VdbeAddOp3(v, OP_Add, tmpReg, pWin->regApp, tmpReg);
1172 sqlite3VdbeAddOp3(v, OP_Gt, pWin->regApp+1, lbl, tmpReg);
1173 sqlite3VdbeAddOp3(v, OP_SeekRowid, csr, lbl, tmpReg);
1174 sqlite3VdbeAddOp3(v, OP_Column, csr, pWin->iArgCol, pWin->regResult);
1175 sqlite3VdbeResolveLabel(v, lbl);
1176 sqlite3ReleaseTempReg(pParse, tmpReg);
1177 }
danfe4e25a2018-06-07 20:08:59 +00001178 else if( pFunc->xSFunc==leadStepFunc || pFunc->xSFunc==lagStepFunc ){
dan2a11bb22018-06-11 20:50:25 +00001179 int nArg = pWin->pOwner->x.pList->nExpr;
dane0a5e202018-06-15 16:10:44 +00001180 int iEph = pMWin->iEphCsr;
danfe4e25a2018-06-07 20:08:59 +00001181 int csr = pWin->csrApp;
1182 int lbl = sqlite3VdbeMakeLabel(v);
1183 int tmpReg = sqlite3GetTempReg(pParse);
1184
dan2a11bb22018-06-11 20:50:25 +00001185 if( nArg<3 ){
danfe4e25a2018-06-07 20:08:59 +00001186 sqlite3VdbeAddOp2(v, OP_Null, 0, pWin->regResult);
1187 }else{
1188 sqlite3VdbeAddOp3(v, OP_Column, iEph, pWin->iArgCol+2, pWin->regResult);
1189 }
1190 sqlite3VdbeAddOp2(v, OP_Rowid, iEph, tmpReg);
dan2a11bb22018-06-11 20:50:25 +00001191 if( nArg<2 ){
danfe4e25a2018-06-07 20:08:59 +00001192 int val = (pFunc->xSFunc==leadStepFunc ? 1 : -1);
1193 sqlite3VdbeAddOp2(v, OP_AddImm, tmpReg, val);
1194 }else{
1195 int op = (pFunc->xSFunc==leadStepFunc ? OP_Add : OP_Subtract);
1196 int tmpReg2 = sqlite3GetTempReg(pParse);
1197 sqlite3VdbeAddOp3(v, OP_Column, iEph, pWin->iArgCol+1, tmpReg2);
1198 sqlite3VdbeAddOp3(v, op, tmpReg2, tmpReg, tmpReg);
1199 sqlite3ReleaseTempReg(pParse, tmpReg2);
1200 }
1201
1202 sqlite3VdbeAddOp3(v, OP_SeekRowid, csr, lbl, tmpReg);
1203 sqlite3VdbeAddOp3(v, OP_Column, csr, pWin->iArgCol, pWin->regResult);
1204 sqlite3VdbeResolveLabel(v, lbl);
1205 sqlite3ReleaseTempReg(pParse, tmpReg);
1206 }
danec891fd2018-06-06 20:51:02 +00001207 }
1208 sqlite3VdbeAddOp2(v, OP_Gosub, regGosub, addrGosub);
1209}
1210
dan13078ca2018-06-13 20:29:38 +00001211/*
1212** Invoke the code generated by windowReturnOneRow() and, optionally, the
1213** xInverse() function for each window function, for one or more rows
1214** from the Window.iEphCsr temp table. This routine generates VM code
1215** similar to:
1216**
1217** while( regCtr>0 ){
1218** regCtr--;
1219** windowReturnOneRow()
1220** if( bInverse ){
1221** AggStep (xInverse)
1222** }
1223** Next (Window.iEphCsr)
1224** }
1225*/
danec891fd2018-06-06 20:51:02 +00001226static void windowReturnRows(
1227 Parse *pParse,
dan13078ca2018-06-13 20:29:38 +00001228 Window *pMWin, /* List of window functions */
1229 int regCtr, /* Register containing number of rows */
1230 int regGosub, /* Register for Gosub addrGosub */
1231 int addrGosub, /* Address of sub-routine for ReturnOneRow */
1232 int regInvArg, /* Array of registers for xInverse args */
1233 int regInvSize /* Register containing size of partition */
danec891fd2018-06-06 20:51:02 +00001234){
1235 int addr;
1236 Vdbe *v = sqlite3GetVdbe(pParse);
1237 windowAggFinal(pParse, pMWin, 0);
1238 addr = sqlite3VdbeAddOp3(v, OP_IfPos, regCtr, sqlite3VdbeCurrentAddr(v)+2 ,1);
1239 sqlite3VdbeAddOp2(v, OP_Goto, 0, 0);
1240 windowReturnOneRow(pParse, pMWin, regGosub, addrGosub);
1241 if( regInvArg ){
1242 windowAggStep(pParse, pMWin, pMWin->iEphCsr, 1, regInvArg, regInvSize);
1243 }
1244 sqlite3VdbeAddOp2(v, OP_Next, pMWin->iEphCsr, addr);
1245 sqlite3VdbeJumpHere(v, addr+1); /* The OP_Goto */
1246}
1247
dan54a9ab32018-06-14 14:27:05 +00001248/*
1249** Generate code to set the accumulator register for each window function
1250** in the linked list passed as the second argument to NULL. And perform
1251** any equivalent initialization required by any built-in window functions
1252** in the list.
1253*/
dan2e605682018-06-07 15:54:26 +00001254static int windowInitAccum(Parse *pParse, Window *pMWin){
1255 Vdbe *v = sqlite3GetVdbe(pParse);
1256 int regArg;
1257 int nArg = 0;
1258 Window *pWin;
1259 for(pWin=pMWin; pWin; pWin=pWin->pNextWin){
dan9a947222018-06-14 19:06:36 +00001260 FuncDef *pFunc = pWin->pFunc;
dan2e605682018-06-07 15:54:26 +00001261 sqlite3VdbeAddOp2(v, OP_Null, 0, pWin->regAccum);
dan2a11bb22018-06-11 20:50:25 +00001262 nArg = MAX(nArg, windowArgCount(pWin));
dan9a947222018-06-14 19:06:36 +00001263 if( pFunc->xSFunc==nth_valueStepFunc
1264 || pFunc->xSFunc==first_valueStepFunc
dan7095c002018-06-07 17:45:22 +00001265 ){
dan2e605682018-06-07 15:54:26 +00001266 sqlite3VdbeAddOp2(v, OP_Integer, 0, pWin->regApp);
1267 sqlite3VdbeAddOp2(v, OP_Integer, 0, pWin->regApp+1);
1268 }
dan9a947222018-06-14 19:06:36 +00001269
1270 if( (pFunc->funcFlags & SQLITE_FUNC_MINMAX) && pWin->csrApp ){
1271 assert( pWin->eStart!=TK_UNBOUNDED );
1272 sqlite3VdbeAddOp1(v, OP_ResetSorter, pWin->csrApp);
1273 sqlite3VdbeAddOp2(v, OP_Integer, 0, pWin->regApp+1);
1274 }
dan2e605682018-06-07 15:54:26 +00001275 }
1276 regArg = pParse->nMem+1;
1277 pParse->nMem += nArg;
1278 return regArg;
1279}
1280
1281
dan99652dd2018-05-24 17:49:14 +00001282/*
dan54a9ab32018-06-14 14:27:05 +00001283** This function does the work of sqlite3WindowCodeStep() for all "ROWS"
1284** window frame types except for "BETWEEN UNBOUNDED PRECEDING AND CURRENT
1285** ROW". Pseudo-code for each follows.
1286**
dan09590aa2018-05-25 20:30:17 +00001287** ROWS BETWEEN <expr1> PRECEDING AND <expr2> FOLLOWING
dan09590aa2018-05-25 20:30:17 +00001288**
1289** ...
1290** if( new partition ){
1291** Gosub flush_partition
1292** }
1293** Insert (record in eph-table)
1294** sqlite3WhereEnd()
1295** Gosub flush_partition
1296**
1297** flush_partition:
1298** Once {
1299** OpenDup (iEphCsr -> csrStart)
1300** OpenDup (iEphCsr -> csrEnd)
dan99652dd2018-05-24 17:49:14 +00001301** }
dan09590aa2018-05-25 20:30:17 +00001302** regStart = <expr1> // PRECEDING expression
1303** regEnd = <expr2> // FOLLOWING expression
1304** if( regStart<0 || regEnd<0 ){ error! }
1305** Rewind (csr,csrStart,csrEnd) // if EOF goto flush_partition_done
1306** Next(csrEnd) // if EOF skip Aggstep
1307** Aggstep (csrEnd)
1308** if( (regEnd--)<=0 ){
1309** AggFinal (xValue)
1310** Gosub addrGosub
1311** Next(csr) // if EOF goto flush_partition_done
1312** if( (regStart--)<=0 ){
1313** AggStep (csrStart, xInverse)
1314** Next(csrStart)
1315** }
1316** }
1317** flush_partition_done:
1318** ResetSorter (csr)
1319** Return
dan99652dd2018-05-24 17:49:14 +00001320**
dan09590aa2018-05-25 20:30:17 +00001321** ROWS BETWEEN <expr> PRECEDING AND CURRENT ROW
1322** ROWS BETWEEN CURRENT ROW AND <expr> FOLLOWING
1323** ROWS BETWEEN UNBOUNDED PRECEDING AND <expr> FOLLOWING
1324**
1325** These are similar to the above. For "CURRENT ROW", intialize the
1326** register to 0. For "UNBOUNDED PRECEDING" to infinity.
1327**
1328** ROWS BETWEEN <expr> PRECEDING AND UNBOUNDED FOLLOWING
1329** ROWS BETWEEN CURRENT ROW AND UNBOUNDED FOLLOWING
1330**
1331** Rewind (csr,csrStart,csrEnd) // if EOF goto flush_partition_done
1332** while( 1 ){
1333** Next(csrEnd) // Exit while(1) at EOF
1334** Aggstep (csrEnd)
1335** }
1336** while( 1 ){
dan99652dd2018-05-24 17:49:14 +00001337** AggFinal (xValue)
1338** Gosub addrGosub
dan09590aa2018-05-25 20:30:17 +00001339** Next(csr) // if EOF goto flush_partition_done
dan31f56392018-05-24 21:10:57 +00001340** if( (regStart--)<=0 ){
1341** AggStep (csrStart, xInverse)
1342** Next(csrStart)
dan99652dd2018-05-24 17:49:14 +00001343** }
1344** }
dan99652dd2018-05-24 17:49:14 +00001345**
dan09590aa2018-05-25 20:30:17 +00001346** For the "CURRENT ROW AND UNBOUNDED FOLLOWING" case, the final if()
1347** condition is always true (as if regStart were initialized to 0).
dan99652dd2018-05-24 17:49:14 +00001348**
dan09590aa2018-05-25 20:30:17 +00001349** RANGE BETWEEN CURRENT ROW AND UNBOUNDED FOLLOWING
1350**
1351** This is the only RANGE case handled by this routine. It modifies the
1352** second while( 1 ) loop in "ROWS BETWEEN CURRENT ... UNBOUNDED..." to
1353** be:
1354**
1355** while( 1 ){
1356** AggFinal (xValue)
1357** while( 1 ){
1358** regPeer++
1359** Gosub addrGosub
1360** Next(csr) // if EOF goto flush_partition_done
1361** if( new peer ) break;
1362** }
1363** while( (regPeer--)>0 ){
1364** AggStep (csrStart, xInverse)
1365** Next(csrStart)
1366** }
1367** }
dan99652dd2018-05-24 17:49:14 +00001368**
dan31f56392018-05-24 21:10:57 +00001369** ROWS BETWEEN <expr> FOLLOWING AND <expr> FOLLOWING
1370**
1371** regEnd = regEnd - regStart
1372** Rewind (csr,csrStart,csrEnd) // if EOF goto flush_partition_done
1373** Aggstep (csrEnd)
1374** Next(csrEnd) // if EOF fall-through
1375** if( (regEnd--)<=0 ){
dan31f56392018-05-24 21:10:57 +00001376** if( (regStart--)<=0 ){
1377** AggFinal (xValue)
1378** Gosub addrGosub
1379** Next(csr) // if EOF goto flush_partition_done
1380** }
dane105dd72018-05-25 09:29:11 +00001381** AggStep (csrStart, xInverse)
1382** Next (csrStart)
dan31f56392018-05-24 21:10:57 +00001383** }
1384**
1385** ROWS BETWEEN <expr> PRECEDING AND <expr> PRECEDING
1386**
1387** Replace the bit after "Rewind" in the above with:
1388**
1389** if( (regEnd--)<=0 ){
1390** AggStep (csrEnd)
1391** Next (csrEnd)
1392** }
1393** AggFinal (xValue)
1394** Gosub addrGosub
1395** Next(csr) // if EOF goto flush_partition_done
1396** if( (regStart--)<=0 ){
1397** AggStep (csr2, xInverse)
1398** Next (csr2)
1399** }
1400**
dan99652dd2018-05-24 17:49:14 +00001401*/
danc3a20c12018-05-23 20:55:37 +00001402static void windowCodeRowExprStep(
1403 Parse *pParse,
1404 Select *p,
1405 WhereInfo *pWInfo,
1406 int regGosub,
1407 int addrGosub
1408){
1409 Window *pMWin = p->pWin;
1410 Vdbe *v = sqlite3GetVdbe(pParse);
danc3a20c12018-05-23 20:55:37 +00001411 int regFlushPart; /* Register for "Gosub flush_partition" */
dan31f56392018-05-24 21:10:57 +00001412 int lblFlushPart; /* Label for "Gosub flush_partition" */
1413 int lblFlushDone; /* Label for "Gosub flush_partition_done" */
danc3a20c12018-05-23 20:55:37 +00001414
danf690b572018-06-01 21:00:08 +00001415 int regArg;
danc3a20c12018-05-23 20:55:37 +00001416 int addr;
dan31f56392018-05-24 21:10:57 +00001417 int csrStart = pParse->nTab++;
1418 int csrEnd = pParse->nTab++;
1419 int regStart; /* Value of <expr> PRECEDING */
1420 int regEnd; /* Value of <expr> FOLLOWING */
danc3a20c12018-05-23 20:55:37 +00001421 int addrGoto;
dan31f56392018-05-24 21:10:57 +00001422 int addrTop;
danc3a20c12018-05-23 20:55:37 +00001423 int addrIfPos1;
1424 int addrIfPos2;
dandfa552f2018-06-02 21:04:28 +00001425 int regSize = 0;
dan09590aa2018-05-25 20:30:17 +00001426
dan99652dd2018-05-24 17:49:14 +00001427 assert( pMWin->eStart==TK_PRECEDING
1428 || pMWin->eStart==TK_CURRENT
dane105dd72018-05-25 09:29:11 +00001429 || pMWin->eStart==TK_FOLLOWING
dan99652dd2018-05-24 17:49:14 +00001430 || pMWin->eStart==TK_UNBOUNDED
1431 );
1432 assert( pMWin->eEnd==TK_FOLLOWING
1433 || pMWin->eEnd==TK_CURRENT
1434 || pMWin->eEnd==TK_UNBOUNDED
dan31f56392018-05-24 21:10:57 +00001435 || pMWin->eEnd==TK_PRECEDING
dan99652dd2018-05-24 17:49:14 +00001436 );
1437
danc3a20c12018-05-23 20:55:37 +00001438 /* Allocate register and label for the "flush_partition" sub-routine. */
1439 regFlushPart = ++pParse->nMem;
dan31f56392018-05-24 21:10:57 +00001440 lblFlushPart = sqlite3VdbeMakeLabel(v);
1441 lblFlushDone = sqlite3VdbeMakeLabel(v);
danc3a20c12018-05-23 20:55:37 +00001442
dan31f56392018-05-24 21:10:57 +00001443 regStart = ++pParse->nMem;
1444 regEnd = ++pParse->nMem;
danc3a20c12018-05-23 20:55:37 +00001445
dandfa552f2018-06-02 21:04:28 +00001446 windowPartitionCache(pParse, p, pWInfo, regFlushPart, lblFlushPart, &regSize);
danc3a20c12018-05-23 20:55:37 +00001447
danc3a20c12018-05-23 20:55:37 +00001448 addrGoto = sqlite3VdbeAddOp0(v, OP_Goto);
1449
danc9a86682018-05-30 20:44:58 +00001450 /* Start of "flush_partition" */
dan31f56392018-05-24 21:10:57 +00001451 sqlite3VdbeResolveLabel(v, lblFlushPart);
danc3a20c12018-05-23 20:55:37 +00001452 sqlite3VdbeAddOp2(v, OP_Once, 0, sqlite3VdbeCurrentAddr(v)+3);
dan31f56392018-05-24 21:10:57 +00001453 sqlite3VdbeAddOp2(v, OP_OpenDup, csrStart, pMWin->iEphCsr);
1454 sqlite3VdbeAddOp2(v, OP_OpenDup, csrEnd, pMWin->iEphCsr);
danc3a20c12018-05-23 20:55:37 +00001455
dan31f56392018-05-24 21:10:57 +00001456 /* If either regStart or regEnd are not non-negative integers, throw
dan99652dd2018-05-24 17:49:14 +00001457 ** an exception. */
1458 if( pMWin->pStart ){
dan31f56392018-05-24 21:10:57 +00001459 sqlite3ExprCode(pParse, pMWin->pStart, regStart);
1460 windowCheckFrameValue(pParse, regStart, 0);
dan99652dd2018-05-24 17:49:14 +00001461 }
1462 if( pMWin->pEnd ){
dan31f56392018-05-24 21:10:57 +00001463 sqlite3ExprCode(pParse, pMWin->pEnd, regEnd);
1464 windowCheckFrameValue(pParse, regEnd, 1);
dan99652dd2018-05-24 17:49:14 +00001465 }
danc3a20c12018-05-23 20:55:37 +00001466
danc9a86682018-05-30 20:44:58 +00001467 /* If this is "ROWS <expr1> FOLLOWING AND ROWS <expr2> FOLLOWING", do:
1468 **
dan26522d12018-06-11 18:16:51 +00001469 ** if( regEnd<regStart ){
1470 ** // The frame always consists of 0 rows
1471 ** regStart = regSize;
1472 ** }
danc9a86682018-05-30 20:44:58 +00001473 ** regEnd = regEnd - regStart;
1474 */
1475 if( pMWin->pEnd && pMWin->pStart && pMWin->eStart==TK_FOLLOWING ){
1476 assert( pMWin->eEnd==TK_FOLLOWING );
dan26522d12018-06-11 18:16:51 +00001477 sqlite3VdbeAddOp3(v, OP_Ge, regStart, sqlite3VdbeCurrentAddr(v)+2, regEnd);
1478 sqlite3VdbeAddOp2(v, OP_Copy, regSize, regStart);
danc9a86682018-05-30 20:44:58 +00001479 sqlite3VdbeAddOp3(v, OP_Subtract, regStart, regEnd, regEnd);
1480 }
1481
dan26522d12018-06-11 18:16:51 +00001482 if( pMWin->pEnd && pMWin->pStart && pMWin->eEnd==TK_PRECEDING ){
1483 assert( pMWin->eStart==TK_PRECEDING );
1484 sqlite3VdbeAddOp3(v, OP_Le, regStart, sqlite3VdbeCurrentAddr(v)+3, regEnd);
1485 sqlite3VdbeAddOp2(v, OP_Copy, regSize, regStart);
1486 sqlite3VdbeAddOp2(v, OP_Copy, regSize, regEnd);
1487 }
1488
danc9a86682018-05-30 20:44:58 +00001489 /* Initialize the accumulator register for each window function to NULL */
dan2e605682018-06-07 15:54:26 +00001490 regArg = windowInitAccum(pParse, pMWin);
danc3a20c12018-05-23 20:55:37 +00001491
dan31f56392018-05-24 21:10:57 +00001492 sqlite3VdbeAddOp2(v, OP_Rewind, pMWin->iEphCsr, lblFlushDone);
1493 sqlite3VdbeAddOp2(v, OP_Rewind, csrStart, lblFlushDone);
danc3a20c12018-05-23 20:55:37 +00001494 sqlite3VdbeChangeP5(v, 1);
dan31f56392018-05-24 21:10:57 +00001495 sqlite3VdbeAddOp2(v, OP_Rewind, csrEnd, lblFlushDone);
danc3a20c12018-05-23 20:55:37 +00001496 sqlite3VdbeChangeP5(v, 1);
1497
1498 /* Invoke AggStep function for each window function using the row that
dan31f56392018-05-24 21:10:57 +00001499 ** csrEnd currently points to. Or, if csrEnd is already at EOF,
danc3a20c12018-05-23 20:55:37 +00001500 ** do nothing. */
dan31f56392018-05-24 21:10:57 +00001501 addrTop = sqlite3VdbeCurrentAddr(v);
1502 if( pMWin->eEnd==TK_PRECEDING ){
1503 addrIfPos1 = sqlite3VdbeAddOp3(v, OP_IfPos, regEnd, 0 , 1);
danc3a20c12018-05-23 20:55:37 +00001504 }
dan31f56392018-05-24 21:10:57 +00001505 sqlite3VdbeAddOp2(v, OP_Next, csrEnd, sqlite3VdbeCurrentAddr(v)+2);
1506 addr = sqlite3VdbeAddOp0(v, OP_Goto);
dandfa552f2018-06-02 21:04:28 +00001507 windowAggStep(pParse, pMWin, csrEnd, 0, regArg, regSize);
dan99652dd2018-05-24 17:49:14 +00001508 if( pMWin->eEnd==TK_UNBOUNDED ){
dan31f56392018-05-24 21:10:57 +00001509 sqlite3VdbeAddOp2(v, OP_Goto, 0, addrTop);
1510 sqlite3VdbeJumpHere(v, addr);
1511 addrTop = sqlite3VdbeCurrentAddr(v);
dan99652dd2018-05-24 17:49:14 +00001512 }else{
dan31f56392018-05-24 21:10:57 +00001513 sqlite3VdbeJumpHere(v, addr);
1514 if( pMWin->eEnd==TK_PRECEDING ){
1515 sqlite3VdbeJumpHere(v, addrIfPos1);
1516 }
dan99652dd2018-05-24 17:49:14 +00001517 }
danc3a20c12018-05-23 20:55:37 +00001518
dan99652dd2018-05-24 17:49:14 +00001519 if( pMWin->eEnd==TK_FOLLOWING ){
dan31f56392018-05-24 21:10:57 +00001520 addrIfPos1 = sqlite3VdbeAddOp3(v, OP_IfPos, regEnd, 0 , 1);
dan99652dd2018-05-24 17:49:14 +00001521 }
dane105dd72018-05-25 09:29:11 +00001522 if( pMWin->eStart==TK_FOLLOWING ){
1523 addrIfPos2 = sqlite3VdbeAddOp3(v, OP_IfPos, regStart, 0 , 1);
1524 }
dand6f784e2018-05-28 18:30:45 +00001525 windowAggFinal(pParse, pMWin, 0);
danec891fd2018-06-06 20:51:02 +00001526 windowReturnOneRow(pParse, pMWin, regGosub, addrGosub);
danc3a20c12018-05-23 20:55:37 +00001527 sqlite3VdbeAddOp2(v, OP_Next, pMWin->iEphCsr, sqlite3VdbeCurrentAddr(v)+2);
dan31f56392018-05-24 21:10:57 +00001528 sqlite3VdbeAddOp2(v, OP_Goto, 0, lblFlushDone);
dane105dd72018-05-25 09:29:11 +00001529 if( pMWin->eStart==TK_FOLLOWING ){
1530 sqlite3VdbeJumpHere(v, addrIfPos2);
1531 }
danc3a20c12018-05-23 20:55:37 +00001532
dane105dd72018-05-25 09:29:11 +00001533 if( pMWin->eStart==TK_CURRENT
1534 || pMWin->eStart==TK_PRECEDING
1535 || pMWin->eStart==TK_FOLLOWING
1536 ){
dan09590aa2018-05-25 20:30:17 +00001537 int addrJumpHere = 0;
dan99652dd2018-05-24 17:49:14 +00001538 if( pMWin->eStart==TK_PRECEDING ){
dan09590aa2018-05-25 20:30:17 +00001539 addrJumpHere = sqlite3VdbeAddOp3(v, OP_IfPos, regStart, 0 , 1);
1540 }
dan31f56392018-05-24 21:10:57 +00001541 sqlite3VdbeAddOp2(v, OP_Next, csrStart, sqlite3VdbeCurrentAddr(v)+1);
dandfa552f2018-06-02 21:04:28 +00001542 windowAggStep(pParse, pMWin, csrStart, 1, regArg, regSize);
dan09590aa2018-05-25 20:30:17 +00001543 if( addrJumpHere ){
1544 sqlite3VdbeJumpHere(v, addrJumpHere);
dan99652dd2018-05-24 17:49:14 +00001545 }
danc3a20c12018-05-23 20:55:37 +00001546 }
dan99652dd2018-05-24 17:49:14 +00001547 if( pMWin->eEnd==TK_FOLLOWING ){
1548 sqlite3VdbeJumpHere(v, addrIfPos1);
1549 }
dan31f56392018-05-24 21:10:57 +00001550 sqlite3VdbeAddOp2(v, OP_Goto, 0, addrTop);
danc3a20c12018-05-23 20:55:37 +00001551
1552 /* flush_partition_done: */
dan31f56392018-05-24 21:10:57 +00001553 sqlite3VdbeResolveLabel(v, lblFlushDone);
danc3a20c12018-05-23 20:55:37 +00001554 sqlite3VdbeAddOp1(v, OP_ResetSorter, pMWin->iEphCsr);
1555 sqlite3VdbeAddOp1(v, OP_Return, regFlushPart);
1556
1557 /* Jump to here to skip over flush_partition */
1558 sqlite3VdbeJumpHere(v, addrGoto);
1559}
1560
dan79d45442018-05-26 21:17:29 +00001561/*
dan54a9ab32018-06-14 14:27:05 +00001562** This function does the work of sqlite3WindowCodeStep() for cases that
1563** would normally be handled by windowCodeDefaultStep() when there are
1564** one or more built-in window-functions that require the entire partition
1565** to be cached in a temp table before any rows can be returned. Additionally.
1566** "RANGE BETWEEN CURRENT ROW AND UNBOUNDED FOLLOWING" is always handled by
1567** this function.
1568**
1569** Pseudo-code corresponding to the VM code generated by this function
1570** for each type of window follows.
1571**
dan79d45442018-05-26 21:17:29 +00001572** RANGE BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW
1573**
danf690b572018-06-01 21:00:08 +00001574** flush_partition:
1575** Once {
1576** OpenDup (iEphCsr -> csrLead)
1577** }
1578** Integer ctr 0
1579** foreach row (csrLead){
1580** if( new peer ){
1581** AggFinal (xValue)
1582** for(i=0; i<ctr; i++){
1583** Gosub addrGosub
1584** Next iEphCsr
1585** }
1586** Integer ctr 0
1587** }
1588** AggStep (csrLead)
1589** Incr ctr
1590** }
1591**
1592** AggFinal (xFinalize)
1593** for(i=0; i<ctr; i++){
1594** Gosub addrGosub
1595** Next iEphCsr
1596** }
1597**
1598** ResetSorter (csr)
1599** Return
1600**
1601** ROWS BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW
dan54a9ab32018-06-14 14:27:05 +00001602**
1603** As above, except that the "if( new peer )" branch is always taken.
1604**
danf690b572018-06-01 21:00:08 +00001605** RANGE BETWEEN CURRENT ROW AND CURRENT ROW
dan54a9ab32018-06-14 14:27:05 +00001606**
1607** As above, except that each of the for() loops becomes:
1608**
1609** for(i=0; i<ctr; i++){
1610** Gosub addrGosub
1611** AggStep (xInverse, iEphCsr)
1612** Next iEphCsr
1613** }
1614**
1615** RANGE BETWEEN UNBOUNDED PRECEDING AND UNBOUNDED FOLLOWING
1616**
1617** flush_partition:
1618** Once {
1619** OpenDup (iEphCsr -> csrLead)
1620** }
1621** foreach row (csrLead) {
1622** AggStep (csrLead)
1623** }
1624** foreach row (iEphCsr) {
1625** Gosub addrGosub
1626** }
1627**
1628** RANGE BETWEEN CURRENT ROW AND UNBOUNDED FOLLOWING
1629**
1630** flush_partition:
1631** Once {
1632** OpenDup (iEphCsr -> csrLead)
1633** }
1634** foreach row (csrLead){
1635** AggStep (csrLead)
1636** }
1637** Rewind (csrLead)
1638** Integer ctr 0
1639** foreach row (csrLead){
1640** if( new peer ){
1641** AggFinal (xValue)
1642** for(i=0; i<ctr; i++){
1643** Gosub addrGosub
1644** AggStep (xInverse, iEphCsr)
1645** Next iEphCsr
1646** }
1647** Integer ctr 0
1648** }
1649** Incr ctr
1650** }
1651**
1652** AggFinal (xFinalize)
1653** for(i=0; i<ctr; i++){
1654** Gosub addrGosub
1655** Next iEphCsr
1656** }
1657**
1658** ResetSorter (csr)
1659** Return
danf690b572018-06-01 21:00:08 +00001660*/
1661static void windowCodeCacheStep(
1662 Parse *pParse,
1663 Select *p,
1664 WhereInfo *pWInfo,
1665 int regGosub,
1666 int addrGosub
1667){
1668 Window *pMWin = p->pWin;
1669 Vdbe *v = sqlite3GetVdbe(pParse);
danf690b572018-06-01 21:00:08 +00001670 int k;
1671 int addr;
1672 ExprList *pPart = pMWin->pPartition;
1673 ExprList *pOrderBy = pMWin->pOrderBy;
dan54a9ab32018-06-14 14:27:05 +00001674 int nPeer = pOrderBy ? pOrderBy->nExpr : 0;
danf690b572018-06-01 21:00:08 +00001675 int regNewPeer;
1676
1677 int addrGoto; /* Address of Goto used to jump flush_par.. */
dan13078ca2018-06-13 20:29:38 +00001678 int addrNext; /* Jump here for next iteration of loop */
danf690b572018-06-01 21:00:08 +00001679 int regFlushPart;
1680 int lblFlushPart;
1681 int csrLead;
1682 int regCtr;
1683 int regArg; /* Register array to martial function args */
dandfa552f2018-06-02 21:04:28 +00001684 int regSize;
dan13078ca2018-06-13 20:29:38 +00001685 int lblEmpty;
dan54a9ab32018-06-14 14:27:05 +00001686 int bReverse = pMWin->pOrderBy && pMWin->eStart==TK_CURRENT
1687 && pMWin->eEnd==TK_UNBOUNDED;
danf690b572018-06-01 21:00:08 +00001688
1689 assert( (pMWin->eStart==TK_UNBOUNDED && pMWin->eEnd==TK_CURRENT)
danec891fd2018-06-06 20:51:02 +00001690 || (pMWin->eStart==TK_UNBOUNDED && pMWin->eEnd==TK_UNBOUNDED)
1691 || (pMWin->eStart==TK_CURRENT && pMWin->eEnd==TK_CURRENT)
dan13078ca2018-06-13 20:29:38 +00001692 || (pMWin->eStart==TK_CURRENT && pMWin->eEnd==TK_UNBOUNDED)
danf690b572018-06-01 21:00:08 +00001693 );
1694
dan13078ca2018-06-13 20:29:38 +00001695 lblEmpty = sqlite3VdbeMakeLabel(v);
danf690b572018-06-01 21:00:08 +00001696 regNewPeer = pParse->nMem+1;
1697 pParse->nMem += nPeer;
1698
1699 /* Allocate register and label for the "flush_partition" sub-routine. */
1700 regFlushPart = ++pParse->nMem;
1701 lblFlushPart = sqlite3VdbeMakeLabel(v);
1702
1703 csrLead = pParse->nTab++;
1704 regCtr = ++pParse->nMem;
1705
dandfa552f2018-06-02 21:04:28 +00001706 windowPartitionCache(pParse, p, pWInfo, regFlushPart, lblFlushPart, &regSize);
danf690b572018-06-01 21:00:08 +00001707 addrGoto = sqlite3VdbeAddOp0(v, OP_Goto);
1708
1709 /* Start of "flush_partition" */
1710 sqlite3VdbeResolveLabel(v, lblFlushPart);
1711 sqlite3VdbeAddOp2(v, OP_Once, 0, sqlite3VdbeCurrentAddr(v)+2);
1712 sqlite3VdbeAddOp2(v, OP_OpenDup, csrLead, pMWin->iEphCsr);
1713
1714 /* Initialize the accumulator register for each window function to NULL */
dan2e605682018-06-07 15:54:26 +00001715 regArg = windowInitAccum(pParse, pMWin);
danf690b572018-06-01 21:00:08 +00001716
1717 sqlite3VdbeAddOp2(v, OP_Integer, 0, regCtr);
dan13078ca2018-06-13 20:29:38 +00001718 sqlite3VdbeAddOp2(v, OP_Rewind, csrLead, lblEmpty);
1719 sqlite3VdbeAddOp2(v, OP_Rewind, pMWin->iEphCsr, lblEmpty);
danf690b572018-06-01 21:00:08 +00001720
dan13078ca2018-06-13 20:29:38 +00001721 if( bReverse ){
1722 int addr = sqlite3VdbeCurrentAddr(v);
1723 windowAggStep(pParse, pMWin, csrLead, 0, regArg, regSize);
1724 sqlite3VdbeAddOp2(v, OP_Next, csrLead, addr);
1725 sqlite3VdbeAddOp2(v, OP_Rewind, csrLead, lblEmpty);
1726 }
1727 addrNext = sqlite3VdbeCurrentAddr(v);
1728
1729 if( pOrderBy && (pMWin->eEnd==TK_CURRENT || pMWin->eStart==TK_CURRENT) ){
1730 int bCurrent = (pMWin->eStart==TK_CURRENT);
danec891fd2018-06-06 20:51:02 +00001731 int addrJump = 0; /* Address of OP_Jump below */
1732 if( pMWin->eType==TK_RANGE ){
1733 int iOff = pMWin->nBufferCol + (pPart ? pPart->nExpr : 0);
1734 int regPeer = pMWin->regPart + (pPart ? pPart->nExpr : 0);
1735 KeyInfo *pKeyInfo = sqlite3KeyInfoFromExprList(pParse, pOrderBy, 0, 0);
1736 for(k=0; k<nPeer; k++){
1737 sqlite3VdbeAddOp3(v, OP_Column, csrLead, iOff+k, regNewPeer+k);
1738 }
1739 addr = sqlite3VdbeAddOp3(v, OP_Compare, regNewPeer, regPeer, nPeer);
1740 sqlite3VdbeAppendP4(v, (void*)pKeyInfo, P4_KEYINFO);
1741 addrJump = sqlite3VdbeAddOp3(v, OP_Jump, addr+2, 0, addr+2);
1742 sqlite3VdbeAddOp3(v, OP_Copy, regNewPeer, regPeer, nPeer-1);
danf690b572018-06-01 21:00:08 +00001743 }
1744
dan13078ca2018-06-13 20:29:38 +00001745 windowReturnRows(pParse, pMWin, regCtr, regGosub, addrGosub,
danec891fd2018-06-06 20:51:02 +00001746 (bCurrent ? regArg : 0), (bCurrent ? regSize : 0)
1747 );
1748 if( addrJump ) sqlite3VdbeJumpHere(v, addrJump);
danf690b572018-06-01 21:00:08 +00001749 }
1750
dan13078ca2018-06-13 20:29:38 +00001751 if( bReverse==0 ){
1752 windowAggStep(pParse, pMWin, csrLead, 0, regArg, regSize);
1753 }
danf690b572018-06-01 21:00:08 +00001754 sqlite3VdbeAddOp2(v, OP_AddImm, regCtr, 1);
dan13078ca2018-06-13 20:29:38 +00001755 sqlite3VdbeAddOp2(v, OP_Next, csrLead, addrNext);
danf690b572018-06-01 21:00:08 +00001756
dan13078ca2018-06-13 20:29:38 +00001757 windowReturnRows(pParse, pMWin, regCtr, regGosub, addrGosub, 0, 0);
danf690b572018-06-01 21:00:08 +00001758
dan13078ca2018-06-13 20:29:38 +00001759 sqlite3VdbeResolveLabel(v, lblEmpty);
danf690b572018-06-01 21:00:08 +00001760 sqlite3VdbeAddOp1(v, OP_ResetSorter, pMWin->iEphCsr);
1761 sqlite3VdbeAddOp1(v, OP_Return, regFlushPart);
1762
1763 /* Jump to here to skip over flush_partition */
1764 sqlite3VdbeJumpHere(v, addrGoto);
1765}
1766
1767
1768/*
1769** RANGE BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW
1770**
dan79d45442018-05-26 21:17:29 +00001771** ...
1772** if( new partition ){
1773** AggFinal (xFinalize)
1774** Gosub addrGosub
1775** ResetSorter eph-table
1776** }
1777** else if( new peer ){
1778** AggFinal (xValue)
1779** Gosub addrGosub
1780** ResetSorter eph-table
1781** }
1782** AggStep
1783** Insert (record into eph-table)
1784** sqlite3WhereEnd()
1785** AggFinal (xFinalize)
1786** Gosub addrGosub
danf690b572018-06-01 21:00:08 +00001787**
1788** RANGE BETWEEN UNBOUNDED PRECEDING AND UNBOUNDED FOLLOWING
1789**
1790** As above, except take no action for a "new peer". Invoke
1791** the sub-routine once only for each partition.
1792**
1793** RANGE BETWEEN CURRENT ROW AND CURRENT ROW
1794**
1795** As above, except that the "new peer" condition is handled in the
1796** same way as "new partition" (so there is no "else if" block).
1797**
1798** ROWS BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW
1799**
1800** As above, except assume every row is a "new peer".
dan79d45442018-05-26 21:17:29 +00001801*/
danc3a20c12018-05-23 20:55:37 +00001802static void windowCodeDefaultStep(
1803 Parse *pParse,
1804 Select *p,
1805 WhereInfo *pWInfo,
1806 int regGosub,
1807 int addrGosub
1808){
1809 Window *pMWin = p->pWin;
1810 Vdbe *v = sqlite3GetVdbe(pParse);
danc3a20c12018-05-23 20:55:37 +00001811 int k;
1812 int iSubCsr = p->pSrc->a[0].iCursor;
1813 int nSub = p->pSrc->a[0].pTab->nCol;
1814 int reg = pParse->nMem+1;
1815 int regRecord = reg+nSub;
1816 int regRowid = regRecord+1;
1817 int addr;
dand6f784e2018-05-28 18:30:45 +00001818 ExprList *pPart = pMWin->pPartition;
1819 ExprList *pOrderBy = pMWin->pOrderBy;
danc3a20c12018-05-23 20:55:37 +00001820
dan79d45442018-05-26 21:17:29 +00001821 assert( pMWin->eType==TK_RANGE
1822 || (pMWin->eStart==TK_UNBOUNDED && pMWin->eEnd==TK_CURRENT)
1823 );
1824
dand6f784e2018-05-28 18:30:45 +00001825 assert( (pMWin->eStart==TK_UNBOUNDED && pMWin->eEnd==TK_CURRENT)
1826 || (pMWin->eStart==TK_UNBOUNDED && pMWin->eEnd==TK_UNBOUNDED)
1827 || (pMWin->eStart==TK_CURRENT && pMWin->eEnd==TK_CURRENT)
1828 || (pMWin->eStart==TK_CURRENT && pMWin->eEnd==TK_UNBOUNDED && !pOrderBy)
1829 );
1830
1831 if( pMWin->eEnd==TK_UNBOUNDED ){
1832 pOrderBy = 0;
1833 }
1834
danc3a20c12018-05-23 20:55:37 +00001835 pParse->nMem += nSub + 2;
1836
1837 /* Martial the row returned by the sub-select into an array of
1838 ** registers. */
1839 for(k=0; k<nSub; k++){
1840 sqlite3VdbeAddOp3(v, OP_Column, iSubCsr, k, reg+k);
1841 }
1842
1843 /* Check if this is the start of a new partition or peer group. */
dand6f784e2018-05-28 18:30:45 +00001844 if( pPart || pOrderBy ){
danc3a20c12018-05-23 20:55:37 +00001845 int nPart = (pPart ? pPart->nExpr : 0);
danc3a20c12018-05-23 20:55:37 +00001846 int addrGoto = 0;
1847 int addrJump = 0;
dand6f784e2018-05-28 18:30:45 +00001848 int nPeer = (pOrderBy ? pOrderBy->nExpr : 0);
danc3a20c12018-05-23 20:55:37 +00001849
1850 if( pPart ){
1851 int regNewPart = reg + pMWin->nBufferCol;
1852 KeyInfo *pKeyInfo = sqlite3KeyInfoFromExprList(pParse, pPart, 0, 0);
1853 addr = sqlite3VdbeAddOp3(v, OP_Compare, regNewPart, pMWin->regPart,nPart);
1854 sqlite3VdbeAppendP4(v, (void*)pKeyInfo, P4_KEYINFO);
1855 addrJump = sqlite3VdbeAddOp3(v, OP_Jump, addr+2, 0, addr+2);
dand6f784e2018-05-28 18:30:45 +00001856 windowAggFinal(pParse, pMWin, 1);
danc3a20c12018-05-23 20:55:37 +00001857 if( pOrderBy ){
1858 addrGoto = sqlite3VdbeAddOp0(v, OP_Goto);
1859 }
1860 }
1861
1862 if( pOrderBy ){
1863 int regNewPeer = reg + pMWin->nBufferCol + nPart;
1864 int regPeer = pMWin->regPart + nPart;
1865
danc3a20c12018-05-23 20:55:37 +00001866 if( addrJump ) sqlite3VdbeJumpHere(v, addrJump);
dan79d45442018-05-26 21:17:29 +00001867 if( pMWin->eType==TK_RANGE ){
1868 KeyInfo *pKeyInfo = sqlite3KeyInfoFromExprList(pParse, pOrderBy, 0, 0);
1869 addr = sqlite3VdbeAddOp3(v, OP_Compare, regNewPeer, regPeer, nPeer);
1870 sqlite3VdbeAppendP4(v, (void*)pKeyInfo, P4_KEYINFO);
1871 addrJump = sqlite3VdbeAddOp3(v, OP_Jump, addr+2, 0, addr+2);
1872 }else{
1873 addrJump = 0;
1874 }
dand6f784e2018-05-28 18:30:45 +00001875 windowAggFinal(pParse, pMWin, pMWin->eStart==TK_CURRENT);
danc3a20c12018-05-23 20:55:37 +00001876 if( addrGoto ) sqlite3VdbeJumpHere(v, addrGoto);
1877 }
1878
dandacf1de2018-06-08 16:11:55 +00001879 sqlite3VdbeAddOp2(v, OP_Rewind, pMWin->iEphCsr,sqlite3VdbeCurrentAddr(v)+3);
danc3a20c12018-05-23 20:55:37 +00001880 sqlite3VdbeAddOp2(v, OP_Gosub, regGosub, addrGosub);
dandacf1de2018-06-08 16:11:55 +00001881 sqlite3VdbeAddOp2(v, OP_Next, pMWin->iEphCsr, sqlite3VdbeCurrentAddr(v)-1);
1882
danc3a20c12018-05-23 20:55:37 +00001883 sqlite3VdbeAddOp1(v, OP_ResetSorter, pMWin->iEphCsr);
1884 sqlite3VdbeAddOp3(
1885 v, OP_Copy, reg+pMWin->nBufferCol, pMWin->regPart, nPart+nPeer-1
1886 );
1887
dan79d45442018-05-26 21:17:29 +00001888 if( addrJump ) sqlite3VdbeJumpHere(v, addrJump);
danc3a20c12018-05-23 20:55:37 +00001889 }
1890
1891 /* Invoke step function for window functions */
dandfa552f2018-06-02 21:04:28 +00001892 windowAggStep(pParse, pMWin, -1, 0, reg, 0);
danc3a20c12018-05-23 20:55:37 +00001893
1894 /* Buffer the current row in the ephemeral table. */
1895 if( pMWin->nBufferCol>0 ){
1896 sqlite3VdbeAddOp3(v, OP_MakeRecord, reg, pMWin->nBufferCol, regRecord);
1897 }else{
1898 sqlite3VdbeAddOp2(v, OP_Blob, 0, regRecord);
1899 sqlite3VdbeAppendP4(v, (void*)"", 0);
1900 }
1901 sqlite3VdbeAddOp2(v, OP_NewRowid, pMWin->iEphCsr, regRowid);
1902 sqlite3VdbeAddOp3(v, OP_Insert, pMWin->iEphCsr, regRecord, regRowid);
1903
1904 /* End the database scan loop. */
1905 sqlite3WhereEnd(pWInfo);
1906
dand6f784e2018-05-28 18:30:45 +00001907 windowAggFinal(pParse, pMWin, 1);
dandacf1de2018-06-08 16:11:55 +00001908 sqlite3VdbeAddOp2(v, OP_Rewind, pMWin->iEphCsr,sqlite3VdbeCurrentAddr(v)+3);
danc3a20c12018-05-23 20:55:37 +00001909 sqlite3VdbeAddOp2(v, OP_Gosub, regGosub, addrGosub);
dandacf1de2018-06-08 16:11:55 +00001910 sqlite3VdbeAddOp2(v, OP_Next, pMWin->iEphCsr, sqlite3VdbeCurrentAddr(v)-1);
danc3a20c12018-05-23 20:55:37 +00001911}
1912
dan13078ca2018-06-13 20:29:38 +00001913/*
1914** Allocate and return a duplicate of the Window object indicated by the
1915** third argument. Set the Window.pOwner field of the new object to
1916** pOwner.
1917*/
dan2a11bb22018-06-11 20:50:25 +00001918Window *sqlite3WindowDup(sqlite3 *db, Expr *pOwner, Window *p){
dandacf1de2018-06-08 16:11:55 +00001919 Window *pNew = 0;
1920 if( p ){
1921 pNew = sqlite3DbMallocZero(db, sizeof(Window));
1922 if( pNew ){
danc95f38d2018-06-18 20:34:43 +00001923 pNew->zName = sqlite3DbStrDup(db, p->zName);
dandacf1de2018-06-08 16:11:55 +00001924 pNew->pFilter = sqlite3ExprDup(db, p->pFilter, 0);
1925 pNew->pPartition = sqlite3ExprListDup(db, p->pPartition, 0);
1926 pNew->pOrderBy = sqlite3ExprListDup(db, p->pOrderBy, 0);
1927 pNew->eType = p->eType;
1928 pNew->eEnd = p->eEnd;
1929 pNew->eStart = p->eStart;
dan303451a2018-06-14 20:52:08 +00001930 pNew->pStart = sqlite3ExprDup(db, p->pStart, 0);
1931 pNew->pEnd = sqlite3ExprDup(db, p->pEnd, 0);
dan2a11bb22018-06-11 20:50:25 +00001932 pNew->pOwner = pOwner;
dandacf1de2018-06-08 16:11:55 +00001933 }
1934 }
1935 return pNew;
1936}
danc3a20c12018-05-23 20:55:37 +00001937
danf9eae182018-05-21 19:45:11 +00001938/*
danc95f38d2018-06-18 20:34:43 +00001939** Return a copy of the linked list of Window objects passed as the
1940** second argument.
1941*/
1942Window *sqlite3WindowListDup(sqlite3 *db, Window *p){
1943 Window *pWin;
1944 Window *pRet = 0;
1945 Window **pp = &pRet;
1946
1947 for(pWin=p; pWin; pWin=pWin->pNextWin){
1948 *pp = sqlite3WindowDup(db, 0, pWin);
1949 if( *pp==0 ) break;
1950 pp = &((*pp)->pNextWin);
1951 }
1952
1953 return pRet;
1954}
1955
1956/*
dan2a11bb22018-06-11 20:50:25 +00001957** sqlite3WhereBegin() has already been called for the SELECT statement
1958** passed as the second argument when this function is invoked. It generates
1959** code to populate the Window.regResult register for each window function and
1960** invoke the sub-routine at instruction addrGosub once for each row.
1961** This function calls sqlite3WhereEnd() before returning.
danf9eae182018-05-21 19:45:11 +00001962*/
1963void sqlite3WindowCodeStep(
dan2a11bb22018-06-11 20:50:25 +00001964 Parse *pParse, /* Parse context */
1965 Select *p, /* Rewritten SELECT statement */
1966 WhereInfo *pWInfo, /* Context returned by sqlite3WhereBegin() */
1967 int regGosub, /* Register for OP_Gosub */
1968 int addrGosub /* OP_Gosub here to return each row */
danf9eae182018-05-21 19:45:11 +00001969){
danf9eae182018-05-21 19:45:11 +00001970 Window *pMWin = p->pWin;
danf9eae182018-05-21 19:45:11 +00001971
dan54a9ab32018-06-14 14:27:05 +00001972 /* There are three different functions that may be used to do the work
1973 ** of this one, depending on the window frame and the specific built-in
1974 ** window functions used (if any).
1975 **
1976 ** windowCodeRowExprStep() handles all "ROWS" window frames, except for:
dan26522d12018-06-11 18:16:51 +00001977 **
dan13078ca2018-06-13 20:29:38 +00001978 ** ROWS BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW
dan54a9ab32018-06-14 14:27:05 +00001979 **
1980 ** The exception is because windowCodeRowExprStep() implements all window
1981 ** frame types by caching the entire partition in a temp table, and
1982 ** "ROWS BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW" is easy enough to
1983 ** implement without such a cache.
1984 **
1985 ** windowCodeCacheStep() is used for:
1986 **
1987 ** RANGE BETWEEN CURRENT ROW AND UNBOUNDED FOLLOWING
1988 **
1989 ** It is also used for anything not handled by windowCodeRowExprStep()
1990 ** that invokes a built-in window function that requires the entire
1991 ** partition to be cached in a temp table before any rows are returned
1992 ** (e.g. nth_value() or percent_rank()).
1993 **
1994 ** Finally, assuming there is no built-in window function that requires
1995 ** the partition to be cached, windowCodeDefaultStep() is used for:
1996 **
1997 ** RANGE BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW
1998 ** RANGE BETWEEN UNBOUNDED PRECEDING AND UNBOUNDED FOLLOWING
1999 ** RANGE BETWEEN CURRENT ROW AND CURRENT ROW
2000 ** ROWS BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW
2001 **
2002 ** windowCodeDefaultStep() is the only one of the three functions that
2003 ** does not cache each partition in a temp table before beginning to
2004 ** return rows.
dan26522d12018-06-11 18:16:51 +00002005 */
dan54a9ab32018-06-14 14:27:05 +00002006 if( pMWin->eType==TK_ROWS
2007 && (pMWin->eStart!=TK_UNBOUNDED||pMWin->eEnd!=TK_CURRENT||!pMWin->pOrderBy)
dan09590aa2018-05-25 20:30:17 +00002008 ){
danc3a20c12018-05-23 20:55:37 +00002009 windowCodeRowExprStep(pParse, p, pWInfo, regGosub, addrGosub);
dan13078ca2018-06-13 20:29:38 +00002010 }else{
2011 Window *pWin;
dan54a9ab32018-06-14 14:27:05 +00002012 int bCache = 0; /* True to use CacheStep() */
danf9eae182018-05-21 19:45:11 +00002013
dan54a9ab32018-06-14 14:27:05 +00002014 if( pMWin->eStart==TK_CURRENT && pMWin->eEnd==TK_UNBOUNDED ){
dan13078ca2018-06-13 20:29:38 +00002015 bCache = 1;
2016 }else{
dan13078ca2018-06-13 20:29:38 +00002017 for(pWin=pMWin; pWin; pWin=pWin->pNextWin){
2018 FuncDef *pFunc = pWin->pFunc;
2019 if( (pFunc->funcFlags & SQLITE_FUNC_WINDOW_SIZE)
dan54a9ab32018-06-14 14:27:05 +00002020 || (pFunc->xSFunc==nth_valueStepFunc)
2021 || (pFunc->xSFunc==first_valueStepFunc)
2022 || (pFunc->xSFunc==leadStepFunc)
2023 || (pFunc->xSFunc==lagStepFunc)
2024 ){
dan13078ca2018-06-13 20:29:38 +00002025 bCache = 1;
2026 break;
2027 }
2028 }
2029 }
2030
2031 /* Otherwise, call windowCodeDefaultStep(). */
2032 if( bCache ){
dandfa552f2018-06-02 21:04:28 +00002033 windowCodeCacheStep(pParse, p, pWInfo, regGosub, addrGosub);
dan13078ca2018-06-13 20:29:38 +00002034 }else{
2035 windowCodeDefaultStep(pParse, p, pWInfo, regGosub, addrGosub);
dandfa552f2018-06-02 21:04:28 +00002036 }
danf690b572018-06-01 21:00:08 +00002037 }
danf9eae182018-05-21 19:45:11 +00002038}
2039