Many hyperlinks are disabled.
Use anonymous login
to enable hyperlinks.
Overview
Comment: | Fix "RANGE BETWEEN CURRENT ROW AND UNBOUNDED FOLLOWING" window frame processing. |
---|---|
Downloads: | Tarball | ZIP archive |
Timelines: | family | ancestors | descendants | both | exp-window-functions |
Files: | files | file ages | folders |
SHA3-256: |
b4e9c686697a5211a3bfa47e63f0684e |
User & Date: | dan 2018-05-25 20:30:17.495 |
Context
2018-05-26
| ||
21:17 | More fixes for different window frame types. (check-in: 2c85668a0f user: dan tags: exp-window-functions) | |
2018-05-25
| ||
20:30 | Fix "RANGE BETWEEN CURRENT ROW AND UNBOUNDED FOLLOWING" window frame processing. (check-in: b4e9c68669 user: dan tags: exp-window-functions) | |
09:36 | Merge latest trunk changes into this branch. (check-in: 6232519899 user: dan tags: exp-window-functions) | |
Changes
Changes to src/window.c.
︙ | ︙ | |||
110 111 112 113 114 115 116 | sqlite3VdbeAddOp3(v, OP_AggStep0, bInverse, reg, pWin->regAccum); sqlite3VdbeAppendP4(v, pWin->pFunc, P4_FUNCDEF); sqlite3VdbeChangeP5(v, (u8)pWin->nArg); } } /* | | > > > > > > > > | | > > > > | > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > < < < < < < < < | < < < < < < < | | | | < | > | | > > > > > > > > | > > > | > | 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 | sqlite3VdbeAddOp3(v, OP_AggStep0, bInverse, reg, pWin->regAccum); sqlite3VdbeAppendP4(v, pWin->pFunc, P4_FUNCDEF); sqlite3VdbeChangeP5(v, (u8)pWin->nArg); } } /* ** ROWS BETWEEN <expr1> PRECEDING AND <expr2> FOLLOWING ** ---------------------------------------------------- ** ** Pseudo-code for the implementation of this window frame type is as ** follows. sqlite3WhereBegin() has already been called to generate the ** top of the main loop when this function is called. ** ** Each time the sub-routine at addrGosub is invoked, a single output ** row is generated based on the current row indicated by Window.iEphCsr. ** ** ... ** if( new partition ){ ** Gosub flush_partition ** } ** Insert (record in eph-table) ** sqlite3WhereEnd() ** Gosub flush_partition ** ** flush_partition: ** Once { ** OpenDup (iEphCsr -> csrStart) ** OpenDup (iEphCsr -> csrEnd) ** } ** regStart = <expr1> // PRECEDING expression ** regEnd = <expr2> // FOLLOWING expression ** if( regStart<0 || regEnd<0 ){ error! } ** Rewind (csr,csrStart,csrEnd) // if EOF goto flush_partition_done ** Next(csrEnd) // if EOF skip Aggstep ** Aggstep (csrEnd) ** if( (regEnd--)<=0 ){ ** AggFinal (xValue) ** Gosub addrGosub ** Next(csr) // if EOF goto flush_partition_done ** if( (regStart--)<=0 ){ ** AggStep (csrStart, xInverse) ** Next(csrStart) ** } ** } ** flush_partition_done: ** ResetSorter (csr) ** Return ** ** ROWS BETWEEN <expr> PRECEDING AND CURRENT ROW ** ROWS BETWEEN CURRENT ROW AND <expr> FOLLOWING ** ROWS BETWEEN UNBOUNDED PRECEDING AND <expr> FOLLOWING ** ** These are similar to the above. For "CURRENT ROW", intialize the ** register to 0. For "UNBOUNDED PRECEDING" to infinity. ** ** ROWS BETWEEN <expr> PRECEDING AND UNBOUNDED FOLLOWING ** ROWS BETWEEN CURRENT ROW AND UNBOUNDED FOLLOWING ** ** Rewind (csr,csrStart,csrEnd) // if EOF goto flush_partition_done ** while( 1 ){ ** Next(csrEnd) // Exit while(1) at EOF ** Aggstep (csrEnd) ** } ** while( 1 ){ ** AggFinal (xValue) ** Gosub addrGosub ** Next(csr) // if EOF goto flush_partition_done ** if( (regStart--)<=0 ){ ** AggStep (csrStart, xInverse) ** Next(csrStart) ** } ** } ** ** For the "CURRENT ROW AND UNBOUNDED FOLLOWING" case, the final if() ** condition is always true (as if regStart were initialized to 0). ** ** RANGE BETWEEN CURRENT ROW AND UNBOUNDED FOLLOWING ** ** This is the only RANGE case handled by this routine. It modifies the ** second while( 1 ) loop in "ROWS BETWEEN CURRENT ... UNBOUNDED..." to ** be: ** ** while( 1 ){ ** AggFinal (xValue) ** while( 1 ){ ** regPeer++ ** Gosub addrGosub ** Next(csr) // if EOF goto flush_partition_done ** if( new peer ) break; ** } ** while( (regPeer--)>0 ){ ** AggStep (csrStart, xInverse) ** Next(csrStart) ** } ** } ** ** ROWS BETWEEN <expr> FOLLOWING AND <expr> FOLLOWING ** ** regEnd = regEnd - regStart ** Rewind (csr,csrStart,csrEnd) // if EOF goto flush_partition_done ** Aggstep (csrEnd) ** Next(csrEnd) // if EOF fall-through |
︙ | ︙ | |||
215 216 217 218 219 220 221 222 223 224 225 226 227 228 | int regStart; /* Value of <expr> PRECEDING */ int regEnd; /* Value of <expr> FOLLOWING */ int addrNext; int addrGoto; int addrTop; int addrIfPos1; int addrIfPos2; assert( pMWin->eStart==TK_PRECEDING || pMWin->eStart==TK_CURRENT || pMWin->eStart==TK_FOLLOWING || pMWin->eStart==TK_UNBOUNDED ); assert( pMWin->eEnd==TK_FOLLOWING | > > > > > | 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 | int regStart; /* Value of <expr> PRECEDING */ int regEnd; /* Value of <expr> FOLLOWING */ int addrNext; int addrGoto; int addrTop; int addrIfPos1; int addrIfPos2; int regPeer = 0; /* Number of peers in current group */ int regPeerVal = 0; /* Array of values identifying peer group */ int iPeer = 0; /* Column offset in eph-table of peer vals */ int nPeerVal; /* Number of peer values */ assert( pMWin->eStart==TK_PRECEDING || pMWin->eStart==TK_CURRENT || pMWin->eStart==TK_FOLLOWING || pMWin->eStart==TK_UNBOUNDED ); assert( pMWin->eEnd==TK_FOLLOWING |
︙ | ︙ | |||
330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 | if( pMWin->eEnd==TK_FOLLOWING ){ addrIfPos1 = sqlite3VdbeAddOp3(v, OP_IfPos, regEnd, 0 , 1); } if( pMWin->eStart==TK_FOLLOWING ){ addrIfPos2 = sqlite3VdbeAddOp3(v, OP_IfPos, regStart, 0 , 1); } for(pWin=pMWin; pWin; pWin=pWin->pNextWin){ sqlite3VdbeAddOp2(v, OP_Null, 0, pWin->regResult); sqlite3VdbeAddOp3(v, OP_AggFinal, pWin->regAccum, pWin->nArg, pWin->regResult ); sqlite3VdbeAppendP4(v, pWin->pFunc, P4_FUNCDEF); } sqlite3VdbeAddOp2(v, OP_Gosub, regGosub, addrGosub); sqlite3VdbeAddOp2(v, OP_Next, pMWin->iEphCsr, sqlite3VdbeCurrentAddr(v)+2); sqlite3VdbeAddOp2(v, OP_Goto, 0, lblFlushDone); if( pMWin->eStart==TK_FOLLOWING ){ sqlite3VdbeJumpHere(v, addrIfPos2); } if( pMWin->eStart==TK_CURRENT || pMWin->eStart==TK_PRECEDING || pMWin->eStart==TK_FOLLOWING ){ if( pMWin->eStart==TK_PRECEDING ){ | > > > > > > > > > > > > > > > > > > > > > > > > > > > > | > > > > | > > > | | 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 | if( pMWin->eEnd==TK_FOLLOWING ){ addrIfPos1 = sqlite3VdbeAddOp3(v, OP_IfPos, regEnd, 0 , 1); } if( pMWin->eStart==TK_FOLLOWING ){ addrIfPos2 = sqlite3VdbeAddOp3(v, OP_IfPos, regStart, 0 , 1); } if( pMWin->eType==TK_RANGE ){ assert( pMWin->eStart==TK_CURRENT && pMWin->pOrderBy ); regPeer = ++pParse->nMem; regPeerVal = pParse->nMem+1; iPeer = pMWin->nBufferCol + (pMWin->pPartition?pMWin->pPartition->nExpr:0); nPeerVal = pMWin->pOrderBy->nExpr; pParse->nMem += (2 * nPeerVal); for(k=0; k<nPeerVal; k++){ sqlite3VdbeAddOp3(v, OP_Column, pMWin->iEphCsr, iPeer+k, regPeerVal+k); } sqlite3VdbeAddOp2(v, OP_Integer, 0, regPeer); } for(pWin=pMWin; pWin; pWin=pWin->pNextWin){ sqlite3VdbeAddOp2(v, OP_Null, 0, pWin->regResult); sqlite3VdbeAddOp3(v, OP_AggFinal, pWin->regAccum, pWin->nArg, pWin->regResult ); sqlite3VdbeAppendP4(v, pWin->pFunc, P4_FUNCDEF); } if( pMWin->eType==TK_RANGE ){ sqlite3VdbeAddOp2(v, OP_AddImm, regPeer, 1); } sqlite3VdbeAddOp2(v, OP_Gosub, regGosub, addrGosub); sqlite3VdbeAddOp2(v, OP_Next, pMWin->iEphCsr, sqlite3VdbeCurrentAddr(v)+2); sqlite3VdbeAddOp2(v, OP_Goto, 0, lblFlushDone); if( pMWin->eType==TK_RANGE ){ KeyInfo *pKeyInfo = sqlite3KeyInfoFromExprList(pParse, pMWin->pOrderBy,0,0); int addrJump = sqlite3VdbeCurrentAddr(v)-4; for(k=0; k<nPeerVal; k++){ int iOut = regPeerVal + nPeerVal + k; sqlite3VdbeAddOp3(v, OP_Column, pMWin->iEphCsr, iPeer+k, iOut); } sqlite3VdbeAddOp3(v, OP_Compare, regPeerVal, regPeerVal+nPeerVal, nPeerVal); sqlite3VdbeAppendP4(v, (void*)pKeyInfo, P4_KEYINFO); addr = sqlite3VdbeCurrentAddr(v)+1; sqlite3VdbeAddOp3(v, OP_Jump, addr, addrJump, addr); } if( pMWin->eStart==TK_FOLLOWING ){ sqlite3VdbeJumpHere(v, addrIfPos2); } if( pMWin->eStart==TK_CURRENT || pMWin->eStart==TK_PRECEDING || pMWin->eStart==TK_FOLLOWING ){ int addrJumpHere = 0; if( pMWin->eStart==TK_PRECEDING ){ addrJumpHere = sqlite3VdbeAddOp3(v, OP_IfPos, regStart, 0 , 1); } if( pMWin->eType==TK_RANGE ){ sqlite3VdbeAddOp3(v, OP_IfPos, regPeer, sqlite3VdbeCurrentAddr(v)+2, 1); addrJumpHere = sqlite3VdbeAddOp0(v, OP_Goto); } sqlite3VdbeAddOp2(v, OP_Next, csrStart, sqlite3VdbeCurrentAddr(v)+1); windowAggStep(pParse, pMWin, csrStart, 1, reg); if( pMWin->eType==TK_RANGE ){ sqlite3VdbeAddOp2(v, OP_Goto, 0, addrJumpHere-1); } if( addrJumpHere ){ sqlite3VdbeJumpHere(v, addrJumpHere); } } if( pMWin->eEnd==TK_FOLLOWING ){ sqlite3VdbeJumpHere(v, addrIfPos1); } sqlite3VdbeAddOp2(v, OP_Goto, 0, addrTop); |
︙ | ︙ | |||
549 550 551 552 553 554 555 | ** if( new partition ){ ** AggFinal (xFinalize) ** } ** AggStep ** AggFinal (xValue) ** Gosub addrGosub ** sqlite3WhereEnd() | < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < | > > > | 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 | ** if( new partition ){ ** AggFinal (xFinalize) ** } ** AggStep ** AggFinal (xValue) ** Gosub addrGosub ** sqlite3WhereEnd() ** */ void sqlite3WindowCodeStep( Parse *pParse, Select *p, WhereInfo *pWInfo, int regGosub, int addrGosub, int *pbLoop ){ Window *pMWin = p->pWin; if( (pMWin->eType==TK_ROWS && (pMWin->eStart!=TK_UNBOUNDED || pMWin->eEnd!=TK_CURRENT)) || (pMWin->eStart==TK_CURRENT && pMWin->eEnd==TK_UNBOUNDED) ){ *pbLoop = 0; windowCodeRowExprStep(pParse, p, pWInfo, regGosub, addrGosub); return; } *pbLoop = 1; windowCodeDefaultStep(pParse, p, pWInfo, regGosub, addrGosub); } |
Changes to test/window2.tcl.
︙ | ︙ | |||
283 284 285 286 287 288 289 290 291 292 293 294 295 296 | execsql_test 2.22 { SELECT a, sum(d) OVER ( PARTITION BY b ORDER BY d ROWS BETWEEN 1 FOLLOWING AND UNBOUNDED FOLLOWING ) FROM t1 } ========== puts $::fd finish_test ========== # execsql_test 3.1 { # SELECT a, sum(d) OVER ( | > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > | 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 | execsql_test 2.22 { SELECT a, sum(d) OVER ( PARTITION BY b ORDER BY d ROWS BETWEEN 1 FOLLOWING AND UNBOUNDED FOLLOWING ) FROM t1 } execsql_test 2.23 { SELECT a, sum(d) OVER ( ORDER BY d ROWS BETWEEN CURRENT ROW AND UNBOUNDED FOLLOWING ) FROM t1 } execsql_test 2.24 { SELECT a, sum(d) OVER ( PARTITION BY a%2 ORDER BY d ROWS BETWEEN CURRENT ROW AND UNBOUNDED FOLLOWING ) FROM t1 } execsql_test 2.25 { SELECT a, sum(d) OVER ( ORDER BY d ROWS BETWEEN UNBOUNDED PRECEDING AND UNBOUNDED FOLLOWING ) FROM t1 } execsql_test 2.26 { SELECT a, sum(d) OVER ( PARTITION BY b ORDER BY d ROWS BETWEEN UNBOUNDED PRECEDING AND UNBOUNDED FOLLOWING ) FROM t1 } execsql_test 2.27 { SELECT a, sum(d) OVER ( ORDER BY d ROWS BETWEEN CURRENT ROW AND CURRENT ROW ) FROM t1 } execsql_test 2.28 { SELECT a, sum(d) OVER ( PARTITION BY b ORDER BY d ROWS BETWEEN CURRENT ROW AND CURRENT ROW ) FROM t1 } execsql_test 2.29 { SELECT a, sum(d) OVER ( ORDER BY d RANGE BETWEEN CURRENT ROW AND UNBOUNDED FOLLOWING ) FROM t1 } execsql_test 2.30 { SELECT a, sum(d) OVER ( ORDER BY b RANGE BETWEEN CURRENT ROW AND UNBOUNDED FOLLOWING ) FROM t1 } ========== puts $::fd finish_test ========== # execsql_test 3.1 { # SELECT a, sum(d) OVER ( |
︙ | ︙ |
Changes to test/window2.test.
︙ | ︙ | |||
195 196 197 198 199 200 201 202 203 204 205 206 207 | SELECT a, sum(d) OVER ( PARTITION BY b ORDER BY d ROWS BETWEEN 1 FOLLOWING AND UNBOUNDED FOLLOWING ) FROM t1 } {2 10 4 6 6 {} 1 8 3 5 5 {}} #========================================================================== finish_test #========================================================================== finish_test | > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > | 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 | SELECT a, sum(d) OVER ( PARTITION BY b ORDER BY d ROWS BETWEEN 1 FOLLOWING AND UNBOUNDED FOLLOWING ) FROM t1 } {2 10 4 6 6 {} 1 8 3 5 5 {}} do_execsql_test 2.23 { SELECT a, sum(d) OVER ( ORDER BY d ROWS BETWEEN CURRENT ROW AND UNBOUNDED FOLLOWING ) FROM t1 } {1 21 2 20 3 18 4 15 5 11 6 6} do_execsql_test 2.24 { SELECT a, sum(d) OVER ( PARTITION BY a%2 ORDER BY d ROWS BETWEEN CURRENT ROW AND UNBOUNDED FOLLOWING ) FROM t1 } {2 12 4 10 6 6 1 9 3 8 5 5} do_execsql_test 2.25 { SELECT a, sum(d) OVER ( ORDER BY d ROWS BETWEEN UNBOUNDED PRECEDING AND UNBOUNDED FOLLOWING ) FROM t1 } {1 21 2 21 3 21 4 21 5 21 6 21} do_execsql_test 2.26 { SELECT a, sum(d) OVER ( PARTITION BY b ORDER BY d ROWS BETWEEN UNBOUNDED PRECEDING AND UNBOUNDED FOLLOWING ) FROM t1 } {2 12 4 12 6 12 1 9 3 9 5 9} do_execsql_test 2.27 { SELECT a, sum(d) OVER ( ORDER BY d ROWS BETWEEN CURRENT ROW AND CURRENT ROW ) FROM t1 } {1 1 2 2 3 3 4 4 5 5 6 6} do_execsql_test 2.28 { SELECT a, sum(d) OVER ( PARTITION BY b ORDER BY d ROWS BETWEEN CURRENT ROW AND CURRENT ROW ) FROM t1 } {2 2 4 4 6 6 1 1 3 3 5 5} do_execsql_test 2.29 { SELECT a, sum(d) OVER ( ORDER BY d RANGE BETWEEN CURRENT ROW AND UNBOUNDED FOLLOWING ) FROM t1 } {1 21 2 20 3 18 4 15 5 11 6 6} do_execsql_test 2.30 { SELECT a, sum(d) OVER ( ORDER BY b RANGE BETWEEN CURRENT ROW AND UNBOUNDED FOLLOWING ) FROM t1 } {2 21 4 21 6 21 1 9 3 9 5 9} #========================================================================== finish_test #========================================================================== finish_test |