dan | 86fb6e1 | 2018-05-16 20:58:07 +0000 | [diff] [blame] | 1 | /* |
dan | 26522d1 | 2018-06-11 18:16:51 +0000 | [diff] [blame] | 2 | ** 2018 May 08 |
dan | 86fb6e1 | 2018-05-16 20:58:07 +0000 | [diff] [blame] | 3 | ** |
| 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 | |
dan | dfa552f | 2018-06-02 21:04:28 +0000 | [diff] [blame] | 15 | /* |
dan | 7392569 | 2018-06-12 18:40:17 +0000 | [diff] [blame] | 16 | ** 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. |
dan | c0bb445 | 2018-06-12 20:53:38 +0000 | [diff] [blame] | 132 | ** |
| 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. |
dan | 7392569 | 2018-06-12 18:40:17 +0000 | [diff] [blame] | 137 | */ |
| 138 | |
| 139 | /* |
dan | dfa552f | 2018-06-02 21:04:28 +0000 | [diff] [blame] | 140 | ** 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 | */ |
| 145 | static 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 | } |
dan | 2a11bb2 | 2018-06-11 20:50:25 +0000 | [diff] [blame] | 153 | static void row_numberInvFunc( |
dan | dfa552f | 2018-06-02 21:04:28 +0000 | [diff] [blame] | 154 | sqlite3_context *pCtx, |
| 155 | int nArg, |
| 156 | sqlite3_value **apArg |
| 157 | ){ |
| 158 | } |
| 159 | static 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 | /* |
dan | 2a11bb2 | 2018-06-11 20:50:25 +0000 | [diff] [blame] | 165 | ** Context object type used by rank(), dense_rank(), percent_rank() and |
| 166 | ** cume_dist(). |
dan | dfa552f | 2018-06-02 21:04:28 +0000 | [diff] [blame] | 167 | */ |
| 168 | struct CallCount { |
| 169 | i64 nValue; |
| 170 | i64 nStep; |
| 171 | i64 nTotal; |
| 172 | }; |
| 173 | |
| 174 | /* |
| 175 | ** Implementation of built-in window function dense_rank(). |
| 176 | */ |
| 177 | static 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 | } |
dan | 2a11bb2 | 2018-06-11 20:50:25 +0000 | [diff] [blame] | 186 | static void dense_rankInvFunc( |
dan | dfa552f | 2018-06-02 21:04:28 +0000 | [diff] [blame] | 187 | sqlite3_context *pCtx, |
| 188 | int nArg, |
| 189 | sqlite3_value **apArg |
| 190 | ){ |
| 191 | } |
| 192 | static 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 | */ |
| 207 | static 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 | } |
dan | 2a11bb2 | 2018-06-11 20:50:25 +0000 | [diff] [blame] | 221 | static void rankInvFunc( |
dan | dfa552f | 2018-06-02 21:04:28 +0000 | [diff] [blame] | 222 | sqlite3_context *pCtx, |
| 223 | int nArg, |
| 224 | sqlite3_value **apArg |
| 225 | ){ |
| 226 | } |
| 227 | static 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 | */ |
| 239 | static 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 | } |
dan | 2a11bb2 | 2018-06-11 20:50:25 +0000 | [diff] [blame] | 258 | static void percent_rankInvFunc( |
dan | dfa552f | 2018-06-02 21:04:28 +0000 | [diff] [blame] | 259 | sqlite3_context *pCtx, |
| 260 | int nArg, |
| 261 | sqlite3_value **apArg |
| 262 | ){ |
| 263 | } |
| 264 | static 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 | |
dan | f1abe36 | 2018-06-04 08:22:09 +0000 | [diff] [blame] | 278 | static 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 | } |
dan | 2a11bb2 | 2018-06-11 20:50:25 +0000 | [diff] [blame] | 294 | static void cume_distInvFunc( |
dan | f1abe36 | 2018-06-04 08:22:09 +0000 | [diff] [blame] | 295 | sqlite3_context *pCtx, |
| 296 | int nArg, |
| 297 | sqlite3_value **apArg |
| 298 | ){ |
| 299 | } |
| 300 | static void cume_distValueFunc(sqlite3_context *pCtx){ |
| 301 | struct CallCount *p; |
| 302 | p = (struct CallCount*)sqlite3_aggregate_context(pCtx, sizeof(*p)); |
dan | 660af93 | 2018-06-18 16:55:22 +0000 | [diff] [blame] | 303 | if( p && p->nTotal ){ |
dan | f1abe36 | 2018-06-04 08:22:09 +0000 | [diff] [blame] | 304 | double r = (double)(p->nStep) / (double)(p->nTotal); |
| 305 | sqlite3_result_double(pCtx, r); |
| 306 | } |
| 307 | } |
| 308 | |
dan | 2a11bb2 | 2018-06-11 20:50:25 +0000 | [diff] [blame] | 309 | /* |
| 310 | ** Context object for ntile() window function. |
| 311 | */ |
dan | 6bc5c9e | 2018-06-04 18:55:11 +0000 | [diff] [blame] | 312 | struct 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 |
dan | 6bc5c9e | 2018-06-04 18:55:11 +0000 | [diff] [blame] | 323 | */ |
| 324 | static 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 | } |
dan | 2a11bb2 | 2018-06-11 20:50:25 +0000 | [diff] [blame] | 345 | static void ntileInvFunc( |
dan | 6bc5c9e | 2018-06-04 18:55:11 +0000 | [diff] [blame] | 346 | sqlite3_context *pCtx, |
| 347 | int nArg, |
| 348 | sqlite3_value **apArg |
| 349 | ){ |
| 350 | } |
| 351 | static 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 | |
dan | 2a11bb2 | 2018-06-11 20:50:25 +0000 | [diff] [blame] | 374 | /* |
| 375 | ** Context object for last_value() window function. |
| 376 | */ |
dan | 1c5ed62 | 2018-06-05 16:16:17 +0000 | [diff] [blame] | 377 | struct LastValueCtx { |
| 378 | sqlite3_value *pVal; |
| 379 | int nVal; |
| 380 | }; |
| 381 | |
| 382 | /* |
| 383 | ** Implementation of last_value(). |
| 384 | */ |
| 385 | static 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]); |
dan | 6fde179 | 2018-06-15 19:01:35 +0000 | [diff] [blame] | 395 | if( p->pVal==0 ){ |
| 396 | sqlite3_result_error_nomem(pCtx); |
| 397 | }else{ |
| 398 | p->nVal++; |
| 399 | } |
dan | 1c5ed62 | 2018-06-05 16:16:17 +0000 | [diff] [blame] | 400 | } |
| 401 | } |
dan | 2a11bb2 | 2018-06-11 20:50:25 +0000 | [diff] [blame] | 402 | static void last_valueInvFunc( |
dan | 1c5ed62 | 2018-06-05 16:16:17 +0000 | [diff] [blame] | 403 | 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 | } |
| 417 | static 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 | } |
| 424 | static 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 | |
dan | 2a11bb2 | 2018-06-11 20:50:25 +0000 | [diff] [blame] | 434 | /* |
| 435 | ** No-op implementations of nth_value(), first_value(), lead() and lag(). |
| 436 | ** These are all implemented inline using VDBE instructions. |
| 437 | */ |
| 438 | static void nth_valueStepFunc(sqlite3_context *pCtx, int n, sqlite3_value **a){} |
| 439 | static void nth_valueInvFunc(sqlite3_context *pCtx, int n, sqlite3_value **ap){} |
| 440 | static void nth_valueValueFunc(sqlite3_context *pCtx){} |
| 441 | static void first_valueStepFunc(sqlite3_context *p, int n, sqlite3_value **ap){} |
| 442 | static void first_valueInvFunc(sqlite3_context *p, int n, sqlite3_value **ap){} |
| 443 | static void first_valueValueFunc(sqlite3_context *pCtx){} |
| 444 | static void leadStepFunc(sqlite3_context *pCtx, int n, sqlite3_value **ap){} |
| 445 | static void leadInvFunc(sqlite3_context *pCtx, int n, sqlite3_value **ap){} |
| 446 | static void leadValueFunc(sqlite3_context *pCtx){} |
| 447 | static void lagStepFunc(sqlite3_context *pCtx, int n, sqlite3_value **ap){} |
| 448 | static void lagInvFunc(sqlite3_context *pCtx, int n, sqlite3_value **ap){} |
| 449 | static void lagValueFunc(sqlite3_context *pCtx){} |
dan | fe4e25a | 2018-06-07 20:08:59 +0000 | [diff] [blame] | 450 | |
dan | dfa552f | 2018-06-02 21:04:28 +0000 | [diff] [blame] | 451 | #define WINDOWFUNC(name,nArg,extra) { \ |
| 452 | nArg, (SQLITE_UTF8|SQLITE_FUNC_WINDOW|extra), 0, 0, \ |
| 453 | name ## StepFunc, name ## ValueFunc, name ## ValueFunc, \ |
dan | 2a11bb2 | 2018-06-11 20:50:25 +0000 | [diff] [blame] | 454 | name ## InvFunc, #name \ |
dan | dfa552f | 2018-06-02 21:04:28 +0000 | [diff] [blame] | 455 | } |
| 456 | |
dan | 1c5ed62 | 2018-06-05 16:16:17 +0000 | [diff] [blame] | 457 | #define WINDOWFUNCF(name,nArg,extra) { \ |
| 458 | nArg, (SQLITE_UTF8|SQLITE_FUNC_WINDOW|extra), 0, 0, \ |
| 459 | name ## StepFunc, name ## FinalizeFunc, name ## ValueFunc, \ |
dan | 2a11bb2 | 2018-06-11 20:50:25 +0000 | [diff] [blame] | 460 | name ## InvFunc, #name \ |
dan | 1c5ed62 | 2018-06-05 16:16:17 +0000 | [diff] [blame] | 461 | } |
| 462 | |
dan | dfa552f | 2018-06-02 21:04:28 +0000 | [diff] [blame] | 463 | /* |
| 464 | ** Register those built-in window functions that are not also aggregates. |
| 465 | */ |
| 466 | void 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), |
dan | f1abe36 | 2018-06-04 08:22:09 +0000 | [diff] [blame] | 472 | WINDOWFUNC(cume_dist, 0, SQLITE_FUNC_WINDOW_SIZE), |
dan | 6bc5c9e | 2018-06-04 18:55:11 +0000 | [diff] [blame] | 473 | WINDOWFUNC(ntile, 1, SQLITE_FUNC_WINDOW_SIZE), |
dan | 1c5ed62 | 2018-06-05 16:16:17 +0000 | [diff] [blame] | 474 | WINDOWFUNCF(last_value, 1, 0), |
dan | dfa552f | 2018-06-02 21:04:28 +0000 | [diff] [blame] | 475 | WINDOWFUNC(nth_value, 2, 0), |
dan | 7095c00 | 2018-06-07 17:45:22 +0000 | [diff] [blame] | 476 | WINDOWFUNC(first_value, 1, 0), |
dan | fe4e25a | 2018-06-07 20:08:59 +0000 | [diff] [blame] | 477 | 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), |
dan | dfa552f | 2018-06-02 21:04:28 +0000 | [diff] [blame] | 479 | }; |
| 480 | sqlite3InsertBuiltinFuncs(aWindowFuncs, ArraySize(aWindowFuncs)); |
| 481 | } |
| 482 | |
dan | c0bb445 | 2018-06-12 20:53:38 +0000 | [diff] [blame] | 483 | /* |
| 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 | */ |
dan | e3bf632 | 2018-06-08 20:58:27 +0000 | [diff] [blame] | 500 | void sqlite3WindowUpdate( |
| 501 | Parse *pParse, |
dan | c0bb445 | 2018-06-12 20:53:38 +0000 | [diff] [blame] | 502 | Window *pList, /* List of named windows for this SELECT */ |
| 503 | Window *pWin, /* Window frame to update */ |
| 504 | FuncDef *pFunc /* Window function definition */ |
dan | e3bf632 | 2018-06-08 20:58:27 +0000 | [diff] [blame] | 505 | ){ |
dan | c95f38d | 2018-06-18 20:34:43 +0000 | [diff] [blame^] | 506 | if( pWin->zName && pWin->eType==0 ){ |
dan | e3bf632 | 2018-06-08 20:58:27 +0000 | [diff] [blame] | 507 | Window *p; |
| 508 | for(p=pList; p; p=p->pNextWin){ |
| 509 | if( sqlite3StrICmp(p->zName, pWin->zName)==0 ) break; |
| 510 | } |
| 511 | if( p==0 ){ |
| 512 | sqlite3ErrorMsg(pParse, "no such window: %s", pWin->zName); |
| 513 | return; |
| 514 | } |
| 515 | pWin->pPartition = sqlite3ExprListDup(pParse->db, p->pPartition, 0); |
| 516 | pWin->pOrderBy = sqlite3ExprListDup(pParse->db, p->pOrderBy, 0); |
| 517 | pWin->pStart = sqlite3ExprDup(pParse->db, p->pStart, 0); |
| 518 | pWin->pEnd = sqlite3ExprDup(pParse->db, p->pEnd, 0); |
| 519 | pWin->eStart = p->eStart; |
| 520 | pWin->eEnd = p->eEnd; |
dan | c95f38d | 2018-06-18 20:34:43 +0000 | [diff] [blame^] | 521 | pWin->eType = p->eType; |
dan | e3bf632 | 2018-06-08 20:58:27 +0000 | [diff] [blame] | 522 | } |
dan | dfa552f | 2018-06-02 21:04:28 +0000 | [diff] [blame] | 523 | if( pFunc->funcFlags & SQLITE_FUNC_WINDOW ){ |
| 524 | sqlite3 *db = pParse->db; |
dan | 8b98560 | 2018-06-09 17:43:45 +0000 | [diff] [blame] | 525 | if( pWin->pFilter ){ |
| 526 | sqlite3ErrorMsg(pParse, |
| 527 | "FILTER clause may only be used with aggregate window functions" |
| 528 | ); |
| 529 | }else |
dan | 6bc5c9e | 2018-06-04 18:55:11 +0000 | [diff] [blame] | 530 | if( pFunc->xSFunc==row_numberStepFunc || pFunc->xSFunc==ntileStepFunc ){ |
dan | dfa552f | 2018-06-02 21:04:28 +0000 | [diff] [blame] | 531 | 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; |
dan | 8b98560 | 2018-06-09 17:43:45 +0000 | [diff] [blame] | 537 | }else |
dan | dfa552f | 2018-06-02 21:04:28 +0000 | [diff] [blame] | 538 | |
| 539 | if( pFunc->xSFunc==dense_rankStepFunc || pFunc->xSFunc==rankStepFunc |
dan | f1abe36 | 2018-06-04 08:22:09 +0000 | [diff] [blame] | 540 | || pFunc->xSFunc==percent_rankStepFunc || pFunc->xSFunc==cume_distStepFunc |
dan | dfa552f | 2018-06-02 21:04:28 +0000 | [diff] [blame] | 541 | ){ |
| 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 | } |
dan | 2a11bb2 | 2018-06-11 20:50:25 +0000 | [diff] [blame] | 550 | pWin->pFunc = pFunc; |
dan | dfa552f | 2018-06-02 21:04:28 +0000 | [diff] [blame] | 551 | } |
| 552 | |
dan | c0bb445 | 2018-06-12 20:53:38 +0000 | [diff] [blame] | 553 | /* |
| 554 | ** Context object passed through sqlite3WalkExprList() to |
| 555 | ** selectWindowRewriteExprCb() by selectWindowRewriteEList(). |
| 556 | */ |
dan | dfa552f | 2018-06-02 21:04:28 +0000 | [diff] [blame] | 557 | typedef struct WindowRewrite WindowRewrite; |
| 558 | struct WindowRewrite { |
| 559 | Window *pWin; |
| 560 | ExprList *pSub; |
| 561 | }; |
| 562 | |
dan | c0bb445 | 2018-06-12 20:53:38 +0000 | [diff] [blame] | 563 | /* |
| 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 | */ |
dan | dfa552f | 2018-06-02 21:04:28 +0000 | [diff] [blame] | 568 | static 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 ){ |
dan | 2a11bb2 | 2018-06-11 20:50:25 +0000 | [diff] [blame] | 581 | assert( pWin->pOwner==pExpr ); |
dan | dfa552f | 2018-06-02 21:04:28 +0000 | [diff] [blame] | 582 | return WRC_Prune; |
| 583 | } |
| 584 | } |
| 585 | } |
| 586 | /* Fall through. */ |
| 587 | |
dan | 7392569 | 2018-06-12 18:40:17 +0000 | [diff] [blame] | 588 | case TK_AGG_FUNCTION: |
dan | dfa552f | 2018-06-02 21:04:28 +0000 | [diff] [blame] | 589 | 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 | } |
dan | c0bb445 | 2018-06-12 20:53:38 +0000 | [diff] [blame] | 613 | static int selectWindowRewriteSelectCb(Walker *pWalker, Select *pSelect){ |
| 614 | return WRC_Prune; |
| 615 | } |
dan | dfa552f | 2018-06-02 21:04:28 +0000 | [diff] [blame] | 616 | |
dan | c0bb445 | 2018-06-12 20:53:38 +0000 | [diff] [blame] | 617 | |
| 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 | */ |
dan | 13b08bb | 2018-06-15 20:46:12 +0000 | [diff] [blame] | 631 | static void selectWindowRewriteEList( |
dan | dfa552f | 2018-06-02 21:04:28 +0000 | [diff] [blame] | 632 | 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; |
dan | dfa552f | 2018-06-02 21:04:28 +0000 | [diff] [blame] | 639 | |
| 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 | |
dan | 13b08bb | 2018-06-15 20:46:12 +0000 | [diff] [blame] | 651 | (void)sqlite3WalkExprList(&sWalker, pEList); |
dan | dfa552f | 2018-06-02 21:04:28 +0000 | [diff] [blame] | 652 | |
| 653 | *ppSub = sRewrite.pSub; |
dan | dfa552f | 2018-06-02 21:04:28 +0000 | [diff] [blame] | 654 | } |
| 655 | |
dan | c0bb445 | 2018-06-12 20:53:38 +0000 | [diff] [blame] | 656 | /* |
| 657 | ** Append a copy of each expression in expression-list pAppend to |
| 658 | ** expression list pList. Return a pointer to the result list. |
| 659 | */ |
dan | dfa552f | 2018-06-02 21:04:28 +0000 | [diff] [blame] | 660 | static 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 |
dan | c0bb445 | 2018-06-12 20:53:38 +0000 | [diff] [blame] | 681 | ** are invoked in the correct order as described under "SELECT REWRITING" |
| 682 | ** at the top of this file. |
dan | dfa552f | 2018-06-02 21:04:28 +0000 | [diff] [blame] | 683 | */ |
| 684 | int sqlite3WindowRewrite(Parse *pParse, Select *p){ |
| 685 | int rc = SQLITE_OK; |
| 686 | if( p->pWin ){ |
| 687 | Vdbe *v = sqlite3GetVdbe(pParse); |
dan | dfa552f | 2018-06-02 21:04:28 +0000 | [diff] [blame] | 688 | sqlite3 *db = pParse->db; |
| 689 | Select *pSub = 0; /* The subquery */ |
| 690 | SrcList *pSrc = p->pSrc; |
| 691 | Expr *pWhere = p->pWhere; |
| 692 | ExprList *pGroupBy = p->pGroupBy; |
| 693 | Expr *pHaving = p->pHaving; |
| 694 | ExprList *pSort = 0; |
| 695 | |
| 696 | ExprList *pSublist = 0; /* Expression list for sub-query */ |
| 697 | Window *pMWin = p->pWin; /* Master window object */ |
| 698 | Window *pWin; /* Window object iterator */ |
| 699 | |
| 700 | p->pSrc = 0; |
| 701 | p->pWhere = 0; |
| 702 | p->pGroupBy = 0; |
| 703 | p->pHaving = 0; |
| 704 | |
| 705 | /* Assign a cursor number for the ephemeral table used to buffer rows. |
| 706 | ** The OpenEphemeral instruction is coded later, after it is known how |
| 707 | ** many columns the table will have. */ |
| 708 | pMWin->iEphCsr = pParse->nTab++; |
| 709 | |
dan | 13b08bb | 2018-06-15 20:46:12 +0000 | [diff] [blame] | 710 | selectWindowRewriteEList(pParse, pMWin, p->pEList, &pSublist); |
| 711 | selectWindowRewriteEList(pParse, pMWin, p->pOrderBy, &pSublist); |
dan | dfa552f | 2018-06-02 21:04:28 +0000 | [diff] [blame] | 712 | 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); |
dan | 8b98560 | 2018-06-09 17:43:45 +0000 | [diff] [blame] | 731 | if( pWin->pFilter ){ |
| 732 | Expr *pFilter = sqlite3ExprDup(db, pWin->pFilter, 0); |
| 733 | pSublist = sqlite3ExprListAppend(pParse, pSublist, pFilter); |
| 734 | } |
dan | dfa552f | 2018-06-02 21:04:28 +0000 | [diff] [blame] | 735 | 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); |
dan | 6fde179 | 2018-06-15 19:01:35 +0000 | [diff] [blame] | 744 | assert( p->pSrc || db->mallocFailed ); |
dan | dfa552f | 2018-06-02 21:04:28 +0000 | [diff] [blame] | 745 | if( p->pSrc ){ |
dan | dfa552f | 2018-06-02 21:04:28 +0000 | [diff] [blame] | 746 | 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; |
dan | 7392569 | 2018-06-12 18:40:17 +0000 | [diff] [blame] | 752 | p->selFlags &= ~SF_Aggregate; |
| 753 | sqlite3SelectPrep(pParse, pSub, 0); |
dan | dfa552f | 2018-06-02 21:04:28 +0000 | [diff] [blame] | 754 | } |
dan | dfa552f | 2018-06-02 21:04:28 +0000 | [diff] [blame] | 755 | |
dan | 6fde179 | 2018-06-15 19:01:35 +0000 | [diff] [blame] | 756 | sqlite3VdbeAddOp2(v, OP_OpenEphemeral, pMWin->iEphCsr, pSublist->nExpr); |
| 757 | }else{ |
| 758 | sqlite3SelectDelete(db, pSub); |
| 759 | } |
| 760 | if( db->mallocFailed ) rc = SQLITE_NOMEM; |
dan | dfa552f | 2018-06-02 21:04:28 +0000 | [diff] [blame] | 761 | } |
| 762 | |
| 763 | return rc; |
| 764 | } |
| 765 | |
dan | c0bb445 | 2018-06-12 20:53:38 +0000 | [diff] [blame] | 766 | /* |
| 767 | ** Free the Window object passed as the second argument. |
| 768 | */ |
dan | 86fb6e1 | 2018-05-16 20:58:07 +0000 | [diff] [blame] | 769 | void 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); |
dan | e3bf632 | 2018-06-08 20:58:27 +0000 | [diff] [blame] | 776 | sqlite3DbFree(db, p->zName); |
dan | 86fb6e1 | 2018-05-16 20:58:07 +0000 | [diff] [blame] | 777 | sqlite3DbFree(db, p); |
| 778 | } |
| 779 | } |
| 780 | |
dan | c0bb445 | 2018-06-12 20:53:38 +0000 | [diff] [blame] | 781 | /* |
| 782 | ** Free the linked list of Window objects starting at the second argument. |
| 783 | */ |
dan | e3bf632 | 2018-06-08 20:58:27 +0000 | [diff] [blame] | 784 | void sqlite3WindowListDelete(sqlite3 *db, Window *p){ |
| 785 | while( p ){ |
| 786 | Window *pNext = p->pNextWin; |
| 787 | sqlite3WindowDelete(db, p); |
| 788 | p = pNext; |
| 789 | } |
| 790 | } |
| 791 | |
dan | c0bb445 | 2018-06-12 20:53:38 +0000 | [diff] [blame] | 792 | /* |
| 793 | ** Allocate and return a new Window object. |
| 794 | */ |
dan | 86fb6e1 | 2018-05-16 20:58:07 +0000 | [diff] [blame] | 795 | Window *sqlite3WindowAlloc( |
| 796 | Parse *pParse, |
| 797 | int eType, |
dan | c3a20c1 | 2018-05-23 20:55:37 +0000 | [diff] [blame] | 798 | int eStart, Expr *pStart, |
| 799 | int eEnd, Expr *pEnd |
dan | 86fb6e1 | 2018-05-16 20:58:07 +0000 | [diff] [blame] | 800 | ){ |
| 801 | Window *pWin = (Window*)sqlite3DbMallocZero(pParse->db, sizeof(Window)); |
| 802 | |
| 803 | if( pWin ){ |
dan | c95f38d | 2018-06-18 20:34:43 +0000 | [diff] [blame^] | 804 | assert( eType ); |
dan | 86fb6e1 | 2018-05-16 20:58:07 +0000 | [diff] [blame] | 805 | 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 | |
dan | c0bb445 | 2018-06-12 20:53:38 +0000 | [diff] [blame] | 818 | /* |
| 819 | ** Attach window object pWin to expression p. |
| 820 | */ |
dan | 86fb6e1 | 2018-05-16 20:58:07 +0000 | [diff] [blame] | 821 | void sqlite3WindowAttach(Parse *pParse, Expr *p, Window *pWin){ |
| 822 | if( p ){ |
| 823 | p->pWin = pWin; |
dan | 2a11bb2 | 2018-06-11 20:50:25 +0000 | [diff] [blame] | 824 | if( pWin ) pWin->pOwner = p; |
dan | 86fb6e1 | 2018-05-16 20:58:07 +0000 | [diff] [blame] | 825 | }else{ |
| 826 | sqlite3WindowDelete(pParse->db, pWin); |
| 827 | } |
| 828 | } |
dan | e2f781b | 2018-05-17 19:24:08 +0000 | [diff] [blame] | 829 | |
| 830 | /* |
| 831 | ** Return 0 if the two window objects are identical, or non-zero otherwise. |
dan | 13078ca | 2018-06-13 20:29:38 +0000 | [diff] [blame] | 832 | ** Identical window objects can be processed in a single scan. |
dan | e2f781b | 2018-05-17 19:24:08 +0000 | [diff] [blame] | 833 | */ |
| 834 | int 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 | |
dan | 13078ca | 2018-06-13 20:29:38 +0000 | [diff] [blame] | 845 | |
| 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 | */ |
| 851 | void sqlite3WindowCodeInit(Parse *pParse, Window *pMWin){ |
dan | c9a8668 | 2018-05-30 20:44:58 +0000 | [diff] [blame] | 852 | Window *pWin; |
dan | 13078ca | 2018-06-13 20:29:38 +0000 | [diff] [blame] | 853 | 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 | |
dan | c9a8668 | 2018-05-30 20:44:58 +0000 | [diff] [blame] | 862 | for(pWin=pMWin; pWin; pWin=pWin->pNextWin){ |
dan | ec891fd | 2018-06-06 20:51:02 +0000 | [diff] [blame] | 863 | FuncDef *p = pWin->pFunc; |
| 864 | if( (p->funcFlags & SQLITE_FUNC_MINMAX) && pWin->eStart!=TK_UNBOUNDED ){ |
dan | 9a94722 | 2018-06-14 19:06:36 +0000 | [diff] [blame] | 865 | /* 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 | */ |
dan | c9a8668 | 2018-05-30 20:44:58 +0000 | [diff] [blame] | 872 | ExprList *pList = pWin->pOwner->x.pList; |
| 873 | KeyInfo *pKeyInfo = sqlite3KeyInfoFromExprList(pParse, pList, 0, 0); |
dan | c9a8668 | 2018-05-30 20:44:58 +0000 | [diff] [blame] | 874 | 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 | } |
dan | 7095c00 | 2018-06-07 17:45:22 +0000 | [diff] [blame] | 885 | else if( p->xSFunc==nth_valueStepFunc || p->xSFunc==first_valueStepFunc ){ |
dan | ec891fd | 2018-06-06 20:51:02 +0000 | [diff] [blame] | 886 | /* 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; |
dan | ec891fd | 2018-06-06 20:51:02 +0000 | [diff] [blame] | 892 | sqlite3VdbeAddOp2(v, OP_OpenDup, pWin->csrApp, pMWin->iEphCsr); |
| 893 | } |
dan | fe4e25a | 2018-06-07 20:08:59 +0000 | [diff] [blame] | 894 | 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 | } |
dan | c9a8668 | 2018-05-30 20:44:58 +0000 | [diff] [blame] | 899 | } |
| 900 | } |
| 901 | |
dan | 13078ca | 2018-06-13 20:29:38 +0000 | [diff] [blame] | 902 | /* |
| 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 | */ |
dan | c3a20c1 | 2018-05-23 20:55:37 +0000 | [diff] [blame] | 908 | static 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); |
dan | 13078ca | 2018-06-13 20:29:38 +0000 | [diff] [blame] | 914 | int regZero = sqlite3GetTempReg(pParse); |
dan | c3a20c1 | 2018-05-23 20:55:37 +0000 | [diff] [blame] | 915 | 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); |
dan | 13078ca | 2018-06-13 20:29:38 +0000 | [diff] [blame] | 920 | sqlite3ReleaseTempReg(pParse, regZero); |
dan | c3a20c1 | 2018-05-23 20:55:37 +0000 | [diff] [blame] | 921 | } |
| 922 | |
dan | 13078ca | 2018-06-13 20:29:38 +0000 | [diff] [blame] | 923 | /* |
| 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 | */ |
dan | 2a11bb2 | 2018-06-11 20:50:25 +0000 | [diff] [blame] | 927 | static int windowArgCount(Window *pWin){ |
| 928 | ExprList *pList = pWin->pOwner->x.pList; |
| 929 | return (pList ? pList->nExpr : 0); |
| 930 | } |
| 931 | |
dan | c9a8668 | 2018-05-30 20:44:58 +0000 | [diff] [blame] | 932 | /* |
| 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 |
dan | 13078ca | 2018-06-13 20:29:38 +0000 | [diff] [blame] | 935 | ** 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. |
dan | c9a8668 | 2018-05-30 20:44:58 +0000 | [diff] [blame] | 950 | */ |
dan | 31f5639 | 2018-05-24 21:10:57 +0000 | [diff] [blame] | 951 | static void windowAggStep( |
| 952 | Parse *pParse, |
dan | 13078ca | 2018-06-13 20:29:38 +0000 | [diff] [blame] | 953 | 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 */ |
dan | dfa552f | 2018-06-02 21:04:28 +0000 | [diff] [blame] | 957 | int regPartSize /* Register containing size of partition */ |
dan | 31f5639 | 2018-05-24 21:10:57 +0000 | [diff] [blame] | 958 | ){ |
| 959 | Vdbe *v = sqlite3GetVdbe(pParse); |
| 960 | Window *pWin; |
| 961 | for(pWin=pMWin; pWin; pWin=pWin->pNextWin){ |
dan | dfa552f | 2018-06-02 21:04:28 +0000 | [diff] [blame] | 962 | int flags = pWin->pFunc->funcFlags; |
dan | c9a8668 | 2018-05-30 20:44:58 +0000 | [diff] [blame] | 963 | int regArg; |
dan | 2a11bb2 | 2018-06-11 20:50:25 +0000 | [diff] [blame] | 964 | int nArg = windowArgCount(pWin); |
dan | dfa552f | 2018-06-02 21:04:28 +0000 | [diff] [blame] | 965 | |
dan | 6bc5c9e | 2018-06-04 18:55:11 +0000 | [diff] [blame] | 966 | if( csr>=0 ){ |
dan | c9a8668 | 2018-05-30 20:44:58 +0000 | [diff] [blame] | 967 | int i; |
dan | 2a11bb2 | 2018-06-11 20:50:25 +0000 | [diff] [blame] | 968 | for(i=0; i<nArg; i++){ |
dan | c9a8668 | 2018-05-30 20:44:58 +0000 | [diff] [blame] | 969 | sqlite3VdbeAddOp3(v, OP_Column, csr, pWin->iArgCol+i, reg+i); |
| 970 | } |
| 971 | regArg = reg; |
dan | 6bc5c9e | 2018-06-04 18:55:11 +0000 | [diff] [blame] | 972 | 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 | } |
dan | c9a8668 | 2018-05-30 20:44:58 +0000 | [diff] [blame] | 980 | }else{ |
dan | 6bc5c9e | 2018-06-04 18:55:11 +0000 | [diff] [blame] | 981 | assert( !(flags & SQLITE_FUNC_WINDOW_SIZE) ); |
dan | c9a8668 | 2018-05-30 20:44:58 +0000 | [diff] [blame] | 982 | regArg = reg + pWin->iArgCol; |
dan | 31f5639 | 2018-05-24 21:10:57 +0000 | [diff] [blame] | 983 | } |
dan | c9a8668 | 2018-05-30 20:44:58 +0000 | [diff] [blame] | 984 | |
dan | ec891fd | 2018-06-06 20:51:02 +0000 | [diff] [blame] | 985 | if( (pWin->pFunc->funcFlags & SQLITE_FUNC_MINMAX) |
| 986 | && pWin->eStart!=TK_UNBOUNDED |
| 987 | ){ |
dan | c9a8668 | 2018-05-30 20:44:58 +0000 | [diff] [blame] | 988 | 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 | } |
dan | ec891fd | 2018-06-06 20:51:02 +0000 | [diff] [blame] | 998 | }else if( pWin->regApp ){ |
dan | 7095c00 | 2018-06-07 17:45:22 +0000 | [diff] [blame] | 999 | assert( pWin->pFunc->xSFunc==nth_valueStepFunc |
| 1000 | || pWin->pFunc->xSFunc==first_valueStepFunc |
| 1001 | ); |
dan | ec891fd | 2018-06-06 20:51:02 +0000 | [diff] [blame] | 1002 | assert( bInverse==0 || bInverse==1 ); |
| 1003 | sqlite3VdbeAddOp2(v, OP_AddImm, pWin->regApp+1-bInverse, 1); |
dan | fe4e25a | 2018-06-07 20:08:59 +0000 | [diff] [blame] | 1004 | }else if( pWin->pFunc->xSFunc==leadStepFunc |
| 1005 | || pWin->pFunc->xSFunc==lagStepFunc |
| 1006 | ){ |
| 1007 | /* no-op */ |
dan | c9a8668 | 2018-05-30 20:44:58 +0000 | [diff] [blame] | 1008 | }else{ |
dan | 8b98560 | 2018-06-09 17:43:45 +0000 | [diff] [blame] | 1009 | int addrIf = 0; |
| 1010 | if( pWin->pFilter ){ |
| 1011 | int regTmp; |
dan | 2a11bb2 | 2018-06-11 20:50:25 +0000 | [diff] [blame] | 1012 | assert( nArg==pWin->pOwner->x.pList->nExpr ); |
dan | 8b98560 | 2018-06-09 17:43:45 +0000 | [diff] [blame] | 1013 | if( csr>0 ){ |
| 1014 | regTmp = sqlite3GetTempReg(pParse); |
dan | 2a11bb2 | 2018-06-11 20:50:25 +0000 | [diff] [blame] | 1015 | sqlite3VdbeAddOp3(v, OP_Column, csr, pWin->iArgCol+nArg,regTmp); |
dan | 8b98560 | 2018-06-09 17:43:45 +0000 | [diff] [blame] | 1016 | }else{ |
dan | 2a11bb2 | 2018-06-11 20:50:25 +0000 | [diff] [blame] | 1017 | regTmp = regArg + nArg; |
dan | 8b98560 | 2018-06-09 17:43:45 +0000 | [diff] [blame] | 1018 | } |
| 1019 | addrIf = sqlite3VdbeAddOp3(v, OP_IfNot, regTmp, 0, 1); |
| 1020 | if( csr>0 ){ |
| 1021 | sqlite3ReleaseTempReg(pParse, regTmp); |
| 1022 | } |
| 1023 | } |
dan | c9a8668 | 2018-05-30 20:44:58 +0000 | [diff] [blame] | 1024 | if( pWin->pFunc->funcFlags & SQLITE_FUNC_NEEDCOLL ){ |
| 1025 | CollSeq *pColl; |
dan | 8b98560 | 2018-06-09 17:43:45 +0000 | [diff] [blame] | 1026 | pColl = sqlite3ExprNNCollSeq(pParse, pWin->pOwner->x.pList->a[0].pExpr); |
dan | c9a8668 | 2018-05-30 20:44:58 +0000 | [diff] [blame] | 1027 | 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); |
dan | dfa552f | 2018-06-02 21:04:28 +0000 | [diff] [blame] | 1031 | sqlite3VdbeChangeP5(v, (u8)nArg); |
dan | 8b98560 | 2018-06-09 17:43:45 +0000 | [diff] [blame] | 1032 | if( addrIf ) sqlite3VdbeJumpHere(v, addrIf); |
dan | c9a8668 | 2018-05-30 20:44:58 +0000 | [diff] [blame] | 1033 | } |
dan | 31f5639 | 2018-05-24 21:10:57 +0000 | [diff] [blame] | 1034 | } |
| 1035 | } |
| 1036 | |
dan | 13078ca | 2018-06-13 20:29:38 +0000 | [diff] [blame] | 1037 | /* |
| 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 | */ |
dan | d6f784e | 2018-05-28 18:30:45 +0000 | [diff] [blame] | 1043 | static 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){ |
dan | ec891fd | 2018-06-06 20:51:02 +0000 | [diff] [blame] | 1048 | if( (pWin->pFunc->funcFlags & SQLITE_FUNC_MINMAX) |
| 1049 | && pWin->eStart!=TK_UNBOUNDED |
| 1050 | ){ |
dan | d6f784e | 2018-05-28 18:30:45 +0000 | [diff] [blame] | 1051 | sqlite3VdbeAddOp2(v, OP_Null, 0, pWin->regResult); |
dan | c9a8668 | 2018-05-30 20:44:58 +0000 | [diff] [blame] | 1052 | 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 | } |
dan | ec891fd | 2018-06-06 20:51:02 +0000 | [diff] [blame] | 1058 | }else if( pWin->regApp ){ |
dan | d6f784e | 2018-05-28 18:30:45 +0000 | [diff] [blame] | 1059 | }else{ |
dan | c9a8668 | 2018-05-30 20:44:58 +0000 | [diff] [blame] | 1060 | if( bFinal==0 ){ |
| 1061 | sqlite3VdbeAddOp2(v, OP_Null, 0, pWin->regResult); |
| 1062 | } |
dan | 2a11bb2 | 2018-06-11 20:50:25 +0000 | [diff] [blame] | 1063 | sqlite3VdbeAddOp2(v, OP_AggFinal, pWin->regAccum, windowArgCount(pWin)); |
dan | c9a8668 | 2018-05-30 20:44:58 +0000 | [diff] [blame] | 1064 | sqlite3VdbeAppendP4(v, pWin->pFunc, P4_FUNCDEF); |
| 1065 | if( bFinal ){ |
| 1066 | sqlite3VdbeAddOp2(v, OP_Copy, pWin->regAccum, pWin->regResult); |
dan | 8b98560 | 2018-06-09 17:43:45 +0000 | [diff] [blame] | 1067 | sqlite3VdbeAddOp2(v, OP_Null, 0, pWin->regAccum); |
dan | c9a8668 | 2018-05-30 20:44:58 +0000 | [diff] [blame] | 1068 | }else{ |
| 1069 | sqlite3VdbeChangeP3(v, -1, pWin->regResult); |
| 1070 | } |
dan | d6f784e | 2018-05-28 18:30:45 +0000 | [diff] [blame] | 1071 | } |
| 1072 | } |
| 1073 | } |
| 1074 | |
dan | 13078ca | 2018-06-13 20:29:38 +0000 | [diff] [blame] | 1075 | /* |
| 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 | */ |
dan | f690b57 | 2018-06-01 21:00:08 +0000 | [diff] [blame] | 1080 | static void windowPartitionCache( |
| 1081 | Parse *pParse, |
dan | 13078ca | 2018-06-13 20:29:38 +0000 | [diff] [blame] | 1082 | 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 */ |
dan | f690b57 | 2018-06-01 21:00:08 +0000 | [diff] [blame] | 1087 | ){ |
| 1088 | Window *pMWin = p->pWin; |
| 1089 | Vdbe *v = sqlite3GetVdbe(pParse); |
dan | f690b57 | 2018-06-01 21:00:08 +0000 | [diff] [blame] | 1090 | 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 | |
dan | dfa552f | 2018-06-02 21:04:28 +0000 | [diff] [blame] | 1098 | *pRegSize = regRowid; |
dan | f690b57 | 2018-06-01 21:00:08 +0000 | [diff] [blame] | 1099 | 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; |
dan | e0a5e20 | 2018-06-15 16:10:44 +0000 | [diff] [blame] | 1113 | int nPart = pPart->nExpr; |
dan | f690b57 | 2018-06-01 21:00:08 +0000 | [diff] [blame] | 1114 | 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 | } |
dan | d6f784e | 2018-05-28 18:30:45 +0000 | [diff] [blame] | 1134 | |
dan | 13078ca | 2018-06-13 20:29:38 +0000 | [diff] [blame] | 1135 | /* |
| 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 | */ |
dan | ec891fd | 2018-06-06 20:51:02 +0000 | [diff] [blame] | 1148 | static 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; |
dan | 7095c00 | 2018-06-07 17:45:22 +0000 | [diff] [blame] | 1158 | if( pFunc->xSFunc==nth_valueStepFunc |
| 1159 | || pFunc->xSFunc==first_valueStepFunc |
| 1160 | ){ |
dan | ec891fd | 2018-06-06 20:51:02 +0000 | [diff] [blame] | 1161 | int csr = pWin->csrApp; |
| 1162 | int lbl = sqlite3VdbeMakeLabel(v); |
| 1163 | int tmpReg = sqlite3GetTempReg(pParse); |
| 1164 | sqlite3VdbeAddOp2(v, OP_Null, 0, pWin->regResult); |
dan | 7095c00 | 2018-06-07 17:45:22 +0000 | [diff] [blame] | 1165 | |
| 1166 | if( pFunc->xSFunc==nth_valueStepFunc ){ |
dan | 6fde179 | 2018-06-15 19:01:35 +0000 | [diff] [blame] | 1167 | sqlite3VdbeAddOp3(v, OP_Column, pMWin->iEphCsr, pWin->iArgCol+1,tmpReg); |
dan | 7095c00 | 2018-06-07 17:45:22 +0000 | [diff] [blame] | 1168 | }else{ |
| 1169 | sqlite3VdbeAddOp2(v, OP_Integer, 1, tmpReg); |
| 1170 | } |
dan | ec891fd | 2018-06-06 20:51:02 +0000 | [diff] [blame] | 1171 | 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 | } |
dan | fe4e25a | 2018-06-07 20:08:59 +0000 | [diff] [blame] | 1178 | else if( pFunc->xSFunc==leadStepFunc || pFunc->xSFunc==lagStepFunc ){ |
dan | 2a11bb2 | 2018-06-11 20:50:25 +0000 | [diff] [blame] | 1179 | int nArg = pWin->pOwner->x.pList->nExpr; |
dan | e0a5e20 | 2018-06-15 16:10:44 +0000 | [diff] [blame] | 1180 | int iEph = pMWin->iEphCsr; |
dan | fe4e25a | 2018-06-07 20:08:59 +0000 | [diff] [blame] | 1181 | int csr = pWin->csrApp; |
| 1182 | int lbl = sqlite3VdbeMakeLabel(v); |
| 1183 | int tmpReg = sqlite3GetTempReg(pParse); |
| 1184 | |
dan | 2a11bb2 | 2018-06-11 20:50:25 +0000 | [diff] [blame] | 1185 | if( nArg<3 ){ |
dan | fe4e25a | 2018-06-07 20:08:59 +0000 | [diff] [blame] | 1186 | 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); |
dan | 2a11bb2 | 2018-06-11 20:50:25 +0000 | [diff] [blame] | 1191 | if( nArg<2 ){ |
dan | fe4e25a | 2018-06-07 20:08:59 +0000 | [diff] [blame] | 1192 | 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 | } |
dan | ec891fd | 2018-06-06 20:51:02 +0000 | [diff] [blame] | 1207 | } |
| 1208 | sqlite3VdbeAddOp2(v, OP_Gosub, regGosub, addrGosub); |
| 1209 | } |
| 1210 | |
dan | 13078ca | 2018-06-13 20:29:38 +0000 | [diff] [blame] | 1211 | /* |
| 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 | */ |
dan | ec891fd | 2018-06-06 20:51:02 +0000 | [diff] [blame] | 1226 | static void windowReturnRows( |
| 1227 | Parse *pParse, |
dan | 13078ca | 2018-06-13 20:29:38 +0000 | [diff] [blame] | 1228 | 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 */ |
dan | ec891fd | 2018-06-06 20:51:02 +0000 | [diff] [blame] | 1234 | ){ |
| 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 | |
dan | 54a9ab3 | 2018-06-14 14:27:05 +0000 | [diff] [blame] | 1248 | /* |
| 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 | */ |
dan | 2e60568 | 2018-06-07 15:54:26 +0000 | [diff] [blame] | 1254 | static 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){ |
dan | 9a94722 | 2018-06-14 19:06:36 +0000 | [diff] [blame] | 1260 | FuncDef *pFunc = pWin->pFunc; |
dan | 2e60568 | 2018-06-07 15:54:26 +0000 | [diff] [blame] | 1261 | sqlite3VdbeAddOp2(v, OP_Null, 0, pWin->regAccum); |
dan | 2a11bb2 | 2018-06-11 20:50:25 +0000 | [diff] [blame] | 1262 | nArg = MAX(nArg, windowArgCount(pWin)); |
dan | 9a94722 | 2018-06-14 19:06:36 +0000 | [diff] [blame] | 1263 | if( pFunc->xSFunc==nth_valueStepFunc |
| 1264 | || pFunc->xSFunc==first_valueStepFunc |
dan | 7095c00 | 2018-06-07 17:45:22 +0000 | [diff] [blame] | 1265 | ){ |
dan | 2e60568 | 2018-06-07 15:54:26 +0000 | [diff] [blame] | 1266 | sqlite3VdbeAddOp2(v, OP_Integer, 0, pWin->regApp); |
| 1267 | sqlite3VdbeAddOp2(v, OP_Integer, 0, pWin->regApp+1); |
| 1268 | } |
dan | 9a94722 | 2018-06-14 19:06:36 +0000 | [diff] [blame] | 1269 | |
| 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 | } |
dan | 2e60568 | 2018-06-07 15:54:26 +0000 | [diff] [blame] | 1275 | } |
| 1276 | regArg = pParse->nMem+1; |
| 1277 | pParse->nMem += nArg; |
| 1278 | return regArg; |
| 1279 | } |
| 1280 | |
| 1281 | |
dan | 99652dd | 2018-05-24 17:49:14 +0000 | [diff] [blame] | 1282 | /* |
dan | 54a9ab3 | 2018-06-14 14:27:05 +0000 | [diff] [blame] | 1283 | ** 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 | ** |
dan | 09590aa | 2018-05-25 20:30:17 +0000 | [diff] [blame] | 1287 | ** ROWS BETWEEN <expr1> PRECEDING AND <expr2> FOLLOWING |
dan | 09590aa | 2018-05-25 20:30:17 +0000 | [diff] [blame] | 1288 | ** |
| 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) |
dan | 99652dd | 2018-05-24 17:49:14 +0000 | [diff] [blame] | 1301 | ** } |
dan | 09590aa | 2018-05-25 20:30:17 +0000 | [diff] [blame] | 1302 | ** 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 |
dan | 99652dd | 2018-05-24 17:49:14 +0000 | [diff] [blame] | 1320 | ** |
dan | 09590aa | 2018-05-25 20:30:17 +0000 | [diff] [blame] | 1321 | ** 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 ){ |
dan | 99652dd | 2018-05-24 17:49:14 +0000 | [diff] [blame] | 1337 | ** AggFinal (xValue) |
| 1338 | ** Gosub addrGosub |
dan | 09590aa | 2018-05-25 20:30:17 +0000 | [diff] [blame] | 1339 | ** Next(csr) // if EOF goto flush_partition_done |
dan | 31f5639 | 2018-05-24 21:10:57 +0000 | [diff] [blame] | 1340 | ** if( (regStart--)<=0 ){ |
| 1341 | ** AggStep (csrStart, xInverse) |
| 1342 | ** Next(csrStart) |
dan | 99652dd | 2018-05-24 17:49:14 +0000 | [diff] [blame] | 1343 | ** } |
| 1344 | ** } |
dan | 99652dd | 2018-05-24 17:49:14 +0000 | [diff] [blame] | 1345 | ** |
dan | 09590aa | 2018-05-25 20:30:17 +0000 | [diff] [blame] | 1346 | ** For the "CURRENT ROW AND UNBOUNDED FOLLOWING" case, the final if() |
| 1347 | ** condition is always true (as if regStart were initialized to 0). |
dan | 99652dd | 2018-05-24 17:49:14 +0000 | [diff] [blame] | 1348 | ** |
dan | 09590aa | 2018-05-25 20:30:17 +0000 | [diff] [blame] | 1349 | ** 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 | ** } |
dan | 99652dd | 2018-05-24 17:49:14 +0000 | [diff] [blame] | 1368 | ** |
dan | 31f5639 | 2018-05-24 21:10:57 +0000 | [diff] [blame] | 1369 | ** 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 ){ |
dan | 31f5639 | 2018-05-24 21:10:57 +0000 | [diff] [blame] | 1376 | ** if( (regStart--)<=0 ){ |
| 1377 | ** AggFinal (xValue) |
| 1378 | ** Gosub addrGosub |
| 1379 | ** Next(csr) // if EOF goto flush_partition_done |
| 1380 | ** } |
dan | e105dd7 | 2018-05-25 09:29:11 +0000 | [diff] [blame] | 1381 | ** AggStep (csrStart, xInverse) |
| 1382 | ** Next (csrStart) |
dan | 31f5639 | 2018-05-24 21:10:57 +0000 | [diff] [blame] | 1383 | ** } |
| 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 | ** |
dan | 99652dd | 2018-05-24 17:49:14 +0000 | [diff] [blame] | 1401 | */ |
dan | c3a20c1 | 2018-05-23 20:55:37 +0000 | [diff] [blame] | 1402 | static 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); |
dan | c3a20c1 | 2018-05-23 20:55:37 +0000 | [diff] [blame] | 1411 | int regFlushPart; /* Register for "Gosub flush_partition" */ |
dan | 31f5639 | 2018-05-24 21:10:57 +0000 | [diff] [blame] | 1412 | int lblFlushPart; /* Label for "Gosub flush_partition" */ |
| 1413 | int lblFlushDone; /* Label for "Gosub flush_partition_done" */ |
dan | c3a20c1 | 2018-05-23 20:55:37 +0000 | [diff] [blame] | 1414 | |
dan | f690b57 | 2018-06-01 21:00:08 +0000 | [diff] [blame] | 1415 | int regArg; |
dan | c3a20c1 | 2018-05-23 20:55:37 +0000 | [diff] [blame] | 1416 | int addr; |
dan | 31f5639 | 2018-05-24 21:10:57 +0000 | [diff] [blame] | 1417 | int csrStart = pParse->nTab++; |
| 1418 | int csrEnd = pParse->nTab++; |
| 1419 | int regStart; /* Value of <expr> PRECEDING */ |
| 1420 | int regEnd; /* Value of <expr> FOLLOWING */ |
dan | c3a20c1 | 2018-05-23 20:55:37 +0000 | [diff] [blame] | 1421 | int addrGoto; |
dan | 31f5639 | 2018-05-24 21:10:57 +0000 | [diff] [blame] | 1422 | int addrTop; |
dan | c3a20c1 | 2018-05-23 20:55:37 +0000 | [diff] [blame] | 1423 | int addrIfPos1; |
| 1424 | int addrIfPos2; |
dan | dfa552f | 2018-06-02 21:04:28 +0000 | [diff] [blame] | 1425 | int regSize = 0; |
dan | 09590aa | 2018-05-25 20:30:17 +0000 | [diff] [blame] | 1426 | |
dan | 99652dd | 2018-05-24 17:49:14 +0000 | [diff] [blame] | 1427 | assert( pMWin->eStart==TK_PRECEDING |
| 1428 | || pMWin->eStart==TK_CURRENT |
dan | e105dd7 | 2018-05-25 09:29:11 +0000 | [diff] [blame] | 1429 | || pMWin->eStart==TK_FOLLOWING |
dan | 99652dd | 2018-05-24 17:49:14 +0000 | [diff] [blame] | 1430 | || pMWin->eStart==TK_UNBOUNDED |
| 1431 | ); |
| 1432 | assert( pMWin->eEnd==TK_FOLLOWING |
| 1433 | || pMWin->eEnd==TK_CURRENT |
| 1434 | || pMWin->eEnd==TK_UNBOUNDED |
dan | 31f5639 | 2018-05-24 21:10:57 +0000 | [diff] [blame] | 1435 | || pMWin->eEnd==TK_PRECEDING |
dan | 99652dd | 2018-05-24 17:49:14 +0000 | [diff] [blame] | 1436 | ); |
| 1437 | |
dan | c3a20c1 | 2018-05-23 20:55:37 +0000 | [diff] [blame] | 1438 | /* Allocate register and label for the "flush_partition" sub-routine. */ |
| 1439 | regFlushPart = ++pParse->nMem; |
dan | 31f5639 | 2018-05-24 21:10:57 +0000 | [diff] [blame] | 1440 | lblFlushPart = sqlite3VdbeMakeLabel(v); |
| 1441 | lblFlushDone = sqlite3VdbeMakeLabel(v); |
dan | c3a20c1 | 2018-05-23 20:55:37 +0000 | [diff] [blame] | 1442 | |
dan | 31f5639 | 2018-05-24 21:10:57 +0000 | [diff] [blame] | 1443 | regStart = ++pParse->nMem; |
| 1444 | regEnd = ++pParse->nMem; |
dan | c3a20c1 | 2018-05-23 20:55:37 +0000 | [diff] [blame] | 1445 | |
dan | dfa552f | 2018-06-02 21:04:28 +0000 | [diff] [blame] | 1446 | windowPartitionCache(pParse, p, pWInfo, regFlushPart, lblFlushPart, ®Size); |
dan | c3a20c1 | 2018-05-23 20:55:37 +0000 | [diff] [blame] | 1447 | |
dan | c3a20c1 | 2018-05-23 20:55:37 +0000 | [diff] [blame] | 1448 | addrGoto = sqlite3VdbeAddOp0(v, OP_Goto); |
| 1449 | |
dan | c9a8668 | 2018-05-30 20:44:58 +0000 | [diff] [blame] | 1450 | /* Start of "flush_partition" */ |
dan | 31f5639 | 2018-05-24 21:10:57 +0000 | [diff] [blame] | 1451 | sqlite3VdbeResolveLabel(v, lblFlushPart); |
dan | c3a20c1 | 2018-05-23 20:55:37 +0000 | [diff] [blame] | 1452 | sqlite3VdbeAddOp2(v, OP_Once, 0, sqlite3VdbeCurrentAddr(v)+3); |
dan | 31f5639 | 2018-05-24 21:10:57 +0000 | [diff] [blame] | 1453 | sqlite3VdbeAddOp2(v, OP_OpenDup, csrStart, pMWin->iEphCsr); |
| 1454 | sqlite3VdbeAddOp2(v, OP_OpenDup, csrEnd, pMWin->iEphCsr); |
dan | c3a20c1 | 2018-05-23 20:55:37 +0000 | [diff] [blame] | 1455 | |
dan | 31f5639 | 2018-05-24 21:10:57 +0000 | [diff] [blame] | 1456 | /* If either regStart or regEnd are not non-negative integers, throw |
dan | 99652dd | 2018-05-24 17:49:14 +0000 | [diff] [blame] | 1457 | ** an exception. */ |
| 1458 | if( pMWin->pStart ){ |
dan | 31f5639 | 2018-05-24 21:10:57 +0000 | [diff] [blame] | 1459 | sqlite3ExprCode(pParse, pMWin->pStart, regStart); |
| 1460 | windowCheckFrameValue(pParse, regStart, 0); |
dan | 99652dd | 2018-05-24 17:49:14 +0000 | [diff] [blame] | 1461 | } |
| 1462 | if( pMWin->pEnd ){ |
dan | 31f5639 | 2018-05-24 21:10:57 +0000 | [diff] [blame] | 1463 | sqlite3ExprCode(pParse, pMWin->pEnd, regEnd); |
| 1464 | windowCheckFrameValue(pParse, regEnd, 1); |
dan | 99652dd | 2018-05-24 17:49:14 +0000 | [diff] [blame] | 1465 | } |
dan | c3a20c1 | 2018-05-23 20:55:37 +0000 | [diff] [blame] | 1466 | |
dan | c9a8668 | 2018-05-30 20:44:58 +0000 | [diff] [blame] | 1467 | /* If this is "ROWS <expr1> FOLLOWING AND ROWS <expr2> FOLLOWING", do: |
| 1468 | ** |
dan | 26522d1 | 2018-06-11 18:16:51 +0000 | [diff] [blame] | 1469 | ** if( regEnd<regStart ){ |
| 1470 | ** // The frame always consists of 0 rows |
| 1471 | ** regStart = regSize; |
| 1472 | ** } |
dan | c9a8668 | 2018-05-30 20:44:58 +0000 | [diff] [blame] | 1473 | ** regEnd = regEnd - regStart; |
| 1474 | */ |
| 1475 | if( pMWin->pEnd && pMWin->pStart && pMWin->eStart==TK_FOLLOWING ){ |
| 1476 | assert( pMWin->eEnd==TK_FOLLOWING ); |
dan | 26522d1 | 2018-06-11 18:16:51 +0000 | [diff] [blame] | 1477 | sqlite3VdbeAddOp3(v, OP_Ge, regStart, sqlite3VdbeCurrentAddr(v)+2, regEnd); |
| 1478 | sqlite3VdbeAddOp2(v, OP_Copy, regSize, regStart); |
dan | c9a8668 | 2018-05-30 20:44:58 +0000 | [diff] [blame] | 1479 | sqlite3VdbeAddOp3(v, OP_Subtract, regStart, regEnd, regEnd); |
| 1480 | } |
| 1481 | |
dan | 26522d1 | 2018-06-11 18:16:51 +0000 | [diff] [blame] | 1482 | 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 | |
dan | c9a8668 | 2018-05-30 20:44:58 +0000 | [diff] [blame] | 1489 | /* Initialize the accumulator register for each window function to NULL */ |
dan | 2e60568 | 2018-06-07 15:54:26 +0000 | [diff] [blame] | 1490 | regArg = windowInitAccum(pParse, pMWin); |
dan | c3a20c1 | 2018-05-23 20:55:37 +0000 | [diff] [blame] | 1491 | |
dan | 31f5639 | 2018-05-24 21:10:57 +0000 | [diff] [blame] | 1492 | sqlite3VdbeAddOp2(v, OP_Rewind, pMWin->iEphCsr, lblFlushDone); |
| 1493 | sqlite3VdbeAddOp2(v, OP_Rewind, csrStart, lblFlushDone); |
dan | c3a20c1 | 2018-05-23 20:55:37 +0000 | [diff] [blame] | 1494 | sqlite3VdbeChangeP5(v, 1); |
dan | 31f5639 | 2018-05-24 21:10:57 +0000 | [diff] [blame] | 1495 | sqlite3VdbeAddOp2(v, OP_Rewind, csrEnd, lblFlushDone); |
dan | c3a20c1 | 2018-05-23 20:55:37 +0000 | [diff] [blame] | 1496 | sqlite3VdbeChangeP5(v, 1); |
| 1497 | |
| 1498 | /* Invoke AggStep function for each window function using the row that |
dan | 31f5639 | 2018-05-24 21:10:57 +0000 | [diff] [blame] | 1499 | ** csrEnd currently points to. Or, if csrEnd is already at EOF, |
dan | c3a20c1 | 2018-05-23 20:55:37 +0000 | [diff] [blame] | 1500 | ** do nothing. */ |
dan | 31f5639 | 2018-05-24 21:10:57 +0000 | [diff] [blame] | 1501 | addrTop = sqlite3VdbeCurrentAddr(v); |
| 1502 | if( pMWin->eEnd==TK_PRECEDING ){ |
| 1503 | addrIfPos1 = sqlite3VdbeAddOp3(v, OP_IfPos, regEnd, 0 , 1); |
dan | c3a20c1 | 2018-05-23 20:55:37 +0000 | [diff] [blame] | 1504 | } |
dan | 31f5639 | 2018-05-24 21:10:57 +0000 | [diff] [blame] | 1505 | sqlite3VdbeAddOp2(v, OP_Next, csrEnd, sqlite3VdbeCurrentAddr(v)+2); |
| 1506 | addr = sqlite3VdbeAddOp0(v, OP_Goto); |
dan | dfa552f | 2018-06-02 21:04:28 +0000 | [diff] [blame] | 1507 | windowAggStep(pParse, pMWin, csrEnd, 0, regArg, regSize); |
dan | 99652dd | 2018-05-24 17:49:14 +0000 | [diff] [blame] | 1508 | if( pMWin->eEnd==TK_UNBOUNDED ){ |
dan | 31f5639 | 2018-05-24 21:10:57 +0000 | [diff] [blame] | 1509 | sqlite3VdbeAddOp2(v, OP_Goto, 0, addrTop); |
| 1510 | sqlite3VdbeJumpHere(v, addr); |
| 1511 | addrTop = sqlite3VdbeCurrentAddr(v); |
dan | 99652dd | 2018-05-24 17:49:14 +0000 | [diff] [blame] | 1512 | }else{ |
dan | 31f5639 | 2018-05-24 21:10:57 +0000 | [diff] [blame] | 1513 | sqlite3VdbeJumpHere(v, addr); |
| 1514 | if( pMWin->eEnd==TK_PRECEDING ){ |
| 1515 | sqlite3VdbeJumpHere(v, addrIfPos1); |
| 1516 | } |
dan | 99652dd | 2018-05-24 17:49:14 +0000 | [diff] [blame] | 1517 | } |
dan | c3a20c1 | 2018-05-23 20:55:37 +0000 | [diff] [blame] | 1518 | |
dan | 99652dd | 2018-05-24 17:49:14 +0000 | [diff] [blame] | 1519 | if( pMWin->eEnd==TK_FOLLOWING ){ |
dan | 31f5639 | 2018-05-24 21:10:57 +0000 | [diff] [blame] | 1520 | addrIfPos1 = sqlite3VdbeAddOp3(v, OP_IfPos, regEnd, 0 , 1); |
dan | 99652dd | 2018-05-24 17:49:14 +0000 | [diff] [blame] | 1521 | } |
dan | e105dd7 | 2018-05-25 09:29:11 +0000 | [diff] [blame] | 1522 | if( pMWin->eStart==TK_FOLLOWING ){ |
| 1523 | addrIfPos2 = sqlite3VdbeAddOp3(v, OP_IfPos, regStart, 0 , 1); |
| 1524 | } |
dan | d6f784e | 2018-05-28 18:30:45 +0000 | [diff] [blame] | 1525 | windowAggFinal(pParse, pMWin, 0); |
dan | ec891fd | 2018-06-06 20:51:02 +0000 | [diff] [blame] | 1526 | windowReturnOneRow(pParse, pMWin, regGosub, addrGosub); |
dan | c3a20c1 | 2018-05-23 20:55:37 +0000 | [diff] [blame] | 1527 | sqlite3VdbeAddOp2(v, OP_Next, pMWin->iEphCsr, sqlite3VdbeCurrentAddr(v)+2); |
dan | 31f5639 | 2018-05-24 21:10:57 +0000 | [diff] [blame] | 1528 | sqlite3VdbeAddOp2(v, OP_Goto, 0, lblFlushDone); |
dan | e105dd7 | 2018-05-25 09:29:11 +0000 | [diff] [blame] | 1529 | if( pMWin->eStart==TK_FOLLOWING ){ |
| 1530 | sqlite3VdbeJumpHere(v, addrIfPos2); |
| 1531 | } |
dan | c3a20c1 | 2018-05-23 20:55:37 +0000 | [diff] [blame] | 1532 | |
dan | e105dd7 | 2018-05-25 09:29:11 +0000 | [diff] [blame] | 1533 | if( pMWin->eStart==TK_CURRENT |
| 1534 | || pMWin->eStart==TK_PRECEDING |
| 1535 | || pMWin->eStart==TK_FOLLOWING |
| 1536 | ){ |
dan | 09590aa | 2018-05-25 20:30:17 +0000 | [diff] [blame] | 1537 | int addrJumpHere = 0; |
dan | 99652dd | 2018-05-24 17:49:14 +0000 | [diff] [blame] | 1538 | if( pMWin->eStart==TK_PRECEDING ){ |
dan | 09590aa | 2018-05-25 20:30:17 +0000 | [diff] [blame] | 1539 | addrJumpHere = sqlite3VdbeAddOp3(v, OP_IfPos, regStart, 0 , 1); |
| 1540 | } |
dan | 31f5639 | 2018-05-24 21:10:57 +0000 | [diff] [blame] | 1541 | sqlite3VdbeAddOp2(v, OP_Next, csrStart, sqlite3VdbeCurrentAddr(v)+1); |
dan | dfa552f | 2018-06-02 21:04:28 +0000 | [diff] [blame] | 1542 | windowAggStep(pParse, pMWin, csrStart, 1, regArg, regSize); |
dan | 09590aa | 2018-05-25 20:30:17 +0000 | [diff] [blame] | 1543 | if( addrJumpHere ){ |
| 1544 | sqlite3VdbeJumpHere(v, addrJumpHere); |
dan | 99652dd | 2018-05-24 17:49:14 +0000 | [diff] [blame] | 1545 | } |
dan | c3a20c1 | 2018-05-23 20:55:37 +0000 | [diff] [blame] | 1546 | } |
dan | 99652dd | 2018-05-24 17:49:14 +0000 | [diff] [blame] | 1547 | if( pMWin->eEnd==TK_FOLLOWING ){ |
| 1548 | sqlite3VdbeJumpHere(v, addrIfPos1); |
| 1549 | } |
dan | 31f5639 | 2018-05-24 21:10:57 +0000 | [diff] [blame] | 1550 | sqlite3VdbeAddOp2(v, OP_Goto, 0, addrTop); |
dan | c3a20c1 | 2018-05-23 20:55:37 +0000 | [diff] [blame] | 1551 | |
| 1552 | /* flush_partition_done: */ |
dan | 31f5639 | 2018-05-24 21:10:57 +0000 | [diff] [blame] | 1553 | sqlite3VdbeResolveLabel(v, lblFlushDone); |
dan | c3a20c1 | 2018-05-23 20:55:37 +0000 | [diff] [blame] | 1554 | 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 | |
dan | 79d4544 | 2018-05-26 21:17:29 +0000 | [diff] [blame] | 1561 | /* |
dan | 54a9ab3 | 2018-06-14 14:27:05 +0000 | [diff] [blame] | 1562 | ** 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 | ** |
dan | 79d4544 | 2018-05-26 21:17:29 +0000 | [diff] [blame] | 1572 | ** RANGE BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW |
| 1573 | ** |
dan | f690b57 | 2018-06-01 21:00:08 +0000 | [diff] [blame] | 1574 | ** 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 |
dan | 54a9ab3 | 2018-06-14 14:27:05 +0000 | [diff] [blame] | 1602 | ** |
| 1603 | ** As above, except that the "if( new peer )" branch is always taken. |
| 1604 | ** |
dan | f690b57 | 2018-06-01 21:00:08 +0000 | [diff] [blame] | 1605 | ** RANGE BETWEEN CURRENT ROW AND CURRENT ROW |
dan | 54a9ab3 | 2018-06-14 14:27:05 +0000 | [diff] [blame] | 1606 | ** |
| 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 |
dan | f690b57 | 2018-06-01 21:00:08 +0000 | [diff] [blame] | 1660 | */ |
| 1661 | static 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); |
dan | f690b57 | 2018-06-01 21:00:08 +0000 | [diff] [blame] | 1670 | int k; |
| 1671 | int addr; |
| 1672 | ExprList *pPart = pMWin->pPartition; |
| 1673 | ExprList *pOrderBy = pMWin->pOrderBy; |
dan | 54a9ab3 | 2018-06-14 14:27:05 +0000 | [diff] [blame] | 1674 | int nPeer = pOrderBy ? pOrderBy->nExpr : 0; |
dan | f690b57 | 2018-06-01 21:00:08 +0000 | [diff] [blame] | 1675 | int regNewPeer; |
| 1676 | |
| 1677 | int addrGoto; /* Address of Goto used to jump flush_par.. */ |
dan | 13078ca | 2018-06-13 20:29:38 +0000 | [diff] [blame] | 1678 | int addrNext; /* Jump here for next iteration of loop */ |
dan | f690b57 | 2018-06-01 21:00:08 +0000 | [diff] [blame] | 1679 | int regFlushPart; |
| 1680 | int lblFlushPart; |
| 1681 | int csrLead; |
| 1682 | int regCtr; |
| 1683 | int regArg; /* Register array to martial function args */ |
dan | dfa552f | 2018-06-02 21:04:28 +0000 | [diff] [blame] | 1684 | int regSize; |
dan | 13078ca | 2018-06-13 20:29:38 +0000 | [diff] [blame] | 1685 | int lblEmpty; |
dan | 54a9ab3 | 2018-06-14 14:27:05 +0000 | [diff] [blame] | 1686 | int bReverse = pMWin->pOrderBy && pMWin->eStart==TK_CURRENT |
| 1687 | && pMWin->eEnd==TK_UNBOUNDED; |
dan | f690b57 | 2018-06-01 21:00:08 +0000 | [diff] [blame] | 1688 | |
| 1689 | assert( (pMWin->eStart==TK_UNBOUNDED && pMWin->eEnd==TK_CURRENT) |
dan | ec891fd | 2018-06-06 20:51:02 +0000 | [diff] [blame] | 1690 | || (pMWin->eStart==TK_UNBOUNDED && pMWin->eEnd==TK_UNBOUNDED) |
| 1691 | || (pMWin->eStart==TK_CURRENT && pMWin->eEnd==TK_CURRENT) |
dan | 13078ca | 2018-06-13 20:29:38 +0000 | [diff] [blame] | 1692 | || (pMWin->eStart==TK_CURRENT && pMWin->eEnd==TK_UNBOUNDED) |
dan | f690b57 | 2018-06-01 21:00:08 +0000 | [diff] [blame] | 1693 | ); |
| 1694 | |
dan | 13078ca | 2018-06-13 20:29:38 +0000 | [diff] [blame] | 1695 | lblEmpty = sqlite3VdbeMakeLabel(v); |
dan | f690b57 | 2018-06-01 21:00:08 +0000 | [diff] [blame] | 1696 | 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 | |
dan | dfa552f | 2018-06-02 21:04:28 +0000 | [diff] [blame] | 1706 | windowPartitionCache(pParse, p, pWInfo, regFlushPart, lblFlushPart, ®Size); |
dan | f690b57 | 2018-06-01 21:00:08 +0000 | [diff] [blame] | 1707 | 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 */ |
dan | 2e60568 | 2018-06-07 15:54:26 +0000 | [diff] [blame] | 1715 | regArg = windowInitAccum(pParse, pMWin); |
dan | f690b57 | 2018-06-01 21:00:08 +0000 | [diff] [blame] | 1716 | |
| 1717 | sqlite3VdbeAddOp2(v, OP_Integer, 0, regCtr); |
dan | 13078ca | 2018-06-13 20:29:38 +0000 | [diff] [blame] | 1718 | sqlite3VdbeAddOp2(v, OP_Rewind, csrLead, lblEmpty); |
| 1719 | sqlite3VdbeAddOp2(v, OP_Rewind, pMWin->iEphCsr, lblEmpty); |
dan | f690b57 | 2018-06-01 21:00:08 +0000 | [diff] [blame] | 1720 | |
dan | 13078ca | 2018-06-13 20:29:38 +0000 | [diff] [blame] | 1721 | 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); |
dan | ec891fd | 2018-06-06 20:51:02 +0000 | [diff] [blame] | 1731 | 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); |
dan | f690b57 | 2018-06-01 21:00:08 +0000 | [diff] [blame] | 1743 | } |
| 1744 | |
dan | 13078ca | 2018-06-13 20:29:38 +0000 | [diff] [blame] | 1745 | windowReturnRows(pParse, pMWin, regCtr, regGosub, addrGosub, |
dan | ec891fd | 2018-06-06 20:51:02 +0000 | [diff] [blame] | 1746 | (bCurrent ? regArg : 0), (bCurrent ? regSize : 0) |
| 1747 | ); |
| 1748 | if( addrJump ) sqlite3VdbeJumpHere(v, addrJump); |
dan | f690b57 | 2018-06-01 21:00:08 +0000 | [diff] [blame] | 1749 | } |
| 1750 | |
dan | 13078ca | 2018-06-13 20:29:38 +0000 | [diff] [blame] | 1751 | if( bReverse==0 ){ |
| 1752 | windowAggStep(pParse, pMWin, csrLead, 0, regArg, regSize); |
| 1753 | } |
dan | f690b57 | 2018-06-01 21:00:08 +0000 | [diff] [blame] | 1754 | sqlite3VdbeAddOp2(v, OP_AddImm, regCtr, 1); |
dan | 13078ca | 2018-06-13 20:29:38 +0000 | [diff] [blame] | 1755 | sqlite3VdbeAddOp2(v, OP_Next, csrLead, addrNext); |
dan | f690b57 | 2018-06-01 21:00:08 +0000 | [diff] [blame] | 1756 | |
dan | 13078ca | 2018-06-13 20:29:38 +0000 | [diff] [blame] | 1757 | windowReturnRows(pParse, pMWin, regCtr, regGosub, addrGosub, 0, 0); |
dan | f690b57 | 2018-06-01 21:00:08 +0000 | [diff] [blame] | 1758 | |
dan | 13078ca | 2018-06-13 20:29:38 +0000 | [diff] [blame] | 1759 | sqlite3VdbeResolveLabel(v, lblEmpty); |
dan | f690b57 | 2018-06-01 21:00:08 +0000 | [diff] [blame] | 1760 | 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 | ** |
dan | 79d4544 | 2018-05-26 21:17:29 +0000 | [diff] [blame] | 1771 | ** ... |
| 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 |
dan | f690b57 | 2018-06-01 21:00:08 +0000 | [diff] [blame] | 1787 | ** |
| 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". |
dan | 79d4544 | 2018-05-26 21:17:29 +0000 | [diff] [blame] | 1801 | */ |
dan | c3a20c1 | 2018-05-23 20:55:37 +0000 | [diff] [blame] | 1802 | static 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); |
dan | c3a20c1 | 2018-05-23 20:55:37 +0000 | [diff] [blame] | 1811 | 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; |
dan | d6f784e | 2018-05-28 18:30:45 +0000 | [diff] [blame] | 1818 | ExprList *pPart = pMWin->pPartition; |
| 1819 | ExprList *pOrderBy = pMWin->pOrderBy; |
dan | c3a20c1 | 2018-05-23 20:55:37 +0000 | [diff] [blame] | 1820 | |
dan | 79d4544 | 2018-05-26 21:17:29 +0000 | [diff] [blame] | 1821 | assert( pMWin->eType==TK_RANGE |
| 1822 | || (pMWin->eStart==TK_UNBOUNDED && pMWin->eEnd==TK_CURRENT) |
| 1823 | ); |
| 1824 | |
dan | d6f784e | 2018-05-28 18:30:45 +0000 | [diff] [blame] | 1825 | 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 | |
dan | c3a20c1 | 2018-05-23 20:55:37 +0000 | [diff] [blame] | 1835 | 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. */ |
dan | d6f784e | 2018-05-28 18:30:45 +0000 | [diff] [blame] | 1844 | if( pPart || pOrderBy ){ |
dan | c3a20c1 | 2018-05-23 20:55:37 +0000 | [diff] [blame] | 1845 | int nPart = (pPart ? pPart->nExpr : 0); |
dan | c3a20c1 | 2018-05-23 20:55:37 +0000 | [diff] [blame] | 1846 | int addrGoto = 0; |
| 1847 | int addrJump = 0; |
dan | d6f784e | 2018-05-28 18:30:45 +0000 | [diff] [blame] | 1848 | int nPeer = (pOrderBy ? pOrderBy->nExpr : 0); |
dan | c3a20c1 | 2018-05-23 20:55:37 +0000 | [diff] [blame] | 1849 | |
| 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); |
dan | d6f784e | 2018-05-28 18:30:45 +0000 | [diff] [blame] | 1856 | windowAggFinal(pParse, pMWin, 1); |
dan | c3a20c1 | 2018-05-23 20:55:37 +0000 | [diff] [blame] | 1857 | 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 | |
dan | c3a20c1 | 2018-05-23 20:55:37 +0000 | [diff] [blame] | 1866 | if( addrJump ) sqlite3VdbeJumpHere(v, addrJump); |
dan | 79d4544 | 2018-05-26 21:17:29 +0000 | [diff] [blame] | 1867 | 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 | } |
dan | d6f784e | 2018-05-28 18:30:45 +0000 | [diff] [blame] | 1875 | windowAggFinal(pParse, pMWin, pMWin->eStart==TK_CURRENT); |
dan | c3a20c1 | 2018-05-23 20:55:37 +0000 | [diff] [blame] | 1876 | if( addrGoto ) sqlite3VdbeJumpHere(v, addrGoto); |
| 1877 | } |
| 1878 | |
dan | dacf1de | 2018-06-08 16:11:55 +0000 | [diff] [blame] | 1879 | sqlite3VdbeAddOp2(v, OP_Rewind, pMWin->iEphCsr,sqlite3VdbeCurrentAddr(v)+3); |
dan | c3a20c1 | 2018-05-23 20:55:37 +0000 | [diff] [blame] | 1880 | sqlite3VdbeAddOp2(v, OP_Gosub, regGosub, addrGosub); |
dan | dacf1de | 2018-06-08 16:11:55 +0000 | [diff] [blame] | 1881 | sqlite3VdbeAddOp2(v, OP_Next, pMWin->iEphCsr, sqlite3VdbeCurrentAddr(v)-1); |
| 1882 | |
dan | c3a20c1 | 2018-05-23 20:55:37 +0000 | [diff] [blame] | 1883 | sqlite3VdbeAddOp1(v, OP_ResetSorter, pMWin->iEphCsr); |
| 1884 | sqlite3VdbeAddOp3( |
| 1885 | v, OP_Copy, reg+pMWin->nBufferCol, pMWin->regPart, nPart+nPeer-1 |
| 1886 | ); |
| 1887 | |
dan | 79d4544 | 2018-05-26 21:17:29 +0000 | [diff] [blame] | 1888 | if( addrJump ) sqlite3VdbeJumpHere(v, addrJump); |
dan | c3a20c1 | 2018-05-23 20:55:37 +0000 | [diff] [blame] | 1889 | } |
| 1890 | |
| 1891 | /* Invoke step function for window functions */ |
dan | dfa552f | 2018-06-02 21:04:28 +0000 | [diff] [blame] | 1892 | windowAggStep(pParse, pMWin, -1, 0, reg, 0); |
dan | c3a20c1 | 2018-05-23 20:55:37 +0000 | [diff] [blame] | 1893 | |
| 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 | |
dan | d6f784e | 2018-05-28 18:30:45 +0000 | [diff] [blame] | 1907 | windowAggFinal(pParse, pMWin, 1); |
dan | dacf1de | 2018-06-08 16:11:55 +0000 | [diff] [blame] | 1908 | sqlite3VdbeAddOp2(v, OP_Rewind, pMWin->iEphCsr,sqlite3VdbeCurrentAddr(v)+3); |
dan | c3a20c1 | 2018-05-23 20:55:37 +0000 | [diff] [blame] | 1909 | sqlite3VdbeAddOp2(v, OP_Gosub, regGosub, addrGosub); |
dan | dacf1de | 2018-06-08 16:11:55 +0000 | [diff] [blame] | 1910 | sqlite3VdbeAddOp2(v, OP_Next, pMWin->iEphCsr, sqlite3VdbeCurrentAddr(v)-1); |
dan | c3a20c1 | 2018-05-23 20:55:37 +0000 | [diff] [blame] | 1911 | } |
| 1912 | |
dan | 13078ca | 2018-06-13 20:29:38 +0000 | [diff] [blame] | 1913 | /* |
| 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 | */ |
dan | 2a11bb2 | 2018-06-11 20:50:25 +0000 | [diff] [blame] | 1918 | Window *sqlite3WindowDup(sqlite3 *db, Expr *pOwner, Window *p){ |
dan | dacf1de | 2018-06-08 16:11:55 +0000 | [diff] [blame] | 1919 | Window *pNew = 0; |
| 1920 | if( p ){ |
| 1921 | pNew = sqlite3DbMallocZero(db, sizeof(Window)); |
| 1922 | if( pNew ){ |
dan | c95f38d | 2018-06-18 20:34:43 +0000 | [diff] [blame^] | 1923 | pNew->zName = sqlite3DbStrDup(db, p->zName); |
dan | dacf1de | 2018-06-08 16:11:55 +0000 | [diff] [blame] | 1924 | 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; |
dan | 303451a | 2018-06-14 20:52:08 +0000 | [diff] [blame] | 1930 | pNew->pStart = sqlite3ExprDup(db, p->pStart, 0); |
| 1931 | pNew->pEnd = sqlite3ExprDup(db, p->pEnd, 0); |
dan | 2a11bb2 | 2018-06-11 20:50:25 +0000 | [diff] [blame] | 1932 | pNew->pOwner = pOwner; |
dan | dacf1de | 2018-06-08 16:11:55 +0000 | [diff] [blame] | 1933 | } |
| 1934 | } |
| 1935 | return pNew; |
| 1936 | } |
dan | c3a20c1 | 2018-05-23 20:55:37 +0000 | [diff] [blame] | 1937 | |
dan | f9eae18 | 2018-05-21 19:45:11 +0000 | [diff] [blame] | 1938 | /* |
dan | c95f38d | 2018-06-18 20:34:43 +0000 | [diff] [blame^] | 1939 | ** Return a copy of the linked list of Window objects passed as the |
| 1940 | ** second argument. |
| 1941 | */ |
| 1942 | Window *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 | /* |
dan | 2a11bb2 | 2018-06-11 20:50:25 +0000 | [diff] [blame] | 1957 | ** 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. |
dan | f9eae18 | 2018-05-21 19:45:11 +0000 | [diff] [blame] | 1962 | */ |
| 1963 | void sqlite3WindowCodeStep( |
dan | 2a11bb2 | 2018-06-11 20:50:25 +0000 | [diff] [blame] | 1964 | 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 */ |
dan | f9eae18 | 2018-05-21 19:45:11 +0000 | [diff] [blame] | 1969 | ){ |
dan | f9eae18 | 2018-05-21 19:45:11 +0000 | [diff] [blame] | 1970 | Window *pMWin = p->pWin; |
dan | f9eae18 | 2018-05-21 19:45:11 +0000 | [diff] [blame] | 1971 | |
dan | 54a9ab3 | 2018-06-14 14:27:05 +0000 | [diff] [blame] | 1972 | /* 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: |
dan | 26522d1 | 2018-06-11 18:16:51 +0000 | [diff] [blame] | 1977 | ** |
dan | 13078ca | 2018-06-13 20:29:38 +0000 | [diff] [blame] | 1978 | ** ROWS BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW |
dan | 54a9ab3 | 2018-06-14 14:27:05 +0000 | [diff] [blame] | 1979 | ** |
| 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. |
dan | 26522d1 | 2018-06-11 18:16:51 +0000 | [diff] [blame] | 2005 | */ |
dan | 54a9ab3 | 2018-06-14 14:27:05 +0000 | [diff] [blame] | 2006 | if( pMWin->eType==TK_ROWS |
| 2007 | && (pMWin->eStart!=TK_UNBOUNDED||pMWin->eEnd!=TK_CURRENT||!pMWin->pOrderBy) |
dan | 09590aa | 2018-05-25 20:30:17 +0000 | [diff] [blame] | 2008 | ){ |
dan | c3a20c1 | 2018-05-23 20:55:37 +0000 | [diff] [blame] | 2009 | windowCodeRowExprStep(pParse, p, pWInfo, regGosub, addrGosub); |
dan | 13078ca | 2018-06-13 20:29:38 +0000 | [diff] [blame] | 2010 | }else{ |
| 2011 | Window *pWin; |
dan | 54a9ab3 | 2018-06-14 14:27:05 +0000 | [diff] [blame] | 2012 | int bCache = 0; /* True to use CacheStep() */ |
dan | f9eae18 | 2018-05-21 19:45:11 +0000 | [diff] [blame] | 2013 | |
dan | 54a9ab3 | 2018-06-14 14:27:05 +0000 | [diff] [blame] | 2014 | if( pMWin->eStart==TK_CURRENT && pMWin->eEnd==TK_UNBOUNDED ){ |
dan | 13078ca | 2018-06-13 20:29:38 +0000 | [diff] [blame] | 2015 | bCache = 1; |
| 2016 | }else{ |
dan | 13078ca | 2018-06-13 20:29:38 +0000 | [diff] [blame] | 2017 | for(pWin=pMWin; pWin; pWin=pWin->pNextWin){ |
| 2018 | FuncDef *pFunc = pWin->pFunc; |
| 2019 | if( (pFunc->funcFlags & SQLITE_FUNC_WINDOW_SIZE) |
dan | 54a9ab3 | 2018-06-14 14:27:05 +0000 | [diff] [blame] | 2020 | || (pFunc->xSFunc==nth_valueStepFunc) |
| 2021 | || (pFunc->xSFunc==first_valueStepFunc) |
| 2022 | || (pFunc->xSFunc==leadStepFunc) |
| 2023 | || (pFunc->xSFunc==lagStepFunc) |
| 2024 | ){ |
dan | 13078ca | 2018-06-13 20:29:38 +0000 | [diff] [blame] | 2025 | bCache = 1; |
| 2026 | break; |
| 2027 | } |
| 2028 | } |
| 2029 | } |
| 2030 | |
| 2031 | /* Otherwise, call windowCodeDefaultStep(). */ |
| 2032 | if( bCache ){ |
dan | dfa552f | 2018-06-02 21:04:28 +0000 | [diff] [blame] | 2033 | windowCodeCacheStep(pParse, p, pWInfo, regGosub, addrGosub); |
dan | 13078ca | 2018-06-13 20:29:38 +0000 | [diff] [blame] | 2034 | }else{ |
| 2035 | windowCodeDefaultStep(pParse, p, pWInfo, regGosub, addrGosub); |
dan | dfa552f | 2018-06-02 21:04:28 +0000 | [diff] [blame] | 2036 | } |
dan | f690b57 | 2018-06-01 21:00:08 +0000 | [diff] [blame] | 2037 | } |
dan | f9eae18 | 2018-05-21 19:45:11 +0000 | [diff] [blame] | 2038 | } |
| 2039 | |