blob: 5aa8800111ef1de5b398e12217b4114c72f2a5de [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
dan67a9b8e2018-06-22 20:51:35 +000015#ifndef SQLITE_OMIT_WINDOWFUNC
16
dandfa552f2018-06-02 21:04:28 +000017/*
dan73925692018-06-12 18:40:17 +000018** SELECT REWRITING
19**
20** Any SELECT statement that contains one or more window functions in
21** either the select list or ORDER BY clause (the only two places window
22** functions may be used) is transformed by function sqlite3WindowRewrite()
23** in order to support window function processing. For example, with the
24** schema:
25**
26** CREATE TABLE t1(a, b, c, d, e, f, g);
27**
28** the statement:
29**
30** SELECT a+1, max(b) OVER (PARTITION BY c ORDER BY d) FROM t1 ORDER BY e;
31**
32** is transformed to:
33**
34** SELECT a+1, max(b) OVER (PARTITION BY c ORDER BY d) FROM (
35** SELECT a, e, c, d, b FROM t1 ORDER BY c, d
36** ) ORDER BY e;
37**
38** The flattening optimization is disabled when processing this transformed
39** SELECT statement. This allows the implementation of the window function
40** (in this case max()) to process rows sorted in order of (c, d), which
41** makes things easier for obvious reasons. More generally:
42**
43** * FROM, WHERE, GROUP BY and HAVING clauses are all moved to
44** the sub-query.
45**
46** * ORDER BY, LIMIT and OFFSET remain part of the parent query.
47**
48** * Terminals from each of the expression trees that make up the
49** select-list and ORDER BY expressions in the parent query are
50** selected by the sub-query. For the purposes of the transformation,
51** terminals are column references and aggregate functions.
52**
53** If there is more than one window function in the SELECT that uses
54** the same window declaration (the OVER bit), then a single scan may
55** be used to process more than one window function. For example:
56**
57** SELECT max(b) OVER (PARTITION BY c ORDER BY d),
58** min(e) OVER (PARTITION BY c ORDER BY d)
59** FROM t1;
60**
61** is transformed in the same way as the example above. However:
62**
63** SELECT max(b) OVER (PARTITION BY c ORDER BY d),
64** min(e) OVER (PARTITION BY a ORDER BY b)
65** FROM t1;
66**
67** Must be transformed to:
68**
69** SELECT max(b) OVER (PARTITION BY c ORDER BY d) FROM (
70** SELECT e, min(e) OVER (PARTITION BY a ORDER BY b), c, d, b FROM
71** SELECT a, e, c, d, b FROM t1 ORDER BY a, b
72** ) ORDER BY c, d
73** ) ORDER BY e;
74**
75** so that both min() and max() may process rows in the order defined by
76** their respective window declarations.
77**
78** INTERFACE WITH SELECT.C
79**
80** When processing the rewritten SELECT statement, code in select.c calls
81** sqlite3WhereBegin() to begin iterating through the results of the
82** sub-query, which is always implemented as a co-routine. It then calls
83** sqlite3WindowCodeStep() to process rows and finish the scan by calling
84** sqlite3WhereEnd().
85**
86** sqlite3WindowCodeStep() generates VM code so that, for each row returned
87** by the sub-query a sub-routine (OP_Gosub) coded by select.c is invoked.
88** When the sub-routine is invoked:
89**
90** * The results of all window-functions for the row are stored
91** in the associated Window.regResult registers.
92**
93** * The required terminal values are stored in the current row of
94** temp table Window.iEphCsr.
95**
96** In some cases, depending on the window frame and the specific window
97** functions invoked, sqlite3WindowCodeStep() caches each entire partition
98** in a temp table before returning any rows. In other cases it does not.
99** This detail is encapsulated within this file, the code generated by
100** select.c is the same in either case.
101**
102** BUILT-IN WINDOW FUNCTIONS
103**
104** This implementation features the following built-in window functions:
105**
106** row_number()
107** rank()
108** dense_rank()
109** percent_rank()
110** cume_dist()
111** ntile(N)
112** lead(expr [, offset [, default]])
113** lag(expr [, offset [, default]])
114** first_value(expr)
115** last_value(expr)
116** nth_value(expr, N)
117**
118** These are the same built-in window functions supported by Postgres.
119** Although the behaviour of aggregate window functions (functions that
120** can be used as either aggregates or window funtions) allows them to
121** be implemented using an API, built-in window functions are much more
122** esoteric. Additionally, some window functions (e.g. nth_value())
123** may only be implemented by caching the entire partition in memory.
124** As such, some built-in window functions use the same API as aggregate
125** window functions and some are implemented directly using VDBE
126** instructions. Additionally, for those functions that use the API, the
127** window frame is sometimes modified before the SELECT statement is
128** rewritten. For example, regardless of the specified window frame, the
129** row_number() function always uses:
130**
131** ROWS BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW
132**
133** See sqlite3WindowUpdate() for details.
danc0bb4452018-06-12 20:53:38 +0000134**
135** As well as some of the built-in window functions, aggregate window
136** functions min() and max() are implemented using VDBE instructions if
137** the start of the window frame is declared as anything other than
138** UNBOUNDED PRECEDING.
dan73925692018-06-12 18:40:17 +0000139*/
140
141/*
dandfa552f2018-06-02 21:04:28 +0000142** Implementation of built-in window function row_number(). Assumes that the
143** window frame has been coerced to:
144**
145** ROWS BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW
146*/
147static void row_numberStepFunc(
148 sqlite3_context *pCtx,
149 int nArg,
150 sqlite3_value **apArg
151){
152 i64 *p = (i64*)sqlite3_aggregate_context(pCtx, sizeof(*p));
153 if( p ) (*p)++;
154}
dan2a11bb22018-06-11 20:50:25 +0000155static void row_numberInvFunc(
dandfa552f2018-06-02 21:04:28 +0000156 sqlite3_context *pCtx,
157 int nArg,
158 sqlite3_value **apArg
159){
160}
161static void row_numberValueFunc(sqlite3_context *pCtx){
162 i64 *p = (i64*)sqlite3_aggregate_context(pCtx, sizeof(*p));
163 sqlite3_result_int64(pCtx, (p ? *p : 0));
164}
165
166/*
dan2a11bb22018-06-11 20:50:25 +0000167** Context object type used by rank(), dense_rank(), percent_rank() and
168** cume_dist().
dandfa552f2018-06-02 21:04:28 +0000169*/
170struct CallCount {
171 i64 nValue;
172 i64 nStep;
173 i64 nTotal;
174};
175
176/*
dan9c277582018-06-20 09:23:49 +0000177** Implementation of built-in window function dense_rank(). Assumes that
178** the window frame has been set to:
179**
180** RANGE BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW
dandfa552f2018-06-02 21:04:28 +0000181*/
182static void dense_rankStepFunc(
183 sqlite3_context *pCtx,
184 int nArg,
185 sqlite3_value **apArg
186){
187 struct CallCount *p;
188 p = (struct CallCount*)sqlite3_aggregate_context(pCtx, sizeof(*p));
189 if( p ) p->nStep = 1;
190}
dan2a11bb22018-06-11 20:50:25 +0000191static void dense_rankInvFunc(
dandfa552f2018-06-02 21:04:28 +0000192 sqlite3_context *pCtx,
193 int nArg,
194 sqlite3_value **apArg
195){
196}
197static void dense_rankValueFunc(sqlite3_context *pCtx){
198 struct CallCount *p;
199 p = (struct CallCount*)sqlite3_aggregate_context(pCtx, sizeof(*p));
200 if( p ){
201 if( p->nStep ){
202 p->nValue++;
203 p->nStep = 0;
204 }
205 sqlite3_result_int64(pCtx, p->nValue);
206 }
207}
208
209/*
dan9c277582018-06-20 09:23:49 +0000210** Implementation of built-in window function rank(). Assumes that
211** the window frame has been set to:
212**
213** RANGE BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW
dandfa552f2018-06-02 21:04:28 +0000214*/
215static void rankStepFunc(
216 sqlite3_context *pCtx,
217 int nArg,
218 sqlite3_value **apArg
219){
220 struct CallCount *p;
221 p = (struct CallCount*)sqlite3_aggregate_context(pCtx, sizeof(*p));
222 if( p ){
223 p->nStep++;
224 if( p->nValue==0 ){
225 p->nValue = p->nStep;
226 }
227 }
228}
dan2a11bb22018-06-11 20:50:25 +0000229static void rankInvFunc(
dandfa552f2018-06-02 21:04:28 +0000230 sqlite3_context *pCtx,
231 int nArg,
232 sqlite3_value **apArg
233){
234}
235static void rankValueFunc(sqlite3_context *pCtx){
236 struct CallCount *p;
237 p = (struct CallCount*)sqlite3_aggregate_context(pCtx, sizeof(*p));
238 if( p ){
239 sqlite3_result_int64(pCtx, p->nValue);
240 p->nValue = 0;
241 }
242}
243
244/*
dan9c277582018-06-20 09:23:49 +0000245** Implementation of built-in window function percent_rank(). Assumes that
246** the window frame has been set to:
247**
248** RANGE BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW
dandfa552f2018-06-02 21:04:28 +0000249*/
250static void percent_rankStepFunc(
251 sqlite3_context *pCtx,
252 int nArg,
253 sqlite3_value **apArg
254){
255 struct CallCount *p;
256 assert( nArg==1 );
257
dan9c277582018-06-20 09:23:49 +0000258 assert( sqlite3VdbeAssertAggContext(pCtx) );
dandfa552f2018-06-02 21:04:28 +0000259 p = (struct CallCount*)sqlite3_aggregate_context(pCtx, sizeof(*p));
dan9c277582018-06-20 09:23:49 +0000260 if( ALWAYS(p) ){
dandfa552f2018-06-02 21:04:28 +0000261 if( p->nTotal==0 ){
262 p->nTotal = sqlite3_value_int64(apArg[0]);
263 }
264 p->nStep++;
265 if( p->nValue==0 ){
266 p->nValue = p->nStep;
267 }
268 }
269}
dan2a11bb22018-06-11 20:50:25 +0000270static void percent_rankInvFunc(
dandfa552f2018-06-02 21:04:28 +0000271 sqlite3_context *pCtx,
272 int nArg,
273 sqlite3_value **apArg
274){
275}
276static void percent_rankValueFunc(sqlite3_context *pCtx){
277 struct CallCount *p;
278 p = (struct CallCount*)sqlite3_aggregate_context(pCtx, sizeof(*p));
279 if( p ){
280 if( p->nTotal>1 ){
281 double r = (double)(p->nValue-1) / (double)(p->nTotal-1);
282 sqlite3_result_double(pCtx, r);
283 }else{
danb7306f62018-06-21 19:20:39 +0000284 sqlite3_result_double(pCtx, 0.0);
dandfa552f2018-06-02 21:04:28 +0000285 }
286 p->nValue = 0;
287 }
288}
289
dan9c277582018-06-20 09:23:49 +0000290/*
291** Implementation of built-in window function cume_dist(). Assumes that
292** the window frame has been set to:
293**
294** RANGE BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW
295*/
danf1abe362018-06-04 08:22:09 +0000296static void cume_distStepFunc(
297 sqlite3_context *pCtx,
298 int nArg,
299 sqlite3_value **apArg
300){
301 struct CallCount *p;
302 assert( nArg==1 );
303
dan9c277582018-06-20 09:23:49 +0000304 assert( sqlite3VdbeAssertAggContext(pCtx) );
danf1abe362018-06-04 08:22:09 +0000305 p = (struct CallCount*)sqlite3_aggregate_context(pCtx, sizeof(*p));
dan9c277582018-06-20 09:23:49 +0000306 if( ALWAYS(p) ){
danf1abe362018-06-04 08:22:09 +0000307 if( p->nTotal==0 ){
308 p->nTotal = sqlite3_value_int64(apArg[0]);
309 }
310 p->nStep++;
311 }
312}
dan2a11bb22018-06-11 20:50:25 +0000313static void cume_distInvFunc(
danf1abe362018-06-04 08:22:09 +0000314 sqlite3_context *pCtx,
315 int nArg,
316 sqlite3_value **apArg
317){
318}
319static void cume_distValueFunc(sqlite3_context *pCtx){
320 struct CallCount *p;
321 p = (struct CallCount*)sqlite3_aggregate_context(pCtx, sizeof(*p));
dan660af932018-06-18 16:55:22 +0000322 if( p && p->nTotal ){
danf1abe362018-06-04 08:22:09 +0000323 double r = (double)(p->nStep) / (double)(p->nTotal);
324 sqlite3_result_double(pCtx, r);
325 }
326}
327
dan2a11bb22018-06-11 20:50:25 +0000328/*
329** Context object for ntile() window function.
330*/
dan6bc5c9e2018-06-04 18:55:11 +0000331struct NtileCtx {
332 i64 nTotal; /* Total rows in partition */
333 i64 nParam; /* Parameter passed to ntile(N) */
334 i64 iRow; /* Current row */
335};
336
337/*
338** Implementation of ntile(). This assumes that the window frame has
339** been coerced to:
340**
341** ROWS UNBOUNDED PRECEDING AND CURRENT ROW
dan6bc5c9e2018-06-04 18:55:11 +0000342*/
343static void ntileStepFunc(
344 sqlite3_context *pCtx,
345 int nArg,
346 sqlite3_value **apArg
347){
348 struct NtileCtx *p;
349 assert( nArg==2 );
350 p = (struct NtileCtx*)sqlite3_aggregate_context(pCtx, sizeof(*p));
dan17074e32018-06-22 17:57:10 +0000351 if( p ){
dan6bc5c9e2018-06-04 18:55:11 +0000352 if( p->nTotal==0 ){
353 p->nParam = sqlite3_value_int64(apArg[0]);
354 p->nTotal = sqlite3_value_int64(apArg[1]);
355 if( p->nParam<=0 ){
356 sqlite3_result_error(
357 pCtx, "argument of ntile must be a positive integer", -1
358 );
359 }
360 }
361 p->iRow++;
362 }
363}
dan2a11bb22018-06-11 20:50:25 +0000364static void ntileInvFunc(
dan6bc5c9e2018-06-04 18:55:11 +0000365 sqlite3_context *pCtx,
366 int nArg,
367 sqlite3_value **apArg
368){
369}
370static void ntileValueFunc(sqlite3_context *pCtx){
371 struct NtileCtx *p;
372 p = (struct NtileCtx*)sqlite3_aggregate_context(pCtx, sizeof(*p));
373 if( p && p->nParam>0 ){
374 int nSize = (p->nTotal / p->nParam);
375 if( nSize==0 ){
376 sqlite3_result_int64(pCtx, p->iRow);
377 }else{
378 i64 nLarge = p->nTotal - p->nParam*nSize;
379 i64 iSmall = nLarge*(nSize+1);
380 i64 iRow = p->iRow-1;
381
382 assert( (nLarge*(nSize+1) + (p->nParam-nLarge)*nSize)==p->nTotal );
383
384 if( iRow<iSmall ){
385 sqlite3_result_int64(pCtx, 1 + iRow/(nSize+1));
386 }else{
387 sqlite3_result_int64(pCtx, 1 + nLarge + (iRow-iSmall)/nSize);
388 }
389 }
390 }
391}
392
dan2a11bb22018-06-11 20:50:25 +0000393/*
394** Context object for last_value() window function.
395*/
dan1c5ed622018-06-05 16:16:17 +0000396struct LastValueCtx {
397 sqlite3_value *pVal;
398 int nVal;
399};
400
401/*
402** Implementation of last_value().
403*/
404static void last_valueStepFunc(
405 sqlite3_context *pCtx,
406 int nArg,
407 sqlite3_value **apArg
408){
409 struct LastValueCtx *p;
dan9c277582018-06-20 09:23:49 +0000410 p = (struct LastValueCtx*)sqlite3_aggregate_context(pCtx, sizeof(*p));
dan1c5ed622018-06-05 16:16:17 +0000411 if( p ){
412 sqlite3_value_free(p->pVal);
413 p->pVal = sqlite3_value_dup(apArg[0]);
dan6fde1792018-06-15 19:01:35 +0000414 if( p->pVal==0 ){
415 sqlite3_result_error_nomem(pCtx);
416 }else{
417 p->nVal++;
418 }
dan1c5ed622018-06-05 16:16:17 +0000419 }
420}
dan2a11bb22018-06-11 20:50:25 +0000421static void last_valueInvFunc(
dan1c5ed622018-06-05 16:16:17 +0000422 sqlite3_context *pCtx,
423 int nArg,
424 sqlite3_value **apArg
425){
426 struct LastValueCtx *p;
dan9c277582018-06-20 09:23:49 +0000427 p = (struct LastValueCtx*)sqlite3_aggregate_context(pCtx, sizeof(*p));
428 if( ALWAYS(p) ){
dan1c5ed622018-06-05 16:16:17 +0000429 p->nVal--;
430 if( p->nVal==0 ){
431 sqlite3_value_free(p->pVal);
432 p->pVal = 0;
433 }
434 }
435}
436static void last_valueValueFunc(sqlite3_context *pCtx){
437 struct LastValueCtx *p;
dan9c277582018-06-20 09:23:49 +0000438 p = (struct LastValueCtx*)sqlite3_aggregate_context(pCtx, sizeof(*p));
dan1c5ed622018-06-05 16:16:17 +0000439 if( p && p->pVal ){
440 sqlite3_result_value(pCtx, p->pVal);
441 }
442}
443static void last_valueFinalizeFunc(sqlite3_context *pCtx){
444 struct LastValueCtx *p;
dan9c277582018-06-20 09:23:49 +0000445 p = (struct LastValueCtx*)sqlite3_aggregate_context(pCtx, sizeof(*p));
dan1c5ed622018-06-05 16:16:17 +0000446 if( p && p->pVal ){
447 sqlite3_result_value(pCtx, p->pVal);
448 sqlite3_value_free(p->pVal);
449 p->pVal = 0;
450 }
451}
452
dan2a11bb22018-06-11 20:50:25 +0000453/*
454** No-op implementations of nth_value(), first_value(), lead() and lag().
455** These are all implemented inline using VDBE instructions.
456*/
457static void nth_valueStepFunc(sqlite3_context *pCtx, int n, sqlite3_value **a){}
458static void nth_valueInvFunc(sqlite3_context *pCtx, int n, sqlite3_value **ap){}
459static void nth_valueValueFunc(sqlite3_context *pCtx){}
460static void first_valueStepFunc(sqlite3_context *p, int n, sqlite3_value **ap){}
461static void first_valueInvFunc(sqlite3_context *p, int n, sqlite3_value **ap){}
462static void first_valueValueFunc(sqlite3_context *pCtx){}
463static void leadStepFunc(sqlite3_context *pCtx, int n, sqlite3_value **ap){}
464static void leadInvFunc(sqlite3_context *pCtx, int n, sqlite3_value **ap){}
465static void leadValueFunc(sqlite3_context *pCtx){}
466static void lagStepFunc(sqlite3_context *pCtx, int n, sqlite3_value **ap){}
467static void lagInvFunc(sqlite3_context *pCtx, int n, sqlite3_value **ap){}
468static void lagValueFunc(sqlite3_context *pCtx){}
danfe4e25a2018-06-07 20:08:59 +0000469
dandfa552f2018-06-02 21:04:28 +0000470#define WINDOWFUNC(name,nArg,extra) { \
471 nArg, (SQLITE_UTF8|SQLITE_FUNC_WINDOW|extra), 0, 0, \
472 name ## StepFunc, name ## ValueFunc, name ## ValueFunc, \
dan2a11bb22018-06-11 20:50:25 +0000473 name ## InvFunc, #name \
dandfa552f2018-06-02 21:04:28 +0000474}
475
dan1c5ed622018-06-05 16:16:17 +0000476#define WINDOWFUNCF(name,nArg,extra) { \
477 nArg, (SQLITE_UTF8|SQLITE_FUNC_WINDOW|extra), 0, 0, \
478 name ## StepFunc, name ## FinalizeFunc, name ## ValueFunc, \
dan2a11bb22018-06-11 20:50:25 +0000479 name ## InvFunc, #name \
dan1c5ed622018-06-05 16:16:17 +0000480}
481
dandfa552f2018-06-02 21:04:28 +0000482/*
483** Register those built-in window functions that are not also aggregates.
484*/
485void sqlite3WindowFunctions(void){
486 static FuncDef aWindowFuncs[] = {
487 WINDOWFUNC(row_number, 0, 0),
488 WINDOWFUNC(dense_rank, 0, 0),
489 WINDOWFUNC(rank, 0, 0),
490 WINDOWFUNC(percent_rank, 0, SQLITE_FUNC_WINDOW_SIZE),
danf1abe362018-06-04 08:22:09 +0000491 WINDOWFUNC(cume_dist, 0, SQLITE_FUNC_WINDOW_SIZE),
dan6bc5c9e2018-06-04 18:55:11 +0000492 WINDOWFUNC(ntile, 1, SQLITE_FUNC_WINDOW_SIZE),
dan1c5ed622018-06-05 16:16:17 +0000493 WINDOWFUNCF(last_value, 1, 0),
dandfa552f2018-06-02 21:04:28 +0000494 WINDOWFUNC(nth_value, 2, 0),
dan7095c002018-06-07 17:45:22 +0000495 WINDOWFUNC(first_value, 1, 0),
danfe4e25a2018-06-07 20:08:59 +0000496 WINDOWFUNC(lead, 1, 0), WINDOWFUNC(lead, 2, 0), WINDOWFUNC(lead, 3, 0),
497 WINDOWFUNC(lag, 1, 0), WINDOWFUNC(lag, 2, 0), WINDOWFUNC(lag, 3, 0),
dandfa552f2018-06-02 21:04:28 +0000498 };
499 sqlite3InsertBuiltinFuncs(aWindowFuncs, ArraySize(aWindowFuncs));
500}
501
danc0bb4452018-06-12 20:53:38 +0000502/*
503** This function is called immediately after resolving the function name
504** for a window function within a SELECT statement. Argument pList is a
505** linked list of WINDOW definitions for the current SELECT statement.
506** Argument pFunc is the function definition just resolved and pWin
507** is the Window object representing the associated OVER clause. This
508** function updates the contents of pWin as follows:
509**
510** * If the OVER clause refered to a named window (as in "max(x) OVER win"),
511** search list pList for a matching WINDOW definition, and update pWin
512** accordingly. If no such WINDOW clause can be found, leave an error
513** in pParse.
514**
515** * If the function is a built-in window function that requires the
516** window to be coerced (see "BUILT-IN WINDOW FUNCTIONS" at the top
517** of this file), pWin is updated here.
518*/
dane3bf6322018-06-08 20:58:27 +0000519void sqlite3WindowUpdate(
520 Parse *pParse,
danc0bb4452018-06-12 20:53:38 +0000521 Window *pList, /* List of named windows for this SELECT */
522 Window *pWin, /* Window frame to update */
523 FuncDef *pFunc /* Window function definition */
dane3bf6322018-06-08 20:58:27 +0000524){
danc95f38d2018-06-18 20:34:43 +0000525 if( pWin->zName && pWin->eType==0 ){
dane3bf6322018-06-08 20:58:27 +0000526 Window *p;
527 for(p=pList; p; p=p->pNextWin){
528 if( sqlite3StrICmp(p->zName, pWin->zName)==0 ) break;
529 }
530 if( p==0 ){
531 sqlite3ErrorMsg(pParse, "no such window: %s", pWin->zName);
532 return;
533 }
534 pWin->pPartition = sqlite3ExprListDup(pParse->db, p->pPartition, 0);
535 pWin->pOrderBy = sqlite3ExprListDup(pParse->db, p->pOrderBy, 0);
536 pWin->pStart = sqlite3ExprDup(pParse->db, p->pStart, 0);
537 pWin->pEnd = sqlite3ExprDup(pParse->db, p->pEnd, 0);
538 pWin->eStart = p->eStart;
539 pWin->eEnd = p->eEnd;
danc95f38d2018-06-18 20:34:43 +0000540 pWin->eType = p->eType;
dane3bf6322018-06-08 20:58:27 +0000541 }
dandfa552f2018-06-02 21:04:28 +0000542 if( pFunc->funcFlags & SQLITE_FUNC_WINDOW ){
543 sqlite3 *db = pParse->db;
dan8b985602018-06-09 17:43:45 +0000544 if( pWin->pFilter ){
545 sqlite3ErrorMsg(pParse,
546 "FILTER clause may only be used with aggregate window functions"
547 );
548 }else
dan6bc5c9e2018-06-04 18:55:11 +0000549 if( pFunc->xSFunc==row_numberStepFunc || pFunc->xSFunc==ntileStepFunc ){
dandfa552f2018-06-02 21:04:28 +0000550 sqlite3ExprDelete(db, pWin->pStart);
551 sqlite3ExprDelete(db, pWin->pEnd);
552 pWin->pStart = pWin->pEnd = 0;
553 pWin->eType = TK_ROWS;
554 pWin->eStart = TK_UNBOUNDED;
555 pWin->eEnd = TK_CURRENT;
dan8b985602018-06-09 17:43:45 +0000556 }else
dandfa552f2018-06-02 21:04:28 +0000557
558 if( pFunc->xSFunc==dense_rankStepFunc || pFunc->xSFunc==rankStepFunc
danf1abe362018-06-04 08:22:09 +0000559 || pFunc->xSFunc==percent_rankStepFunc || pFunc->xSFunc==cume_distStepFunc
dandfa552f2018-06-02 21:04:28 +0000560 ){
561 sqlite3ExprDelete(db, pWin->pStart);
562 sqlite3ExprDelete(db, pWin->pEnd);
563 pWin->pStart = pWin->pEnd = 0;
564 pWin->eType = TK_RANGE;
565 pWin->eStart = TK_UNBOUNDED;
566 pWin->eEnd = TK_CURRENT;
567 }
568 }
dan2a11bb22018-06-11 20:50:25 +0000569 pWin->pFunc = pFunc;
dandfa552f2018-06-02 21:04:28 +0000570}
571
danc0bb4452018-06-12 20:53:38 +0000572/*
573** Context object passed through sqlite3WalkExprList() to
574** selectWindowRewriteExprCb() by selectWindowRewriteEList().
575*/
dandfa552f2018-06-02 21:04:28 +0000576typedef struct WindowRewrite WindowRewrite;
577struct WindowRewrite {
578 Window *pWin;
579 ExprList *pSub;
580};
581
danc0bb4452018-06-12 20:53:38 +0000582/*
583** Callback function used by selectWindowRewriteEList(). If necessary,
584** this function appends to the output expression-list and updates
585** expression (*ppExpr) in place.
586*/
dandfa552f2018-06-02 21:04:28 +0000587static int selectWindowRewriteExprCb(Walker *pWalker, Expr *pExpr){
588 struct WindowRewrite *p = pWalker->u.pRewrite;
589 Parse *pParse = pWalker->pParse;
590
591 switch( pExpr->op ){
592
593 case TK_FUNCTION:
594 if( pExpr->pWin==0 ){
595 break;
596 }else{
597 Window *pWin;
598 for(pWin=p->pWin; pWin; pWin=pWin->pNextWin){
599 if( pExpr->pWin==pWin ){
dan2a11bb22018-06-11 20:50:25 +0000600 assert( pWin->pOwner==pExpr );
dandfa552f2018-06-02 21:04:28 +0000601 return WRC_Prune;
602 }
603 }
604 }
605 /* Fall through. */
606
dan73925692018-06-12 18:40:17 +0000607 case TK_AGG_FUNCTION:
dandfa552f2018-06-02 21:04:28 +0000608 case TK_COLUMN: {
609 Expr *pDup = sqlite3ExprDup(pParse->db, pExpr, 0);
610 p->pSub = sqlite3ExprListAppend(pParse, p->pSub, pDup);
611 if( p->pSub ){
612 assert( ExprHasProperty(pExpr, EP_Static)==0 );
613 ExprSetProperty(pExpr, EP_Static);
614 sqlite3ExprDelete(pParse->db, pExpr);
615 ExprClearProperty(pExpr, EP_Static);
616 memset(pExpr, 0, sizeof(Expr));
617
618 pExpr->op = TK_COLUMN;
619 pExpr->iColumn = p->pSub->nExpr-1;
620 pExpr->iTable = p->pWin->iEphCsr;
621 }
622
623 break;
624 }
625
626 default: /* no-op */
627 break;
628 }
629
630 return WRC_Continue;
631}
danc0bb4452018-06-12 20:53:38 +0000632static int selectWindowRewriteSelectCb(Walker *pWalker, Select *pSelect){
633 return WRC_Prune;
634}
dandfa552f2018-06-02 21:04:28 +0000635
danc0bb4452018-06-12 20:53:38 +0000636
637/*
638** Iterate through each expression in expression-list pEList. For each:
639**
640** * TK_COLUMN,
641** * aggregate function, or
642** * window function with a Window object that is not a member of the
643** linked list passed as the second argument (pWin)
644**
645** Append the node to output expression-list (*ppSub). And replace it
646** with a TK_COLUMN that reads the (N-1)th element of table
647** pWin->iEphCsr, where N is the number of elements in (*ppSub) after
648** appending the new one.
649*/
dan13b08bb2018-06-15 20:46:12 +0000650static void selectWindowRewriteEList(
dandfa552f2018-06-02 21:04:28 +0000651 Parse *pParse,
652 Window *pWin,
653 ExprList *pEList, /* Rewrite expressions in this list */
654 ExprList **ppSub /* IN/OUT: Sub-select expression-list */
655){
656 Walker sWalker;
657 WindowRewrite sRewrite;
dandfa552f2018-06-02 21:04:28 +0000658
659 memset(&sWalker, 0, sizeof(Walker));
660 memset(&sRewrite, 0, sizeof(WindowRewrite));
661
662 sRewrite.pSub = *ppSub;
663 sRewrite.pWin = pWin;
664
665 sWalker.pParse = pParse;
666 sWalker.xExprCallback = selectWindowRewriteExprCb;
667 sWalker.xSelectCallback = selectWindowRewriteSelectCb;
668 sWalker.u.pRewrite = &sRewrite;
669
dan13b08bb2018-06-15 20:46:12 +0000670 (void)sqlite3WalkExprList(&sWalker, pEList);
dandfa552f2018-06-02 21:04:28 +0000671
672 *ppSub = sRewrite.pSub;
dandfa552f2018-06-02 21:04:28 +0000673}
674
danc0bb4452018-06-12 20:53:38 +0000675/*
676** Append a copy of each expression in expression-list pAppend to
677** expression list pList. Return a pointer to the result list.
678*/
dandfa552f2018-06-02 21:04:28 +0000679static ExprList *exprListAppendList(
680 Parse *pParse, /* Parsing context */
681 ExprList *pList, /* List to which to append. Might be NULL */
682 ExprList *pAppend /* List of values to append. Might be NULL */
683){
684 if( pAppend ){
685 int i;
686 int nInit = pList ? pList->nExpr : 0;
687 for(i=0; i<pAppend->nExpr; i++){
688 Expr *pDup = sqlite3ExprDup(pParse->db, pAppend->a[i].pExpr, 0);
689 pList = sqlite3ExprListAppend(pParse, pList, pDup);
690 if( pList ) pList->a[nInit+i].sortOrder = pAppend->a[i].sortOrder;
691 }
692 }
693 return pList;
694}
695
696/*
697** If the SELECT statement passed as the second argument does not invoke
698** any SQL window functions, this function is a no-op. Otherwise, it
699** rewrites the SELECT statement so that window function xStep functions
danc0bb4452018-06-12 20:53:38 +0000700** are invoked in the correct order as described under "SELECT REWRITING"
701** at the top of this file.
dandfa552f2018-06-02 21:04:28 +0000702*/
703int sqlite3WindowRewrite(Parse *pParse, Select *p){
704 int rc = SQLITE_OK;
705 if( p->pWin ){
706 Vdbe *v = sqlite3GetVdbe(pParse);
dandfa552f2018-06-02 21:04:28 +0000707 sqlite3 *db = pParse->db;
708 Select *pSub = 0; /* The subquery */
709 SrcList *pSrc = p->pSrc;
710 Expr *pWhere = p->pWhere;
711 ExprList *pGroupBy = p->pGroupBy;
712 Expr *pHaving = p->pHaving;
713 ExprList *pSort = 0;
714
715 ExprList *pSublist = 0; /* Expression list for sub-query */
716 Window *pMWin = p->pWin; /* Master window object */
717 Window *pWin; /* Window object iterator */
718
719 p->pSrc = 0;
720 p->pWhere = 0;
721 p->pGroupBy = 0;
722 p->pHaving = 0;
723
724 /* Assign a cursor number for the ephemeral table used to buffer rows.
725 ** The OpenEphemeral instruction is coded later, after it is known how
726 ** many columns the table will have. */
727 pMWin->iEphCsr = pParse->nTab++;
728
dan13b08bb2018-06-15 20:46:12 +0000729 selectWindowRewriteEList(pParse, pMWin, p->pEList, &pSublist);
730 selectWindowRewriteEList(pParse, pMWin, p->pOrderBy, &pSublist);
dandfa552f2018-06-02 21:04:28 +0000731 pMWin->nBufferCol = (pSublist ? pSublist->nExpr : 0);
732
733 /* Create the ORDER BY clause for the sub-select. This is the concatenation
734 ** of the window PARTITION and ORDER BY clauses. Append the same
735 ** expressions to the sub-select expression list. They are required to
736 ** figure out where boundaries for partitions and sets of peer rows. */
737 pSort = sqlite3ExprListDup(db, pMWin->pPartition, 0);
738 if( pMWin->pOrderBy ){
739 pSort = exprListAppendList(pParse, pSort, pMWin->pOrderBy);
740 }
741 pSublist = exprListAppendList(pParse, pSublist, pSort);
742
743 /* Append the arguments passed to each window function to the
744 ** sub-select expression list. Also allocate two registers for each
745 ** window function - one for the accumulator, another for interim
746 ** results. */
747 for(pWin=pMWin; pWin; pWin=pWin->pNextWin){
748 pWin->iArgCol = (pSublist ? pSublist->nExpr : 0);
749 pSublist = exprListAppendList(pParse, pSublist, pWin->pOwner->x.pList);
dan8b985602018-06-09 17:43:45 +0000750 if( pWin->pFilter ){
751 Expr *pFilter = sqlite3ExprDup(db, pWin->pFilter, 0);
752 pSublist = sqlite3ExprListAppend(pParse, pSublist, pFilter);
753 }
dandfa552f2018-06-02 21:04:28 +0000754 pWin->regAccum = ++pParse->nMem;
755 pWin->regResult = ++pParse->nMem;
756 sqlite3VdbeAddOp2(v, OP_Null, 0, pWin->regAccum);
757 }
758
dan9c277582018-06-20 09:23:49 +0000759 /* If there is no ORDER BY or PARTITION BY clause, and the window
760 ** function accepts zero arguments, and there are no other columns
761 ** selected (e.g. "SELECT row_number() OVER () FROM t1"), it is possible
762 ** that pSublist is still NULL here. Add a constant expression here to
763 ** keep everything legal in this case.
764 */
765 if( pSublist==0 ){
766 pSublist = sqlite3ExprListAppend(pParse, 0,
767 sqlite3ExprAlloc(db, TK_INTEGER, &sqlite3IntTokens[0], 0)
768 );
769 }
770
dandfa552f2018-06-02 21:04:28 +0000771 pSub = sqlite3SelectNew(
772 pParse, pSublist, pSrc, pWhere, pGroupBy, pHaving, pSort, 0, 0
773 );
774 p->pSrc = sqlite3SrcListAppend(db, 0, 0, 0);
dan6fde1792018-06-15 19:01:35 +0000775 assert( p->pSrc || db->mallocFailed );
dandfa552f2018-06-02 21:04:28 +0000776 if( p->pSrc ){
dandfa552f2018-06-02 21:04:28 +0000777 p->pSrc->a[0].pSelect = pSub;
778 sqlite3SrcListAssignCursors(pParse, p->pSrc);
779 if( sqlite3ExpandSubquery(pParse, &p->pSrc->a[0]) ){
780 rc = SQLITE_NOMEM;
781 }else{
782 pSub->selFlags |= SF_Expanded;
dan73925692018-06-12 18:40:17 +0000783 p->selFlags &= ~SF_Aggregate;
784 sqlite3SelectPrep(pParse, pSub, 0);
dandfa552f2018-06-02 21:04:28 +0000785 }
dandfa552f2018-06-02 21:04:28 +0000786
dan6fde1792018-06-15 19:01:35 +0000787 sqlite3VdbeAddOp2(v, OP_OpenEphemeral, pMWin->iEphCsr, pSublist->nExpr);
788 }else{
789 sqlite3SelectDelete(db, pSub);
790 }
791 if( db->mallocFailed ) rc = SQLITE_NOMEM;
dandfa552f2018-06-02 21:04:28 +0000792 }
793
794 return rc;
795}
796
danc0bb4452018-06-12 20:53:38 +0000797/*
798** Free the Window object passed as the second argument.
799*/
dan86fb6e12018-05-16 20:58:07 +0000800void sqlite3WindowDelete(sqlite3 *db, Window *p){
801 if( p ){
802 sqlite3ExprDelete(db, p->pFilter);
803 sqlite3ExprListDelete(db, p->pPartition);
804 sqlite3ExprListDelete(db, p->pOrderBy);
805 sqlite3ExprDelete(db, p->pEnd);
806 sqlite3ExprDelete(db, p->pStart);
dane3bf6322018-06-08 20:58:27 +0000807 sqlite3DbFree(db, p->zName);
dan86fb6e12018-05-16 20:58:07 +0000808 sqlite3DbFree(db, p);
809 }
810}
811
danc0bb4452018-06-12 20:53:38 +0000812/*
813** Free the linked list of Window objects starting at the second argument.
814*/
dane3bf6322018-06-08 20:58:27 +0000815void sqlite3WindowListDelete(sqlite3 *db, Window *p){
816 while( p ){
817 Window *pNext = p->pNextWin;
818 sqlite3WindowDelete(db, p);
819 p = pNext;
820 }
821}
822
danc0bb4452018-06-12 20:53:38 +0000823/*
824** Allocate and return a new Window object.
825*/
dan86fb6e12018-05-16 20:58:07 +0000826Window *sqlite3WindowAlloc(
827 Parse *pParse,
828 int eType,
danc3a20c12018-05-23 20:55:37 +0000829 int eStart, Expr *pStart,
830 int eEnd, Expr *pEnd
dan86fb6e12018-05-16 20:58:07 +0000831){
832 Window *pWin = (Window*)sqlite3DbMallocZero(pParse->db, sizeof(Window));
833
834 if( pWin ){
danc95f38d2018-06-18 20:34:43 +0000835 assert( eType );
dan86fb6e12018-05-16 20:58:07 +0000836 pWin->eType = eType;
837 pWin->eStart = eStart;
838 pWin->eEnd = eEnd;
839 pWin->pEnd = pEnd;
840 pWin->pStart = pStart;
841 }else{
842 sqlite3ExprDelete(pParse->db, pEnd);
843 sqlite3ExprDelete(pParse->db, pStart);
844 }
845
846 return pWin;
847}
848
danc0bb4452018-06-12 20:53:38 +0000849/*
850** Attach window object pWin to expression p.
851*/
dan86fb6e12018-05-16 20:58:07 +0000852void sqlite3WindowAttach(Parse *pParse, Expr *p, Window *pWin){
853 if( p ){
854 p->pWin = pWin;
dan2a11bb22018-06-11 20:50:25 +0000855 if( pWin ) pWin->pOwner = p;
dan86fb6e12018-05-16 20:58:07 +0000856 }else{
857 sqlite3WindowDelete(pParse->db, pWin);
858 }
859}
dane2f781b2018-05-17 19:24:08 +0000860
861/*
862** Return 0 if the two window objects are identical, or non-zero otherwise.
dan13078ca2018-06-13 20:29:38 +0000863** Identical window objects can be processed in a single scan.
dane2f781b2018-05-17 19:24:08 +0000864*/
865int sqlite3WindowCompare(Parse *pParse, Window *p1, Window *p2){
866 if( p1->eType!=p2->eType ) return 1;
867 if( p1->eStart!=p2->eStart ) return 1;
868 if( p1->eEnd!=p2->eEnd ) return 1;
869 if( sqlite3ExprCompare(pParse, p1->pStart, p2->pStart, -1) ) return 1;
870 if( sqlite3ExprCompare(pParse, p1->pEnd, p2->pEnd, -1) ) return 1;
871 if( sqlite3ExprListCompare(p1->pPartition, p2->pPartition, -1) ) return 1;
872 if( sqlite3ExprListCompare(p1->pOrderBy, p2->pOrderBy, -1) ) return 1;
873 return 0;
874}
875
dan13078ca2018-06-13 20:29:38 +0000876
877/*
878** This is called by code in select.c before it calls sqlite3WhereBegin()
879** to begin iterating through the sub-query results. It is used to allocate
880** and initialize registers and cursors used by sqlite3WindowCodeStep().
881*/
882void sqlite3WindowCodeInit(Parse *pParse, Window *pMWin){
danc9a86682018-05-30 20:44:58 +0000883 Window *pWin;
dan13078ca2018-06-13 20:29:38 +0000884 Vdbe *v = sqlite3GetVdbe(pParse);
885 int nPart = (pMWin->pPartition ? pMWin->pPartition->nExpr : 0);
886 nPart += (pMWin->pOrderBy ? pMWin->pOrderBy->nExpr : 0);
887 if( nPart ){
888 pMWin->regPart = pParse->nMem+1;
889 pParse->nMem += nPart;
890 sqlite3VdbeAddOp3(v, OP_Null, 0, pMWin->regPart, pMWin->regPart+nPart-1);
891 }
892
danc9a86682018-05-30 20:44:58 +0000893 for(pWin=pMWin; pWin; pWin=pWin->pNextWin){
danec891fd2018-06-06 20:51:02 +0000894 FuncDef *p = pWin->pFunc;
895 if( (p->funcFlags & SQLITE_FUNC_MINMAX) && pWin->eStart!=TK_UNBOUNDED ){
dan9a947222018-06-14 19:06:36 +0000896 /* The inline versions of min() and max() require a single ephemeral
897 ** table and 3 registers. The registers are used as follows:
898 **
899 ** regApp+0: slot to copy min()/max() argument to for MakeRecord
900 ** regApp+1: integer value used to ensure keys are unique
901 ** regApp+2: output of MakeRecord
902 */
danc9a86682018-05-30 20:44:58 +0000903 ExprList *pList = pWin->pOwner->x.pList;
904 KeyInfo *pKeyInfo = sqlite3KeyInfoFromExprList(pParse, pList, 0, 0);
danc9a86682018-05-30 20:44:58 +0000905 pWin->csrApp = pParse->nTab++;
906 pWin->regApp = pParse->nMem+1;
907 pParse->nMem += 3;
908 if( pKeyInfo && pWin->pFunc->zName[1]=='i' ){
909 assert( pKeyInfo->aSortOrder[0]==0 );
910 pKeyInfo->aSortOrder[0] = 1;
911 }
912 sqlite3VdbeAddOp2(v, OP_OpenEphemeral, pWin->csrApp, 2);
913 sqlite3VdbeAppendP4(v, pKeyInfo, P4_KEYINFO);
914 sqlite3VdbeAddOp2(v, OP_Integer, 0, pWin->regApp+1);
915 }
dan7095c002018-06-07 17:45:22 +0000916 else if( p->xSFunc==nth_valueStepFunc || p->xSFunc==first_valueStepFunc ){
danec891fd2018-06-06 20:51:02 +0000917 /* Allocate two registers at pWin->regApp. These will be used to
918 ** store the start and end index of the current frame. */
919 assert( pMWin->iEphCsr );
920 pWin->regApp = pParse->nMem+1;
921 pWin->csrApp = pParse->nTab++;
922 pParse->nMem += 2;
danec891fd2018-06-06 20:51:02 +0000923 sqlite3VdbeAddOp2(v, OP_OpenDup, pWin->csrApp, pMWin->iEphCsr);
924 }
danfe4e25a2018-06-07 20:08:59 +0000925 else if( p->xSFunc==leadStepFunc || p->xSFunc==lagStepFunc ){
926 assert( pMWin->iEphCsr );
927 pWin->csrApp = pParse->nTab++;
928 sqlite3VdbeAddOp2(v, OP_OpenDup, pWin->csrApp, pMWin->iEphCsr);
929 }
danc9a86682018-05-30 20:44:58 +0000930 }
931}
932
dan13078ca2018-06-13 20:29:38 +0000933/*
934** A "PRECEDING <expr>" (bEnd==0) or "FOLLOWING <expr>" (bEnd==1) has just
935** been evaluated and the result left in register reg. This function generates
936** VM code to check that the value is a non-negative integer and throws
937** an exception if it is not.
938*/
danc3a20c12018-05-23 20:55:37 +0000939static void windowCheckFrameValue(Parse *pParse, int reg, int bEnd){
940 static const char *azErr[] = {
941 "frame starting offset must be a non-negative integer",
942 "frame ending offset must be a non-negative integer"
943 };
944 Vdbe *v = sqlite3GetVdbe(pParse);
dan13078ca2018-06-13 20:29:38 +0000945 int regZero = sqlite3GetTempReg(pParse);
danc3a20c12018-05-23 20:55:37 +0000946 sqlite3VdbeAddOp2(v, OP_Integer, 0, regZero);
947 sqlite3VdbeAddOp2(v, OP_MustBeInt, reg, sqlite3VdbeCurrentAddr(v)+2);
948 sqlite3VdbeAddOp3(v, OP_Ge, regZero, sqlite3VdbeCurrentAddr(v)+2, reg);
949 sqlite3VdbeAddOp2(v, OP_Halt, SQLITE_ERROR, OE_Abort);
950 sqlite3VdbeAppendP4(v, (void*)azErr[bEnd], P4_STATIC);
dan13078ca2018-06-13 20:29:38 +0000951 sqlite3ReleaseTempReg(pParse, regZero);
danc3a20c12018-05-23 20:55:37 +0000952}
953
dan13078ca2018-06-13 20:29:38 +0000954/*
955** Return the number of arguments passed to the window-function associated
956** with the object passed as the only argument to this function.
957*/
dan2a11bb22018-06-11 20:50:25 +0000958static int windowArgCount(Window *pWin){
959 ExprList *pList = pWin->pOwner->x.pList;
960 return (pList ? pList->nExpr : 0);
961}
962
danc9a86682018-05-30 20:44:58 +0000963/*
964** Generate VM code to invoke either xStep() (if bInverse is 0) or
965** xInverse (if bInverse is non-zero) for each window function in the
dan13078ca2018-06-13 20:29:38 +0000966** linked list starting at pMWin. Or, for built-in window functions
967** that do not use the standard function API, generate the required
968** inline VM code.
969**
970** If argument csr is greater than or equal to 0, then argument reg is
971** the first register in an array of registers guaranteed to be large
972** enough to hold the array of arguments for each function. In this case
973** the arguments are extracted from the current row of csr into the
974** array of registers before invoking OP_AggStep.
975**
976** Or, if csr is less than zero, then the array of registers at reg is
977** already populated with all columns from the current row of the sub-query.
978**
979** If argument regPartSize is non-zero, then it is a register containing the
980** number of rows in the current partition.
danc9a86682018-05-30 20:44:58 +0000981*/
dan31f56392018-05-24 21:10:57 +0000982static void windowAggStep(
983 Parse *pParse,
dan13078ca2018-06-13 20:29:38 +0000984 Window *pMWin, /* Linked list of window functions */
985 int csr, /* Read arguments from this cursor */
986 int bInverse, /* True to invoke xInverse instead of xStep */
987 int reg, /* Array of registers */
dandfa552f2018-06-02 21:04:28 +0000988 int regPartSize /* Register containing size of partition */
dan31f56392018-05-24 21:10:57 +0000989){
990 Vdbe *v = sqlite3GetVdbe(pParse);
991 Window *pWin;
992 for(pWin=pMWin; pWin; pWin=pWin->pNextWin){
dandfa552f2018-06-02 21:04:28 +0000993 int flags = pWin->pFunc->funcFlags;
danc9a86682018-05-30 20:44:58 +0000994 int regArg;
dan2a11bb22018-06-11 20:50:25 +0000995 int nArg = windowArgCount(pWin);
dandfa552f2018-06-02 21:04:28 +0000996
dan6bc5c9e2018-06-04 18:55:11 +0000997 if( csr>=0 ){
danc9a86682018-05-30 20:44:58 +0000998 int i;
dan2a11bb22018-06-11 20:50:25 +0000999 for(i=0; i<nArg; i++){
danc9a86682018-05-30 20:44:58 +00001000 sqlite3VdbeAddOp3(v, OP_Column, csr, pWin->iArgCol+i, reg+i);
1001 }
1002 regArg = reg;
dan6bc5c9e2018-06-04 18:55:11 +00001003 if( flags & SQLITE_FUNC_WINDOW_SIZE ){
1004 if( nArg==0 ){
1005 regArg = regPartSize;
1006 }else{
1007 sqlite3VdbeAddOp2(v, OP_SCopy, regPartSize, reg+nArg);
1008 }
1009 nArg++;
1010 }
danc9a86682018-05-30 20:44:58 +00001011 }else{
dan6bc5c9e2018-06-04 18:55:11 +00001012 assert( !(flags & SQLITE_FUNC_WINDOW_SIZE) );
danc9a86682018-05-30 20:44:58 +00001013 regArg = reg + pWin->iArgCol;
dan31f56392018-05-24 21:10:57 +00001014 }
danc9a86682018-05-30 20:44:58 +00001015
danec891fd2018-06-06 20:51:02 +00001016 if( (pWin->pFunc->funcFlags & SQLITE_FUNC_MINMAX)
1017 && pWin->eStart!=TK_UNBOUNDED
1018 ){
danc9a86682018-05-30 20:44:58 +00001019 if( bInverse==0 ){
1020 sqlite3VdbeAddOp2(v, OP_AddImm, pWin->regApp+1, 1);
1021 sqlite3VdbeAddOp2(v, OP_SCopy, regArg, pWin->regApp);
1022 sqlite3VdbeAddOp3(v, OP_MakeRecord, pWin->regApp, 2, pWin->regApp+2);
1023 sqlite3VdbeAddOp2(v, OP_IdxInsert, pWin->csrApp, pWin->regApp+2);
1024 }else{
1025 sqlite3VdbeAddOp4Int(v, OP_SeekGE, pWin->csrApp, 0, regArg, 1);
1026 sqlite3VdbeAddOp1(v, OP_Delete, pWin->csrApp);
1027 sqlite3VdbeJumpHere(v, sqlite3VdbeCurrentAddr(v)-2);
1028 }
danec891fd2018-06-06 20:51:02 +00001029 }else if( pWin->regApp ){
dan7095c002018-06-07 17:45:22 +00001030 assert( pWin->pFunc->xSFunc==nth_valueStepFunc
1031 || pWin->pFunc->xSFunc==first_valueStepFunc
1032 );
danec891fd2018-06-06 20:51:02 +00001033 assert( bInverse==0 || bInverse==1 );
1034 sqlite3VdbeAddOp2(v, OP_AddImm, pWin->regApp+1-bInverse, 1);
danfe4e25a2018-06-07 20:08:59 +00001035 }else if( pWin->pFunc->xSFunc==leadStepFunc
1036 || pWin->pFunc->xSFunc==lagStepFunc
1037 ){
1038 /* no-op */
danc9a86682018-05-30 20:44:58 +00001039 }else{
dan8b985602018-06-09 17:43:45 +00001040 int addrIf = 0;
1041 if( pWin->pFilter ){
1042 int regTmp;
dan2a11bb22018-06-11 20:50:25 +00001043 assert( nArg==pWin->pOwner->x.pList->nExpr );
dan8b985602018-06-09 17:43:45 +00001044 if( csr>0 ){
1045 regTmp = sqlite3GetTempReg(pParse);
dan2a11bb22018-06-11 20:50:25 +00001046 sqlite3VdbeAddOp3(v, OP_Column, csr, pWin->iArgCol+nArg,regTmp);
dan8b985602018-06-09 17:43:45 +00001047 }else{
dan2a11bb22018-06-11 20:50:25 +00001048 regTmp = regArg + nArg;
dan8b985602018-06-09 17:43:45 +00001049 }
1050 addrIf = sqlite3VdbeAddOp3(v, OP_IfNot, regTmp, 0, 1);
1051 if( csr>0 ){
1052 sqlite3ReleaseTempReg(pParse, regTmp);
1053 }
1054 }
danc9a86682018-05-30 20:44:58 +00001055 if( pWin->pFunc->funcFlags & SQLITE_FUNC_NEEDCOLL ){
1056 CollSeq *pColl;
dan8b985602018-06-09 17:43:45 +00001057 pColl = sqlite3ExprNNCollSeq(pParse, pWin->pOwner->x.pList->a[0].pExpr);
danc9a86682018-05-30 20:44:58 +00001058 sqlite3VdbeAddOp4(v, OP_CollSeq, 0,0,0, (const char*)pColl, P4_COLLSEQ);
1059 }
1060 sqlite3VdbeAddOp3(v, OP_AggStep0, bInverse, regArg, pWin->regAccum);
1061 sqlite3VdbeAppendP4(v, pWin->pFunc, P4_FUNCDEF);
dandfa552f2018-06-02 21:04:28 +00001062 sqlite3VdbeChangeP5(v, (u8)nArg);
dan8b985602018-06-09 17:43:45 +00001063 if( addrIf ) sqlite3VdbeJumpHere(v, addrIf);
danc9a86682018-05-30 20:44:58 +00001064 }
dan31f56392018-05-24 21:10:57 +00001065 }
1066}
1067
dan13078ca2018-06-13 20:29:38 +00001068/*
1069** Generate VM code to invoke either xValue() (bFinal==0) or xFinalize()
1070** (bFinal==1) for each window function in the linked list starting at
1071** pMWin. Or, for built-in window-functions that do not use the standard
1072** API, generate the equivalent VM code.
1073*/
dand6f784e2018-05-28 18:30:45 +00001074static void windowAggFinal(Parse *pParse, Window *pMWin, int bFinal){
1075 Vdbe *v = sqlite3GetVdbe(pParse);
1076 Window *pWin;
1077
1078 for(pWin=pMWin; pWin; pWin=pWin->pNextWin){
danec891fd2018-06-06 20:51:02 +00001079 if( (pWin->pFunc->funcFlags & SQLITE_FUNC_MINMAX)
1080 && pWin->eStart!=TK_UNBOUNDED
1081 ){
dand6f784e2018-05-28 18:30:45 +00001082 sqlite3VdbeAddOp2(v, OP_Null, 0, pWin->regResult);
danc9a86682018-05-30 20:44:58 +00001083 sqlite3VdbeAddOp1(v, OP_Last, pWin->csrApp);
1084 sqlite3VdbeAddOp3(v, OP_Column, pWin->csrApp, 0, pWin->regResult);
1085 sqlite3VdbeJumpHere(v, sqlite3VdbeCurrentAddr(v)-2);
1086 if( bFinal ){
1087 sqlite3VdbeAddOp1(v, OP_ResetSorter, pWin->csrApp);
1088 }
danec891fd2018-06-06 20:51:02 +00001089 }else if( pWin->regApp ){
dand6f784e2018-05-28 18:30:45 +00001090 }else{
danc9a86682018-05-30 20:44:58 +00001091 if( bFinal==0 ){
1092 sqlite3VdbeAddOp2(v, OP_Null, 0, pWin->regResult);
1093 }
dan2a11bb22018-06-11 20:50:25 +00001094 sqlite3VdbeAddOp2(v, OP_AggFinal, pWin->regAccum, windowArgCount(pWin));
danc9a86682018-05-30 20:44:58 +00001095 sqlite3VdbeAppendP4(v, pWin->pFunc, P4_FUNCDEF);
1096 if( bFinal ){
1097 sqlite3VdbeAddOp2(v, OP_Copy, pWin->regAccum, pWin->regResult);
dan8b985602018-06-09 17:43:45 +00001098 sqlite3VdbeAddOp2(v, OP_Null, 0, pWin->regAccum);
danc9a86682018-05-30 20:44:58 +00001099 }else{
1100 sqlite3VdbeChangeP3(v, -1, pWin->regResult);
1101 }
dand6f784e2018-05-28 18:30:45 +00001102 }
1103 }
1104}
1105
dan13078ca2018-06-13 20:29:38 +00001106/*
1107** This function generates VM code to invoke the sub-routine at address
1108** lblFlushPart once for each partition with the entire partition cached in
1109** the Window.iEphCsr temp table.
1110*/
danf690b572018-06-01 21:00:08 +00001111static void windowPartitionCache(
1112 Parse *pParse,
dan13078ca2018-06-13 20:29:38 +00001113 Select *p, /* The rewritten SELECT statement */
1114 WhereInfo *pWInfo, /* WhereInfo to call WhereEnd() on */
1115 int regFlushPart, /* Register to use with Gosub lblFlushPart */
1116 int lblFlushPart, /* Subroutine to Gosub to */
1117 int *pRegSize /* OUT: Register containing partition size */
danf690b572018-06-01 21:00:08 +00001118){
1119 Window *pMWin = p->pWin;
1120 Vdbe *v = sqlite3GetVdbe(pParse);
danf690b572018-06-01 21:00:08 +00001121 int iSubCsr = p->pSrc->a[0].iCursor;
1122 int nSub = p->pSrc->a[0].pTab->nCol;
1123 int k;
1124
1125 int reg = pParse->nMem+1;
1126 int regRecord = reg+nSub;
1127 int regRowid = regRecord+1;
1128
dandfa552f2018-06-02 21:04:28 +00001129 *pRegSize = regRowid;
danf690b572018-06-01 21:00:08 +00001130 pParse->nMem += nSub + 2;
1131
1132 /* Martial the row returned by the sub-select into an array of
1133 ** registers. */
1134 for(k=0; k<nSub; k++){
1135 sqlite3VdbeAddOp3(v, OP_Column, iSubCsr, k, reg+k);
1136 }
1137 sqlite3VdbeAddOp3(v, OP_MakeRecord, reg, nSub, regRecord);
1138
1139 /* Check if this is the start of a new partition. If so, call the
1140 ** flush_partition sub-routine. */
1141 if( pMWin->pPartition ){
1142 int addr;
1143 ExprList *pPart = pMWin->pPartition;
dane0a5e202018-06-15 16:10:44 +00001144 int nPart = pPart->nExpr;
danf690b572018-06-01 21:00:08 +00001145 int regNewPart = reg + pMWin->nBufferCol;
1146 KeyInfo *pKeyInfo = sqlite3KeyInfoFromExprList(pParse, pPart, 0, 0);
1147
1148 addr = sqlite3VdbeAddOp3(v, OP_Compare, regNewPart, pMWin->regPart,nPart);
1149 sqlite3VdbeAppendP4(v, (void*)pKeyInfo, P4_KEYINFO);
1150 sqlite3VdbeAddOp3(v, OP_Jump, addr+2, addr+4, addr+2);
1151 sqlite3VdbeAddOp3(v, OP_Copy, regNewPart, pMWin->regPart, nPart-1);
1152 sqlite3VdbeAddOp2(v, OP_Gosub, regFlushPart, lblFlushPart);
1153 }
1154
1155 /* Buffer the current row in the ephemeral table. */
1156 sqlite3VdbeAddOp2(v, OP_NewRowid, pMWin->iEphCsr, regRowid);
1157 sqlite3VdbeAddOp3(v, OP_Insert, pMWin->iEphCsr, regRecord, regRowid);
1158
1159 /* End of the input loop */
1160 sqlite3WhereEnd(pWInfo);
1161
1162 /* Invoke "flush_partition" to deal with the final (or only) partition */
1163 sqlite3VdbeAddOp2(v, OP_Gosub, regFlushPart, lblFlushPart);
1164}
dand6f784e2018-05-28 18:30:45 +00001165
dan13078ca2018-06-13 20:29:38 +00001166/*
1167** Invoke the sub-routine at regGosub (generated by code in select.c) to
1168** return the current row of Window.iEphCsr. If all window functions are
1169** aggregate window functions that use the standard API, a single
1170** OP_Gosub instruction is all that this routine generates. Extra VM code
1171** for per-row processing is only generated for the following built-in window
1172** functions:
1173**
1174** nth_value()
1175** first_value()
1176** lag()
1177** lead()
1178*/
danec891fd2018-06-06 20:51:02 +00001179static void windowReturnOneRow(
1180 Parse *pParse,
1181 Window *pMWin,
1182 int regGosub,
1183 int addrGosub
1184){
1185 Vdbe *v = sqlite3GetVdbe(pParse);
1186 Window *pWin;
1187 for(pWin=pMWin; pWin; pWin=pWin->pNextWin){
1188 FuncDef *pFunc = pWin->pFunc;
dan7095c002018-06-07 17:45:22 +00001189 if( pFunc->xSFunc==nth_valueStepFunc
1190 || pFunc->xSFunc==first_valueStepFunc
1191 ){
danec891fd2018-06-06 20:51:02 +00001192 int csr = pWin->csrApp;
1193 int lbl = sqlite3VdbeMakeLabel(v);
1194 int tmpReg = sqlite3GetTempReg(pParse);
1195 sqlite3VdbeAddOp2(v, OP_Null, 0, pWin->regResult);
dan7095c002018-06-07 17:45:22 +00001196
1197 if( pFunc->xSFunc==nth_valueStepFunc ){
dan6fde1792018-06-15 19:01:35 +00001198 sqlite3VdbeAddOp3(v, OP_Column, pMWin->iEphCsr, pWin->iArgCol+1,tmpReg);
dan7095c002018-06-07 17:45:22 +00001199 }else{
1200 sqlite3VdbeAddOp2(v, OP_Integer, 1, tmpReg);
1201 }
danec891fd2018-06-06 20:51:02 +00001202 sqlite3VdbeAddOp3(v, OP_Add, tmpReg, pWin->regApp, tmpReg);
1203 sqlite3VdbeAddOp3(v, OP_Gt, pWin->regApp+1, lbl, tmpReg);
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 }
danfe4e25a2018-06-07 20:08:59 +00001209 else if( pFunc->xSFunc==leadStepFunc || pFunc->xSFunc==lagStepFunc ){
dan2a11bb22018-06-11 20:50:25 +00001210 int nArg = pWin->pOwner->x.pList->nExpr;
dane0a5e202018-06-15 16:10:44 +00001211 int iEph = pMWin->iEphCsr;
danfe4e25a2018-06-07 20:08:59 +00001212 int csr = pWin->csrApp;
1213 int lbl = sqlite3VdbeMakeLabel(v);
1214 int tmpReg = sqlite3GetTempReg(pParse);
1215
dan2a11bb22018-06-11 20:50:25 +00001216 if( nArg<3 ){
danfe4e25a2018-06-07 20:08:59 +00001217 sqlite3VdbeAddOp2(v, OP_Null, 0, pWin->regResult);
1218 }else{
1219 sqlite3VdbeAddOp3(v, OP_Column, iEph, pWin->iArgCol+2, pWin->regResult);
1220 }
1221 sqlite3VdbeAddOp2(v, OP_Rowid, iEph, tmpReg);
dan2a11bb22018-06-11 20:50:25 +00001222 if( nArg<2 ){
danfe4e25a2018-06-07 20:08:59 +00001223 int val = (pFunc->xSFunc==leadStepFunc ? 1 : -1);
1224 sqlite3VdbeAddOp2(v, OP_AddImm, tmpReg, val);
1225 }else{
1226 int op = (pFunc->xSFunc==leadStepFunc ? OP_Add : OP_Subtract);
1227 int tmpReg2 = sqlite3GetTempReg(pParse);
1228 sqlite3VdbeAddOp3(v, OP_Column, iEph, pWin->iArgCol+1, tmpReg2);
1229 sqlite3VdbeAddOp3(v, op, tmpReg2, tmpReg, tmpReg);
1230 sqlite3ReleaseTempReg(pParse, tmpReg2);
1231 }
1232
1233 sqlite3VdbeAddOp3(v, OP_SeekRowid, csr, lbl, tmpReg);
1234 sqlite3VdbeAddOp3(v, OP_Column, csr, pWin->iArgCol, pWin->regResult);
1235 sqlite3VdbeResolveLabel(v, lbl);
1236 sqlite3ReleaseTempReg(pParse, tmpReg);
1237 }
danec891fd2018-06-06 20:51:02 +00001238 }
1239 sqlite3VdbeAddOp2(v, OP_Gosub, regGosub, addrGosub);
1240}
1241
dan13078ca2018-06-13 20:29:38 +00001242/*
1243** Invoke the code generated by windowReturnOneRow() and, optionally, the
1244** xInverse() function for each window function, for one or more rows
1245** from the Window.iEphCsr temp table. This routine generates VM code
1246** similar to:
1247**
1248** while( regCtr>0 ){
1249** regCtr--;
1250** windowReturnOneRow()
1251** if( bInverse ){
1252** AggStep (xInverse)
1253** }
1254** Next (Window.iEphCsr)
1255** }
1256*/
danec891fd2018-06-06 20:51:02 +00001257static void windowReturnRows(
1258 Parse *pParse,
dan13078ca2018-06-13 20:29:38 +00001259 Window *pMWin, /* List of window functions */
1260 int regCtr, /* Register containing number of rows */
1261 int regGosub, /* Register for Gosub addrGosub */
1262 int addrGosub, /* Address of sub-routine for ReturnOneRow */
1263 int regInvArg, /* Array of registers for xInverse args */
1264 int regInvSize /* Register containing size of partition */
danec891fd2018-06-06 20:51:02 +00001265){
1266 int addr;
1267 Vdbe *v = sqlite3GetVdbe(pParse);
1268 windowAggFinal(pParse, pMWin, 0);
1269 addr = sqlite3VdbeAddOp3(v, OP_IfPos, regCtr, sqlite3VdbeCurrentAddr(v)+2 ,1);
1270 sqlite3VdbeAddOp2(v, OP_Goto, 0, 0);
1271 windowReturnOneRow(pParse, pMWin, regGosub, addrGosub);
1272 if( regInvArg ){
1273 windowAggStep(pParse, pMWin, pMWin->iEphCsr, 1, regInvArg, regInvSize);
1274 }
1275 sqlite3VdbeAddOp2(v, OP_Next, pMWin->iEphCsr, addr);
1276 sqlite3VdbeJumpHere(v, addr+1); /* The OP_Goto */
1277}
1278
dan54a9ab32018-06-14 14:27:05 +00001279/*
1280** Generate code to set the accumulator register for each window function
1281** in the linked list passed as the second argument to NULL. And perform
1282** any equivalent initialization required by any built-in window functions
1283** in the list.
1284*/
dan2e605682018-06-07 15:54:26 +00001285static int windowInitAccum(Parse *pParse, Window *pMWin){
1286 Vdbe *v = sqlite3GetVdbe(pParse);
1287 int regArg;
1288 int nArg = 0;
1289 Window *pWin;
1290 for(pWin=pMWin; pWin; pWin=pWin->pNextWin){
dan9a947222018-06-14 19:06:36 +00001291 FuncDef *pFunc = pWin->pFunc;
dan2e605682018-06-07 15:54:26 +00001292 sqlite3VdbeAddOp2(v, OP_Null, 0, pWin->regAccum);
dan2a11bb22018-06-11 20:50:25 +00001293 nArg = MAX(nArg, windowArgCount(pWin));
dan9a947222018-06-14 19:06:36 +00001294 if( pFunc->xSFunc==nth_valueStepFunc
1295 || pFunc->xSFunc==first_valueStepFunc
dan7095c002018-06-07 17:45:22 +00001296 ){
dan2e605682018-06-07 15:54:26 +00001297 sqlite3VdbeAddOp2(v, OP_Integer, 0, pWin->regApp);
1298 sqlite3VdbeAddOp2(v, OP_Integer, 0, pWin->regApp+1);
1299 }
dan9a947222018-06-14 19:06:36 +00001300
1301 if( (pFunc->funcFlags & SQLITE_FUNC_MINMAX) && pWin->csrApp ){
1302 assert( pWin->eStart!=TK_UNBOUNDED );
1303 sqlite3VdbeAddOp1(v, OP_ResetSorter, pWin->csrApp);
1304 sqlite3VdbeAddOp2(v, OP_Integer, 0, pWin->regApp+1);
1305 }
dan2e605682018-06-07 15:54:26 +00001306 }
1307 regArg = pParse->nMem+1;
1308 pParse->nMem += nArg;
1309 return regArg;
1310}
1311
1312
dan99652dd2018-05-24 17:49:14 +00001313/*
dan54a9ab32018-06-14 14:27:05 +00001314** This function does the work of sqlite3WindowCodeStep() for all "ROWS"
1315** window frame types except for "BETWEEN UNBOUNDED PRECEDING AND CURRENT
1316** ROW". Pseudo-code for each follows.
1317**
dan09590aa2018-05-25 20:30:17 +00001318** ROWS BETWEEN <expr1> PRECEDING AND <expr2> FOLLOWING
dan09590aa2018-05-25 20:30:17 +00001319**
1320** ...
1321** if( new partition ){
1322** Gosub flush_partition
1323** }
1324** Insert (record in eph-table)
1325** sqlite3WhereEnd()
1326** Gosub flush_partition
1327**
1328** flush_partition:
1329** Once {
1330** OpenDup (iEphCsr -> csrStart)
1331** OpenDup (iEphCsr -> csrEnd)
dan99652dd2018-05-24 17:49:14 +00001332** }
dan09590aa2018-05-25 20:30:17 +00001333** regStart = <expr1> // PRECEDING expression
1334** regEnd = <expr2> // FOLLOWING expression
1335** if( regStart<0 || regEnd<0 ){ error! }
1336** Rewind (csr,csrStart,csrEnd) // if EOF goto flush_partition_done
1337** Next(csrEnd) // if EOF skip Aggstep
1338** Aggstep (csrEnd)
1339** if( (regEnd--)<=0 ){
1340** AggFinal (xValue)
1341** Gosub addrGosub
1342** Next(csr) // if EOF goto flush_partition_done
1343** if( (regStart--)<=0 ){
1344** AggStep (csrStart, xInverse)
1345** Next(csrStart)
1346** }
1347** }
1348** flush_partition_done:
1349** ResetSorter (csr)
1350** Return
dan99652dd2018-05-24 17:49:14 +00001351**
dan09590aa2018-05-25 20:30:17 +00001352** ROWS BETWEEN <expr> PRECEDING AND CURRENT ROW
1353** ROWS BETWEEN CURRENT ROW AND <expr> FOLLOWING
1354** ROWS BETWEEN UNBOUNDED PRECEDING AND <expr> FOLLOWING
1355**
1356** These are similar to the above. For "CURRENT ROW", intialize the
1357** register to 0. For "UNBOUNDED PRECEDING" to infinity.
1358**
1359** ROWS BETWEEN <expr> PRECEDING AND UNBOUNDED FOLLOWING
1360** ROWS BETWEEN CURRENT ROW AND UNBOUNDED FOLLOWING
1361**
1362** Rewind (csr,csrStart,csrEnd) // if EOF goto flush_partition_done
1363** while( 1 ){
1364** Next(csrEnd) // Exit while(1) at EOF
1365** Aggstep (csrEnd)
1366** }
1367** while( 1 ){
dan99652dd2018-05-24 17:49:14 +00001368** AggFinal (xValue)
1369** Gosub addrGosub
dan09590aa2018-05-25 20:30:17 +00001370** Next(csr) // if EOF goto flush_partition_done
dan31f56392018-05-24 21:10:57 +00001371** if( (regStart--)<=0 ){
1372** AggStep (csrStart, xInverse)
1373** Next(csrStart)
dan99652dd2018-05-24 17:49:14 +00001374** }
1375** }
dan99652dd2018-05-24 17:49:14 +00001376**
dan09590aa2018-05-25 20:30:17 +00001377** For the "CURRENT ROW AND UNBOUNDED FOLLOWING" case, the final if()
1378** condition is always true (as if regStart were initialized to 0).
dan99652dd2018-05-24 17:49:14 +00001379**
dan09590aa2018-05-25 20:30:17 +00001380** RANGE BETWEEN CURRENT ROW AND UNBOUNDED FOLLOWING
1381**
1382** This is the only RANGE case handled by this routine. It modifies the
1383** second while( 1 ) loop in "ROWS BETWEEN CURRENT ... UNBOUNDED..." to
1384** be:
1385**
1386** while( 1 ){
1387** AggFinal (xValue)
1388** while( 1 ){
1389** regPeer++
1390** Gosub addrGosub
1391** Next(csr) // if EOF goto flush_partition_done
1392** if( new peer ) break;
1393** }
1394** while( (regPeer--)>0 ){
1395** AggStep (csrStart, xInverse)
1396** Next(csrStart)
1397** }
1398** }
dan99652dd2018-05-24 17:49:14 +00001399**
dan31f56392018-05-24 21:10:57 +00001400** ROWS BETWEEN <expr> FOLLOWING AND <expr> FOLLOWING
1401**
1402** regEnd = regEnd - regStart
1403** Rewind (csr,csrStart,csrEnd) // if EOF goto flush_partition_done
1404** Aggstep (csrEnd)
1405** Next(csrEnd) // if EOF fall-through
1406** if( (regEnd--)<=0 ){
dan31f56392018-05-24 21:10:57 +00001407** if( (regStart--)<=0 ){
1408** AggFinal (xValue)
1409** Gosub addrGosub
1410** Next(csr) // if EOF goto flush_partition_done
1411** }
dane105dd72018-05-25 09:29:11 +00001412** AggStep (csrStart, xInverse)
1413** Next (csrStart)
dan31f56392018-05-24 21:10:57 +00001414** }
1415**
1416** ROWS BETWEEN <expr> PRECEDING AND <expr> PRECEDING
1417**
1418** Replace the bit after "Rewind" in the above with:
1419**
1420** if( (regEnd--)<=0 ){
1421** AggStep (csrEnd)
1422** Next (csrEnd)
1423** }
1424** AggFinal (xValue)
1425** Gosub addrGosub
1426** Next(csr) // if EOF goto flush_partition_done
1427** if( (regStart--)<=0 ){
1428** AggStep (csr2, xInverse)
1429** Next (csr2)
1430** }
1431**
dan99652dd2018-05-24 17:49:14 +00001432*/
danc3a20c12018-05-23 20:55:37 +00001433static void windowCodeRowExprStep(
1434 Parse *pParse,
1435 Select *p,
1436 WhereInfo *pWInfo,
1437 int regGosub,
1438 int addrGosub
1439){
1440 Window *pMWin = p->pWin;
1441 Vdbe *v = sqlite3GetVdbe(pParse);
danc3a20c12018-05-23 20:55:37 +00001442 int regFlushPart; /* Register for "Gosub flush_partition" */
dan31f56392018-05-24 21:10:57 +00001443 int lblFlushPart; /* Label for "Gosub flush_partition" */
1444 int lblFlushDone; /* Label for "Gosub flush_partition_done" */
danc3a20c12018-05-23 20:55:37 +00001445
danf690b572018-06-01 21:00:08 +00001446 int regArg;
danc3a20c12018-05-23 20:55:37 +00001447 int addr;
dan31f56392018-05-24 21:10:57 +00001448 int csrStart = pParse->nTab++;
1449 int csrEnd = pParse->nTab++;
1450 int regStart; /* Value of <expr> PRECEDING */
1451 int regEnd; /* Value of <expr> FOLLOWING */
danc3a20c12018-05-23 20:55:37 +00001452 int addrGoto;
dan31f56392018-05-24 21:10:57 +00001453 int addrTop;
danc3a20c12018-05-23 20:55:37 +00001454 int addrIfPos1;
1455 int addrIfPos2;
dandfa552f2018-06-02 21:04:28 +00001456 int regSize = 0;
dan09590aa2018-05-25 20:30:17 +00001457
dan99652dd2018-05-24 17:49:14 +00001458 assert( pMWin->eStart==TK_PRECEDING
1459 || pMWin->eStart==TK_CURRENT
dane105dd72018-05-25 09:29:11 +00001460 || pMWin->eStart==TK_FOLLOWING
dan99652dd2018-05-24 17:49:14 +00001461 || pMWin->eStart==TK_UNBOUNDED
1462 );
1463 assert( pMWin->eEnd==TK_FOLLOWING
1464 || pMWin->eEnd==TK_CURRENT
1465 || pMWin->eEnd==TK_UNBOUNDED
dan31f56392018-05-24 21:10:57 +00001466 || pMWin->eEnd==TK_PRECEDING
dan99652dd2018-05-24 17:49:14 +00001467 );
1468
danc3a20c12018-05-23 20:55:37 +00001469 /* Allocate register and label for the "flush_partition" sub-routine. */
1470 regFlushPart = ++pParse->nMem;
dan31f56392018-05-24 21:10:57 +00001471 lblFlushPart = sqlite3VdbeMakeLabel(v);
1472 lblFlushDone = sqlite3VdbeMakeLabel(v);
danc3a20c12018-05-23 20:55:37 +00001473
dan31f56392018-05-24 21:10:57 +00001474 regStart = ++pParse->nMem;
1475 regEnd = ++pParse->nMem;
danc3a20c12018-05-23 20:55:37 +00001476
dandfa552f2018-06-02 21:04:28 +00001477 windowPartitionCache(pParse, p, pWInfo, regFlushPart, lblFlushPart, &regSize);
danc3a20c12018-05-23 20:55:37 +00001478
danc3a20c12018-05-23 20:55:37 +00001479 addrGoto = sqlite3VdbeAddOp0(v, OP_Goto);
1480
danc9a86682018-05-30 20:44:58 +00001481 /* Start of "flush_partition" */
dan31f56392018-05-24 21:10:57 +00001482 sqlite3VdbeResolveLabel(v, lblFlushPart);
danc3a20c12018-05-23 20:55:37 +00001483 sqlite3VdbeAddOp2(v, OP_Once, 0, sqlite3VdbeCurrentAddr(v)+3);
dan31f56392018-05-24 21:10:57 +00001484 sqlite3VdbeAddOp2(v, OP_OpenDup, csrStart, pMWin->iEphCsr);
1485 sqlite3VdbeAddOp2(v, OP_OpenDup, csrEnd, pMWin->iEphCsr);
danc3a20c12018-05-23 20:55:37 +00001486
dan31f56392018-05-24 21:10:57 +00001487 /* If either regStart or regEnd are not non-negative integers, throw
dan99652dd2018-05-24 17:49:14 +00001488 ** an exception. */
1489 if( pMWin->pStart ){
dan31f56392018-05-24 21:10:57 +00001490 sqlite3ExprCode(pParse, pMWin->pStart, regStart);
1491 windowCheckFrameValue(pParse, regStart, 0);
dan99652dd2018-05-24 17:49:14 +00001492 }
1493 if( pMWin->pEnd ){
dan31f56392018-05-24 21:10:57 +00001494 sqlite3ExprCode(pParse, pMWin->pEnd, regEnd);
1495 windowCheckFrameValue(pParse, regEnd, 1);
dan99652dd2018-05-24 17:49:14 +00001496 }
danc3a20c12018-05-23 20:55:37 +00001497
danc9a86682018-05-30 20:44:58 +00001498 /* If this is "ROWS <expr1> FOLLOWING AND ROWS <expr2> FOLLOWING", do:
1499 **
dan26522d12018-06-11 18:16:51 +00001500 ** if( regEnd<regStart ){
1501 ** // The frame always consists of 0 rows
1502 ** regStart = regSize;
1503 ** }
danc9a86682018-05-30 20:44:58 +00001504 ** regEnd = regEnd - regStart;
1505 */
1506 if( pMWin->pEnd && pMWin->pStart && pMWin->eStart==TK_FOLLOWING ){
1507 assert( pMWin->eEnd==TK_FOLLOWING );
dan26522d12018-06-11 18:16:51 +00001508 sqlite3VdbeAddOp3(v, OP_Ge, regStart, sqlite3VdbeCurrentAddr(v)+2, regEnd);
1509 sqlite3VdbeAddOp2(v, OP_Copy, regSize, regStart);
danc9a86682018-05-30 20:44:58 +00001510 sqlite3VdbeAddOp3(v, OP_Subtract, regStart, regEnd, regEnd);
1511 }
1512
dan26522d12018-06-11 18:16:51 +00001513 if( pMWin->pEnd && pMWin->pStart && pMWin->eEnd==TK_PRECEDING ){
1514 assert( pMWin->eStart==TK_PRECEDING );
1515 sqlite3VdbeAddOp3(v, OP_Le, regStart, sqlite3VdbeCurrentAddr(v)+3, regEnd);
1516 sqlite3VdbeAddOp2(v, OP_Copy, regSize, regStart);
1517 sqlite3VdbeAddOp2(v, OP_Copy, regSize, regEnd);
1518 }
1519
danc9a86682018-05-30 20:44:58 +00001520 /* Initialize the accumulator register for each window function to NULL */
dan2e605682018-06-07 15:54:26 +00001521 regArg = windowInitAccum(pParse, pMWin);
danc3a20c12018-05-23 20:55:37 +00001522
dan31f56392018-05-24 21:10:57 +00001523 sqlite3VdbeAddOp2(v, OP_Rewind, pMWin->iEphCsr, lblFlushDone);
1524 sqlite3VdbeAddOp2(v, OP_Rewind, csrStart, lblFlushDone);
danc3a20c12018-05-23 20:55:37 +00001525 sqlite3VdbeChangeP5(v, 1);
dan31f56392018-05-24 21:10:57 +00001526 sqlite3VdbeAddOp2(v, OP_Rewind, csrEnd, lblFlushDone);
danc3a20c12018-05-23 20:55:37 +00001527 sqlite3VdbeChangeP5(v, 1);
1528
1529 /* Invoke AggStep function for each window function using the row that
dan31f56392018-05-24 21:10:57 +00001530 ** csrEnd currently points to. Or, if csrEnd is already at EOF,
danc3a20c12018-05-23 20:55:37 +00001531 ** do nothing. */
dan31f56392018-05-24 21:10:57 +00001532 addrTop = sqlite3VdbeCurrentAddr(v);
1533 if( pMWin->eEnd==TK_PRECEDING ){
1534 addrIfPos1 = sqlite3VdbeAddOp3(v, OP_IfPos, regEnd, 0 , 1);
danc3a20c12018-05-23 20:55:37 +00001535 }
dan31f56392018-05-24 21:10:57 +00001536 sqlite3VdbeAddOp2(v, OP_Next, csrEnd, sqlite3VdbeCurrentAddr(v)+2);
1537 addr = sqlite3VdbeAddOp0(v, OP_Goto);
dandfa552f2018-06-02 21:04:28 +00001538 windowAggStep(pParse, pMWin, csrEnd, 0, regArg, regSize);
dan99652dd2018-05-24 17:49:14 +00001539 if( pMWin->eEnd==TK_UNBOUNDED ){
dan31f56392018-05-24 21:10:57 +00001540 sqlite3VdbeAddOp2(v, OP_Goto, 0, addrTop);
1541 sqlite3VdbeJumpHere(v, addr);
1542 addrTop = sqlite3VdbeCurrentAddr(v);
dan99652dd2018-05-24 17:49:14 +00001543 }else{
dan31f56392018-05-24 21:10:57 +00001544 sqlite3VdbeJumpHere(v, addr);
1545 if( pMWin->eEnd==TK_PRECEDING ){
1546 sqlite3VdbeJumpHere(v, addrIfPos1);
1547 }
dan99652dd2018-05-24 17:49:14 +00001548 }
danc3a20c12018-05-23 20:55:37 +00001549
dan99652dd2018-05-24 17:49:14 +00001550 if( pMWin->eEnd==TK_FOLLOWING ){
dan31f56392018-05-24 21:10:57 +00001551 addrIfPos1 = sqlite3VdbeAddOp3(v, OP_IfPos, regEnd, 0 , 1);
dan99652dd2018-05-24 17:49:14 +00001552 }
dane105dd72018-05-25 09:29:11 +00001553 if( pMWin->eStart==TK_FOLLOWING ){
1554 addrIfPos2 = sqlite3VdbeAddOp3(v, OP_IfPos, regStart, 0 , 1);
1555 }
dand6f784e2018-05-28 18:30:45 +00001556 windowAggFinal(pParse, pMWin, 0);
danec891fd2018-06-06 20:51:02 +00001557 windowReturnOneRow(pParse, pMWin, regGosub, addrGosub);
danc3a20c12018-05-23 20:55:37 +00001558 sqlite3VdbeAddOp2(v, OP_Next, pMWin->iEphCsr, sqlite3VdbeCurrentAddr(v)+2);
dan31f56392018-05-24 21:10:57 +00001559 sqlite3VdbeAddOp2(v, OP_Goto, 0, lblFlushDone);
dane105dd72018-05-25 09:29:11 +00001560 if( pMWin->eStart==TK_FOLLOWING ){
1561 sqlite3VdbeJumpHere(v, addrIfPos2);
1562 }
danc3a20c12018-05-23 20:55:37 +00001563
dane105dd72018-05-25 09:29:11 +00001564 if( pMWin->eStart==TK_CURRENT
1565 || pMWin->eStart==TK_PRECEDING
1566 || pMWin->eStart==TK_FOLLOWING
1567 ){
dan09590aa2018-05-25 20:30:17 +00001568 int addrJumpHere = 0;
dan99652dd2018-05-24 17:49:14 +00001569 if( pMWin->eStart==TK_PRECEDING ){
dan09590aa2018-05-25 20:30:17 +00001570 addrJumpHere = sqlite3VdbeAddOp3(v, OP_IfPos, regStart, 0 , 1);
1571 }
dan31f56392018-05-24 21:10:57 +00001572 sqlite3VdbeAddOp2(v, OP_Next, csrStart, sqlite3VdbeCurrentAddr(v)+1);
dandfa552f2018-06-02 21:04:28 +00001573 windowAggStep(pParse, pMWin, csrStart, 1, regArg, regSize);
dan09590aa2018-05-25 20:30:17 +00001574 if( addrJumpHere ){
1575 sqlite3VdbeJumpHere(v, addrJumpHere);
dan99652dd2018-05-24 17:49:14 +00001576 }
danc3a20c12018-05-23 20:55:37 +00001577 }
dan99652dd2018-05-24 17:49:14 +00001578 if( pMWin->eEnd==TK_FOLLOWING ){
1579 sqlite3VdbeJumpHere(v, addrIfPos1);
1580 }
dan31f56392018-05-24 21:10:57 +00001581 sqlite3VdbeAddOp2(v, OP_Goto, 0, addrTop);
danc3a20c12018-05-23 20:55:37 +00001582
1583 /* flush_partition_done: */
dan31f56392018-05-24 21:10:57 +00001584 sqlite3VdbeResolveLabel(v, lblFlushDone);
danc3a20c12018-05-23 20:55:37 +00001585 sqlite3VdbeAddOp1(v, OP_ResetSorter, pMWin->iEphCsr);
1586 sqlite3VdbeAddOp1(v, OP_Return, regFlushPart);
1587
1588 /* Jump to here to skip over flush_partition */
1589 sqlite3VdbeJumpHere(v, addrGoto);
1590}
1591
dan79d45442018-05-26 21:17:29 +00001592/*
dan54a9ab32018-06-14 14:27:05 +00001593** This function does the work of sqlite3WindowCodeStep() for cases that
1594** would normally be handled by windowCodeDefaultStep() when there are
1595** one or more built-in window-functions that require the entire partition
1596** to be cached in a temp table before any rows can be returned. Additionally.
1597** "RANGE BETWEEN CURRENT ROW AND UNBOUNDED FOLLOWING" is always handled by
1598** this function.
1599**
1600** Pseudo-code corresponding to the VM code generated by this function
1601** for each type of window follows.
1602**
dan79d45442018-05-26 21:17:29 +00001603** RANGE BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW
1604**
danf690b572018-06-01 21:00:08 +00001605** flush_partition:
1606** Once {
1607** OpenDup (iEphCsr -> csrLead)
1608** }
1609** Integer ctr 0
1610** foreach row (csrLead){
1611** if( new peer ){
1612** AggFinal (xValue)
1613** for(i=0; i<ctr; i++){
1614** Gosub addrGosub
1615** Next iEphCsr
1616** }
1617** Integer ctr 0
1618** }
1619** AggStep (csrLead)
1620** Incr ctr
1621** }
1622**
1623** AggFinal (xFinalize)
1624** for(i=0; i<ctr; i++){
1625** Gosub addrGosub
1626** Next iEphCsr
1627** }
1628**
1629** ResetSorter (csr)
1630** Return
1631**
1632** ROWS BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW
dan54a9ab32018-06-14 14:27:05 +00001633**
1634** As above, except that the "if( new peer )" branch is always taken.
1635**
danf690b572018-06-01 21:00:08 +00001636** RANGE BETWEEN CURRENT ROW AND CURRENT ROW
dan54a9ab32018-06-14 14:27:05 +00001637**
1638** As above, except that each of the for() loops becomes:
1639**
1640** for(i=0; i<ctr; i++){
1641** Gosub addrGosub
1642** AggStep (xInverse, iEphCsr)
1643** Next iEphCsr
1644** }
1645**
1646** RANGE BETWEEN UNBOUNDED PRECEDING AND UNBOUNDED FOLLOWING
1647**
1648** flush_partition:
1649** Once {
1650** OpenDup (iEphCsr -> csrLead)
1651** }
1652** foreach row (csrLead) {
1653** AggStep (csrLead)
1654** }
1655** foreach row (iEphCsr) {
1656** Gosub addrGosub
1657** }
1658**
1659** RANGE BETWEEN CURRENT ROW AND UNBOUNDED FOLLOWING
1660**
1661** flush_partition:
1662** Once {
1663** OpenDup (iEphCsr -> csrLead)
1664** }
1665** foreach row (csrLead){
1666** AggStep (csrLead)
1667** }
1668** Rewind (csrLead)
1669** Integer ctr 0
1670** foreach row (csrLead){
1671** if( new peer ){
1672** AggFinal (xValue)
1673** for(i=0; i<ctr; i++){
1674** Gosub addrGosub
1675** AggStep (xInverse, iEphCsr)
1676** Next iEphCsr
1677** }
1678** Integer ctr 0
1679** }
1680** Incr ctr
1681** }
1682**
1683** AggFinal (xFinalize)
1684** for(i=0; i<ctr; i++){
1685** Gosub addrGosub
1686** Next iEphCsr
1687** }
1688**
1689** ResetSorter (csr)
1690** Return
danf690b572018-06-01 21:00:08 +00001691*/
1692static void windowCodeCacheStep(
1693 Parse *pParse,
1694 Select *p,
1695 WhereInfo *pWInfo,
1696 int regGosub,
1697 int addrGosub
1698){
1699 Window *pMWin = p->pWin;
1700 Vdbe *v = sqlite3GetVdbe(pParse);
danf690b572018-06-01 21:00:08 +00001701 int k;
1702 int addr;
1703 ExprList *pPart = pMWin->pPartition;
1704 ExprList *pOrderBy = pMWin->pOrderBy;
dan54a9ab32018-06-14 14:27:05 +00001705 int nPeer = pOrderBy ? pOrderBy->nExpr : 0;
danf690b572018-06-01 21:00:08 +00001706 int regNewPeer;
1707
1708 int addrGoto; /* Address of Goto used to jump flush_par.. */
dan13078ca2018-06-13 20:29:38 +00001709 int addrNext; /* Jump here for next iteration of loop */
danf690b572018-06-01 21:00:08 +00001710 int regFlushPart;
1711 int lblFlushPart;
1712 int csrLead;
1713 int regCtr;
1714 int regArg; /* Register array to martial function args */
dandfa552f2018-06-02 21:04:28 +00001715 int regSize;
dan13078ca2018-06-13 20:29:38 +00001716 int lblEmpty;
dan54a9ab32018-06-14 14:27:05 +00001717 int bReverse = pMWin->pOrderBy && pMWin->eStart==TK_CURRENT
1718 && pMWin->eEnd==TK_UNBOUNDED;
danf690b572018-06-01 21:00:08 +00001719
1720 assert( (pMWin->eStart==TK_UNBOUNDED && pMWin->eEnd==TK_CURRENT)
danec891fd2018-06-06 20:51:02 +00001721 || (pMWin->eStart==TK_UNBOUNDED && pMWin->eEnd==TK_UNBOUNDED)
1722 || (pMWin->eStart==TK_CURRENT && pMWin->eEnd==TK_CURRENT)
dan13078ca2018-06-13 20:29:38 +00001723 || (pMWin->eStart==TK_CURRENT && pMWin->eEnd==TK_UNBOUNDED)
danf690b572018-06-01 21:00:08 +00001724 );
1725
dan13078ca2018-06-13 20:29:38 +00001726 lblEmpty = sqlite3VdbeMakeLabel(v);
danf690b572018-06-01 21:00:08 +00001727 regNewPeer = pParse->nMem+1;
1728 pParse->nMem += nPeer;
1729
1730 /* Allocate register and label for the "flush_partition" sub-routine. */
1731 regFlushPart = ++pParse->nMem;
1732 lblFlushPart = sqlite3VdbeMakeLabel(v);
1733
1734 csrLead = pParse->nTab++;
1735 regCtr = ++pParse->nMem;
1736
dandfa552f2018-06-02 21:04:28 +00001737 windowPartitionCache(pParse, p, pWInfo, regFlushPart, lblFlushPart, &regSize);
danf690b572018-06-01 21:00:08 +00001738 addrGoto = sqlite3VdbeAddOp0(v, OP_Goto);
1739
1740 /* Start of "flush_partition" */
1741 sqlite3VdbeResolveLabel(v, lblFlushPart);
1742 sqlite3VdbeAddOp2(v, OP_Once, 0, sqlite3VdbeCurrentAddr(v)+2);
1743 sqlite3VdbeAddOp2(v, OP_OpenDup, csrLead, pMWin->iEphCsr);
1744
1745 /* Initialize the accumulator register for each window function to NULL */
dan2e605682018-06-07 15:54:26 +00001746 regArg = windowInitAccum(pParse, pMWin);
danf690b572018-06-01 21:00:08 +00001747
1748 sqlite3VdbeAddOp2(v, OP_Integer, 0, regCtr);
dan13078ca2018-06-13 20:29:38 +00001749 sqlite3VdbeAddOp2(v, OP_Rewind, csrLead, lblEmpty);
1750 sqlite3VdbeAddOp2(v, OP_Rewind, pMWin->iEphCsr, lblEmpty);
danf690b572018-06-01 21:00:08 +00001751
dan13078ca2018-06-13 20:29:38 +00001752 if( bReverse ){
1753 int addr = sqlite3VdbeCurrentAddr(v);
1754 windowAggStep(pParse, pMWin, csrLead, 0, regArg, regSize);
1755 sqlite3VdbeAddOp2(v, OP_Next, csrLead, addr);
1756 sqlite3VdbeAddOp2(v, OP_Rewind, csrLead, lblEmpty);
1757 }
1758 addrNext = sqlite3VdbeCurrentAddr(v);
1759
1760 if( pOrderBy && (pMWin->eEnd==TK_CURRENT || pMWin->eStart==TK_CURRENT) ){
1761 int bCurrent = (pMWin->eStart==TK_CURRENT);
danec891fd2018-06-06 20:51:02 +00001762 int addrJump = 0; /* Address of OP_Jump below */
1763 if( pMWin->eType==TK_RANGE ){
1764 int iOff = pMWin->nBufferCol + (pPart ? pPart->nExpr : 0);
1765 int regPeer = pMWin->regPart + (pPart ? pPart->nExpr : 0);
1766 KeyInfo *pKeyInfo = sqlite3KeyInfoFromExprList(pParse, pOrderBy, 0, 0);
1767 for(k=0; k<nPeer; k++){
1768 sqlite3VdbeAddOp3(v, OP_Column, csrLead, iOff+k, regNewPeer+k);
1769 }
1770 addr = sqlite3VdbeAddOp3(v, OP_Compare, regNewPeer, regPeer, nPeer);
1771 sqlite3VdbeAppendP4(v, (void*)pKeyInfo, P4_KEYINFO);
1772 addrJump = sqlite3VdbeAddOp3(v, OP_Jump, addr+2, 0, addr+2);
1773 sqlite3VdbeAddOp3(v, OP_Copy, regNewPeer, regPeer, nPeer-1);
danf690b572018-06-01 21:00:08 +00001774 }
1775
dan13078ca2018-06-13 20:29:38 +00001776 windowReturnRows(pParse, pMWin, regCtr, regGosub, addrGosub,
danec891fd2018-06-06 20:51:02 +00001777 (bCurrent ? regArg : 0), (bCurrent ? regSize : 0)
1778 );
1779 if( addrJump ) sqlite3VdbeJumpHere(v, addrJump);
danf690b572018-06-01 21:00:08 +00001780 }
1781
dan13078ca2018-06-13 20:29:38 +00001782 if( bReverse==0 ){
1783 windowAggStep(pParse, pMWin, csrLead, 0, regArg, regSize);
1784 }
danf690b572018-06-01 21:00:08 +00001785 sqlite3VdbeAddOp2(v, OP_AddImm, regCtr, 1);
dan13078ca2018-06-13 20:29:38 +00001786 sqlite3VdbeAddOp2(v, OP_Next, csrLead, addrNext);
danf690b572018-06-01 21:00:08 +00001787
dan13078ca2018-06-13 20:29:38 +00001788 windowReturnRows(pParse, pMWin, regCtr, regGosub, addrGosub, 0, 0);
danf690b572018-06-01 21:00:08 +00001789
dan13078ca2018-06-13 20:29:38 +00001790 sqlite3VdbeResolveLabel(v, lblEmpty);
danf690b572018-06-01 21:00:08 +00001791 sqlite3VdbeAddOp1(v, OP_ResetSorter, pMWin->iEphCsr);
1792 sqlite3VdbeAddOp1(v, OP_Return, regFlushPart);
1793
1794 /* Jump to here to skip over flush_partition */
1795 sqlite3VdbeJumpHere(v, addrGoto);
1796}
1797
1798
1799/*
1800** RANGE BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW
1801**
dan79d45442018-05-26 21:17:29 +00001802** ...
1803** if( new partition ){
1804** AggFinal (xFinalize)
1805** Gosub addrGosub
1806** ResetSorter eph-table
1807** }
1808** else if( new peer ){
1809** AggFinal (xValue)
1810** Gosub addrGosub
1811** ResetSorter eph-table
1812** }
1813** AggStep
1814** Insert (record into eph-table)
1815** sqlite3WhereEnd()
1816** AggFinal (xFinalize)
1817** Gosub addrGosub
danf690b572018-06-01 21:00:08 +00001818**
1819** RANGE BETWEEN UNBOUNDED PRECEDING AND UNBOUNDED FOLLOWING
1820**
1821** As above, except take no action for a "new peer". Invoke
1822** the sub-routine once only for each partition.
1823**
1824** RANGE BETWEEN CURRENT ROW AND CURRENT ROW
1825**
1826** As above, except that the "new peer" condition is handled in the
1827** same way as "new partition" (so there is no "else if" block).
1828**
1829** ROWS BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW
1830**
1831** As above, except assume every row is a "new peer".
dan79d45442018-05-26 21:17:29 +00001832*/
danc3a20c12018-05-23 20:55:37 +00001833static void windowCodeDefaultStep(
1834 Parse *pParse,
1835 Select *p,
1836 WhereInfo *pWInfo,
1837 int regGosub,
1838 int addrGosub
1839){
1840 Window *pMWin = p->pWin;
1841 Vdbe *v = sqlite3GetVdbe(pParse);
danc3a20c12018-05-23 20:55:37 +00001842 int k;
1843 int iSubCsr = p->pSrc->a[0].iCursor;
1844 int nSub = p->pSrc->a[0].pTab->nCol;
1845 int reg = pParse->nMem+1;
1846 int regRecord = reg+nSub;
1847 int regRowid = regRecord+1;
1848 int addr;
dand6f784e2018-05-28 18:30:45 +00001849 ExprList *pPart = pMWin->pPartition;
1850 ExprList *pOrderBy = pMWin->pOrderBy;
danc3a20c12018-05-23 20:55:37 +00001851
dan79d45442018-05-26 21:17:29 +00001852 assert( pMWin->eType==TK_RANGE
1853 || (pMWin->eStart==TK_UNBOUNDED && pMWin->eEnd==TK_CURRENT)
1854 );
1855
dand6f784e2018-05-28 18:30:45 +00001856 assert( (pMWin->eStart==TK_UNBOUNDED && pMWin->eEnd==TK_CURRENT)
1857 || (pMWin->eStart==TK_UNBOUNDED && pMWin->eEnd==TK_UNBOUNDED)
1858 || (pMWin->eStart==TK_CURRENT && pMWin->eEnd==TK_CURRENT)
1859 || (pMWin->eStart==TK_CURRENT && pMWin->eEnd==TK_UNBOUNDED && !pOrderBy)
1860 );
1861
1862 if( pMWin->eEnd==TK_UNBOUNDED ){
1863 pOrderBy = 0;
1864 }
1865
danc3a20c12018-05-23 20:55:37 +00001866 pParse->nMem += nSub + 2;
1867
1868 /* Martial the row returned by the sub-select into an array of
1869 ** registers. */
1870 for(k=0; k<nSub; k++){
1871 sqlite3VdbeAddOp3(v, OP_Column, iSubCsr, k, reg+k);
1872 }
1873
1874 /* Check if this is the start of a new partition or peer group. */
dand6f784e2018-05-28 18:30:45 +00001875 if( pPart || pOrderBy ){
danc3a20c12018-05-23 20:55:37 +00001876 int nPart = (pPart ? pPart->nExpr : 0);
danc3a20c12018-05-23 20:55:37 +00001877 int addrGoto = 0;
1878 int addrJump = 0;
dand6f784e2018-05-28 18:30:45 +00001879 int nPeer = (pOrderBy ? pOrderBy->nExpr : 0);
danc3a20c12018-05-23 20:55:37 +00001880
1881 if( pPart ){
1882 int regNewPart = reg + pMWin->nBufferCol;
1883 KeyInfo *pKeyInfo = sqlite3KeyInfoFromExprList(pParse, pPart, 0, 0);
1884 addr = sqlite3VdbeAddOp3(v, OP_Compare, regNewPart, pMWin->regPart,nPart);
1885 sqlite3VdbeAppendP4(v, (void*)pKeyInfo, P4_KEYINFO);
1886 addrJump = sqlite3VdbeAddOp3(v, OP_Jump, addr+2, 0, addr+2);
dand6f784e2018-05-28 18:30:45 +00001887 windowAggFinal(pParse, pMWin, 1);
danc3a20c12018-05-23 20:55:37 +00001888 if( pOrderBy ){
1889 addrGoto = sqlite3VdbeAddOp0(v, OP_Goto);
1890 }
1891 }
1892
1893 if( pOrderBy ){
1894 int regNewPeer = reg + pMWin->nBufferCol + nPart;
1895 int regPeer = pMWin->regPart + nPart;
1896
danc3a20c12018-05-23 20:55:37 +00001897 if( addrJump ) sqlite3VdbeJumpHere(v, addrJump);
dan79d45442018-05-26 21:17:29 +00001898 if( pMWin->eType==TK_RANGE ){
1899 KeyInfo *pKeyInfo = sqlite3KeyInfoFromExprList(pParse, pOrderBy, 0, 0);
1900 addr = sqlite3VdbeAddOp3(v, OP_Compare, regNewPeer, regPeer, nPeer);
1901 sqlite3VdbeAppendP4(v, (void*)pKeyInfo, P4_KEYINFO);
1902 addrJump = sqlite3VdbeAddOp3(v, OP_Jump, addr+2, 0, addr+2);
1903 }else{
1904 addrJump = 0;
1905 }
dand6f784e2018-05-28 18:30:45 +00001906 windowAggFinal(pParse, pMWin, pMWin->eStart==TK_CURRENT);
danc3a20c12018-05-23 20:55:37 +00001907 if( addrGoto ) sqlite3VdbeJumpHere(v, addrGoto);
1908 }
1909
dandacf1de2018-06-08 16:11:55 +00001910 sqlite3VdbeAddOp2(v, OP_Rewind, pMWin->iEphCsr,sqlite3VdbeCurrentAddr(v)+3);
danc3a20c12018-05-23 20:55:37 +00001911 sqlite3VdbeAddOp2(v, OP_Gosub, regGosub, addrGosub);
dandacf1de2018-06-08 16:11:55 +00001912 sqlite3VdbeAddOp2(v, OP_Next, pMWin->iEphCsr, sqlite3VdbeCurrentAddr(v)-1);
1913
danc3a20c12018-05-23 20:55:37 +00001914 sqlite3VdbeAddOp1(v, OP_ResetSorter, pMWin->iEphCsr);
1915 sqlite3VdbeAddOp3(
1916 v, OP_Copy, reg+pMWin->nBufferCol, pMWin->regPart, nPart+nPeer-1
1917 );
1918
dan79d45442018-05-26 21:17:29 +00001919 if( addrJump ) sqlite3VdbeJumpHere(v, addrJump);
danc3a20c12018-05-23 20:55:37 +00001920 }
1921
1922 /* Invoke step function for window functions */
dandfa552f2018-06-02 21:04:28 +00001923 windowAggStep(pParse, pMWin, -1, 0, reg, 0);
danc3a20c12018-05-23 20:55:37 +00001924
1925 /* Buffer the current row in the ephemeral table. */
1926 if( pMWin->nBufferCol>0 ){
1927 sqlite3VdbeAddOp3(v, OP_MakeRecord, reg, pMWin->nBufferCol, regRecord);
1928 }else{
1929 sqlite3VdbeAddOp2(v, OP_Blob, 0, regRecord);
1930 sqlite3VdbeAppendP4(v, (void*)"", 0);
1931 }
1932 sqlite3VdbeAddOp2(v, OP_NewRowid, pMWin->iEphCsr, regRowid);
1933 sqlite3VdbeAddOp3(v, OP_Insert, pMWin->iEphCsr, regRecord, regRowid);
1934
1935 /* End the database scan loop. */
1936 sqlite3WhereEnd(pWInfo);
1937
dand6f784e2018-05-28 18:30:45 +00001938 windowAggFinal(pParse, pMWin, 1);
dandacf1de2018-06-08 16:11:55 +00001939 sqlite3VdbeAddOp2(v, OP_Rewind, pMWin->iEphCsr,sqlite3VdbeCurrentAddr(v)+3);
danc3a20c12018-05-23 20:55:37 +00001940 sqlite3VdbeAddOp2(v, OP_Gosub, regGosub, addrGosub);
dandacf1de2018-06-08 16:11:55 +00001941 sqlite3VdbeAddOp2(v, OP_Next, pMWin->iEphCsr, sqlite3VdbeCurrentAddr(v)-1);
danc3a20c12018-05-23 20:55:37 +00001942}
1943
dan13078ca2018-06-13 20:29:38 +00001944/*
1945** Allocate and return a duplicate of the Window object indicated by the
1946** third argument. Set the Window.pOwner field of the new object to
1947** pOwner.
1948*/
dan2a11bb22018-06-11 20:50:25 +00001949Window *sqlite3WindowDup(sqlite3 *db, Expr *pOwner, Window *p){
dandacf1de2018-06-08 16:11:55 +00001950 Window *pNew = 0;
1951 if( p ){
1952 pNew = sqlite3DbMallocZero(db, sizeof(Window));
1953 if( pNew ){
danc95f38d2018-06-18 20:34:43 +00001954 pNew->zName = sqlite3DbStrDup(db, p->zName);
dandacf1de2018-06-08 16:11:55 +00001955 pNew->pFilter = sqlite3ExprDup(db, p->pFilter, 0);
1956 pNew->pPartition = sqlite3ExprListDup(db, p->pPartition, 0);
1957 pNew->pOrderBy = sqlite3ExprListDup(db, p->pOrderBy, 0);
1958 pNew->eType = p->eType;
1959 pNew->eEnd = p->eEnd;
1960 pNew->eStart = p->eStart;
dan303451a2018-06-14 20:52:08 +00001961 pNew->pStart = sqlite3ExprDup(db, p->pStart, 0);
1962 pNew->pEnd = sqlite3ExprDup(db, p->pEnd, 0);
dan2a11bb22018-06-11 20:50:25 +00001963 pNew->pOwner = pOwner;
dandacf1de2018-06-08 16:11:55 +00001964 }
1965 }
1966 return pNew;
1967}
danc3a20c12018-05-23 20:55:37 +00001968
danf9eae182018-05-21 19:45:11 +00001969/*
danc95f38d2018-06-18 20:34:43 +00001970** Return a copy of the linked list of Window objects passed as the
1971** second argument.
1972*/
1973Window *sqlite3WindowListDup(sqlite3 *db, Window *p){
1974 Window *pWin;
1975 Window *pRet = 0;
1976 Window **pp = &pRet;
1977
1978 for(pWin=p; pWin; pWin=pWin->pNextWin){
1979 *pp = sqlite3WindowDup(db, 0, pWin);
1980 if( *pp==0 ) break;
1981 pp = &((*pp)->pNextWin);
1982 }
1983
1984 return pRet;
1985}
1986
1987/*
dan2a11bb22018-06-11 20:50:25 +00001988** sqlite3WhereBegin() has already been called for the SELECT statement
1989** passed as the second argument when this function is invoked. It generates
1990** code to populate the Window.regResult register for each window function and
1991** invoke the sub-routine at instruction addrGosub once for each row.
1992** This function calls sqlite3WhereEnd() before returning.
danf9eae182018-05-21 19:45:11 +00001993*/
1994void sqlite3WindowCodeStep(
dan2a11bb22018-06-11 20:50:25 +00001995 Parse *pParse, /* Parse context */
1996 Select *p, /* Rewritten SELECT statement */
1997 WhereInfo *pWInfo, /* Context returned by sqlite3WhereBegin() */
1998 int regGosub, /* Register for OP_Gosub */
1999 int addrGosub /* OP_Gosub here to return each row */
danf9eae182018-05-21 19:45:11 +00002000){
danf9eae182018-05-21 19:45:11 +00002001 Window *pMWin = p->pWin;
danf9eae182018-05-21 19:45:11 +00002002
dan54a9ab32018-06-14 14:27:05 +00002003 /* There are three different functions that may be used to do the work
2004 ** of this one, depending on the window frame and the specific built-in
2005 ** window functions used (if any).
2006 **
2007 ** windowCodeRowExprStep() handles all "ROWS" window frames, except for:
dan26522d12018-06-11 18:16:51 +00002008 **
dan13078ca2018-06-13 20:29:38 +00002009 ** ROWS BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW
dan54a9ab32018-06-14 14:27:05 +00002010 **
2011 ** The exception is because windowCodeRowExprStep() implements all window
2012 ** frame types by caching the entire partition in a temp table, and
2013 ** "ROWS BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW" is easy enough to
2014 ** implement without such a cache.
2015 **
2016 ** windowCodeCacheStep() is used for:
2017 **
2018 ** RANGE BETWEEN CURRENT ROW AND UNBOUNDED FOLLOWING
2019 **
2020 ** It is also used for anything not handled by windowCodeRowExprStep()
2021 ** that invokes a built-in window function that requires the entire
2022 ** partition to be cached in a temp table before any rows are returned
2023 ** (e.g. nth_value() or percent_rank()).
2024 **
2025 ** Finally, assuming there is no built-in window function that requires
2026 ** the partition to be cached, windowCodeDefaultStep() is used for:
2027 **
2028 ** RANGE BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW
2029 ** RANGE BETWEEN UNBOUNDED PRECEDING AND UNBOUNDED FOLLOWING
2030 ** RANGE BETWEEN CURRENT ROW AND CURRENT ROW
2031 ** ROWS BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW
2032 **
2033 ** windowCodeDefaultStep() is the only one of the three functions that
2034 ** does not cache each partition in a temp table before beginning to
2035 ** return rows.
dan26522d12018-06-11 18:16:51 +00002036 */
dan54a9ab32018-06-14 14:27:05 +00002037 if( pMWin->eType==TK_ROWS
2038 && (pMWin->eStart!=TK_UNBOUNDED||pMWin->eEnd!=TK_CURRENT||!pMWin->pOrderBy)
dan09590aa2018-05-25 20:30:17 +00002039 ){
danc3a20c12018-05-23 20:55:37 +00002040 windowCodeRowExprStep(pParse, p, pWInfo, regGosub, addrGosub);
dan13078ca2018-06-13 20:29:38 +00002041 }else{
2042 Window *pWin;
dan54a9ab32018-06-14 14:27:05 +00002043 int bCache = 0; /* True to use CacheStep() */
danf9eae182018-05-21 19:45:11 +00002044
dan54a9ab32018-06-14 14:27:05 +00002045 if( pMWin->eStart==TK_CURRENT && pMWin->eEnd==TK_UNBOUNDED ){
dan13078ca2018-06-13 20:29:38 +00002046 bCache = 1;
2047 }else{
dan13078ca2018-06-13 20:29:38 +00002048 for(pWin=pMWin; pWin; pWin=pWin->pNextWin){
2049 FuncDef *pFunc = pWin->pFunc;
2050 if( (pFunc->funcFlags & SQLITE_FUNC_WINDOW_SIZE)
dan54a9ab32018-06-14 14:27:05 +00002051 || (pFunc->xSFunc==nth_valueStepFunc)
2052 || (pFunc->xSFunc==first_valueStepFunc)
2053 || (pFunc->xSFunc==leadStepFunc)
2054 || (pFunc->xSFunc==lagStepFunc)
2055 ){
dan13078ca2018-06-13 20:29:38 +00002056 bCache = 1;
2057 break;
2058 }
2059 }
2060 }
2061
2062 /* Otherwise, call windowCodeDefaultStep(). */
2063 if( bCache ){
dandfa552f2018-06-02 21:04:28 +00002064 windowCodeCacheStep(pParse, p, pWInfo, regGosub, addrGosub);
dan13078ca2018-06-13 20:29:38 +00002065 }else{
2066 windowCodeDefaultStep(pParse, p, pWInfo, regGosub, addrGosub);
dandfa552f2018-06-02 21:04:28 +00002067 }
danf690b572018-06-01 21:00:08 +00002068 }
danf9eae182018-05-21 19:45:11 +00002069}
2070
dan67a9b8e2018-06-22 20:51:35 +00002071#endif /* SQLITE_OMIT_WINDOWFUNC */