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 | 67a9b8e | 2018-06-22 20:51:35 +0000 | [diff] [blame^] | 15 | #ifndef SQLITE_OMIT_WINDOWFUNC |
| 16 | |
dan | dfa552f | 2018-06-02 21:04:28 +0000 | [diff] [blame] | 17 | /* |
dan | 7392569 | 2018-06-12 18:40:17 +0000 | [diff] [blame] | 18 | ** SELECT REWRITING |
| 19 | ** |
| 20 | ** Any SELECT statement that contains one or more window functions in |
| 21 | ** either the select list or ORDER BY clause (the only two places window |
| 22 | ** functions may be used) is transformed by function sqlite3WindowRewrite() |
| 23 | ** in order to support window function processing. For example, with the |
| 24 | ** schema: |
| 25 | ** |
| 26 | ** CREATE TABLE t1(a, b, c, d, e, f, g); |
| 27 | ** |
| 28 | ** the statement: |
| 29 | ** |
| 30 | ** SELECT a+1, max(b) OVER (PARTITION BY c ORDER BY d) FROM t1 ORDER BY e; |
| 31 | ** |
| 32 | ** is transformed to: |
| 33 | ** |
| 34 | ** SELECT a+1, max(b) OVER (PARTITION BY c ORDER BY d) FROM ( |
| 35 | ** SELECT a, e, c, d, b FROM t1 ORDER BY c, d |
| 36 | ** ) ORDER BY e; |
| 37 | ** |
| 38 | ** The flattening optimization is disabled when processing this transformed |
| 39 | ** SELECT statement. This allows the implementation of the window function |
| 40 | ** (in this case max()) to process rows sorted in order of (c, d), which |
| 41 | ** makes things easier for obvious reasons. More generally: |
| 42 | ** |
| 43 | ** * FROM, WHERE, GROUP BY and HAVING clauses are all moved to |
| 44 | ** the sub-query. |
| 45 | ** |
| 46 | ** * ORDER BY, LIMIT and OFFSET remain part of the parent query. |
| 47 | ** |
| 48 | ** * Terminals from each of the expression trees that make up the |
| 49 | ** select-list and ORDER BY expressions in the parent query are |
| 50 | ** selected by the sub-query. For the purposes of the transformation, |
| 51 | ** terminals are column references and aggregate functions. |
| 52 | ** |
| 53 | ** If there is more than one window function in the SELECT that uses |
| 54 | ** the same window declaration (the OVER bit), then a single scan may |
| 55 | ** be used to process more than one window function. For example: |
| 56 | ** |
| 57 | ** SELECT max(b) OVER (PARTITION BY c ORDER BY d), |
| 58 | ** min(e) OVER (PARTITION BY c ORDER BY d) |
| 59 | ** FROM t1; |
| 60 | ** |
| 61 | ** is transformed in the same way as the example above. However: |
| 62 | ** |
| 63 | ** SELECT max(b) OVER (PARTITION BY c ORDER BY d), |
| 64 | ** min(e) OVER (PARTITION BY a ORDER BY b) |
| 65 | ** FROM t1; |
| 66 | ** |
| 67 | ** Must be transformed to: |
| 68 | ** |
| 69 | ** SELECT max(b) OVER (PARTITION BY c ORDER BY d) FROM ( |
| 70 | ** SELECT e, min(e) OVER (PARTITION BY a ORDER BY b), c, d, b FROM |
| 71 | ** SELECT a, e, c, d, b FROM t1 ORDER BY a, b |
| 72 | ** ) ORDER BY c, d |
| 73 | ** ) ORDER BY e; |
| 74 | ** |
| 75 | ** so that both min() and max() may process rows in the order defined by |
| 76 | ** their respective window declarations. |
| 77 | ** |
| 78 | ** INTERFACE WITH SELECT.C |
| 79 | ** |
| 80 | ** When processing the rewritten SELECT statement, code in select.c calls |
| 81 | ** sqlite3WhereBegin() to begin iterating through the results of the |
| 82 | ** sub-query, which is always implemented as a co-routine. It then calls |
| 83 | ** sqlite3WindowCodeStep() to process rows and finish the scan by calling |
| 84 | ** sqlite3WhereEnd(). |
| 85 | ** |
| 86 | ** sqlite3WindowCodeStep() generates VM code so that, for each row returned |
| 87 | ** by the sub-query a sub-routine (OP_Gosub) coded by select.c is invoked. |
| 88 | ** When the sub-routine is invoked: |
| 89 | ** |
| 90 | ** * The results of all window-functions for the row are stored |
| 91 | ** in the associated Window.regResult registers. |
| 92 | ** |
| 93 | ** * The required terminal values are stored in the current row of |
| 94 | ** temp table Window.iEphCsr. |
| 95 | ** |
| 96 | ** In some cases, depending on the window frame and the specific window |
| 97 | ** functions invoked, sqlite3WindowCodeStep() caches each entire partition |
| 98 | ** in a temp table before returning any rows. In other cases it does not. |
| 99 | ** This detail is encapsulated within this file, the code generated by |
| 100 | ** select.c is the same in either case. |
| 101 | ** |
| 102 | ** BUILT-IN WINDOW FUNCTIONS |
| 103 | ** |
| 104 | ** This implementation features the following built-in window functions: |
| 105 | ** |
| 106 | ** row_number() |
| 107 | ** rank() |
| 108 | ** dense_rank() |
| 109 | ** percent_rank() |
| 110 | ** cume_dist() |
| 111 | ** ntile(N) |
| 112 | ** lead(expr [, offset [, default]]) |
| 113 | ** lag(expr [, offset [, default]]) |
| 114 | ** first_value(expr) |
| 115 | ** last_value(expr) |
| 116 | ** nth_value(expr, N) |
| 117 | ** |
| 118 | ** These are the same built-in window functions supported by Postgres. |
| 119 | ** Although the behaviour of aggregate window functions (functions that |
| 120 | ** can be used as either aggregates or window funtions) allows them to |
| 121 | ** be implemented using an API, built-in window functions are much more |
| 122 | ** esoteric. Additionally, some window functions (e.g. nth_value()) |
| 123 | ** may only be implemented by caching the entire partition in memory. |
| 124 | ** As such, some built-in window functions use the same API as aggregate |
| 125 | ** window functions and some are implemented directly using VDBE |
| 126 | ** instructions. Additionally, for those functions that use the API, the |
| 127 | ** window frame is sometimes modified before the SELECT statement is |
| 128 | ** rewritten. For example, regardless of the specified window frame, the |
| 129 | ** row_number() function always uses: |
| 130 | ** |
| 131 | ** ROWS BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW |
| 132 | ** |
| 133 | ** See sqlite3WindowUpdate() for details. |
dan | c0bb445 | 2018-06-12 20:53:38 +0000 | [diff] [blame] | 134 | ** |
| 135 | ** As well as some of the built-in window functions, aggregate window |
| 136 | ** functions min() and max() are implemented using VDBE instructions if |
| 137 | ** the start of the window frame is declared as anything other than |
| 138 | ** UNBOUNDED PRECEDING. |
dan | 7392569 | 2018-06-12 18:40:17 +0000 | [diff] [blame] | 139 | */ |
| 140 | |
| 141 | /* |
dan | dfa552f | 2018-06-02 21:04:28 +0000 | [diff] [blame] | 142 | ** Implementation of built-in window function row_number(). Assumes that the |
| 143 | ** window frame has been coerced to: |
| 144 | ** |
| 145 | ** ROWS BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW |
| 146 | */ |
| 147 | static void row_numberStepFunc( |
| 148 | sqlite3_context *pCtx, |
| 149 | int nArg, |
| 150 | sqlite3_value **apArg |
| 151 | ){ |
| 152 | i64 *p = (i64*)sqlite3_aggregate_context(pCtx, sizeof(*p)); |
| 153 | if( p ) (*p)++; |
| 154 | } |
dan | 2a11bb2 | 2018-06-11 20:50:25 +0000 | [diff] [blame] | 155 | static void row_numberInvFunc( |
dan | dfa552f | 2018-06-02 21:04:28 +0000 | [diff] [blame] | 156 | sqlite3_context *pCtx, |
| 157 | int nArg, |
| 158 | sqlite3_value **apArg |
| 159 | ){ |
| 160 | } |
| 161 | static void row_numberValueFunc(sqlite3_context *pCtx){ |
| 162 | i64 *p = (i64*)sqlite3_aggregate_context(pCtx, sizeof(*p)); |
| 163 | sqlite3_result_int64(pCtx, (p ? *p : 0)); |
| 164 | } |
| 165 | |
| 166 | /* |
dan | 2a11bb2 | 2018-06-11 20:50:25 +0000 | [diff] [blame] | 167 | ** Context object type used by rank(), dense_rank(), percent_rank() and |
| 168 | ** cume_dist(). |
dan | dfa552f | 2018-06-02 21:04:28 +0000 | [diff] [blame] | 169 | */ |
| 170 | struct CallCount { |
| 171 | i64 nValue; |
| 172 | i64 nStep; |
| 173 | i64 nTotal; |
| 174 | }; |
| 175 | |
| 176 | /* |
dan | 9c27758 | 2018-06-20 09:23:49 +0000 | [diff] [blame] | 177 | ** Implementation of built-in window function dense_rank(). Assumes that |
| 178 | ** the window frame has been set to: |
| 179 | ** |
| 180 | ** RANGE BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW |
dan | dfa552f | 2018-06-02 21:04:28 +0000 | [diff] [blame] | 181 | */ |
| 182 | static void dense_rankStepFunc( |
| 183 | sqlite3_context *pCtx, |
| 184 | int nArg, |
| 185 | sqlite3_value **apArg |
| 186 | ){ |
| 187 | struct CallCount *p; |
| 188 | p = (struct CallCount*)sqlite3_aggregate_context(pCtx, sizeof(*p)); |
| 189 | if( p ) p->nStep = 1; |
| 190 | } |
dan | 2a11bb2 | 2018-06-11 20:50:25 +0000 | [diff] [blame] | 191 | static void dense_rankInvFunc( |
dan | dfa552f | 2018-06-02 21:04:28 +0000 | [diff] [blame] | 192 | sqlite3_context *pCtx, |
| 193 | int nArg, |
| 194 | sqlite3_value **apArg |
| 195 | ){ |
| 196 | } |
| 197 | static void dense_rankValueFunc(sqlite3_context *pCtx){ |
| 198 | struct CallCount *p; |
| 199 | p = (struct CallCount*)sqlite3_aggregate_context(pCtx, sizeof(*p)); |
| 200 | if( p ){ |
| 201 | if( p->nStep ){ |
| 202 | p->nValue++; |
| 203 | p->nStep = 0; |
| 204 | } |
| 205 | sqlite3_result_int64(pCtx, p->nValue); |
| 206 | } |
| 207 | } |
| 208 | |
| 209 | /* |
dan | 9c27758 | 2018-06-20 09:23:49 +0000 | [diff] [blame] | 210 | ** Implementation of built-in window function rank(). Assumes that |
| 211 | ** the window frame has been set to: |
| 212 | ** |
| 213 | ** RANGE BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW |
dan | dfa552f | 2018-06-02 21:04:28 +0000 | [diff] [blame] | 214 | */ |
| 215 | static void rankStepFunc( |
| 216 | sqlite3_context *pCtx, |
| 217 | int nArg, |
| 218 | sqlite3_value **apArg |
| 219 | ){ |
| 220 | struct CallCount *p; |
| 221 | p = (struct CallCount*)sqlite3_aggregate_context(pCtx, sizeof(*p)); |
| 222 | if( p ){ |
| 223 | p->nStep++; |
| 224 | if( p->nValue==0 ){ |
| 225 | p->nValue = p->nStep; |
| 226 | } |
| 227 | } |
| 228 | } |
dan | 2a11bb2 | 2018-06-11 20:50:25 +0000 | [diff] [blame] | 229 | static void rankInvFunc( |
dan | dfa552f | 2018-06-02 21:04:28 +0000 | [diff] [blame] | 230 | sqlite3_context *pCtx, |
| 231 | int nArg, |
| 232 | sqlite3_value **apArg |
| 233 | ){ |
| 234 | } |
| 235 | static void rankValueFunc(sqlite3_context *pCtx){ |
| 236 | struct CallCount *p; |
| 237 | p = (struct CallCount*)sqlite3_aggregate_context(pCtx, sizeof(*p)); |
| 238 | if( p ){ |
| 239 | sqlite3_result_int64(pCtx, p->nValue); |
| 240 | p->nValue = 0; |
| 241 | } |
| 242 | } |
| 243 | |
| 244 | /* |
dan | 9c27758 | 2018-06-20 09:23:49 +0000 | [diff] [blame] | 245 | ** Implementation of built-in window function percent_rank(). Assumes that |
| 246 | ** the window frame has been set to: |
| 247 | ** |
| 248 | ** RANGE BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW |
dan | dfa552f | 2018-06-02 21:04:28 +0000 | [diff] [blame] | 249 | */ |
| 250 | static void percent_rankStepFunc( |
| 251 | sqlite3_context *pCtx, |
| 252 | int nArg, |
| 253 | sqlite3_value **apArg |
| 254 | ){ |
| 255 | struct CallCount *p; |
| 256 | assert( nArg==1 ); |
| 257 | |
dan | 9c27758 | 2018-06-20 09:23:49 +0000 | [diff] [blame] | 258 | assert( sqlite3VdbeAssertAggContext(pCtx) ); |
dan | dfa552f | 2018-06-02 21:04:28 +0000 | [diff] [blame] | 259 | p = (struct CallCount*)sqlite3_aggregate_context(pCtx, sizeof(*p)); |
dan | 9c27758 | 2018-06-20 09:23:49 +0000 | [diff] [blame] | 260 | if( ALWAYS(p) ){ |
dan | dfa552f | 2018-06-02 21:04:28 +0000 | [diff] [blame] | 261 | if( p->nTotal==0 ){ |
| 262 | p->nTotal = sqlite3_value_int64(apArg[0]); |
| 263 | } |
| 264 | p->nStep++; |
| 265 | if( p->nValue==0 ){ |
| 266 | p->nValue = p->nStep; |
| 267 | } |
| 268 | } |
| 269 | } |
dan | 2a11bb2 | 2018-06-11 20:50:25 +0000 | [diff] [blame] | 270 | static void percent_rankInvFunc( |
dan | dfa552f | 2018-06-02 21:04:28 +0000 | [diff] [blame] | 271 | sqlite3_context *pCtx, |
| 272 | int nArg, |
| 273 | sqlite3_value **apArg |
| 274 | ){ |
| 275 | } |
| 276 | static void percent_rankValueFunc(sqlite3_context *pCtx){ |
| 277 | struct CallCount *p; |
| 278 | p = (struct CallCount*)sqlite3_aggregate_context(pCtx, sizeof(*p)); |
| 279 | if( p ){ |
| 280 | if( p->nTotal>1 ){ |
| 281 | double r = (double)(p->nValue-1) / (double)(p->nTotal-1); |
| 282 | sqlite3_result_double(pCtx, r); |
| 283 | }else{ |
dan | b7306f6 | 2018-06-21 19:20:39 +0000 | [diff] [blame] | 284 | sqlite3_result_double(pCtx, 0.0); |
dan | dfa552f | 2018-06-02 21:04:28 +0000 | [diff] [blame] | 285 | } |
| 286 | p->nValue = 0; |
| 287 | } |
| 288 | } |
| 289 | |
dan | 9c27758 | 2018-06-20 09:23:49 +0000 | [diff] [blame] | 290 | /* |
| 291 | ** Implementation of built-in window function cume_dist(). Assumes that |
| 292 | ** the window frame has been set to: |
| 293 | ** |
| 294 | ** RANGE BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW |
| 295 | */ |
dan | f1abe36 | 2018-06-04 08:22:09 +0000 | [diff] [blame] | 296 | static void cume_distStepFunc( |
| 297 | sqlite3_context *pCtx, |
| 298 | int nArg, |
| 299 | sqlite3_value **apArg |
| 300 | ){ |
| 301 | struct CallCount *p; |
| 302 | assert( nArg==1 ); |
| 303 | |
dan | 9c27758 | 2018-06-20 09:23:49 +0000 | [diff] [blame] | 304 | assert( sqlite3VdbeAssertAggContext(pCtx) ); |
dan | f1abe36 | 2018-06-04 08:22:09 +0000 | [diff] [blame] | 305 | p = (struct CallCount*)sqlite3_aggregate_context(pCtx, sizeof(*p)); |
dan | 9c27758 | 2018-06-20 09:23:49 +0000 | [diff] [blame] | 306 | if( ALWAYS(p) ){ |
dan | f1abe36 | 2018-06-04 08:22:09 +0000 | [diff] [blame] | 307 | if( p->nTotal==0 ){ |
| 308 | p->nTotal = sqlite3_value_int64(apArg[0]); |
| 309 | } |
| 310 | p->nStep++; |
| 311 | } |
| 312 | } |
dan | 2a11bb2 | 2018-06-11 20:50:25 +0000 | [diff] [blame] | 313 | static void cume_distInvFunc( |
dan | f1abe36 | 2018-06-04 08:22:09 +0000 | [diff] [blame] | 314 | sqlite3_context *pCtx, |
| 315 | int nArg, |
| 316 | sqlite3_value **apArg |
| 317 | ){ |
| 318 | } |
| 319 | static void cume_distValueFunc(sqlite3_context *pCtx){ |
| 320 | struct CallCount *p; |
| 321 | p = (struct CallCount*)sqlite3_aggregate_context(pCtx, sizeof(*p)); |
dan | 660af93 | 2018-06-18 16:55:22 +0000 | [diff] [blame] | 322 | if( p && p->nTotal ){ |
dan | f1abe36 | 2018-06-04 08:22:09 +0000 | [diff] [blame] | 323 | double r = (double)(p->nStep) / (double)(p->nTotal); |
| 324 | sqlite3_result_double(pCtx, r); |
| 325 | } |
| 326 | } |
| 327 | |
dan | 2a11bb2 | 2018-06-11 20:50:25 +0000 | [diff] [blame] | 328 | /* |
| 329 | ** Context object for ntile() window function. |
| 330 | */ |
dan | 6bc5c9e | 2018-06-04 18:55:11 +0000 | [diff] [blame] | 331 | struct NtileCtx { |
| 332 | i64 nTotal; /* Total rows in partition */ |
| 333 | i64 nParam; /* Parameter passed to ntile(N) */ |
| 334 | i64 iRow; /* Current row */ |
| 335 | }; |
| 336 | |
| 337 | /* |
| 338 | ** Implementation of ntile(). This assumes that the window frame has |
| 339 | ** been coerced to: |
| 340 | ** |
| 341 | ** ROWS UNBOUNDED PRECEDING AND CURRENT ROW |
dan | 6bc5c9e | 2018-06-04 18:55:11 +0000 | [diff] [blame] | 342 | */ |
| 343 | static void ntileStepFunc( |
| 344 | sqlite3_context *pCtx, |
| 345 | int nArg, |
| 346 | sqlite3_value **apArg |
| 347 | ){ |
| 348 | struct NtileCtx *p; |
| 349 | assert( nArg==2 ); |
| 350 | p = (struct NtileCtx*)sqlite3_aggregate_context(pCtx, sizeof(*p)); |
dan | 17074e3 | 2018-06-22 17:57:10 +0000 | [diff] [blame] | 351 | if( p ){ |
dan | 6bc5c9e | 2018-06-04 18:55:11 +0000 | [diff] [blame] | 352 | if( p->nTotal==0 ){ |
| 353 | p->nParam = sqlite3_value_int64(apArg[0]); |
| 354 | p->nTotal = sqlite3_value_int64(apArg[1]); |
| 355 | if( p->nParam<=0 ){ |
| 356 | sqlite3_result_error( |
| 357 | pCtx, "argument of ntile must be a positive integer", -1 |
| 358 | ); |
| 359 | } |
| 360 | } |
| 361 | p->iRow++; |
| 362 | } |
| 363 | } |
dan | 2a11bb2 | 2018-06-11 20:50:25 +0000 | [diff] [blame] | 364 | static void ntileInvFunc( |
dan | 6bc5c9e | 2018-06-04 18:55:11 +0000 | [diff] [blame] | 365 | sqlite3_context *pCtx, |
| 366 | int nArg, |
| 367 | sqlite3_value **apArg |
| 368 | ){ |
| 369 | } |
| 370 | static void ntileValueFunc(sqlite3_context *pCtx){ |
| 371 | struct NtileCtx *p; |
| 372 | p = (struct NtileCtx*)sqlite3_aggregate_context(pCtx, sizeof(*p)); |
| 373 | if( p && p->nParam>0 ){ |
| 374 | int nSize = (p->nTotal / p->nParam); |
| 375 | if( nSize==0 ){ |
| 376 | sqlite3_result_int64(pCtx, p->iRow); |
| 377 | }else{ |
| 378 | i64 nLarge = p->nTotal - p->nParam*nSize; |
| 379 | i64 iSmall = nLarge*(nSize+1); |
| 380 | i64 iRow = p->iRow-1; |
| 381 | |
| 382 | assert( (nLarge*(nSize+1) + (p->nParam-nLarge)*nSize)==p->nTotal ); |
| 383 | |
| 384 | if( iRow<iSmall ){ |
| 385 | sqlite3_result_int64(pCtx, 1 + iRow/(nSize+1)); |
| 386 | }else{ |
| 387 | sqlite3_result_int64(pCtx, 1 + nLarge + (iRow-iSmall)/nSize); |
| 388 | } |
| 389 | } |
| 390 | } |
| 391 | } |
| 392 | |
dan | 2a11bb2 | 2018-06-11 20:50:25 +0000 | [diff] [blame] | 393 | /* |
| 394 | ** Context object for last_value() window function. |
| 395 | */ |
dan | 1c5ed62 | 2018-06-05 16:16:17 +0000 | [diff] [blame] | 396 | struct LastValueCtx { |
| 397 | sqlite3_value *pVal; |
| 398 | int nVal; |
| 399 | }; |
| 400 | |
| 401 | /* |
| 402 | ** Implementation of last_value(). |
| 403 | */ |
| 404 | static void last_valueStepFunc( |
| 405 | sqlite3_context *pCtx, |
| 406 | int nArg, |
| 407 | sqlite3_value **apArg |
| 408 | ){ |
| 409 | struct LastValueCtx *p; |
dan | 9c27758 | 2018-06-20 09:23:49 +0000 | [diff] [blame] | 410 | p = (struct LastValueCtx*)sqlite3_aggregate_context(pCtx, sizeof(*p)); |
dan | 1c5ed62 | 2018-06-05 16:16:17 +0000 | [diff] [blame] | 411 | if( p ){ |
| 412 | sqlite3_value_free(p->pVal); |
| 413 | p->pVal = sqlite3_value_dup(apArg[0]); |
dan | 6fde179 | 2018-06-15 19:01:35 +0000 | [diff] [blame] | 414 | if( p->pVal==0 ){ |
| 415 | sqlite3_result_error_nomem(pCtx); |
| 416 | }else{ |
| 417 | p->nVal++; |
| 418 | } |
dan | 1c5ed62 | 2018-06-05 16:16:17 +0000 | [diff] [blame] | 419 | } |
| 420 | } |
dan | 2a11bb2 | 2018-06-11 20:50:25 +0000 | [diff] [blame] | 421 | static void last_valueInvFunc( |
dan | 1c5ed62 | 2018-06-05 16:16:17 +0000 | [diff] [blame] | 422 | sqlite3_context *pCtx, |
| 423 | int nArg, |
| 424 | sqlite3_value **apArg |
| 425 | ){ |
| 426 | struct LastValueCtx *p; |
dan | 9c27758 | 2018-06-20 09:23:49 +0000 | [diff] [blame] | 427 | p = (struct LastValueCtx*)sqlite3_aggregate_context(pCtx, sizeof(*p)); |
| 428 | if( ALWAYS(p) ){ |
dan | 1c5ed62 | 2018-06-05 16:16:17 +0000 | [diff] [blame] | 429 | p->nVal--; |
| 430 | if( p->nVal==0 ){ |
| 431 | sqlite3_value_free(p->pVal); |
| 432 | p->pVal = 0; |
| 433 | } |
| 434 | } |
| 435 | } |
| 436 | static void last_valueValueFunc(sqlite3_context *pCtx){ |
| 437 | struct LastValueCtx *p; |
dan | 9c27758 | 2018-06-20 09:23:49 +0000 | [diff] [blame] | 438 | p = (struct LastValueCtx*)sqlite3_aggregate_context(pCtx, sizeof(*p)); |
dan | 1c5ed62 | 2018-06-05 16:16:17 +0000 | [diff] [blame] | 439 | if( p && p->pVal ){ |
| 440 | sqlite3_result_value(pCtx, p->pVal); |
| 441 | } |
| 442 | } |
| 443 | static void last_valueFinalizeFunc(sqlite3_context *pCtx){ |
| 444 | struct LastValueCtx *p; |
dan | 9c27758 | 2018-06-20 09:23:49 +0000 | [diff] [blame] | 445 | p = (struct LastValueCtx*)sqlite3_aggregate_context(pCtx, sizeof(*p)); |
dan | 1c5ed62 | 2018-06-05 16:16:17 +0000 | [diff] [blame] | 446 | if( p && p->pVal ){ |
| 447 | sqlite3_result_value(pCtx, p->pVal); |
| 448 | sqlite3_value_free(p->pVal); |
| 449 | p->pVal = 0; |
| 450 | } |
| 451 | } |
| 452 | |
dan | 2a11bb2 | 2018-06-11 20:50:25 +0000 | [diff] [blame] | 453 | /* |
| 454 | ** No-op implementations of nth_value(), first_value(), lead() and lag(). |
| 455 | ** These are all implemented inline using VDBE instructions. |
| 456 | */ |
| 457 | static void nth_valueStepFunc(sqlite3_context *pCtx, int n, sqlite3_value **a){} |
| 458 | static void nth_valueInvFunc(sqlite3_context *pCtx, int n, sqlite3_value **ap){} |
| 459 | static void nth_valueValueFunc(sqlite3_context *pCtx){} |
| 460 | static void first_valueStepFunc(sqlite3_context *p, int n, sqlite3_value **ap){} |
| 461 | static void first_valueInvFunc(sqlite3_context *p, int n, sqlite3_value **ap){} |
| 462 | static void first_valueValueFunc(sqlite3_context *pCtx){} |
| 463 | static void leadStepFunc(sqlite3_context *pCtx, int n, sqlite3_value **ap){} |
| 464 | static void leadInvFunc(sqlite3_context *pCtx, int n, sqlite3_value **ap){} |
| 465 | static void leadValueFunc(sqlite3_context *pCtx){} |
| 466 | static void lagStepFunc(sqlite3_context *pCtx, int n, sqlite3_value **ap){} |
| 467 | static void lagInvFunc(sqlite3_context *pCtx, int n, sqlite3_value **ap){} |
| 468 | static void lagValueFunc(sqlite3_context *pCtx){} |
dan | fe4e25a | 2018-06-07 20:08:59 +0000 | [diff] [blame] | 469 | |
dan | dfa552f | 2018-06-02 21:04:28 +0000 | [diff] [blame] | 470 | #define WINDOWFUNC(name,nArg,extra) { \ |
| 471 | nArg, (SQLITE_UTF8|SQLITE_FUNC_WINDOW|extra), 0, 0, \ |
| 472 | name ## StepFunc, name ## ValueFunc, name ## ValueFunc, \ |
dan | 2a11bb2 | 2018-06-11 20:50:25 +0000 | [diff] [blame] | 473 | name ## InvFunc, #name \ |
dan | dfa552f | 2018-06-02 21:04:28 +0000 | [diff] [blame] | 474 | } |
| 475 | |
dan | 1c5ed62 | 2018-06-05 16:16:17 +0000 | [diff] [blame] | 476 | #define WINDOWFUNCF(name,nArg,extra) { \ |
| 477 | nArg, (SQLITE_UTF8|SQLITE_FUNC_WINDOW|extra), 0, 0, \ |
| 478 | name ## StepFunc, name ## FinalizeFunc, name ## ValueFunc, \ |
dan | 2a11bb2 | 2018-06-11 20:50:25 +0000 | [diff] [blame] | 479 | name ## InvFunc, #name \ |
dan | 1c5ed62 | 2018-06-05 16:16:17 +0000 | [diff] [blame] | 480 | } |
| 481 | |
dan | dfa552f | 2018-06-02 21:04:28 +0000 | [diff] [blame] | 482 | /* |
| 483 | ** Register those built-in window functions that are not also aggregates. |
| 484 | */ |
| 485 | void sqlite3WindowFunctions(void){ |
| 486 | static FuncDef aWindowFuncs[] = { |
| 487 | WINDOWFUNC(row_number, 0, 0), |
| 488 | WINDOWFUNC(dense_rank, 0, 0), |
| 489 | WINDOWFUNC(rank, 0, 0), |
| 490 | WINDOWFUNC(percent_rank, 0, SQLITE_FUNC_WINDOW_SIZE), |
dan | f1abe36 | 2018-06-04 08:22:09 +0000 | [diff] [blame] | 491 | WINDOWFUNC(cume_dist, 0, SQLITE_FUNC_WINDOW_SIZE), |
dan | 6bc5c9e | 2018-06-04 18:55:11 +0000 | [diff] [blame] | 492 | WINDOWFUNC(ntile, 1, SQLITE_FUNC_WINDOW_SIZE), |
dan | 1c5ed62 | 2018-06-05 16:16:17 +0000 | [diff] [blame] | 493 | WINDOWFUNCF(last_value, 1, 0), |
dan | dfa552f | 2018-06-02 21:04:28 +0000 | [diff] [blame] | 494 | WINDOWFUNC(nth_value, 2, 0), |
dan | 7095c00 | 2018-06-07 17:45:22 +0000 | [diff] [blame] | 495 | WINDOWFUNC(first_value, 1, 0), |
dan | fe4e25a | 2018-06-07 20:08:59 +0000 | [diff] [blame] | 496 | WINDOWFUNC(lead, 1, 0), WINDOWFUNC(lead, 2, 0), WINDOWFUNC(lead, 3, 0), |
| 497 | WINDOWFUNC(lag, 1, 0), WINDOWFUNC(lag, 2, 0), WINDOWFUNC(lag, 3, 0), |
dan | dfa552f | 2018-06-02 21:04:28 +0000 | [diff] [blame] | 498 | }; |
| 499 | sqlite3InsertBuiltinFuncs(aWindowFuncs, ArraySize(aWindowFuncs)); |
| 500 | } |
| 501 | |
dan | c0bb445 | 2018-06-12 20:53:38 +0000 | [diff] [blame] | 502 | /* |
| 503 | ** This function is called immediately after resolving the function name |
| 504 | ** for a window function within a SELECT statement. Argument pList is a |
| 505 | ** linked list of WINDOW definitions for the current SELECT statement. |
| 506 | ** Argument pFunc is the function definition just resolved and pWin |
| 507 | ** is the Window object representing the associated OVER clause. This |
| 508 | ** function updates the contents of pWin as follows: |
| 509 | ** |
| 510 | ** * If the OVER clause refered to a named window (as in "max(x) OVER win"), |
| 511 | ** search list pList for a matching WINDOW definition, and update pWin |
| 512 | ** accordingly. If no such WINDOW clause can be found, leave an error |
| 513 | ** in pParse. |
| 514 | ** |
| 515 | ** * If the function is a built-in window function that requires the |
| 516 | ** window to be coerced (see "BUILT-IN WINDOW FUNCTIONS" at the top |
| 517 | ** of this file), pWin is updated here. |
| 518 | */ |
dan | e3bf632 | 2018-06-08 20:58:27 +0000 | [diff] [blame] | 519 | void sqlite3WindowUpdate( |
| 520 | Parse *pParse, |
dan | c0bb445 | 2018-06-12 20:53:38 +0000 | [diff] [blame] | 521 | Window *pList, /* List of named windows for this SELECT */ |
| 522 | Window *pWin, /* Window frame to update */ |
| 523 | FuncDef *pFunc /* Window function definition */ |
dan | e3bf632 | 2018-06-08 20:58:27 +0000 | [diff] [blame] | 524 | ){ |
dan | c95f38d | 2018-06-18 20:34:43 +0000 | [diff] [blame] | 525 | if( pWin->zName && pWin->eType==0 ){ |
dan | e3bf632 | 2018-06-08 20:58:27 +0000 | [diff] [blame] | 526 | Window *p; |
| 527 | for(p=pList; p; p=p->pNextWin){ |
| 528 | if( sqlite3StrICmp(p->zName, pWin->zName)==0 ) break; |
| 529 | } |
| 530 | if( p==0 ){ |
| 531 | sqlite3ErrorMsg(pParse, "no such window: %s", pWin->zName); |
| 532 | return; |
| 533 | } |
| 534 | pWin->pPartition = sqlite3ExprListDup(pParse->db, p->pPartition, 0); |
| 535 | pWin->pOrderBy = sqlite3ExprListDup(pParse->db, p->pOrderBy, 0); |
| 536 | pWin->pStart = sqlite3ExprDup(pParse->db, p->pStart, 0); |
| 537 | pWin->pEnd = sqlite3ExprDup(pParse->db, p->pEnd, 0); |
| 538 | pWin->eStart = p->eStart; |
| 539 | pWin->eEnd = p->eEnd; |
dan | c95f38d | 2018-06-18 20:34:43 +0000 | [diff] [blame] | 540 | pWin->eType = p->eType; |
dan | e3bf632 | 2018-06-08 20:58:27 +0000 | [diff] [blame] | 541 | } |
dan | dfa552f | 2018-06-02 21:04:28 +0000 | [diff] [blame] | 542 | if( pFunc->funcFlags & SQLITE_FUNC_WINDOW ){ |
| 543 | sqlite3 *db = pParse->db; |
dan | 8b98560 | 2018-06-09 17:43:45 +0000 | [diff] [blame] | 544 | if( pWin->pFilter ){ |
| 545 | sqlite3ErrorMsg(pParse, |
| 546 | "FILTER clause may only be used with aggregate window functions" |
| 547 | ); |
| 548 | }else |
dan | 6bc5c9e | 2018-06-04 18:55:11 +0000 | [diff] [blame] | 549 | if( pFunc->xSFunc==row_numberStepFunc || pFunc->xSFunc==ntileStepFunc ){ |
dan | dfa552f | 2018-06-02 21:04:28 +0000 | [diff] [blame] | 550 | sqlite3ExprDelete(db, pWin->pStart); |
| 551 | sqlite3ExprDelete(db, pWin->pEnd); |
| 552 | pWin->pStart = pWin->pEnd = 0; |
| 553 | pWin->eType = TK_ROWS; |
| 554 | pWin->eStart = TK_UNBOUNDED; |
| 555 | pWin->eEnd = TK_CURRENT; |
dan | 8b98560 | 2018-06-09 17:43:45 +0000 | [diff] [blame] | 556 | }else |
dan | dfa552f | 2018-06-02 21:04:28 +0000 | [diff] [blame] | 557 | |
| 558 | if( pFunc->xSFunc==dense_rankStepFunc || pFunc->xSFunc==rankStepFunc |
dan | f1abe36 | 2018-06-04 08:22:09 +0000 | [diff] [blame] | 559 | || pFunc->xSFunc==percent_rankStepFunc || pFunc->xSFunc==cume_distStepFunc |
dan | dfa552f | 2018-06-02 21:04:28 +0000 | [diff] [blame] | 560 | ){ |
| 561 | sqlite3ExprDelete(db, pWin->pStart); |
| 562 | sqlite3ExprDelete(db, pWin->pEnd); |
| 563 | pWin->pStart = pWin->pEnd = 0; |
| 564 | pWin->eType = TK_RANGE; |
| 565 | pWin->eStart = TK_UNBOUNDED; |
| 566 | pWin->eEnd = TK_CURRENT; |
| 567 | } |
| 568 | } |
dan | 2a11bb2 | 2018-06-11 20:50:25 +0000 | [diff] [blame] | 569 | pWin->pFunc = pFunc; |
dan | dfa552f | 2018-06-02 21:04:28 +0000 | [diff] [blame] | 570 | } |
| 571 | |
dan | c0bb445 | 2018-06-12 20:53:38 +0000 | [diff] [blame] | 572 | /* |
| 573 | ** Context object passed through sqlite3WalkExprList() to |
| 574 | ** selectWindowRewriteExprCb() by selectWindowRewriteEList(). |
| 575 | */ |
dan | dfa552f | 2018-06-02 21:04:28 +0000 | [diff] [blame] | 576 | typedef struct WindowRewrite WindowRewrite; |
| 577 | struct WindowRewrite { |
| 578 | Window *pWin; |
| 579 | ExprList *pSub; |
| 580 | }; |
| 581 | |
dan | c0bb445 | 2018-06-12 20:53:38 +0000 | [diff] [blame] | 582 | /* |
| 583 | ** Callback function used by selectWindowRewriteEList(). If necessary, |
| 584 | ** this function appends to the output expression-list and updates |
| 585 | ** expression (*ppExpr) in place. |
| 586 | */ |
dan | dfa552f | 2018-06-02 21:04:28 +0000 | [diff] [blame] | 587 | static int selectWindowRewriteExprCb(Walker *pWalker, Expr *pExpr){ |
| 588 | struct WindowRewrite *p = pWalker->u.pRewrite; |
| 589 | Parse *pParse = pWalker->pParse; |
| 590 | |
| 591 | switch( pExpr->op ){ |
| 592 | |
| 593 | case TK_FUNCTION: |
| 594 | if( pExpr->pWin==0 ){ |
| 595 | break; |
| 596 | }else{ |
| 597 | Window *pWin; |
| 598 | for(pWin=p->pWin; pWin; pWin=pWin->pNextWin){ |
| 599 | if( pExpr->pWin==pWin ){ |
dan | 2a11bb2 | 2018-06-11 20:50:25 +0000 | [diff] [blame] | 600 | assert( pWin->pOwner==pExpr ); |
dan | dfa552f | 2018-06-02 21:04:28 +0000 | [diff] [blame] | 601 | return WRC_Prune; |
| 602 | } |
| 603 | } |
| 604 | } |
| 605 | /* Fall through. */ |
| 606 | |
dan | 7392569 | 2018-06-12 18:40:17 +0000 | [diff] [blame] | 607 | case TK_AGG_FUNCTION: |
dan | dfa552f | 2018-06-02 21:04:28 +0000 | [diff] [blame] | 608 | case TK_COLUMN: { |
| 609 | Expr *pDup = sqlite3ExprDup(pParse->db, pExpr, 0); |
| 610 | p->pSub = sqlite3ExprListAppend(pParse, p->pSub, pDup); |
| 611 | if( p->pSub ){ |
| 612 | assert( ExprHasProperty(pExpr, EP_Static)==0 ); |
| 613 | ExprSetProperty(pExpr, EP_Static); |
| 614 | sqlite3ExprDelete(pParse->db, pExpr); |
| 615 | ExprClearProperty(pExpr, EP_Static); |
| 616 | memset(pExpr, 0, sizeof(Expr)); |
| 617 | |
| 618 | pExpr->op = TK_COLUMN; |
| 619 | pExpr->iColumn = p->pSub->nExpr-1; |
| 620 | pExpr->iTable = p->pWin->iEphCsr; |
| 621 | } |
| 622 | |
| 623 | break; |
| 624 | } |
| 625 | |
| 626 | default: /* no-op */ |
| 627 | break; |
| 628 | } |
| 629 | |
| 630 | return WRC_Continue; |
| 631 | } |
dan | c0bb445 | 2018-06-12 20:53:38 +0000 | [diff] [blame] | 632 | static int selectWindowRewriteSelectCb(Walker *pWalker, Select *pSelect){ |
| 633 | return WRC_Prune; |
| 634 | } |
dan | dfa552f | 2018-06-02 21:04:28 +0000 | [diff] [blame] | 635 | |
dan | c0bb445 | 2018-06-12 20:53:38 +0000 | [diff] [blame] | 636 | |
| 637 | /* |
| 638 | ** Iterate through each expression in expression-list pEList. For each: |
| 639 | ** |
| 640 | ** * TK_COLUMN, |
| 641 | ** * aggregate function, or |
| 642 | ** * window function with a Window object that is not a member of the |
| 643 | ** linked list passed as the second argument (pWin) |
| 644 | ** |
| 645 | ** Append the node to output expression-list (*ppSub). And replace it |
| 646 | ** with a TK_COLUMN that reads the (N-1)th element of table |
| 647 | ** pWin->iEphCsr, where N is the number of elements in (*ppSub) after |
| 648 | ** appending the new one. |
| 649 | */ |
dan | 13b08bb | 2018-06-15 20:46:12 +0000 | [diff] [blame] | 650 | static void selectWindowRewriteEList( |
dan | dfa552f | 2018-06-02 21:04:28 +0000 | [diff] [blame] | 651 | Parse *pParse, |
| 652 | Window *pWin, |
| 653 | ExprList *pEList, /* Rewrite expressions in this list */ |
| 654 | ExprList **ppSub /* IN/OUT: Sub-select expression-list */ |
| 655 | ){ |
| 656 | Walker sWalker; |
| 657 | WindowRewrite sRewrite; |
dan | dfa552f | 2018-06-02 21:04:28 +0000 | [diff] [blame] | 658 | |
| 659 | memset(&sWalker, 0, sizeof(Walker)); |
| 660 | memset(&sRewrite, 0, sizeof(WindowRewrite)); |
| 661 | |
| 662 | sRewrite.pSub = *ppSub; |
| 663 | sRewrite.pWin = pWin; |
| 664 | |
| 665 | sWalker.pParse = pParse; |
| 666 | sWalker.xExprCallback = selectWindowRewriteExprCb; |
| 667 | sWalker.xSelectCallback = selectWindowRewriteSelectCb; |
| 668 | sWalker.u.pRewrite = &sRewrite; |
| 669 | |
dan | 13b08bb | 2018-06-15 20:46:12 +0000 | [diff] [blame] | 670 | (void)sqlite3WalkExprList(&sWalker, pEList); |
dan | dfa552f | 2018-06-02 21:04:28 +0000 | [diff] [blame] | 671 | |
| 672 | *ppSub = sRewrite.pSub; |
dan | dfa552f | 2018-06-02 21:04:28 +0000 | [diff] [blame] | 673 | } |
| 674 | |
dan | c0bb445 | 2018-06-12 20:53:38 +0000 | [diff] [blame] | 675 | /* |
| 676 | ** Append a copy of each expression in expression-list pAppend to |
| 677 | ** expression list pList. Return a pointer to the result list. |
| 678 | */ |
dan | dfa552f | 2018-06-02 21:04:28 +0000 | [diff] [blame] | 679 | static ExprList *exprListAppendList( |
| 680 | Parse *pParse, /* Parsing context */ |
| 681 | ExprList *pList, /* List to which to append. Might be NULL */ |
| 682 | ExprList *pAppend /* List of values to append. Might be NULL */ |
| 683 | ){ |
| 684 | if( pAppend ){ |
| 685 | int i; |
| 686 | int nInit = pList ? pList->nExpr : 0; |
| 687 | for(i=0; i<pAppend->nExpr; i++){ |
| 688 | Expr *pDup = sqlite3ExprDup(pParse->db, pAppend->a[i].pExpr, 0); |
| 689 | pList = sqlite3ExprListAppend(pParse, pList, pDup); |
| 690 | if( pList ) pList->a[nInit+i].sortOrder = pAppend->a[i].sortOrder; |
| 691 | } |
| 692 | } |
| 693 | return pList; |
| 694 | } |
| 695 | |
| 696 | /* |
| 697 | ** If the SELECT statement passed as the second argument does not invoke |
| 698 | ** any SQL window functions, this function is a no-op. Otherwise, it |
| 699 | ** rewrites the SELECT statement so that window function xStep functions |
dan | c0bb445 | 2018-06-12 20:53:38 +0000 | [diff] [blame] | 700 | ** are invoked in the correct order as described under "SELECT REWRITING" |
| 701 | ** at the top of this file. |
dan | dfa552f | 2018-06-02 21:04:28 +0000 | [diff] [blame] | 702 | */ |
| 703 | int sqlite3WindowRewrite(Parse *pParse, Select *p){ |
| 704 | int rc = SQLITE_OK; |
| 705 | if( p->pWin ){ |
| 706 | Vdbe *v = sqlite3GetVdbe(pParse); |
dan | dfa552f | 2018-06-02 21:04:28 +0000 | [diff] [blame] | 707 | sqlite3 *db = pParse->db; |
| 708 | Select *pSub = 0; /* The subquery */ |
| 709 | SrcList *pSrc = p->pSrc; |
| 710 | Expr *pWhere = p->pWhere; |
| 711 | ExprList *pGroupBy = p->pGroupBy; |
| 712 | Expr *pHaving = p->pHaving; |
| 713 | ExprList *pSort = 0; |
| 714 | |
| 715 | ExprList *pSublist = 0; /* Expression list for sub-query */ |
| 716 | Window *pMWin = p->pWin; /* Master window object */ |
| 717 | Window *pWin; /* Window object iterator */ |
| 718 | |
| 719 | p->pSrc = 0; |
| 720 | p->pWhere = 0; |
| 721 | p->pGroupBy = 0; |
| 722 | p->pHaving = 0; |
| 723 | |
| 724 | /* Assign a cursor number for the ephemeral table used to buffer rows. |
| 725 | ** The OpenEphemeral instruction is coded later, after it is known how |
| 726 | ** many columns the table will have. */ |
| 727 | pMWin->iEphCsr = pParse->nTab++; |
| 728 | |
dan | 13b08bb | 2018-06-15 20:46:12 +0000 | [diff] [blame] | 729 | selectWindowRewriteEList(pParse, pMWin, p->pEList, &pSublist); |
| 730 | selectWindowRewriteEList(pParse, pMWin, p->pOrderBy, &pSublist); |
dan | dfa552f | 2018-06-02 21:04:28 +0000 | [diff] [blame] | 731 | pMWin->nBufferCol = (pSublist ? pSublist->nExpr : 0); |
| 732 | |
| 733 | /* Create the ORDER BY clause for the sub-select. This is the concatenation |
| 734 | ** of the window PARTITION and ORDER BY clauses. Append the same |
| 735 | ** expressions to the sub-select expression list. They are required to |
| 736 | ** figure out where boundaries for partitions and sets of peer rows. */ |
| 737 | pSort = sqlite3ExprListDup(db, pMWin->pPartition, 0); |
| 738 | if( pMWin->pOrderBy ){ |
| 739 | pSort = exprListAppendList(pParse, pSort, pMWin->pOrderBy); |
| 740 | } |
| 741 | pSublist = exprListAppendList(pParse, pSublist, pSort); |
| 742 | |
| 743 | /* Append the arguments passed to each window function to the |
| 744 | ** sub-select expression list. Also allocate two registers for each |
| 745 | ** window function - one for the accumulator, another for interim |
| 746 | ** results. */ |
| 747 | for(pWin=pMWin; pWin; pWin=pWin->pNextWin){ |
| 748 | pWin->iArgCol = (pSublist ? pSublist->nExpr : 0); |
| 749 | pSublist = exprListAppendList(pParse, pSublist, pWin->pOwner->x.pList); |
dan | 8b98560 | 2018-06-09 17:43:45 +0000 | [diff] [blame] | 750 | if( pWin->pFilter ){ |
| 751 | Expr *pFilter = sqlite3ExprDup(db, pWin->pFilter, 0); |
| 752 | pSublist = sqlite3ExprListAppend(pParse, pSublist, pFilter); |
| 753 | } |
dan | dfa552f | 2018-06-02 21:04:28 +0000 | [diff] [blame] | 754 | pWin->regAccum = ++pParse->nMem; |
| 755 | pWin->regResult = ++pParse->nMem; |
| 756 | sqlite3VdbeAddOp2(v, OP_Null, 0, pWin->regAccum); |
| 757 | } |
| 758 | |
dan | 9c27758 | 2018-06-20 09:23:49 +0000 | [diff] [blame] | 759 | /* If there is no ORDER BY or PARTITION BY clause, and the window |
| 760 | ** function accepts zero arguments, and there are no other columns |
| 761 | ** selected (e.g. "SELECT row_number() OVER () FROM t1"), it is possible |
| 762 | ** that pSublist is still NULL here. Add a constant expression here to |
| 763 | ** keep everything legal in this case. |
| 764 | */ |
| 765 | if( pSublist==0 ){ |
| 766 | pSublist = sqlite3ExprListAppend(pParse, 0, |
| 767 | sqlite3ExprAlloc(db, TK_INTEGER, &sqlite3IntTokens[0], 0) |
| 768 | ); |
| 769 | } |
| 770 | |
dan | dfa552f | 2018-06-02 21:04:28 +0000 | [diff] [blame] | 771 | pSub = sqlite3SelectNew( |
| 772 | pParse, pSublist, pSrc, pWhere, pGroupBy, pHaving, pSort, 0, 0 |
| 773 | ); |
| 774 | p->pSrc = sqlite3SrcListAppend(db, 0, 0, 0); |
dan | 6fde179 | 2018-06-15 19:01:35 +0000 | [diff] [blame] | 775 | assert( p->pSrc || db->mallocFailed ); |
dan | dfa552f | 2018-06-02 21:04:28 +0000 | [diff] [blame] | 776 | if( p->pSrc ){ |
dan | dfa552f | 2018-06-02 21:04:28 +0000 | [diff] [blame] | 777 | p->pSrc->a[0].pSelect = pSub; |
| 778 | sqlite3SrcListAssignCursors(pParse, p->pSrc); |
| 779 | if( sqlite3ExpandSubquery(pParse, &p->pSrc->a[0]) ){ |
| 780 | rc = SQLITE_NOMEM; |
| 781 | }else{ |
| 782 | pSub->selFlags |= SF_Expanded; |
dan | 7392569 | 2018-06-12 18:40:17 +0000 | [diff] [blame] | 783 | p->selFlags &= ~SF_Aggregate; |
| 784 | sqlite3SelectPrep(pParse, pSub, 0); |
dan | dfa552f | 2018-06-02 21:04:28 +0000 | [diff] [blame] | 785 | } |
dan | dfa552f | 2018-06-02 21:04:28 +0000 | [diff] [blame] | 786 | |
dan | 6fde179 | 2018-06-15 19:01:35 +0000 | [diff] [blame] | 787 | sqlite3VdbeAddOp2(v, OP_OpenEphemeral, pMWin->iEphCsr, pSublist->nExpr); |
| 788 | }else{ |
| 789 | sqlite3SelectDelete(db, pSub); |
| 790 | } |
| 791 | if( db->mallocFailed ) rc = SQLITE_NOMEM; |
dan | dfa552f | 2018-06-02 21:04:28 +0000 | [diff] [blame] | 792 | } |
| 793 | |
| 794 | return rc; |
| 795 | } |
| 796 | |
dan | c0bb445 | 2018-06-12 20:53:38 +0000 | [diff] [blame] | 797 | /* |
| 798 | ** Free the Window object passed as the second argument. |
| 799 | */ |
dan | 86fb6e1 | 2018-05-16 20:58:07 +0000 | [diff] [blame] | 800 | void sqlite3WindowDelete(sqlite3 *db, Window *p){ |
| 801 | if( p ){ |
| 802 | sqlite3ExprDelete(db, p->pFilter); |
| 803 | sqlite3ExprListDelete(db, p->pPartition); |
| 804 | sqlite3ExprListDelete(db, p->pOrderBy); |
| 805 | sqlite3ExprDelete(db, p->pEnd); |
| 806 | sqlite3ExprDelete(db, p->pStart); |
dan | e3bf632 | 2018-06-08 20:58:27 +0000 | [diff] [blame] | 807 | sqlite3DbFree(db, p->zName); |
dan | 86fb6e1 | 2018-05-16 20:58:07 +0000 | [diff] [blame] | 808 | sqlite3DbFree(db, p); |
| 809 | } |
| 810 | } |
| 811 | |
dan | c0bb445 | 2018-06-12 20:53:38 +0000 | [diff] [blame] | 812 | /* |
| 813 | ** Free the linked list of Window objects starting at the second argument. |
| 814 | */ |
dan | e3bf632 | 2018-06-08 20:58:27 +0000 | [diff] [blame] | 815 | void sqlite3WindowListDelete(sqlite3 *db, Window *p){ |
| 816 | while( p ){ |
| 817 | Window *pNext = p->pNextWin; |
| 818 | sqlite3WindowDelete(db, p); |
| 819 | p = pNext; |
| 820 | } |
| 821 | } |
| 822 | |
dan | c0bb445 | 2018-06-12 20:53:38 +0000 | [diff] [blame] | 823 | /* |
| 824 | ** Allocate and return a new Window object. |
| 825 | */ |
dan | 86fb6e1 | 2018-05-16 20:58:07 +0000 | [diff] [blame] | 826 | Window *sqlite3WindowAlloc( |
| 827 | Parse *pParse, |
| 828 | int eType, |
dan | c3a20c1 | 2018-05-23 20:55:37 +0000 | [diff] [blame] | 829 | int eStart, Expr *pStart, |
| 830 | int eEnd, Expr *pEnd |
dan | 86fb6e1 | 2018-05-16 20:58:07 +0000 | [diff] [blame] | 831 | ){ |
| 832 | Window *pWin = (Window*)sqlite3DbMallocZero(pParse->db, sizeof(Window)); |
| 833 | |
| 834 | if( pWin ){ |
dan | c95f38d | 2018-06-18 20:34:43 +0000 | [diff] [blame] | 835 | assert( eType ); |
dan | 86fb6e1 | 2018-05-16 20:58:07 +0000 | [diff] [blame] | 836 | pWin->eType = eType; |
| 837 | pWin->eStart = eStart; |
| 838 | pWin->eEnd = eEnd; |
| 839 | pWin->pEnd = pEnd; |
| 840 | pWin->pStart = pStart; |
| 841 | }else{ |
| 842 | sqlite3ExprDelete(pParse->db, pEnd); |
| 843 | sqlite3ExprDelete(pParse->db, pStart); |
| 844 | } |
| 845 | |
| 846 | return pWin; |
| 847 | } |
| 848 | |
dan | c0bb445 | 2018-06-12 20:53:38 +0000 | [diff] [blame] | 849 | /* |
| 850 | ** Attach window object pWin to expression p. |
| 851 | */ |
dan | 86fb6e1 | 2018-05-16 20:58:07 +0000 | [diff] [blame] | 852 | void sqlite3WindowAttach(Parse *pParse, Expr *p, Window *pWin){ |
| 853 | if( p ){ |
| 854 | p->pWin = pWin; |
dan | 2a11bb2 | 2018-06-11 20:50:25 +0000 | [diff] [blame] | 855 | if( pWin ) pWin->pOwner = p; |
dan | 86fb6e1 | 2018-05-16 20:58:07 +0000 | [diff] [blame] | 856 | }else{ |
| 857 | sqlite3WindowDelete(pParse->db, pWin); |
| 858 | } |
| 859 | } |
dan | e2f781b | 2018-05-17 19:24:08 +0000 | [diff] [blame] | 860 | |
| 861 | /* |
| 862 | ** Return 0 if the two window objects are identical, or non-zero otherwise. |
dan | 13078ca | 2018-06-13 20:29:38 +0000 | [diff] [blame] | 863 | ** Identical window objects can be processed in a single scan. |
dan | e2f781b | 2018-05-17 19:24:08 +0000 | [diff] [blame] | 864 | */ |
| 865 | int sqlite3WindowCompare(Parse *pParse, Window *p1, Window *p2){ |
| 866 | if( p1->eType!=p2->eType ) return 1; |
| 867 | if( p1->eStart!=p2->eStart ) return 1; |
| 868 | if( p1->eEnd!=p2->eEnd ) return 1; |
| 869 | if( sqlite3ExprCompare(pParse, p1->pStart, p2->pStart, -1) ) return 1; |
| 870 | if( sqlite3ExprCompare(pParse, p1->pEnd, p2->pEnd, -1) ) return 1; |
| 871 | if( sqlite3ExprListCompare(p1->pPartition, p2->pPartition, -1) ) return 1; |
| 872 | if( sqlite3ExprListCompare(p1->pOrderBy, p2->pOrderBy, -1) ) return 1; |
| 873 | return 0; |
| 874 | } |
| 875 | |
dan | 13078ca | 2018-06-13 20:29:38 +0000 | [diff] [blame] | 876 | |
| 877 | /* |
| 878 | ** This is called by code in select.c before it calls sqlite3WhereBegin() |
| 879 | ** to begin iterating through the sub-query results. It is used to allocate |
| 880 | ** and initialize registers and cursors used by sqlite3WindowCodeStep(). |
| 881 | */ |
| 882 | void sqlite3WindowCodeInit(Parse *pParse, Window *pMWin){ |
dan | c9a8668 | 2018-05-30 20:44:58 +0000 | [diff] [blame] | 883 | Window *pWin; |
dan | 13078ca | 2018-06-13 20:29:38 +0000 | [diff] [blame] | 884 | Vdbe *v = sqlite3GetVdbe(pParse); |
| 885 | int nPart = (pMWin->pPartition ? pMWin->pPartition->nExpr : 0); |
| 886 | nPart += (pMWin->pOrderBy ? pMWin->pOrderBy->nExpr : 0); |
| 887 | if( nPart ){ |
| 888 | pMWin->regPart = pParse->nMem+1; |
| 889 | pParse->nMem += nPart; |
| 890 | sqlite3VdbeAddOp3(v, OP_Null, 0, pMWin->regPart, pMWin->regPart+nPart-1); |
| 891 | } |
| 892 | |
dan | c9a8668 | 2018-05-30 20:44:58 +0000 | [diff] [blame] | 893 | for(pWin=pMWin; pWin; pWin=pWin->pNextWin){ |
dan | ec891fd | 2018-06-06 20:51:02 +0000 | [diff] [blame] | 894 | FuncDef *p = pWin->pFunc; |
| 895 | if( (p->funcFlags & SQLITE_FUNC_MINMAX) && pWin->eStart!=TK_UNBOUNDED ){ |
dan | 9a94722 | 2018-06-14 19:06:36 +0000 | [diff] [blame] | 896 | /* The inline versions of min() and max() require a single ephemeral |
| 897 | ** table and 3 registers. The registers are used as follows: |
| 898 | ** |
| 899 | ** regApp+0: slot to copy min()/max() argument to for MakeRecord |
| 900 | ** regApp+1: integer value used to ensure keys are unique |
| 901 | ** regApp+2: output of MakeRecord |
| 902 | */ |
dan | c9a8668 | 2018-05-30 20:44:58 +0000 | [diff] [blame] | 903 | ExprList *pList = pWin->pOwner->x.pList; |
| 904 | KeyInfo *pKeyInfo = sqlite3KeyInfoFromExprList(pParse, pList, 0, 0); |
dan | c9a8668 | 2018-05-30 20:44:58 +0000 | [diff] [blame] | 905 | pWin->csrApp = pParse->nTab++; |
| 906 | pWin->regApp = pParse->nMem+1; |
| 907 | pParse->nMem += 3; |
| 908 | if( pKeyInfo && pWin->pFunc->zName[1]=='i' ){ |
| 909 | assert( pKeyInfo->aSortOrder[0]==0 ); |
| 910 | pKeyInfo->aSortOrder[0] = 1; |
| 911 | } |
| 912 | sqlite3VdbeAddOp2(v, OP_OpenEphemeral, pWin->csrApp, 2); |
| 913 | sqlite3VdbeAppendP4(v, pKeyInfo, P4_KEYINFO); |
| 914 | sqlite3VdbeAddOp2(v, OP_Integer, 0, pWin->regApp+1); |
| 915 | } |
dan | 7095c00 | 2018-06-07 17:45:22 +0000 | [diff] [blame] | 916 | else if( p->xSFunc==nth_valueStepFunc || p->xSFunc==first_valueStepFunc ){ |
dan | ec891fd | 2018-06-06 20:51:02 +0000 | [diff] [blame] | 917 | /* Allocate two registers at pWin->regApp. These will be used to |
| 918 | ** store the start and end index of the current frame. */ |
| 919 | assert( pMWin->iEphCsr ); |
| 920 | pWin->regApp = pParse->nMem+1; |
| 921 | pWin->csrApp = pParse->nTab++; |
| 922 | pParse->nMem += 2; |
dan | ec891fd | 2018-06-06 20:51:02 +0000 | [diff] [blame] | 923 | sqlite3VdbeAddOp2(v, OP_OpenDup, pWin->csrApp, pMWin->iEphCsr); |
| 924 | } |
dan | fe4e25a | 2018-06-07 20:08:59 +0000 | [diff] [blame] | 925 | else if( p->xSFunc==leadStepFunc || p->xSFunc==lagStepFunc ){ |
| 926 | assert( pMWin->iEphCsr ); |
| 927 | pWin->csrApp = pParse->nTab++; |
| 928 | sqlite3VdbeAddOp2(v, OP_OpenDup, pWin->csrApp, pMWin->iEphCsr); |
| 929 | } |
dan | c9a8668 | 2018-05-30 20:44:58 +0000 | [diff] [blame] | 930 | } |
| 931 | } |
| 932 | |
dan | 13078ca | 2018-06-13 20:29:38 +0000 | [diff] [blame] | 933 | /* |
| 934 | ** A "PRECEDING <expr>" (bEnd==0) or "FOLLOWING <expr>" (bEnd==1) has just |
| 935 | ** been evaluated and the result left in register reg. This function generates |
| 936 | ** VM code to check that the value is a non-negative integer and throws |
| 937 | ** an exception if it is not. |
| 938 | */ |
dan | c3a20c1 | 2018-05-23 20:55:37 +0000 | [diff] [blame] | 939 | static void windowCheckFrameValue(Parse *pParse, int reg, int bEnd){ |
| 940 | static const char *azErr[] = { |
| 941 | "frame starting offset must be a non-negative integer", |
| 942 | "frame ending offset must be a non-negative integer" |
| 943 | }; |
| 944 | Vdbe *v = sqlite3GetVdbe(pParse); |
dan | 13078ca | 2018-06-13 20:29:38 +0000 | [diff] [blame] | 945 | int regZero = sqlite3GetTempReg(pParse); |
dan | c3a20c1 | 2018-05-23 20:55:37 +0000 | [diff] [blame] | 946 | sqlite3VdbeAddOp2(v, OP_Integer, 0, regZero); |
| 947 | sqlite3VdbeAddOp2(v, OP_MustBeInt, reg, sqlite3VdbeCurrentAddr(v)+2); |
| 948 | sqlite3VdbeAddOp3(v, OP_Ge, regZero, sqlite3VdbeCurrentAddr(v)+2, reg); |
| 949 | sqlite3VdbeAddOp2(v, OP_Halt, SQLITE_ERROR, OE_Abort); |
| 950 | sqlite3VdbeAppendP4(v, (void*)azErr[bEnd], P4_STATIC); |
dan | 13078ca | 2018-06-13 20:29:38 +0000 | [diff] [blame] | 951 | sqlite3ReleaseTempReg(pParse, regZero); |
dan | c3a20c1 | 2018-05-23 20:55:37 +0000 | [diff] [blame] | 952 | } |
| 953 | |
dan | 13078ca | 2018-06-13 20:29:38 +0000 | [diff] [blame] | 954 | /* |
| 955 | ** Return the number of arguments passed to the window-function associated |
| 956 | ** with the object passed as the only argument to this function. |
| 957 | */ |
dan | 2a11bb2 | 2018-06-11 20:50:25 +0000 | [diff] [blame] | 958 | static int windowArgCount(Window *pWin){ |
| 959 | ExprList *pList = pWin->pOwner->x.pList; |
| 960 | return (pList ? pList->nExpr : 0); |
| 961 | } |
| 962 | |
dan | c9a8668 | 2018-05-30 20:44:58 +0000 | [diff] [blame] | 963 | /* |
| 964 | ** Generate VM code to invoke either xStep() (if bInverse is 0) or |
| 965 | ** xInverse (if bInverse is non-zero) for each window function in the |
dan | 13078ca | 2018-06-13 20:29:38 +0000 | [diff] [blame] | 966 | ** linked list starting at pMWin. Or, for built-in window functions |
| 967 | ** that do not use the standard function API, generate the required |
| 968 | ** inline VM code. |
| 969 | ** |
| 970 | ** If argument csr is greater than or equal to 0, then argument reg is |
| 971 | ** the first register in an array of registers guaranteed to be large |
| 972 | ** enough to hold the array of arguments for each function. In this case |
| 973 | ** the arguments are extracted from the current row of csr into the |
| 974 | ** array of registers before invoking OP_AggStep. |
| 975 | ** |
| 976 | ** Or, if csr is less than zero, then the array of registers at reg is |
| 977 | ** already populated with all columns from the current row of the sub-query. |
| 978 | ** |
| 979 | ** If argument regPartSize is non-zero, then it is a register containing the |
| 980 | ** number of rows in the current partition. |
dan | c9a8668 | 2018-05-30 20:44:58 +0000 | [diff] [blame] | 981 | */ |
dan | 31f5639 | 2018-05-24 21:10:57 +0000 | [diff] [blame] | 982 | static void windowAggStep( |
| 983 | Parse *pParse, |
dan | 13078ca | 2018-06-13 20:29:38 +0000 | [diff] [blame] | 984 | Window *pMWin, /* Linked list of window functions */ |
| 985 | int csr, /* Read arguments from this cursor */ |
| 986 | int bInverse, /* True to invoke xInverse instead of xStep */ |
| 987 | int reg, /* Array of registers */ |
dan | dfa552f | 2018-06-02 21:04:28 +0000 | [diff] [blame] | 988 | int regPartSize /* Register containing size of partition */ |
dan | 31f5639 | 2018-05-24 21:10:57 +0000 | [diff] [blame] | 989 | ){ |
| 990 | Vdbe *v = sqlite3GetVdbe(pParse); |
| 991 | Window *pWin; |
| 992 | for(pWin=pMWin; pWin; pWin=pWin->pNextWin){ |
dan | dfa552f | 2018-06-02 21:04:28 +0000 | [diff] [blame] | 993 | int flags = pWin->pFunc->funcFlags; |
dan | c9a8668 | 2018-05-30 20:44:58 +0000 | [diff] [blame] | 994 | int regArg; |
dan | 2a11bb2 | 2018-06-11 20:50:25 +0000 | [diff] [blame] | 995 | int nArg = windowArgCount(pWin); |
dan | dfa552f | 2018-06-02 21:04:28 +0000 | [diff] [blame] | 996 | |
dan | 6bc5c9e | 2018-06-04 18:55:11 +0000 | [diff] [blame] | 997 | if( csr>=0 ){ |
dan | c9a8668 | 2018-05-30 20:44:58 +0000 | [diff] [blame] | 998 | int i; |
dan | 2a11bb2 | 2018-06-11 20:50:25 +0000 | [diff] [blame] | 999 | for(i=0; i<nArg; i++){ |
dan | c9a8668 | 2018-05-30 20:44:58 +0000 | [diff] [blame] | 1000 | sqlite3VdbeAddOp3(v, OP_Column, csr, pWin->iArgCol+i, reg+i); |
| 1001 | } |
| 1002 | regArg = reg; |
dan | 6bc5c9e | 2018-06-04 18:55:11 +0000 | [diff] [blame] | 1003 | if( flags & SQLITE_FUNC_WINDOW_SIZE ){ |
| 1004 | if( nArg==0 ){ |
| 1005 | regArg = regPartSize; |
| 1006 | }else{ |
| 1007 | sqlite3VdbeAddOp2(v, OP_SCopy, regPartSize, reg+nArg); |
| 1008 | } |
| 1009 | nArg++; |
| 1010 | } |
dan | c9a8668 | 2018-05-30 20:44:58 +0000 | [diff] [blame] | 1011 | }else{ |
dan | 6bc5c9e | 2018-06-04 18:55:11 +0000 | [diff] [blame] | 1012 | assert( !(flags & SQLITE_FUNC_WINDOW_SIZE) ); |
dan | c9a8668 | 2018-05-30 20:44:58 +0000 | [diff] [blame] | 1013 | regArg = reg + pWin->iArgCol; |
dan | 31f5639 | 2018-05-24 21:10:57 +0000 | [diff] [blame] | 1014 | } |
dan | c9a8668 | 2018-05-30 20:44:58 +0000 | [diff] [blame] | 1015 | |
dan | ec891fd | 2018-06-06 20:51:02 +0000 | [diff] [blame] | 1016 | if( (pWin->pFunc->funcFlags & SQLITE_FUNC_MINMAX) |
| 1017 | && pWin->eStart!=TK_UNBOUNDED |
| 1018 | ){ |
dan | c9a8668 | 2018-05-30 20:44:58 +0000 | [diff] [blame] | 1019 | if( bInverse==0 ){ |
| 1020 | sqlite3VdbeAddOp2(v, OP_AddImm, pWin->regApp+1, 1); |
| 1021 | sqlite3VdbeAddOp2(v, OP_SCopy, regArg, pWin->regApp); |
| 1022 | sqlite3VdbeAddOp3(v, OP_MakeRecord, pWin->regApp, 2, pWin->regApp+2); |
| 1023 | sqlite3VdbeAddOp2(v, OP_IdxInsert, pWin->csrApp, pWin->regApp+2); |
| 1024 | }else{ |
| 1025 | sqlite3VdbeAddOp4Int(v, OP_SeekGE, pWin->csrApp, 0, regArg, 1); |
| 1026 | sqlite3VdbeAddOp1(v, OP_Delete, pWin->csrApp); |
| 1027 | sqlite3VdbeJumpHere(v, sqlite3VdbeCurrentAddr(v)-2); |
| 1028 | } |
dan | ec891fd | 2018-06-06 20:51:02 +0000 | [diff] [blame] | 1029 | }else if( pWin->regApp ){ |
dan | 7095c00 | 2018-06-07 17:45:22 +0000 | [diff] [blame] | 1030 | assert( pWin->pFunc->xSFunc==nth_valueStepFunc |
| 1031 | || pWin->pFunc->xSFunc==first_valueStepFunc |
| 1032 | ); |
dan | ec891fd | 2018-06-06 20:51:02 +0000 | [diff] [blame] | 1033 | assert( bInverse==0 || bInverse==1 ); |
| 1034 | sqlite3VdbeAddOp2(v, OP_AddImm, pWin->regApp+1-bInverse, 1); |
dan | fe4e25a | 2018-06-07 20:08:59 +0000 | [diff] [blame] | 1035 | }else if( pWin->pFunc->xSFunc==leadStepFunc |
| 1036 | || pWin->pFunc->xSFunc==lagStepFunc |
| 1037 | ){ |
| 1038 | /* no-op */ |
dan | c9a8668 | 2018-05-30 20:44:58 +0000 | [diff] [blame] | 1039 | }else{ |
dan | 8b98560 | 2018-06-09 17:43:45 +0000 | [diff] [blame] | 1040 | int addrIf = 0; |
| 1041 | if( pWin->pFilter ){ |
| 1042 | int regTmp; |
dan | 2a11bb2 | 2018-06-11 20:50:25 +0000 | [diff] [blame] | 1043 | assert( nArg==pWin->pOwner->x.pList->nExpr ); |
dan | 8b98560 | 2018-06-09 17:43:45 +0000 | [diff] [blame] | 1044 | if( csr>0 ){ |
| 1045 | regTmp = sqlite3GetTempReg(pParse); |
dan | 2a11bb2 | 2018-06-11 20:50:25 +0000 | [diff] [blame] | 1046 | sqlite3VdbeAddOp3(v, OP_Column, csr, pWin->iArgCol+nArg,regTmp); |
dan | 8b98560 | 2018-06-09 17:43:45 +0000 | [diff] [blame] | 1047 | }else{ |
dan | 2a11bb2 | 2018-06-11 20:50:25 +0000 | [diff] [blame] | 1048 | regTmp = regArg + nArg; |
dan | 8b98560 | 2018-06-09 17:43:45 +0000 | [diff] [blame] | 1049 | } |
| 1050 | addrIf = sqlite3VdbeAddOp3(v, OP_IfNot, regTmp, 0, 1); |
| 1051 | if( csr>0 ){ |
| 1052 | sqlite3ReleaseTempReg(pParse, regTmp); |
| 1053 | } |
| 1054 | } |
dan | c9a8668 | 2018-05-30 20:44:58 +0000 | [diff] [blame] | 1055 | if( pWin->pFunc->funcFlags & SQLITE_FUNC_NEEDCOLL ){ |
| 1056 | CollSeq *pColl; |
dan | 8b98560 | 2018-06-09 17:43:45 +0000 | [diff] [blame] | 1057 | pColl = sqlite3ExprNNCollSeq(pParse, pWin->pOwner->x.pList->a[0].pExpr); |
dan | c9a8668 | 2018-05-30 20:44:58 +0000 | [diff] [blame] | 1058 | sqlite3VdbeAddOp4(v, OP_CollSeq, 0,0,0, (const char*)pColl, P4_COLLSEQ); |
| 1059 | } |
| 1060 | sqlite3VdbeAddOp3(v, OP_AggStep0, bInverse, regArg, pWin->regAccum); |
| 1061 | sqlite3VdbeAppendP4(v, pWin->pFunc, P4_FUNCDEF); |
dan | dfa552f | 2018-06-02 21:04:28 +0000 | [diff] [blame] | 1062 | sqlite3VdbeChangeP5(v, (u8)nArg); |
dan | 8b98560 | 2018-06-09 17:43:45 +0000 | [diff] [blame] | 1063 | if( addrIf ) sqlite3VdbeJumpHere(v, addrIf); |
dan | c9a8668 | 2018-05-30 20:44:58 +0000 | [diff] [blame] | 1064 | } |
dan | 31f5639 | 2018-05-24 21:10:57 +0000 | [diff] [blame] | 1065 | } |
| 1066 | } |
| 1067 | |
dan | 13078ca | 2018-06-13 20:29:38 +0000 | [diff] [blame] | 1068 | /* |
| 1069 | ** Generate VM code to invoke either xValue() (bFinal==0) or xFinalize() |
| 1070 | ** (bFinal==1) for each window function in the linked list starting at |
| 1071 | ** pMWin. Or, for built-in window-functions that do not use the standard |
| 1072 | ** API, generate the equivalent VM code. |
| 1073 | */ |
dan | d6f784e | 2018-05-28 18:30:45 +0000 | [diff] [blame] | 1074 | static void windowAggFinal(Parse *pParse, Window *pMWin, int bFinal){ |
| 1075 | Vdbe *v = sqlite3GetVdbe(pParse); |
| 1076 | Window *pWin; |
| 1077 | |
| 1078 | for(pWin=pMWin; pWin; pWin=pWin->pNextWin){ |
dan | ec891fd | 2018-06-06 20:51:02 +0000 | [diff] [blame] | 1079 | if( (pWin->pFunc->funcFlags & SQLITE_FUNC_MINMAX) |
| 1080 | && pWin->eStart!=TK_UNBOUNDED |
| 1081 | ){ |
dan | d6f784e | 2018-05-28 18:30:45 +0000 | [diff] [blame] | 1082 | sqlite3VdbeAddOp2(v, OP_Null, 0, pWin->regResult); |
dan | c9a8668 | 2018-05-30 20:44:58 +0000 | [diff] [blame] | 1083 | sqlite3VdbeAddOp1(v, OP_Last, pWin->csrApp); |
| 1084 | sqlite3VdbeAddOp3(v, OP_Column, pWin->csrApp, 0, pWin->regResult); |
| 1085 | sqlite3VdbeJumpHere(v, sqlite3VdbeCurrentAddr(v)-2); |
| 1086 | if( bFinal ){ |
| 1087 | sqlite3VdbeAddOp1(v, OP_ResetSorter, pWin->csrApp); |
| 1088 | } |
dan | ec891fd | 2018-06-06 20:51:02 +0000 | [diff] [blame] | 1089 | }else if( pWin->regApp ){ |
dan | d6f784e | 2018-05-28 18:30:45 +0000 | [diff] [blame] | 1090 | }else{ |
dan | c9a8668 | 2018-05-30 20:44:58 +0000 | [diff] [blame] | 1091 | if( bFinal==0 ){ |
| 1092 | sqlite3VdbeAddOp2(v, OP_Null, 0, pWin->regResult); |
| 1093 | } |
dan | 2a11bb2 | 2018-06-11 20:50:25 +0000 | [diff] [blame] | 1094 | sqlite3VdbeAddOp2(v, OP_AggFinal, pWin->regAccum, windowArgCount(pWin)); |
dan | c9a8668 | 2018-05-30 20:44:58 +0000 | [diff] [blame] | 1095 | sqlite3VdbeAppendP4(v, pWin->pFunc, P4_FUNCDEF); |
| 1096 | if( bFinal ){ |
| 1097 | sqlite3VdbeAddOp2(v, OP_Copy, pWin->regAccum, pWin->regResult); |
dan | 8b98560 | 2018-06-09 17:43:45 +0000 | [diff] [blame] | 1098 | sqlite3VdbeAddOp2(v, OP_Null, 0, pWin->regAccum); |
dan | c9a8668 | 2018-05-30 20:44:58 +0000 | [diff] [blame] | 1099 | }else{ |
| 1100 | sqlite3VdbeChangeP3(v, -1, pWin->regResult); |
| 1101 | } |
dan | d6f784e | 2018-05-28 18:30:45 +0000 | [diff] [blame] | 1102 | } |
| 1103 | } |
| 1104 | } |
| 1105 | |
dan | 13078ca | 2018-06-13 20:29:38 +0000 | [diff] [blame] | 1106 | /* |
| 1107 | ** This function generates VM code to invoke the sub-routine at address |
| 1108 | ** lblFlushPart once for each partition with the entire partition cached in |
| 1109 | ** the Window.iEphCsr temp table. |
| 1110 | */ |
dan | f690b57 | 2018-06-01 21:00:08 +0000 | [diff] [blame] | 1111 | static void windowPartitionCache( |
| 1112 | Parse *pParse, |
dan | 13078ca | 2018-06-13 20:29:38 +0000 | [diff] [blame] | 1113 | Select *p, /* The rewritten SELECT statement */ |
| 1114 | WhereInfo *pWInfo, /* WhereInfo to call WhereEnd() on */ |
| 1115 | int regFlushPart, /* Register to use with Gosub lblFlushPart */ |
| 1116 | int lblFlushPart, /* Subroutine to Gosub to */ |
| 1117 | int *pRegSize /* OUT: Register containing partition size */ |
dan | f690b57 | 2018-06-01 21:00:08 +0000 | [diff] [blame] | 1118 | ){ |
| 1119 | Window *pMWin = p->pWin; |
| 1120 | Vdbe *v = sqlite3GetVdbe(pParse); |
dan | f690b57 | 2018-06-01 21:00:08 +0000 | [diff] [blame] | 1121 | int iSubCsr = p->pSrc->a[0].iCursor; |
| 1122 | int nSub = p->pSrc->a[0].pTab->nCol; |
| 1123 | int k; |
| 1124 | |
| 1125 | int reg = pParse->nMem+1; |
| 1126 | int regRecord = reg+nSub; |
| 1127 | int regRowid = regRecord+1; |
| 1128 | |
dan | dfa552f | 2018-06-02 21:04:28 +0000 | [diff] [blame] | 1129 | *pRegSize = regRowid; |
dan | f690b57 | 2018-06-01 21:00:08 +0000 | [diff] [blame] | 1130 | pParse->nMem += nSub + 2; |
| 1131 | |
| 1132 | /* Martial the row returned by the sub-select into an array of |
| 1133 | ** registers. */ |
| 1134 | for(k=0; k<nSub; k++){ |
| 1135 | sqlite3VdbeAddOp3(v, OP_Column, iSubCsr, k, reg+k); |
| 1136 | } |
| 1137 | sqlite3VdbeAddOp3(v, OP_MakeRecord, reg, nSub, regRecord); |
| 1138 | |
| 1139 | /* Check if this is the start of a new partition. If so, call the |
| 1140 | ** flush_partition sub-routine. */ |
| 1141 | if( pMWin->pPartition ){ |
| 1142 | int addr; |
| 1143 | ExprList *pPart = pMWin->pPartition; |
dan | e0a5e20 | 2018-06-15 16:10:44 +0000 | [diff] [blame] | 1144 | int nPart = pPart->nExpr; |
dan | f690b57 | 2018-06-01 21:00:08 +0000 | [diff] [blame] | 1145 | int regNewPart = reg + pMWin->nBufferCol; |
| 1146 | KeyInfo *pKeyInfo = sqlite3KeyInfoFromExprList(pParse, pPart, 0, 0); |
| 1147 | |
| 1148 | addr = sqlite3VdbeAddOp3(v, OP_Compare, regNewPart, pMWin->regPart,nPart); |
| 1149 | sqlite3VdbeAppendP4(v, (void*)pKeyInfo, P4_KEYINFO); |
| 1150 | sqlite3VdbeAddOp3(v, OP_Jump, addr+2, addr+4, addr+2); |
| 1151 | sqlite3VdbeAddOp3(v, OP_Copy, regNewPart, pMWin->regPart, nPart-1); |
| 1152 | sqlite3VdbeAddOp2(v, OP_Gosub, regFlushPart, lblFlushPart); |
| 1153 | } |
| 1154 | |
| 1155 | /* Buffer the current row in the ephemeral table. */ |
| 1156 | sqlite3VdbeAddOp2(v, OP_NewRowid, pMWin->iEphCsr, regRowid); |
| 1157 | sqlite3VdbeAddOp3(v, OP_Insert, pMWin->iEphCsr, regRecord, regRowid); |
| 1158 | |
| 1159 | /* End of the input loop */ |
| 1160 | sqlite3WhereEnd(pWInfo); |
| 1161 | |
| 1162 | /* Invoke "flush_partition" to deal with the final (or only) partition */ |
| 1163 | sqlite3VdbeAddOp2(v, OP_Gosub, regFlushPart, lblFlushPart); |
| 1164 | } |
dan | d6f784e | 2018-05-28 18:30:45 +0000 | [diff] [blame] | 1165 | |
dan | 13078ca | 2018-06-13 20:29:38 +0000 | [diff] [blame] | 1166 | /* |
| 1167 | ** Invoke the sub-routine at regGosub (generated by code in select.c) to |
| 1168 | ** return the current row of Window.iEphCsr. If all window functions are |
| 1169 | ** aggregate window functions that use the standard API, a single |
| 1170 | ** OP_Gosub instruction is all that this routine generates. Extra VM code |
| 1171 | ** for per-row processing is only generated for the following built-in window |
| 1172 | ** functions: |
| 1173 | ** |
| 1174 | ** nth_value() |
| 1175 | ** first_value() |
| 1176 | ** lag() |
| 1177 | ** lead() |
| 1178 | */ |
dan | ec891fd | 2018-06-06 20:51:02 +0000 | [diff] [blame] | 1179 | static void windowReturnOneRow( |
| 1180 | Parse *pParse, |
| 1181 | Window *pMWin, |
| 1182 | int regGosub, |
| 1183 | int addrGosub |
| 1184 | ){ |
| 1185 | Vdbe *v = sqlite3GetVdbe(pParse); |
| 1186 | Window *pWin; |
| 1187 | for(pWin=pMWin; pWin; pWin=pWin->pNextWin){ |
| 1188 | FuncDef *pFunc = pWin->pFunc; |
dan | 7095c00 | 2018-06-07 17:45:22 +0000 | [diff] [blame] | 1189 | if( pFunc->xSFunc==nth_valueStepFunc |
| 1190 | || pFunc->xSFunc==first_valueStepFunc |
| 1191 | ){ |
dan | ec891fd | 2018-06-06 20:51:02 +0000 | [diff] [blame] | 1192 | int csr = pWin->csrApp; |
| 1193 | int lbl = sqlite3VdbeMakeLabel(v); |
| 1194 | int tmpReg = sqlite3GetTempReg(pParse); |
| 1195 | sqlite3VdbeAddOp2(v, OP_Null, 0, pWin->regResult); |
dan | 7095c00 | 2018-06-07 17:45:22 +0000 | [diff] [blame] | 1196 | |
| 1197 | if( pFunc->xSFunc==nth_valueStepFunc ){ |
dan | 6fde179 | 2018-06-15 19:01:35 +0000 | [diff] [blame] | 1198 | sqlite3VdbeAddOp3(v, OP_Column, pMWin->iEphCsr, pWin->iArgCol+1,tmpReg); |
dan | 7095c00 | 2018-06-07 17:45:22 +0000 | [diff] [blame] | 1199 | }else{ |
| 1200 | sqlite3VdbeAddOp2(v, OP_Integer, 1, tmpReg); |
| 1201 | } |
dan | ec891fd | 2018-06-06 20:51:02 +0000 | [diff] [blame] | 1202 | sqlite3VdbeAddOp3(v, OP_Add, tmpReg, pWin->regApp, tmpReg); |
| 1203 | sqlite3VdbeAddOp3(v, OP_Gt, pWin->regApp+1, lbl, tmpReg); |
| 1204 | sqlite3VdbeAddOp3(v, OP_SeekRowid, csr, lbl, tmpReg); |
| 1205 | sqlite3VdbeAddOp3(v, OP_Column, csr, pWin->iArgCol, pWin->regResult); |
| 1206 | sqlite3VdbeResolveLabel(v, lbl); |
| 1207 | sqlite3ReleaseTempReg(pParse, tmpReg); |
| 1208 | } |
dan | fe4e25a | 2018-06-07 20:08:59 +0000 | [diff] [blame] | 1209 | else if( pFunc->xSFunc==leadStepFunc || pFunc->xSFunc==lagStepFunc ){ |
dan | 2a11bb2 | 2018-06-11 20:50:25 +0000 | [diff] [blame] | 1210 | int nArg = pWin->pOwner->x.pList->nExpr; |
dan | e0a5e20 | 2018-06-15 16:10:44 +0000 | [diff] [blame] | 1211 | int iEph = pMWin->iEphCsr; |
dan | fe4e25a | 2018-06-07 20:08:59 +0000 | [diff] [blame] | 1212 | int csr = pWin->csrApp; |
| 1213 | int lbl = sqlite3VdbeMakeLabel(v); |
| 1214 | int tmpReg = sqlite3GetTempReg(pParse); |
| 1215 | |
dan | 2a11bb2 | 2018-06-11 20:50:25 +0000 | [diff] [blame] | 1216 | if( nArg<3 ){ |
dan | fe4e25a | 2018-06-07 20:08:59 +0000 | [diff] [blame] | 1217 | sqlite3VdbeAddOp2(v, OP_Null, 0, pWin->regResult); |
| 1218 | }else{ |
| 1219 | sqlite3VdbeAddOp3(v, OP_Column, iEph, pWin->iArgCol+2, pWin->regResult); |
| 1220 | } |
| 1221 | sqlite3VdbeAddOp2(v, OP_Rowid, iEph, tmpReg); |
dan | 2a11bb2 | 2018-06-11 20:50:25 +0000 | [diff] [blame] | 1222 | if( nArg<2 ){ |
dan | fe4e25a | 2018-06-07 20:08:59 +0000 | [diff] [blame] | 1223 | int val = (pFunc->xSFunc==leadStepFunc ? 1 : -1); |
| 1224 | sqlite3VdbeAddOp2(v, OP_AddImm, tmpReg, val); |
| 1225 | }else{ |
| 1226 | int op = (pFunc->xSFunc==leadStepFunc ? OP_Add : OP_Subtract); |
| 1227 | int tmpReg2 = sqlite3GetTempReg(pParse); |
| 1228 | sqlite3VdbeAddOp3(v, OP_Column, iEph, pWin->iArgCol+1, tmpReg2); |
| 1229 | sqlite3VdbeAddOp3(v, op, tmpReg2, tmpReg, tmpReg); |
| 1230 | sqlite3ReleaseTempReg(pParse, tmpReg2); |
| 1231 | } |
| 1232 | |
| 1233 | sqlite3VdbeAddOp3(v, OP_SeekRowid, csr, lbl, tmpReg); |
| 1234 | sqlite3VdbeAddOp3(v, OP_Column, csr, pWin->iArgCol, pWin->regResult); |
| 1235 | sqlite3VdbeResolveLabel(v, lbl); |
| 1236 | sqlite3ReleaseTempReg(pParse, tmpReg); |
| 1237 | } |
dan | ec891fd | 2018-06-06 20:51:02 +0000 | [diff] [blame] | 1238 | } |
| 1239 | sqlite3VdbeAddOp2(v, OP_Gosub, regGosub, addrGosub); |
| 1240 | } |
| 1241 | |
dan | 13078ca | 2018-06-13 20:29:38 +0000 | [diff] [blame] | 1242 | /* |
| 1243 | ** Invoke the code generated by windowReturnOneRow() and, optionally, the |
| 1244 | ** xInverse() function for each window function, for one or more rows |
| 1245 | ** from the Window.iEphCsr temp table. This routine generates VM code |
| 1246 | ** similar to: |
| 1247 | ** |
| 1248 | ** while( regCtr>0 ){ |
| 1249 | ** regCtr--; |
| 1250 | ** windowReturnOneRow() |
| 1251 | ** if( bInverse ){ |
| 1252 | ** AggStep (xInverse) |
| 1253 | ** } |
| 1254 | ** Next (Window.iEphCsr) |
| 1255 | ** } |
| 1256 | */ |
dan | ec891fd | 2018-06-06 20:51:02 +0000 | [diff] [blame] | 1257 | static void windowReturnRows( |
| 1258 | Parse *pParse, |
dan | 13078ca | 2018-06-13 20:29:38 +0000 | [diff] [blame] | 1259 | Window *pMWin, /* List of window functions */ |
| 1260 | int regCtr, /* Register containing number of rows */ |
| 1261 | int regGosub, /* Register for Gosub addrGosub */ |
| 1262 | int addrGosub, /* Address of sub-routine for ReturnOneRow */ |
| 1263 | int regInvArg, /* Array of registers for xInverse args */ |
| 1264 | int regInvSize /* Register containing size of partition */ |
dan | ec891fd | 2018-06-06 20:51:02 +0000 | [diff] [blame] | 1265 | ){ |
| 1266 | int addr; |
| 1267 | Vdbe *v = sqlite3GetVdbe(pParse); |
| 1268 | windowAggFinal(pParse, pMWin, 0); |
| 1269 | addr = sqlite3VdbeAddOp3(v, OP_IfPos, regCtr, sqlite3VdbeCurrentAddr(v)+2 ,1); |
| 1270 | sqlite3VdbeAddOp2(v, OP_Goto, 0, 0); |
| 1271 | windowReturnOneRow(pParse, pMWin, regGosub, addrGosub); |
| 1272 | if( regInvArg ){ |
| 1273 | windowAggStep(pParse, pMWin, pMWin->iEphCsr, 1, regInvArg, regInvSize); |
| 1274 | } |
| 1275 | sqlite3VdbeAddOp2(v, OP_Next, pMWin->iEphCsr, addr); |
| 1276 | sqlite3VdbeJumpHere(v, addr+1); /* The OP_Goto */ |
| 1277 | } |
| 1278 | |
dan | 54a9ab3 | 2018-06-14 14:27:05 +0000 | [diff] [blame] | 1279 | /* |
| 1280 | ** Generate code to set the accumulator register for each window function |
| 1281 | ** in the linked list passed as the second argument to NULL. And perform |
| 1282 | ** any equivalent initialization required by any built-in window functions |
| 1283 | ** in the list. |
| 1284 | */ |
dan | 2e60568 | 2018-06-07 15:54:26 +0000 | [diff] [blame] | 1285 | static int windowInitAccum(Parse *pParse, Window *pMWin){ |
| 1286 | Vdbe *v = sqlite3GetVdbe(pParse); |
| 1287 | int regArg; |
| 1288 | int nArg = 0; |
| 1289 | Window *pWin; |
| 1290 | for(pWin=pMWin; pWin; pWin=pWin->pNextWin){ |
dan | 9a94722 | 2018-06-14 19:06:36 +0000 | [diff] [blame] | 1291 | FuncDef *pFunc = pWin->pFunc; |
dan | 2e60568 | 2018-06-07 15:54:26 +0000 | [diff] [blame] | 1292 | sqlite3VdbeAddOp2(v, OP_Null, 0, pWin->regAccum); |
dan | 2a11bb2 | 2018-06-11 20:50:25 +0000 | [diff] [blame] | 1293 | nArg = MAX(nArg, windowArgCount(pWin)); |
dan | 9a94722 | 2018-06-14 19:06:36 +0000 | [diff] [blame] | 1294 | if( pFunc->xSFunc==nth_valueStepFunc |
| 1295 | || pFunc->xSFunc==first_valueStepFunc |
dan | 7095c00 | 2018-06-07 17:45:22 +0000 | [diff] [blame] | 1296 | ){ |
dan | 2e60568 | 2018-06-07 15:54:26 +0000 | [diff] [blame] | 1297 | sqlite3VdbeAddOp2(v, OP_Integer, 0, pWin->regApp); |
| 1298 | sqlite3VdbeAddOp2(v, OP_Integer, 0, pWin->regApp+1); |
| 1299 | } |
dan | 9a94722 | 2018-06-14 19:06:36 +0000 | [diff] [blame] | 1300 | |
| 1301 | if( (pFunc->funcFlags & SQLITE_FUNC_MINMAX) && pWin->csrApp ){ |
| 1302 | assert( pWin->eStart!=TK_UNBOUNDED ); |
| 1303 | sqlite3VdbeAddOp1(v, OP_ResetSorter, pWin->csrApp); |
| 1304 | sqlite3VdbeAddOp2(v, OP_Integer, 0, pWin->regApp+1); |
| 1305 | } |
dan | 2e60568 | 2018-06-07 15:54:26 +0000 | [diff] [blame] | 1306 | } |
| 1307 | regArg = pParse->nMem+1; |
| 1308 | pParse->nMem += nArg; |
| 1309 | return regArg; |
| 1310 | } |
| 1311 | |
| 1312 | |
dan | 99652dd | 2018-05-24 17:49:14 +0000 | [diff] [blame] | 1313 | /* |
dan | 54a9ab3 | 2018-06-14 14:27:05 +0000 | [diff] [blame] | 1314 | ** This function does the work of sqlite3WindowCodeStep() for all "ROWS" |
| 1315 | ** window frame types except for "BETWEEN UNBOUNDED PRECEDING AND CURRENT |
| 1316 | ** ROW". Pseudo-code for each follows. |
| 1317 | ** |
dan | 09590aa | 2018-05-25 20:30:17 +0000 | [diff] [blame] | 1318 | ** ROWS BETWEEN <expr1> PRECEDING AND <expr2> FOLLOWING |
dan | 09590aa | 2018-05-25 20:30:17 +0000 | [diff] [blame] | 1319 | ** |
| 1320 | ** ... |
| 1321 | ** if( new partition ){ |
| 1322 | ** Gosub flush_partition |
| 1323 | ** } |
| 1324 | ** Insert (record in eph-table) |
| 1325 | ** sqlite3WhereEnd() |
| 1326 | ** Gosub flush_partition |
| 1327 | ** |
| 1328 | ** flush_partition: |
| 1329 | ** Once { |
| 1330 | ** OpenDup (iEphCsr -> csrStart) |
| 1331 | ** OpenDup (iEphCsr -> csrEnd) |
dan | 99652dd | 2018-05-24 17:49:14 +0000 | [diff] [blame] | 1332 | ** } |
dan | 09590aa | 2018-05-25 20:30:17 +0000 | [diff] [blame] | 1333 | ** regStart = <expr1> // PRECEDING expression |
| 1334 | ** regEnd = <expr2> // FOLLOWING expression |
| 1335 | ** if( regStart<0 || regEnd<0 ){ error! } |
| 1336 | ** Rewind (csr,csrStart,csrEnd) // if EOF goto flush_partition_done |
| 1337 | ** Next(csrEnd) // if EOF skip Aggstep |
| 1338 | ** Aggstep (csrEnd) |
| 1339 | ** if( (regEnd--)<=0 ){ |
| 1340 | ** AggFinal (xValue) |
| 1341 | ** Gosub addrGosub |
| 1342 | ** Next(csr) // if EOF goto flush_partition_done |
| 1343 | ** if( (regStart--)<=0 ){ |
| 1344 | ** AggStep (csrStart, xInverse) |
| 1345 | ** Next(csrStart) |
| 1346 | ** } |
| 1347 | ** } |
| 1348 | ** flush_partition_done: |
| 1349 | ** ResetSorter (csr) |
| 1350 | ** Return |
dan | 99652dd | 2018-05-24 17:49:14 +0000 | [diff] [blame] | 1351 | ** |
dan | 09590aa | 2018-05-25 20:30:17 +0000 | [diff] [blame] | 1352 | ** ROWS BETWEEN <expr> PRECEDING AND CURRENT ROW |
| 1353 | ** ROWS BETWEEN CURRENT ROW AND <expr> FOLLOWING |
| 1354 | ** ROWS BETWEEN UNBOUNDED PRECEDING AND <expr> FOLLOWING |
| 1355 | ** |
| 1356 | ** These are similar to the above. For "CURRENT ROW", intialize the |
| 1357 | ** register to 0. For "UNBOUNDED PRECEDING" to infinity. |
| 1358 | ** |
| 1359 | ** ROWS BETWEEN <expr> PRECEDING AND UNBOUNDED FOLLOWING |
| 1360 | ** ROWS BETWEEN CURRENT ROW AND UNBOUNDED FOLLOWING |
| 1361 | ** |
| 1362 | ** Rewind (csr,csrStart,csrEnd) // if EOF goto flush_partition_done |
| 1363 | ** while( 1 ){ |
| 1364 | ** Next(csrEnd) // Exit while(1) at EOF |
| 1365 | ** Aggstep (csrEnd) |
| 1366 | ** } |
| 1367 | ** while( 1 ){ |
dan | 99652dd | 2018-05-24 17:49:14 +0000 | [diff] [blame] | 1368 | ** AggFinal (xValue) |
| 1369 | ** Gosub addrGosub |
dan | 09590aa | 2018-05-25 20:30:17 +0000 | [diff] [blame] | 1370 | ** Next(csr) // if EOF goto flush_partition_done |
dan | 31f5639 | 2018-05-24 21:10:57 +0000 | [diff] [blame] | 1371 | ** if( (regStart--)<=0 ){ |
| 1372 | ** AggStep (csrStart, xInverse) |
| 1373 | ** Next(csrStart) |
dan | 99652dd | 2018-05-24 17:49:14 +0000 | [diff] [blame] | 1374 | ** } |
| 1375 | ** } |
dan | 99652dd | 2018-05-24 17:49:14 +0000 | [diff] [blame] | 1376 | ** |
dan | 09590aa | 2018-05-25 20:30:17 +0000 | [diff] [blame] | 1377 | ** For the "CURRENT ROW AND UNBOUNDED FOLLOWING" case, the final if() |
| 1378 | ** condition is always true (as if regStart were initialized to 0). |
dan | 99652dd | 2018-05-24 17:49:14 +0000 | [diff] [blame] | 1379 | ** |
dan | 09590aa | 2018-05-25 20:30:17 +0000 | [diff] [blame] | 1380 | ** RANGE BETWEEN CURRENT ROW AND UNBOUNDED FOLLOWING |
| 1381 | ** |
| 1382 | ** This is the only RANGE case handled by this routine. It modifies the |
| 1383 | ** second while( 1 ) loop in "ROWS BETWEEN CURRENT ... UNBOUNDED..." to |
| 1384 | ** be: |
| 1385 | ** |
| 1386 | ** while( 1 ){ |
| 1387 | ** AggFinal (xValue) |
| 1388 | ** while( 1 ){ |
| 1389 | ** regPeer++ |
| 1390 | ** Gosub addrGosub |
| 1391 | ** Next(csr) // if EOF goto flush_partition_done |
| 1392 | ** if( new peer ) break; |
| 1393 | ** } |
| 1394 | ** while( (regPeer--)>0 ){ |
| 1395 | ** AggStep (csrStart, xInverse) |
| 1396 | ** Next(csrStart) |
| 1397 | ** } |
| 1398 | ** } |
dan | 99652dd | 2018-05-24 17:49:14 +0000 | [diff] [blame] | 1399 | ** |
dan | 31f5639 | 2018-05-24 21:10:57 +0000 | [diff] [blame] | 1400 | ** ROWS BETWEEN <expr> FOLLOWING AND <expr> FOLLOWING |
| 1401 | ** |
| 1402 | ** regEnd = regEnd - regStart |
| 1403 | ** Rewind (csr,csrStart,csrEnd) // if EOF goto flush_partition_done |
| 1404 | ** Aggstep (csrEnd) |
| 1405 | ** Next(csrEnd) // if EOF fall-through |
| 1406 | ** if( (regEnd--)<=0 ){ |
dan | 31f5639 | 2018-05-24 21:10:57 +0000 | [diff] [blame] | 1407 | ** if( (regStart--)<=0 ){ |
| 1408 | ** AggFinal (xValue) |
| 1409 | ** Gosub addrGosub |
| 1410 | ** Next(csr) // if EOF goto flush_partition_done |
| 1411 | ** } |
dan | e105dd7 | 2018-05-25 09:29:11 +0000 | [diff] [blame] | 1412 | ** AggStep (csrStart, xInverse) |
| 1413 | ** Next (csrStart) |
dan | 31f5639 | 2018-05-24 21:10:57 +0000 | [diff] [blame] | 1414 | ** } |
| 1415 | ** |
| 1416 | ** ROWS BETWEEN <expr> PRECEDING AND <expr> PRECEDING |
| 1417 | ** |
| 1418 | ** Replace the bit after "Rewind" in the above with: |
| 1419 | ** |
| 1420 | ** if( (regEnd--)<=0 ){ |
| 1421 | ** AggStep (csrEnd) |
| 1422 | ** Next (csrEnd) |
| 1423 | ** } |
| 1424 | ** AggFinal (xValue) |
| 1425 | ** Gosub addrGosub |
| 1426 | ** Next(csr) // if EOF goto flush_partition_done |
| 1427 | ** if( (regStart--)<=0 ){ |
| 1428 | ** AggStep (csr2, xInverse) |
| 1429 | ** Next (csr2) |
| 1430 | ** } |
| 1431 | ** |
dan | 99652dd | 2018-05-24 17:49:14 +0000 | [diff] [blame] | 1432 | */ |
dan | c3a20c1 | 2018-05-23 20:55:37 +0000 | [diff] [blame] | 1433 | static void windowCodeRowExprStep( |
| 1434 | Parse *pParse, |
| 1435 | Select *p, |
| 1436 | WhereInfo *pWInfo, |
| 1437 | int regGosub, |
| 1438 | int addrGosub |
| 1439 | ){ |
| 1440 | Window *pMWin = p->pWin; |
| 1441 | Vdbe *v = sqlite3GetVdbe(pParse); |
dan | c3a20c1 | 2018-05-23 20:55:37 +0000 | [diff] [blame] | 1442 | int regFlushPart; /* Register for "Gosub flush_partition" */ |
dan | 31f5639 | 2018-05-24 21:10:57 +0000 | [diff] [blame] | 1443 | int lblFlushPart; /* Label for "Gosub flush_partition" */ |
| 1444 | int lblFlushDone; /* Label for "Gosub flush_partition_done" */ |
dan | c3a20c1 | 2018-05-23 20:55:37 +0000 | [diff] [blame] | 1445 | |
dan | f690b57 | 2018-06-01 21:00:08 +0000 | [diff] [blame] | 1446 | int regArg; |
dan | c3a20c1 | 2018-05-23 20:55:37 +0000 | [diff] [blame] | 1447 | int addr; |
dan | 31f5639 | 2018-05-24 21:10:57 +0000 | [diff] [blame] | 1448 | int csrStart = pParse->nTab++; |
| 1449 | int csrEnd = pParse->nTab++; |
| 1450 | int regStart; /* Value of <expr> PRECEDING */ |
| 1451 | int regEnd; /* Value of <expr> FOLLOWING */ |
dan | c3a20c1 | 2018-05-23 20:55:37 +0000 | [diff] [blame] | 1452 | int addrGoto; |
dan | 31f5639 | 2018-05-24 21:10:57 +0000 | [diff] [blame] | 1453 | int addrTop; |
dan | c3a20c1 | 2018-05-23 20:55:37 +0000 | [diff] [blame] | 1454 | int addrIfPos1; |
| 1455 | int addrIfPos2; |
dan | dfa552f | 2018-06-02 21:04:28 +0000 | [diff] [blame] | 1456 | int regSize = 0; |
dan | 09590aa | 2018-05-25 20:30:17 +0000 | [diff] [blame] | 1457 | |
dan | 99652dd | 2018-05-24 17:49:14 +0000 | [diff] [blame] | 1458 | assert( pMWin->eStart==TK_PRECEDING |
| 1459 | || pMWin->eStart==TK_CURRENT |
dan | e105dd7 | 2018-05-25 09:29:11 +0000 | [diff] [blame] | 1460 | || pMWin->eStart==TK_FOLLOWING |
dan | 99652dd | 2018-05-24 17:49:14 +0000 | [diff] [blame] | 1461 | || pMWin->eStart==TK_UNBOUNDED |
| 1462 | ); |
| 1463 | assert( pMWin->eEnd==TK_FOLLOWING |
| 1464 | || pMWin->eEnd==TK_CURRENT |
| 1465 | || pMWin->eEnd==TK_UNBOUNDED |
dan | 31f5639 | 2018-05-24 21:10:57 +0000 | [diff] [blame] | 1466 | || pMWin->eEnd==TK_PRECEDING |
dan | 99652dd | 2018-05-24 17:49:14 +0000 | [diff] [blame] | 1467 | ); |
| 1468 | |
dan | c3a20c1 | 2018-05-23 20:55:37 +0000 | [diff] [blame] | 1469 | /* Allocate register and label for the "flush_partition" sub-routine. */ |
| 1470 | regFlushPart = ++pParse->nMem; |
dan | 31f5639 | 2018-05-24 21:10:57 +0000 | [diff] [blame] | 1471 | lblFlushPart = sqlite3VdbeMakeLabel(v); |
| 1472 | lblFlushDone = sqlite3VdbeMakeLabel(v); |
dan | c3a20c1 | 2018-05-23 20:55:37 +0000 | [diff] [blame] | 1473 | |
dan | 31f5639 | 2018-05-24 21:10:57 +0000 | [diff] [blame] | 1474 | regStart = ++pParse->nMem; |
| 1475 | regEnd = ++pParse->nMem; |
dan | c3a20c1 | 2018-05-23 20:55:37 +0000 | [diff] [blame] | 1476 | |
dan | dfa552f | 2018-06-02 21:04:28 +0000 | [diff] [blame] | 1477 | windowPartitionCache(pParse, p, pWInfo, regFlushPart, lblFlushPart, ®Size); |
dan | c3a20c1 | 2018-05-23 20:55:37 +0000 | [diff] [blame] | 1478 | |
dan | c3a20c1 | 2018-05-23 20:55:37 +0000 | [diff] [blame] | 1479 | addrGoto = sqlite3VdbeAddOp0(v, OP_Goto); |
| 1480 | |
dan | c9a8668 | 2018-05-30 20:44:58 +0000 | [diff] [blame] | 1481 | /* Start of "flush_partition" */ |
dan | 31f5639 | 2018-05-24 21:10:57 +0000 | [diff] [blame] | 1482 | sqlite3VdbeResolveLabel(v, lblFlushPart); |
dan | c3a20c1 | 2018-05-23 20:55:37 +0000 | [diff] [blame] | 1483 | sqlite3VdbeAddOp2(v, OP_Once, 0, sqlite3VdbeCurrentAddr(v)+3); |
dan | 31f5639 | 2018-05-24 21:10:57 +0000 | [diff] [blame] | 1484 | sqlite3VdbeAddOp2(v, OP_OpenDup, csrStart, pMWin->iEphCsr); |
| 1485 | sqlite3VdbeAddOp2(v, OP_OpenDup, csrEnd, pMWin->iEphCsr); |
dan | c3a20c1 | 2018-05-23 20:55:37 +0000 | [diff] [blame] | 1486 | |
dan | 31f5639 | 2018-05-24 21:10:57 +0000 | [diff] [blame] | 1487 | /* If either regStart or regEnd are not non-negative integers, throw |
dan | 99652dd | 2018-05-24 17:49:14 +0000 | [diff] [blame] | 1488 | ** an exception. */ |
| 1489 | if( pMWin->pStart ){ |
dan | 31f5639 | 2018-05-24 21:10:57 +0000 | [diff] [blame] | 1490 | sqlite3ExprCode(pParse, pMWin->pStart, regStart); |
| 1491 | windowCheckFrameValue(pParse, regStart, 0); |
dan | 99652dd | 2018-05-24 17:49:14 +0000 | [diff] [blame] | 1492 | } |
| 1493 | if( pMWin->pEnd ){ |
dan | 31f5639 | 2018-05-24 21:10:57 +0000 | [diff] [blame] | 1494 | sqlite3ExprCode(pParse, pMWin->pEnd, regEnd); |
| 1495 | windowCheckFrameValue(pParse, regEnd, 1); |
dan | 99652dd | 2018-05-24 17:49:14 +0000 | [diff] [blame] | 1496 | } |
dan | c3a20c1 | 2018-05-23 20:55:37 +0000 | [diff] [blame] | 1497 | |
dan | c9a8668 | 2018-05-30 20:44:58 +0000 | [diff] [blame] | 1498 | /* If this is "ROWS <expr1> FOLLOWING AND ROWS <expr2> FOLLOWING", do: |
| 1499 | ** |
dan | 26522d1 | 2018-06-11 18:16:51 +0000 | [diff] [blame] | 1500 | ** if( regEnd<regStart ){ |
| 1501 | ** // The frame always consists of 0 rows |
| 1502 | ** regStart = regSize; |
| 1503 | ** } |
dan | c9a8668 | 2018-05-30 20:44:58 +0000 | [diff] [blame] | 1504 | ** regEnd = regEnd - regStart; |
| 1505 | */ |
| 1506 | if( pMWin->pEnd && pMWin->pStart && pMWin->eStart==TK_FOLLOWING ){ |
| 1507 | assert( pMWin->eEnd==TK_FOLLOWING ); |
dan | 26522d1 | 2018-06-11 18:16:51 +0000 | [diff] [blame] | 1508 | sqlite3VdbeAddOp3(v, OP_Ge, regStart, sqlite3VdbeCurrentAddr(v)+2, regEnd); |
| 1509 | sqlite3VdbeAddOp2(v, OP_Copy, regSize, regStart); |
dan | c9a8668 | 2018-05-30 20:44:58 +0000 | [diff] [blame] | 1510 | sqlite3VdbeAddOp3(v, OP_Subtract, regStart, regEnd, regEnd); |
| 1511 | } |
| 1512 | |
dan | 26522d1 | 2018-06-11 18:16:51 +0000 | [diff] [blame] | 1513 | if( pMWin->pEnd && pMWin->pStart && pMWin->eEnd==TK_PRECEDING ){ |
| 1514 | assert( pMWin->eStart==TK_PRECEDING ); |
| 1515 | sqlite3VdbeAddOp3(v, OP_Le, regStart, sqlite3VdbeCurrentAddr(v)+3, regEnd); |
| 1516 | sqlite3VdbeAddOp2(v, OP_Copy, regSize, regStart); |
| 1517 | sqlite3VdbeAddOp2(v, OP_Copy, regSize, regEnd); |
| 1518 | } |
| 1519 | |
dan | c9a8668 | 2018-05-30 20:44:58 +0000 | [diff] [blame] | 1520 | /* Initialize the accumulator register for each window function to NULL */ |
dan | 2e60568 | 2018-06-07 15:54:26 +0000 | [diff] [blame] | 1521 | regArg = windowInitAccum(pParse, pMWin); |
dan | c3a20c1 | 2018-05-23 20:55:37 +0000 | [diff] [blame] | 1522 | |
dan | 31f5639 | 2018-05-24 21:10:57 +0000 | [diff] [blame] | 1523 | sqlite3VdbeAddOp2(v, OP_Rewind, pMWin->iEphCsr, lblFlushDone); |
| 1524 | sqlite3VdbeAddOp2(v, OP_Rewind, csrStart, lblFlushDone); |
dan | c3a20c1 | 2018-05-23 20:55:37 +0000 | [diff] [blame] | 1525 | sqlite3VdbeChangeP5(v, 1); |
dan | 31f5639 | 2018-05-24 21:10:57 +0000 | [diff] [blame] | 1526 | sqlite3VdbeAddOp2(v, OP_Rewind, csrEnd, lblFlushDone); |
dan | c3a20c1 | 2018-05-23 20:55:37 +0000 | [diff] [blame] | 1527 | sqlite3VdbeChangeP5(v, 1); |
| 1528 | |
| 1529 | /* Invoke AggStep function for each window function using the row that |
dan | 31f5639 | 2018-05-24 21:10:57 +0000 | [diff] [blame] | 1530 | ** csrEnd currently points to. Or, if csrEnd is already at EOF, |
dan | c3a20c1 | 2018-05-23 20:55:37 +0000 | [diff] [blame] | 1531 | ** do nothing. */ |
dan | 31f5639 | 2018-05-24 21:10:57 +0000 | [diff] [blame] | 1532 | addrTop = sqlite3VdbeCurrentAddr(v); |
| 1533 | if( pMWin->eEnd==TK_PRECEDING ){ |
| 1534 | addrIfPos1 = sqlite3VdbeAddOp3(v, OP_IfPos, regEnd, 0 , 1); |
dan | c3a20c1 | 2018-05-23 20:55:37 +0000 | [diff] [blame] | 1535 | } |
dan | 31f5639 | 2018-05-24 21:10:57 +0000 | [diff] [blame] | 1536 | sqlite3VdbeAddOp2(v, OP_Next, csrEnd, sqlite3VdbeCurrentAddr(v)+2); |
| 1537 | addr = sqlite3VdbeAddOp0(v, OP_Goto); |
dan | dfa552f | 2018-06-02 21:04:28 +0000 | [diff] [blame] | 1538 | windowAggStep(pParse, pMWin, csrEnd, 0, regArg, regSize); |
dan | 99652dd | 2018-05-24 17:49:14 +0000 | [diff] [blame] | 1539 | if( pMWin->eEnd==TK_UNBOUNDED ){ |
dan | 31f5639 | 2018-05-24 21:10:57 +0000 | [diff] [blame] | 1540 | sqlite3VdbeAddOp2(v, OP_Goto, 0, addrTop); |
| 1541 | sqlite3VdbeJumpHere(v, addr); |
| 1542 | addrTop = sqlite3VdbeCurrentAddr(v); |
dan | 99652dd | 2018-05-24 17:49:14 +0000 | [diff] [blame] | 1543 | }else{ |
dan | 31f5639 | 2018-05-24 21:10:57 +0000 | [diff] [blame] | 1544 | sqlite3VdbeJumpHere(v, addr); |
| 1545 | if( pMWin->eEnd==TK_PRECEDING ){ |
| 1546 | sqlite3VdbeJumpHere(v, addrIfPos1); |
| 1547 | } |
dan | 99652dd | 2018-05-24 17:49:14 +0000 | [diff] [blame] | 1548 | } |
dan | c3a20c1 | 2018-05-23 20:55:37 +0000 | [diff] [blame] | 1549 | |
dan | 99652dd | 2018-05-24 17:49:14 +0000 | [diff] [blame] | 1550 | if( pMWin->eEnd==TK_FOLLOWING ){ |
dan | 31f5639 | 2018-05-24 21:10:57 +0000 | [diff] [blame] | 1551 | addrIfPos1 = sqlite3VdbeAddOp3(v, OP_IfPos, regEnd, 0 , 1); |
dan | 99652dd | 2018-05-24 17:49:14 +0000 | [diff] [blame] | 1552 | } |
dan | e105dd7 | 2018-05-25 09:29:11 +0000 | [diff] [blame] | 1553 | if( pMWin->eStart==TK_FOLLOWING ){ |
| 1554 | addrIfPos2 = sqlite3VdbeAddOp3(v, OP_IfPos, regStart, 0 , 1); |
| 1555 | } |
dan | d6f784e | 2018-05-28 18:30:45 +0000 | [diff] [blame] | 1556 | windowAggFinal(pParse, pMWin, 0); |
dan | ec891fd | 2018-06-06 20:51:02 +0000 | [diff] [blame] | 1557 | windowReturnOneRow(pParse, pMWin, regGosub, addrGosub); |
dan | c3a20c1 | 2018-05-23 20:55:37 +0000 | [diff] [blame] | 1558 | sqlite3VdbeAddOp2(v, OP_Next, pMWin->iEphCsr, sqlite3VdbeCurrentAddr(v)+2); |
dan | 31f5639 | 2018-05-24 21:10:57 +0000 | [diff] [blame] | 1559 | sqlite3VdbeAddOp2(v, OP_Goto, 0, lblFlushDone); |
dan | e105dd7 | 2018-05-25 09:29:11 +0000 | [diff] [blame] | 1560 | if( pMWin->eStart==TK_FOLLOWING ){ |
| 1561 | sqlite3VdbeJumpHere(v, addrIfPos2); |
| 1562 | } |
dan | c3a20c1 | 2018-05-23 20:55:37 +0000 | [diff] [blame] | 1563 | |
dan | e105dd7 | 2018-05-25 09:29:11 +0000 | [diff] [blame] | 1564 | if( pMWin->eStart==TK_CURRENT |
| 1565 | || pMWin->eStart==TK_PRECEDING |
| 1566 | || pMWin->eStart==TK_FOLLOWING |
| 1567 | ){ |
dan | 09590aa | 2018-05-25 20:30:17 +0000 | [diff] [blame] | 1568 | int addrJumpHere = 0; |
dan | 99652dd | 2018-05-24 17:49:14 +0000 | [diff] [blame] | 1569 | if( pMWin->eStart==TK_PRECEDING ){ |
dan | 09590aa | 2018-05-25 20:30:17 +0000 | [diff] [blame] | 1570 | addrJumpHere = sqlite3VdbeAddOp3(v, OP_IfPos, regStart, 0 , 1); |
| 1571 | } |
dan | 31f5639 | 2018-05-24 21:10:57 +0000 | [diff] [blame] | 1572 | sqlite3VdbeAddOp2(v, OP_Next, csrStart, sqlite3VdbeCurrentAddr(v)+1); |
dan | dfa552f | 2018-06-02 21:04:28 +0000 | [diff] [blame] | 1573 | windowAggStep(pParse, pMWin, csrStart, 1, regArg, regSize); |
dan | 09590aa | 2018-05-25 20:30:17 +0000 | [diff] [blame] | 1574 | if( addrJumpHere ){ |
| 1575 | sqlite3VdbeJumpHere(v, addrJumpHere); |
dan | 99652dd | 2018-05-24 17:49:14 +0000 | [diff] [blame] | 1576 | } |
dan | c3a20c1 | 2018-05-23 20:55:37 +0000 | [diff] [blame] | 1577 | } |
dan | 99652dd | 2018-05-24 17:49:14 +0000 | [diff] [blame] | 1578 | if( pMWin->eEnd==TK_FOLLOWING ){ |
| 1579 | sqlite3VdbeJumpHere(v, addrIfPos1); |
| 1580 | } |
dan | 31f5639 | 2018-05-24 21:10:57 +0000 | [diff] [blame] | 1581 | sqlite3VdbeAddOp2(v, OP_Goto, 0, addrTop); |
dan | c3a20c1 | 2018-05-23 20:55:37 +0000 | [diff] [blame] | 1582 | |
| 1583 | /* flush_partition_done: */ |
dan | 31f5639 | 2018-05-24 21:10:57 +0000 | [diff] [blame] | 1584 | sqlite3VdbeResolveLabel(v, lblFlushDone); |
dan | c3a20c1 | 2018-05-23 20:55:37 +0000 | [diff] [blame] | 1585 | sqlite3VdbeAddOp1(v, OP_ResetSorter, pMWin->iEphCsr); |
| 1586 | sqlite3VdbeAddOp1(v, OP_Return, regFlushPart); |
| 1587 | |
| 1588 | /* Jump to here to skip over flush_partition */ |
| 1589 | sqlite3VdbeJumpHere(v, addrGoto); |
| 1590 | } |
| 1591 | |
dan | 79d4544 | 2018-05-26 21:17:29 +0000 | [diff] [blame] | 1592 | /* |
dan | 54a9ab3 | 2018-06-14 14:27:05 +0000 | [diff] [blame] | 1593 | ** This function does the work of sqlite3WindowCodeStep() for cases that |
| 1594 | ** would normally be handled by windowCodeDefaultStep() when there are |
| 1595 | ** one or more built-in window-functions that require the entire partition |
| 1596 | ** to be cached in a temp table before any rows can be returned. Additionally. |
| 1597 | ** "RANGE BETWEEN CURRENT ROW AND UNBOUNDED FOLLOWING" is always handled by |
| 1598 | ** this function. |
| 1599 | ** |
| 1600 | ** Pseudo-code corresponding to the VM code generated by this function |
| 1601 | ** for each type of window follows. |
| 1602 | ** |
dan | 79d4544 | 2018-05-26 21:17:29 +0000 | [diff] [blame] | 1603 | ** RANGE BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW |
| 1604 | ** |
dan | f690b57 | 2018-06-01 21:00:08 +0000 | [diff] [blame] | 1605 | ** flush_partition: |
| 1606 | ** Once { |
| 1607 | ** OpenDup (iEphCsr -> csrLead) |
| 1608 | ** } |
| 1609 | ** Integer ctr 0 |
| 1610 | ** foreach row (csrLead){ |
| 1611 | ** if( new peer ){ |
| 1612 | ** AggFinal (xValue) |
| 1613 | ** for(i=0; i<ctr; i++){ |
| 1614 | ** Gosub addrGosub |
| 1615 | ** Next iEphCsr |
| 1616 | ** } |
| 1617 | ** Integer ctr 0 |
| 1618 | ** } |
| 1619 | ** AggStep (csrLead) |
| 1620 | ** Incr ctr |
| 1621 | ** } |
| 1622 | ** |
| 1623 | ** AggFinal (xFinalize) |
| 1624 | ** for(i=0; i<ctr; i++){ |
| 1625 | ** Gosub addrGosub |
| 1626 | ** Next iEphCsr |
| 1627 | ** } |
| 1628 | ** |
| 1629 | ** ResetSorter (csr) |
| 1630 | ** Return |
| 1631 | ** |
| 1632 | ** ROWS BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW |
dan | 54a9ab3 | 2018-06-14 14:27:05 +0000 | [diff] [blame] | 1633 | ** |
| 1634 | ** As above, except that the "if( new peer )" branch is always taken. |
| 1635 | ** |
dan | f690b57 | 2018-06-01 21:00:08 +0000 | [diff] [blame] | 1636 | ** RANGE BETWEEN CURRENT ROW AND CURRENT ROW |
dan | 54a9ab3 | 2018-06-14 14:27:05 +0000 | [diff] [blame] | 1637 | ** |
| 1638 | ** As above, except that each of the for() loops becomes: |
| 1639 | ** |
| 1640 | ** for(i=0; i<ctr; i++){ |
| 1641 | ** Gosub addrGosub |
| 1642 | ** AggStep (xInverse, iEphCsr) |
| 1643 | ** Next iEphCsr |
| 1644 | ** } |
| 1645 | ** |
| 1646 | ** RANGE BETWEEN UNBOUNDED PRECEDING AND UNBOUNDED FOLLOWING |
| 1647 | ** |
| 1648 | ** flush_partition: |
| 1649 | ** Once { |
| 1650 | ** OpenDup (iEphCsr -> csrLead) |
| 1651 | ** } |
| 1652 | ** foreach row (csrLead) { |
| 1653 | ** AggStep (csrLead) |
| 1654 | ** } |
| 1655 | ** foreach row (iEphCsr) { |
| 1656 | ** Gosub addrGosub |
| 1657 | ** } |
| 1658 | ** |
| 1659 | ** RANGE BETWEEN CURRENT ROW AND UNBOUNDED FOLLOWING |
| 1660 | ** |
| 1661 | ** flush_partition: |
| 1662 | ** Once { |
| 1663 | ** OpenDup (iEphCsr -> csrLead) |
| 1664 | ** } |
| 1665 | ** foreach row (csrLead){ |
| 1666 | ** AggStep (csrLead) |
| 1667 | ** } |
| 1668 | ** Rewind (csrLead) |
| 1669 | ** Integer ctr 0 |
| 1670 | ** foreach row (csrLead){ |
| 1671 | ** if( new peer ){ |
| 1672 | ** AggFinal (xValue) |
| 1673 | ** for(i=0; i<ctr; i++){ |
| 1674 | ** Gosub addrGosub |
| 1675 | ** AggStep (xInverse, iEphCsr) |
| 1676 | ** Next iEphCsr |
| 1677 | ** } |
| 1678 | ** Integer ctr 0 |
| 1679 | ** } |
| 1680 | ** Incr ctr |
| 1681 | ** } |
| 1682 | ** |
| 1683 | ** AggFinal (xFinalize) |
| 1684 | ** for(i=0; i<ctr; i++){ |
| 1685 | ** Gosub addrGosub |
| 1686 | ** Next iEphCsr |
| 1687 | ** } |
| 1688 | ** |
| 1689 | ** ResetSorter (csr) |
| 1690 | ** Return |
dan | f690b57 | 2018-06-01 21:00:08 +0000 | [diff] [blame] | 1691 | */ |
| 1692 | static void windowCodeCacheStep( |
| 1693 | Parse *pParse, |
| 1694 | Select *p, |
| 1695 | WhereInfo *pWInfo, |
| 1696 | int regGosub, |
| 1697 | int addrGosub |
| 1698 | ){ |
| 1699 | Window *pMWin = p->pWin; |
| 1700 | Vdbe *v = sqlite3GetVdbe(pParse); |
dan | f690b57 | 2018-06-01 21:00:08 +0000 | [diff] [blame] | 1701 | int k; |
| 1702 | int addr; |
| 1703 | ExprList *pPart = pMWin->pPartition; |
| 1704 | ExprList *pOrderBy = pMWin->pOrderBy; |
dan | 54a9ab3 | 2018-06-14 14:27:05 +0000 | [diff] [blame] | 1705 | int nPeer = pOrderBy ? pOrderBy->nExpr : 0; |
dan | f690b57 | 2018-06-01 21:00:08 +0000 | [diff] [blame] | 1706 | int regNewPeer; |
| 1707 | |
| 1708 | int addrGoto; /* Address of Goto used to jump flush_par.. */ |
dan | 13078ca | 2018-06-13 20:29:38 +0000 | [diff] [blame] | 1709 | int addrNext; /* Jump here for next iteration of loop */ |
dan | f690b57 | 2018-06-01 21:00:08 +0000 | [diff] [blame] | 1710 | int regFlushPart; |
| 1711 | int lblFlushPart; |
| 1712 | int csrLead; |
| 1713 | int regCtr; |
| 1714 | int regArg; /* Register array to martial function args */ |
dan | dfa552f | 2018-06-02 21:04:28 +0000 | [diff] [blame] | 1715 | int regSize; |
dan | 13078ca | 2018-06-13 20:29:38 +0000 | [diff] [blame] | 1716 | int lblEmpty; |
dan | 54a9ab3 | 2018-06-14 14:27:05 +0000 | [diff] [blame] | 1717 | int bReverse = pMWin->pOrderBy && pMWin->eStart==TK_CURRENT |
| 1718 | && pMWin->eEnd==TK_UNBOUNDED; |
dan | f690b57 | 2018-06-01 21:00:08 +0000 | [diff] [blame] | 1719 | |
| 1720 | assert( (pMWin->eStart==TK_UNBOUNDED && pMWin->eEnd==TK_CURRENT) |
dan | ec891fd | 2018-06-06 20:51:02 +0000 | [diff] [blame] | 1721 | || (pMWin->eStart==TK_UNBOUNDED && pMWin->eEnd==TK_UNBOUNDED) |
| 1722 | || (pMWin->eStart==TK_CURRENT && pMWin->eEnd==TK_CURRENT) |
dan | 13078ca | 2018-06-13 20:29:38 +0000 | [diff] [blame] | 1723 | || (pMWin->eStart==TK_CURRENT && pMWin->eEnd==TK_UNBOUNDED) |
dan | f690b57 | 2018-06-01 21:00:08 +0000 | [diff] [blame] | 1724 | ); |
| 1725 | |
dan | 13078ca | 2018-06-13 20:29:38 +0000 | [diff] [blame] | 1726 | lblEmpty = sqlite3VdbeMakeLabel(v); |
dan | f690b57 | 2018-06-01 21:00:08 +0000 | [diff] [blame] | 1727 | regNewPeer = pParse->nMem+1; |
| 1728 | pParse->nMem += nPeer; |
| 1729 | |
| 1730 | /* Allocate register and label for the "flush_partition" sub-routine. */ |
| 1731 | regFlushPart = ++pParse->nMem; |
| 1732 | lblFlushPart = sqlite3VdbeMakeLabel(v); |
| 1733 | |
| 1734 | csrLead = pParse->nTab++; |
| 1735 | regCtr = ++pParse->nMem; |
| 1736 | |
dan | dfa552f | 2018-06-02 21:04:28 +0000 | [diff] [blame] | 1737 | windowPartitionCache(pParse, p, pWInfo, regFlushPart, lblFlushPart, ®Size); |
dan | f690b57 | 2018-06-01 21:00:08 +0000 | [diff] [blame] | 1738 | addrGoto = sqlite3VdbeAddOp0(v, OP_Goto); |
| 1739 | |
| 1740 | /* Start of "flush_partition" */ |
| 1741 | sqlite3VdbeResolveLabel(v, lblFlushPart); |
| 1742 | sqlite3VdbeAddOp2(v, OP_Once, 0, sqlite3VdbeCurrentAddr(v)+2); |
| 1743 | sqlite3VdbeAddOp2(v, OP_OpenDup, csrLead, pMWin->iEphCsr); |
| 1744 | |
| 1745 | /* Initialize the accumulator register for each window function to NULL */ |
dan | 2e60568 | 2018-06-07 15:54:26 +0000 | [diff] [blame] | 1746 | regArg = windowInitAccum(pParse, pMWin); |
dan | f690b57 | 2018-06-01 21:00:08 +0000 | [diff] [blame] | 1747 | |
| 1748 | sqlite3VdbeAddOp2(v, OP_Integer, 0, regCtr); |
dan | 13078ca | 2018-06-13 20:29:38 +0000 | [diff] [blame] | 1749 | sqlite3VdbeAddOp2(v, OP_Rewind, csrLead, lblEmpty); |
| 1750 | sqlite3VdbeAddOp2(v, OP_Rewind, pMWin->iEphCsr, lblEmpty); |
dan | f690b57 | 2018-06-01 21:00:08 +0000 | [diff] [blame] | 1751 | |
dan | 13078ca | 2018-06-13 20:29:38 +0000 | [diff] [blame] | 1752 | if( bReverse ){ |
| 1753 | int addr = sqlite3VdbeCurrentAddr(v); |
| 1754 | windowAggStep(pParse, pMWin, csrLead, 0, regArg, regSize); |
| 1755 | sqlite3VdbeAddOp2(v, OP_Next, csrLead, addr); |
| 1756 | sqlite3VdbeAddOp2(v, OP_Rewind, csrLead, lblEmpty); |
| 1757 | } |
| 1758 | addrNext = sqlite3VdbeCurrentAddr(v); |
| 1759 | |
| 1760 | if( pOrderBy && (pMWin->eEnd==TK_CURRENT || pMWin->eStart==TK_CURRENT) ){ |
| 1761 | int bCurrent = (pMWin->eStart==TK_CURRENT); |
dan | ec891fd | 2018-06-06 20:51:02 +0000 | [diff] [blame] | 1762 | int addrJump = 0; /* Address of OP_Jump below */ |
| 1763 | if( pMWin->eType==TK_RANGE ){ |
| 1764 | int iOff = pMWin->nBufferCol + (pPart ? pPart->nExpr : 0); |
| 1765 | int regPeer = pMWin->regPart + (pPart ? pPart->nExpr : 0); |
| 1766 | KeyInfo *pKeyInfo = sqlite3KeyInfoFromExprList(pParse, pOrderBy, 0, 0); |
| 1767 | for(k=0; k<nPeer; k++){ |
| 1768 | sqlite3VdbeAddOp3(v, OP_Column, csrLead, iOff+k, regNewPeer+k); |
| 1769 | } |
| 1770 | addr = sqlite3VdbeAddOp3(v, OP_Compare, regNewPeer, regPeer, nPeer); |
| 1771 | sqlite3VdbeAppendP4(v, (void*)pKeyInfo, P4_KEYINFO); |
| 1772 | addrJump = sqlite3VdbeAddOp3(v, OP_Jump, addr+2, 0, addr+2); |
| 1773 | sqlite3VdbeAddOp3(v, OP_Copy, regNewPeer, regPeer, nPeer-1); |
dan | f690b57 | 2018-06-01 21:00:08 +0000 | [diff] [blame] | 1774 | } |
| 1775 | |
dan | 13078ca | 2018-06-13 20:29:38 +0000 | [diff] [blame] | 1776 | windowReturnRows(pParse, pMWin, regCtr, regGosub, addrGosub, |
dan | ec891fd | 2018-06-06 20:51:02 +0000 | [diff] [blame] | 1777 | (bCurrent ? regArg : 0), (bCurrent ? regSize : 0) |
| 1778 | ); |
| 1779 | if( addrJump ) sqlite3VdbeJumpHere(v, addrJump); |
dan | f690b57 | 2018-06-01 21:00:08 +0000 | [diff] [blame] | 1780 | } |
| 1781 | |
dan | 13078ca | 2018-06-13 20:29:38 +0000 | [diff] [blame] | 1782 | if( bReverse==0 ){ |
| 1783 | windowAggStep(pParse, pMWin, csrLead, 0, regArg, regSize); |
| 1784 | } |
dan | f690b57 | 2018-06-01 21:00:08 +0000 | [diff] [blame] | 1785 | sqlite3VdbeAddOp2(v, OP_AddImm, regCtr, 1); |
dan | 13078ca | 2018-06-13 20:29:38 +0000 | [diff] [blame] | 1786 | sqlite3VdbeAddOp2(v, OP_Next, csrLead, addrNext); |
dan | f690b57 | 2018-06-01 21:00:08 +0000 | [diff] [blame] | 1787 | |
dan | 13078ca | 2018-06-13 20:29:38 +0000 | [diff] [blame] | 1788 | windowReturnRows(pParse, pMWin, regCtr, regGosub, addrGosub, 0, 0); |
dan | f690b57 | 2018-06-01 21:00:08 +0000 | [diff] [blame] | 1789 | |
dan | 13078ca | 2018-06-13 20:29:38 +0000 | [diff] [blame] | 1790 | sqlite3VdbeResolveLabel(v, lblEmpty); |
dan | f690b57 | 2018-06-01 21:00:08 +0000 | [diff] [blame] | 1791 | sqlite3VdbeAddOp1(v, OP_ResetSorter, pMWin->iEphCsr); |
| 1792 | sqlite3VdbeAddOp1(v, OP_Return, regFlushPart); |
| 1793 | |
| 1794 | /* Jump to here to skip over flush_partition */ |
| 1795 | sqlite3VdbeJumpHere(v, addrGoto); |
| 1796 | } |
| 1797 | |
| 1798 | |
| 1799 | /* |
| 1800 | ** RANGE BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW |
| 1801 | ** |
dan | 79d4544 | 2018-05-26 21:17:29 +0000 | [diff] [blame] | 1802 | ** ... |
| 1803 | ** if( new partition ){ |
| 1804 | ** AggFinal (xFinalize) |
| 1805 | ** Gosub addrGosub |
| 1806 | ** ResetSorter eph-table |
| 1807 | ** } |
| 1808 | ** else if( new peer ){ |
| 1809 | ** AggFinal (xValue) |
| 1810 | ** Gosub addrGosub |
| 1811 | ** ResetSorter eph-table |
| 1812 | ** } |
| 1813 | ** AggStep |
| 1814 | ** Insert (record into eph-table) |
| 1815 | ** sqlite3WhereEnd() |
| 1816 | ** AggFinal (xFinalize) |
| 1817 | ** Gosub addrGosub |
dan | f690b57 | 2018-06-01 21:00:08 +0000 | [diff] [blame] | 1818 | ** |
| 1819 | ** RANGE BETWEEN UNBOUNDED PRECEDING AND UNBOUNDED FOLLOWING |
| 1820 | ** |
| 1821 | ** As above, except take no action for a "new peer". Invoke |
| 1822 | ** the sub-routine once only for each partition. |
| 1823 | ** |
| 1824 | ** RANGE BETWEEN CURRENT ROW AND CURRENT ROW |
| 1825 | ** |
| 1826 | ** As above, except that the "new peer" condition is handled in the |
| 1827 | ** same way as "new partition" (so there is no "else if" block). |
| 1828 | ** |
| 1829 | ** ROWS BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW |
| 1830 | ** |
| 1831 | ** As above, except assume every row is a "new peer". |
dan | 79d4544 | 2018-05-26 21:17:29 +0000 | [diff] [blame] | 1832 | */ |
dan | c3a20c1 | 2018-05-23 20:55:37 +0000 | [diff] [blame] | 1833 | static void windowCodeDefaultStep( |
| 1834 | Parse *pParse, |
| 1835 | Select *p, |
| 1836 | WhereInfo *pWInfo, |
| 1837 | int regGosub, |
| 1838 | int addrGosub |
| 1839 | ){ |
| 1840 | Window *pMWin = p->pWin; |
| 1841 | Vdbe *v = sqlite3GetVdbe(pParse); |
dan | c3a20c1 | 2018-05-23 20:55:37 +0000 | [diff] [blame] | 1842 | int k; |
| 1843 | int iSubCsr = p->pSrc->a[0].iCursor; |
| 1844 | int nSub = p->pSrc->a[0].pTab->nCol; |
| 1845 | int reg = pParse->nMem+1; |
| 1846 | int regRecord = reg+nSub; |
| 1847 | int regRowid = regRecord+1; |
| 1848 | int addr; |
dan | d6f784e | 2018-05-28 18:30:45 +0000 | [diff] [blame] | 1849 | ExprList *pPart = pMWin->pPartition; |
| 1850 | ExprList *pOrderBy = pMWin->pOrderBy; |
dan | c3a20c1 | 2018-05-23 20:55:37 +0000 | [diff] [blame] | 1851 | |
dan | 79d4544 | 2018-05-26 21:17:29 +0000 | [diff] [blame] | 1852 | assert( pMWin->eType==TK_RANGE |
| 1853 | || (pMWin->eStart==TK_UNBOUNDED && pMWin->eEnd==TK_CURRENT) |
| 1854 | ); |
| 1855 | |
dan | d6f784e | 2018-05-28 18:30:45 +0000 | [diff] [blame] | 1856 | assert( (pMWin->eStart==TK_UNBOUNDED && pMWin->eEnd==TK_CURRENT) |
| 1857 | || (pMWin->eStart==TK_UNBOUNDED && pMWin->eEnd==TK_UNBOUNDED) |
| 1858 | || (pMWin->eStart==TK_CURRENT && pMWin->eEnd==TK_CURRENT) |
| 1859 | || (pMWin->eStart==TK_CURRENT && pMWin->eEnd==TK_UNBOUNDED && !pOrderBy) |
| 1860 | ); |
| 1861 | |
| 1862 | if( pMWin->eEnd==TK_UNBOUNDED ){ |
| 1863 | pOrderBy = 0; |
| 1864 | } |
| 1865 | |
dan | c3a20c1 | 2018-05-23 20:55:37 +0000 | [diff] [blame] | 1866 | pParse->nMem += nSub + 2; |
| 1867 | |
| 1868 | /* Martial the row returned by the sub-select into an array of |
| 1869 | ** registers. */ |
| 1870 | for(k=0; k<nSub; k++){ |
| 1871 | sqlite3VdbeAddOp3(v, OP_Column, iSubCsr, k, reg+k); |
| 1872 | } |
| 1873 | |
| 1874 | /* Check if this is the start of a new partition or peer group. */ |
dan | d6f784e | 2018-05-28 18:30:45 +0000 | [diff] [blame] | 1875 | if( pPart || pOrderBy ){ |
dan | c3a20c1 | 2018-05-23 20:55:37 +0000 | [diff] [blame] | 1876 | int nPart = (pPart ? pPart->nExpr : 0); |
dan | c3a20c1 | 2018-05-23 20:55:37 +0000 | [diff] [blame] | 1877 | int addrGoto = 0; |
| 1878 | int addrJump = 0; |
dan | d6f784e | 2018-05-28 18:30:45 +0000 | [diff] [blame] | 1879 | int nPeer = (pOrderBy ? pOrderBy->nExpr : 0); |
dan | c3a20c1 | 2018-05-23 20:55:37 +0000 | [diff] [blame] | 1880 | |
| 1881 | if( pPart ){ |
| 1882 | int regNewPart = reg + pMWin->nBufferCol; |
| 1883 | KeyInfo *pKeyInfo = sqlite3KeyInfoFromExprList(pParse, pPart, 0, 0); |
| 1884 | addr = sqlite3VdbeAddOp3(v, OP_Compare, regNewPart, pMWin->regPart,nPart); |
| 1885 | sqlite3VdbeAppendP4(v, (void*)pKeyInfo, P4_KEYINFO); |
| 1886 | addrJump = sqlite3VdbeAddOp3(v, OP_Jump, addr+2, 0, addr+2); |
dan | d6f784e | 2018-05-28 18:30:45 +0000 | [diff] [blame] | 1887 | windowAggFinal(pParse, pMWin, 1); |
dan | c3a20c1 | 2018-05-23 20:55:37 +0000 | [diff] [blame] | 1888 | if( pOrderBy ){ |
| 1889 | addrGoto = sqlite3VdbeAddOp0(v, OP_Goto); |
| 1890 | } |
| 1891 | } |
| 1892 | |
| 1893 | if( pOrderBy ){ |
| 1894 | int regNewPeer = reg + pMWin->nBufferCol + nPart; |
| 1895 | int regPeer = pMWin->regPart + nPart; |
| 1896 | |
dan | c3a20c1 | 2018-05-23 20:55:37 +0000 | [diff] [blame] | 1897 | if( addrJump ) sqlite3VdbeJumpHere(v, addrJump); |
dan | 79d4544 | 2018-05-26 21:17:29 +0000 | [diff] [blame] | 1898 | if( pMWin->eType==TK_RANGE ){ |
| 1899 | KeyInfo *pKeyInfo = sqlite3KeyInfoFromExprList(pParse, pOrderBy, 0, 0); |
| 1900 | addr = sqlite3VdbeAddOp3(v, OP_Compare, regNewPeer, regPeer, nPeer); |
| 1901 | sqlite3VdbeAppendP4(v, (void*)pKeyInfo, P4_KEYINFO); |
| 1902 | addrJump = sqlite3VdbeAddOp3(v, OP_Jump, addr+2, 0, addr+2); |
| 1903 | }else{ |
| 1904 | addrJump = 0; |
| 1905 | } |
dan | d6f784e | 2018-05-28 18:30:45 +0000 | [diff] [blame] | 1906 | windowAggFinal(pParse, pMWin, pMWin->eStart==TK_CURRENT); |
dan | c3a20c1 | 2018-05-23 20:55:37 +0000 | [diff] [blame] | 1907 | if( addrGoto ) sqlite3VdbeJumpHere(v, addrGoto); |
| 1908 | } |
| 1909 | |
dan | dacf1de | 2018-06-08 16:11:55 +0000 | [diff] [blame] | 1910 | sqlite3VdbeAddOp2(v, OP_Rewind, pMWin->iEphCsr,sqlite3VdbeCurrentAddr(v)+3); |
dan | c3a20c1 | 2018-05-23 20:55:37 +0000 | [diff] [blame] | 1911 | sqlite3VdbeAddOp2(v, OP_Gosub, regGosub, addrGosub); |
dan | dacf1de | 2018-06-08 16:11:55 +0000 | [diff] [blame] | 1912 | sqlite3VdbeAddOp2(v, OP_Next, pMWin->iEphCsr, sqlite3VdbeCurrentAddr(v)-1); |
| 1913 | |
dan | c3a20c1 | 2018-05-23 20:55:37 +0000 | [diff] [blame] | 1914 | sqlite3VdbeAddOp1(v, OP_ResetSorter, pMWin->iEphCsr); |
| 1915 | sqlite3VdbeAddOp3( |
| 1916 | v, OP_Copy, reg+pMWin->nBufferCol, pMWin->regPart, nPart+nPeer-1 |
| 1917 | ); |
| 1918 | |
dan | 79d4544 | 2018-05-26 21:17:29 +0000 | [diff] [blame] | 1919 | if( addrJump ) sqlite3VdbeJumpHere(v, addrJump); |
dan | c3a20c1 | 2018-05-23 20:55:37 +0000 | [diff] [blame] | 1920 | } |
| 1921 | |
| 1922 | /* Invoke step function for window functions */ |
dan | dfa552f | 2018-06-02 21:04:28 +0000 | [diff] [blame] | 1923 | windowAggStep(pParse, pMWin, -1, 0, reg, 0); |
dan | c3a20c1 | 2018-05-23 20:55:37 +0000 | [diff] [blame] | 1924 | |
| 1925 | /* Buffer the current row in the ephemeral table. */ |
| 1926 | if( pMWin->nBufferCol>0 ){ |
| 1927 | sqlite3VdbeAddOp3(v, OP_MakeRecord, reg, pMWin->nBufferCol, regRecord); |
| 1928 | }else{ |
| 1929 | sqlite3VdbeAddOp2(v, OP_Blob, 0, regRecord); |
| 1930 | sqlite3VdbeAppendP4(v, (void*)"", 0); |
| 1931 | } |
| 1932 | sqlite3VdbeAddOp2(v, OP_NewRowid, pMWin->iEphCsr, regRowid); |
| 1933 | sqlite3VdbeAddOp3(v, OP_Insert, pMWin->iEphCsr, regRecord, regRowid); |
| 1934 | |
| 1935 | /* End the database scan loop. */ |
| 1936 | sqlite3WhereEnd(pWInfo); |
| 1937 | |
dan | d6f784e | 2018-05-28 18:30:45 +0000 | [diff] [blame] | 1938 | windowAggFinal(pParse, pMWin, 1); |
dan | dacf1de | 2018-06-08 16:11:55 +0000 | [diff] [blame] | 1939 | sqlite3VdbeAddOp2(v, OP_Rewind, pMWin->iEphCsr,sqlite3VdbeCurrentAddr(v)+3); |
dan | c3a20c1 | 2018-05-23 20:55:37 +0000 | [diff] [blame] | 1940 | sqlite3VdbeAddOp2(v, OP_Gosub, regGosub, addrGosub); |
dan | dacf1de | 2018-06-08 16:11:55 +0000 | [diff] [blame] | 1941 | sqlite3VdbeAddOp2(v, OP_Next, pMWin->iEphCsr, sqlite3VdbeCurrentAddr(v)-1); |
dan | c3a20c1 | 2018-05-23 20:55:37 +0000 | [diff] [blame] | 1942 | } |
| 1943 | |
dan | 13078ca | 2018-06-13 20:29:38 +0000 | [diff] [blame] | 1944 | /* |
| 1945 | ** Allocate and return a duplicate of the Window object indicated by the |
| 1946 | ** third argument. Set the Window.pOwner field of the new object to |
| 1947 | ** pOwner. |
| 1948 | */ |
dan | 2a11bb2 | 2018-06-11 20:50:25 +0000 | [diff] [blame] | 1949 | Window *sqlite3WindowDup(sqlite3 *db, Expr *pOwner, Window *p){ |
dan | dacf1de | 2018-06-08 16:11:55 +0000 | [diff] [blame] | 1950 | Window *pNew = 0; |
| 1951 | if( p ){ |
| 1952 | pNew = sqlite3DbMallocZero(db, sizeof(Window)); |
| 1953 | if( pNew ){ |
dan | c95f38d | 2018-06-18 20:34:43 +0000 | [diff] [blame] | 1954 | pNew->zName = sqlite3DbStrDup(db, p->zName); |
dan | dacf1de | 2018-06-08 16:11:55 +0000 | [diff] [blame] | 1955 | pNew->pFilter = sqlite3ExprDup(db, p->pFilter, 0); |
| 1956 | pNew->pPartition = sqlite3ExprListDup(db, p->pPartition, 0); |
| 1957 | pNew->pOrderBy = sqlite3ExprListDup(db, p->pOrderBy, 0); |
| 1958 | pNew->eType = p->eType; |
| 1959 | pNew->eEnd = p->eEnd; |
| 1960 | pNew->eStart = p->eStart; |
dan | 303451a | 2018-06-14 20:52:08 +0000 | [diff] [blame] | 1961 | pNew->pStart = sqlite3ExprDup(db, p->pStart, 0); |
| 1962 | pNew->pEnd = sqlite3ExprDup(db, p->pEnd, 0); |
dan | 2a11bb2 | 2018-06-11 20:50:25 +0000 | [diff] [blame] | 1963 | pNew->pOwner = pOwner; |
dan | dacf1de | 2018-06-08 16:11:55 +0000 | [diff] [blame] | 1964 | } |
| 1965 | } |
| 1966 | return pNew; |
| 1967 | } |
dan | c3a20c1 | 2018-05-23 20:55:37 +0000 | [diff] [blame] | 1968 | |
dan | f9eae18 | 2018-05-21 19:45:11 +0000 | [diff] [blame] | 1969 | /* |
dan | c95f38d | 2018-06-18 20:34:43 +0000 | [diff] [blame] | 1970 | ** Return a copy of the linked list of Window objects passed as the |
| 1971 | ** second argument. |
| 1972 | */ |
| 1973 | Window *sqlite3WindowListDup(sqlite3 *db, Window *p){ |
| 1974 | Window *pWin; |
| 1975 | Window *pRet = 0; |
| 1976 | Window **pp = &pRet; |
| 1977 | |
| 1978 | for(pWin=p; pWin; pWin=pWin->pNextWin){ |
| 1979 | *pp = sqlite3WindowDup(db, 0, pWin); |
| 1980 | if( *pp==0 ) break; |
| 1981 | pp = &((*pp)->pNextWin); |
| 1982 | } |
| 1983 | |
| 1984 | return pRet; |
| 1985 | } |
| 1986 | |
| 1987 | /* |
dan | 2a11bb2 | 2018-06-11 20:50:25 +0000 | [diff] [blame] | 1988 | ** sqlite3WhereBegin() has already been called for the SELECT statement |
| 1989 | ** passed as the second argument when this function is invoked. It generates |
| 1990 | ** code to populate the Window.regResult register for each window function and |
| 1991 | ** invoke the sub-routine at instruction addrGosub once for each row. |
| 1992 | ** This function calls sqlite3WhereEnd() before returning. |
dan | f9eae18 | 2018-05-21 19:45:11 +0000 | [diff] [blame] | 1993 | */ |
| 1994 | void sqlite3WindowCodeStep( |
dan | 2a11bb2 | 2018-06-11 20:50:25 +0000 | [diff] [blame] | 1995 | Parse *pParse, /* Parse context */ |
| 1996 | Select *p, /* Rewritten SELECT statement */ |
| 1997 | WhereInfo *pWInfo, /* Context returned by sqlite3WhereBegin() */ |
| 1998 | int regGosub, /* Register for OP_Gosub */ |
| 1999 | int addrGosub /* OP_Gosub here to return each row */ |
dan | f9eae18 | 2018-05-21 19:45:11 +0000 | [diff] [blame] | 2000 | ){ |
dan | f9eae18 | 2018-05-21 19:45:11 +0000 | [diff] [blame] | 2001 | Window *pMWin = p->pWin; |
dan | f9eae18 | 2018-05-21 19:45:11 +0000 | [diff] [blame] | 2002 | |
dan | 54a9ab3 | 2018-06-14 14:27:05 +0000 | [diff] [blame] | 2003 | /* There are three different functions that may be used to do the work |
| 2004 | ** of this one, depending on the window frame and the specific built-in |
| 2005 | ** window functions used (if any). |
| 2006 | ** |
| 2007 | ** windowCodeRowExprStep() handles all "ROWS" window frames, except for: |
dan | 26522d1 | 2018-06-11 18:16:51 +0000 | [diff] [blame] | 2008 | ** |
dan | 13078ca | 2018-06-13 20:29:38 +0000 | [diff] [blame] | 2009 | ** ROWS BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW |
dan | 54a9ab3 | 2018-06-14 14:27:05 +0000 | [diff] [blame] | 2010 | ** |
| 2011 | ** The exception is because windowCodeRowExprStep() implements all window |
| 2012 | ** frame types by caching the entire partition in a temp table, and |
| 2013 | ** "ROWS BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW" is easy enough to |
| 2014 | ** implement without such a cache. |
| 2015 | ** |
| 2016 | ** windowCodeCacheStep() is used for: |
| 2017 | ** |
| 2018 | ** RANGE BETWEEN CURRENT ROW AND UNBOUNDED FOLLOWING |
| 2019 | ** |
| 2020 | ** It is also used for anything not handled by windowCodeRowExprStep() |
| 2021 | ** that invokes a built-in window function that requires the entire |
| 2022 | ** partition to be cached in a temp table before any rows are returned |
| 2023 | ** (e.g. nth_value() or percent_rank()). |
| 2024 | ** |
| 2025 | ** Finally, assuming there is no built-in window function that requires |
| 2026 | ** the partition to be cached, windowCodeDefaultStep() is used for: |
| 2027 | ** |
| 2028 | ** RANGE BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW |
| 2029 | ** RANGE BETWEEN UNBOUNDED PRECEDING AND UNBOUNDED FOLLOWING |
| 2030 | ** RANGE BETWEEN CURRENT ROW AND CURRENT ROW |
| 2031 | ** ROWS BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW |
| 2032 | ** |
| 2033 | ** windowCodeDefaultStep() is the only one of the three functions that |
| 2034 | ** does not cache each partition in a temp table before beginning to |
| 2035 | ** return rows. |
dan | 26522d1 | 2018-06-11 18:16:51 +0000 | [diff] [blame] | 2036 | */ |
dan | 54a9ab3 | 2018-06-14 14:27:05 +0000 | [diff] [blame] | 2037 | if( pMWin->eType==TK_ROWS |
| 2038 | && (pMWin->eStart!=TK_UNBOUNDED||pMWin->eEnd!=TK_CURRENT||!pMWin->pOrderBy) |
dan | 09590aa | 2018-05-25 20:30:17 +0000 | [diff] [blame] | 2039 | ){ |
dan | c3a20c1 | 2018-05-23 20:55:37 +0000 | [diff] [blame] | 2040 | windowCodeRowExprStep(pParse, p, pWInfo, regGosub, addrGosub); |
dan | 13078ca | 2018-06-13 20:29:38 +0000 | [diff] [blame] | 2041 | }else{ |
| 2042 | Window *pWin; |
dan | 54a9ab3 | 2018-06-14 14:27:05 +0000 | [diff] [blame] | 2043 | int bCache = 0; /* True to use CacheStep() */ |
dan | f9eae18 | 2018-05-21 19:45:11 +0000 | [diff] [blame] | 2044 | |
dan | 54a9ab3 | 2018-06-14 14:27:05 +0000 | [diff] [blame] | 2045 | if( pMWin->eStart==TK_CURRENT && pMWin->eEnd==TK_UNBOUNDED ){ |
dan | 13078ca | 2018-06-13 20:29:38 +0000 | [diff] [blame] | 2046 | bCache = 1; |
| 2047 | }else{ |
dan | 13078ca | 2018-06-13 20:29:38 +0000 | [diff] [blame] | 2048 | for(pWin=pMWin; pWin; pWin=pWin->pNextWin){ |
| 2049 | FuncDef *pFunc = pWin->pFunc; |
| 2050 | if( (pFunc->funcFlags & SQLITE_FUNC_WINDOW_SIZE) |
dan | 54a9ab3 | 2018-06-14 14:27:05 +0000 | [diff] [blame] | 2051 | || (pFunc->xSFunc==nth_valueStepFunc) |
| 2052 | || (pFunc->xSFunc==first_valueStepFunc) |
| 2053 | || (pFunc->xSFunc==leadStepFunc) |
| 2054 | || (pFunc->xSFunc==lagStepFunc) |
| 2055 | ){ |
dan | 13078ca | 2018-06-13 20:29:38 +0000 | [diff] [blame] | 2056 | bCache = 1; |
| 2057 | break; |
| 2058 | } |
| 2059 | } |
| 2060 | } |
| 2061 | |
| 2062 | /* Otherwise, call windowCodeDefaultStep(). */ |
| 2063 | if( bCache ){ |
dan | dfa552f | 2018-06-02 21:04:28 +0000 | [diff] [blame] | 2064 | windowCodeCacheStep(pParse, p, pWInfo, regGosub, addrGosub); |
dan | 13078ca | 2018-06-13 20:29:38 +0000 | [diff] [blame] | 2065 | }else{ |
| 2066 | windowCodeDefaultStep(pParse, p, pWInfo, regGosub, addrGosub); |
dan | dfa552f | 2018-06-02 21:04:28 +0000 | [diff] [blame] | 2067 | } |
dan | f690b57 | 2018-06-01 21:00:08 +0000 | [diff] [blame] | 2068 | } |
dan | f9eae18 | 2018-05-21 19:45:11 +0000 | [diff] [blame] | 2069 | } |
| 2070 | |
dan | 67a9b8e | 2018-06-22 20:51:35 +0000 | [diff] [blame^] | 2071 | #endif /* SQLITE_OMIT_WINDOWFUNC */ |