win.test 45.8 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13
#
#  Window Functions Tests
#

--disable_warnings
drop table if exists t1,t2;
drop view if exists v1;
--enable_warnings

--echo # ########################################################################
--echo # # Parser tests
--echo # ########################################################################
--echo #
14
--echo #  Check what happens when one attempts to use window function without OVER clause
15 16 17 18 19 20 21 22 23
create table t1 (a int, b int);
insert into t1 values (1,1),(2,2);

--error ER_PARSE_ERROR
select row_number() from t1;
--error ER_PARSE_ERROR
select rank() from t1;

--echo # Attempt to use window function in the WHERE clause
24
--error ER_WRONG_PLACEMENT_OF_WINDOW_FUNCTION
25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49
select * from t1 where 1=rank() over (order by a);
--error ER_WRONG_PLACEMENT_OF_WINDOW_FUNCTION
select * from t1 where 1>row_number() over (partition by b order by a);
drop table t1;

--echo # ########################################################################
--echo # # Functionality tests
--echo # ########################################################################
--echo #
--echo #  Check if ROW_NUMBER() works in basic cases
create table t1(a int, b int, x char(32));
insert into t1 values (2, 10, 'xx');
insert into t1 values (2, 10, 'zz');
insert into t1 values (2, 20, 'yy');
insert into t1 values (3, 10, 'xxx');
insert into t1 values (3, 20, 'vvv');

--sorted_result
select a, row_number() over (partition by a order by b) from t1;

select a, b, x, row_number() over (partition by a order by x) from t1;

drop table t1;

create table t1 (pk int primary key, a int, b int);
50
insert into t1 values
51 52 53 54 55 56
  (1, 10, 22),
  (2, 11, 21),
  (3, 12, 20),
  (4, 13, 19),
  (5, 14, 18);

57 58 59
select
  pk, a, b,
  row_number() over (order by a),
60 61 62 63 64 65 66 67 68
  row_number() over (order by b)
from t1;

drop table t1;

--echo #
--echo # Try RANK() function
--echo #
create table t2 (
69
  pk int primary key,
70 71 72 73 74 75 76 77 78 79 80 81 82 83 84
  a int
);

insert into t2 values
( 1 , 0),
( 2 , 0),
( 3 , 1),
( 4 , 1),
( 8 , 2),
( 5 , 2),
( 6 , 2),
( 7 , 2),
( 9 , 4),
(10 , 4);

85
--sorted_result
86
select pk, a, rank() over (order by a) from t2;
87
--sorted_result
88 89 90 91 92 93 94 95 96 97 98 99 100 101 102
select pk, a, rank() over (order by a desc) from t2;

drop table t2;

--echo #
--echo # Try Aggregates as window functions. With frames.
--echo #
create table t0 (a int);
insert into t0 values (0),(1),(2),(3),(4),(5),(6),(7),(8),(9);

create table t1 (pk int, c int);
insert into t1 select a+1,1 from t0;
update t1 set c=2 where pk not in (1,2,3,4);
select * from t1;

103 104 105
select
  pk, c,
  count(*) over (partition by c order by pk
106 107 108
                 rows between 2 preceding and 2 following) as CNT
from t1;

109 110 111
select
  pk, c,
  count(*) over (partition by c order by pk
112 113 114 115
                 rows between 1 preceding and 2 following) as CNT
from t1;

select
116
  pk, c,
117
  count(*) over (partition by c order by pk
118
                 rows between 2 preceding and current row) as CNT
119 120
from t1;

121 122
select
  pk,c,
123 124 125 126
  count(*) over (partition by c order by pk rows
                 between 1 following and 2 following) as CNT
from t1;

127 128
select
  pk,c,
129 130 131 132 133
  count(*) over (partition by c order by pk rows
                 between 2 preceding and 1 preceding) as CNT
from t1;

select
134
  pk, c,
135
  count(*) over (partition by c order by pk
136
                 rows between current row and 1 following) as CNT
137 138 139
from t1;

--echo # Check ORDER BY DESC
140 141
select
  pk, c,
142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159
  count(*) over (partition by c order by pk desc
                 rows between 2 preceding and 2 following) as CNT
from t1;

drop table t0,t1;

--echo #
--echo # Resolution of window names
--echo #

create table t0 (a int);
insert into t0 values (0),(1),(2),(3),(4),(5),(6),(7),(8),(9);

create table t1 (pk int, c int);
insert into t1 select a+1,1 from t0;
update t1 set c=2 where pk not in (1,2,3,4);
select * from t1;

160
select
161 162 163
  pk, c,
  count(*) over w1 as CNT
from t1
164
window w1 as (partition by c order by pk
165 166
              rows between 2 preceding and 2 following);

167 168
select
  pk, c,
169 170 171 172
  count(*) over (w1 rows between 2 preceding and 2 following) as CNT
from t1
window w1 as (partition by c order by pk);

173 174
select
  pk, c,
175 176 177 178
  count(*) over (w1 order by pk rows between 2 preceding and 2 following) as CNT
from t1
window w1 as (partition by c);

179 180
select
  pk, c,
181 182 183 184
  count(*) over (w2 rows between 2 preceding and 2 following) as CNT
from t1
window w1 as (partition by c), w2 as (w1 order by pk);

185 186
select
  pk, c,
187 188 189
  count(*) over w3 as CNT
from t1
window
190
  w1 as (partition by c),
191 192 193 194
  w2 as (w1 order by pk),
  w3 as (w2 rows between 2 preceding and 2 following);

--error ER_WRONG_WINDOW_SPEC_NAME
195 196
select
  pk, c,
197 198
  count(*) over w as CNT
from t1
199
window w1 as (partition by c order by pk
200 201 202
              rows between 2 preceding and 2 following);

--error ER_DUP_WINDOW_NAME
203 204
select
  pk, c,
205 206 207 208 209
  count(*) over (w2 rows between 2 preceding and 2 following) as CNT
from t1
window w1 as (partition by c), w1 as (order by pk);

--error ER_WRONG_WINDOW_SPEC_NAME
210 211
select
  pk, c,
212 213 214 215 216
  count(*) over (w2 rows between 2 preceding and 2 following) as CNT
from t1
window w1 as (partition by c), w2 as (w partition by c order by pk);

--error ER_PARTITION_LIST_IN_REFERENCING_WINDOW_SPEC
217 218
select
  pk, c,
219 220 221 222 223
  count(*) over (w2 rows between 2 preceding and 2 following) as CNT
from t1
window w1 as (partition by c), w2 as (w1 partition by c order by pk);

--error ER_ORDER_LIST_IN_REFERENCING_WINDOW_SPEC
224 225
select
  pk, c,
226 227 228 229 230
  count(*) over (w2 rows between 2 preceding and 2 following) as CNT
from t1
window w1 as (partition by c order by pk), w2 as (w1 order by pk);

--error ER_WINDOW_FRAME_IN_REFERENCED_WINDOW_SPEC
231 232
select
  pk, c,
233 234 235
  count(*) over w3 as CNT
from t1
window
236
  w1 as (partition by c),
237 238 239 240
  w2 as (w1 order by pk rows between 3 preceding and 2 following),
  w3 as (w2 rows between 2 preceding and 2 following);

--error ER_BAD_COMBINATION_OF_WINDOW_FRAME_BOUND_SPECS
241 242
select
  pk, c,
243 244
  count(*) over w1 as CNT
from t1
245
window w1 as (partition by c order by pk
246 247 248
              rows between unbounded following and 2 following);

--error ER_BAD_COMBINATION_OF_WINDOW_FRAME_BOUND_SPECS
249 250
select
  pk, c,
251 252 253 254 255
  count(*) over (w1 rows between 2 preceding and unbounded preceding) as CNT
from t1
window w1 as (partition by c order by pk);

--error ER_BAD_COMBINATION_OF_WINDOW_FRAME_BOUND_SPECS
256 257
select
  pk, c,
258 259 260 261 262
  count(*) over (w1 order by pk rows between current row and 2 preceding) as CNT
from t1
window w1 as (partition by c);

--error ER_BAD_COMBINATION_OF_WINDOW_FRAME_BOUND_SPECS
263 264
select
  pk, c,
265 266 267 268
  count(*) over (w2 rows between 2 following and current row) as CNT
from t1
window w1 as (partition by c), w2 as (w1 order by pk);

269 270 271
--error ER_WRONG_PLACEMENT_OF_WINDOW_FUNCTION
select
  pk, c
272 273 274
from t1 where rank() over w1 > 2
window w1 as (partition by c order by pk);

275
--error ER_WRONG_PLACEMENT_OF_WINDOW_FUNCTION
276 277
select
  c, max(pk) as m
278
from t1
279 280 281
  group by c + rank() over w1
window w1 as (order by m);

282
--error ER_WRONG_PLACEMENT_OF_WINDOW_FUNCTION
283
select
284 285
  c, max(pk) as m, rank() over w1 as r
from t1
286 287 288
  group by c+r
window w1 as (order by m);

289
--error ER_WRONG_PLACEMENT_OF_WINDOW_FUNCTION
290 291
select
  c, max(pk) as m, rank() over w1 as r
292
from t1
293 294 295
  group by c having c+r > 3
window w1 as (order by m);

296
--error ER_WINDOW_FUNCTION_IN_WINDOW_SPEC
297
select
298 299 300
  c, max(pk) as m, rank() over w1 as r,
    rank() over (partition by r+1 order by m)
from t1
301 302 303
  group by c
window w1 as (order by m);

304
--error ER_WINDOW_FUNCTION_IN_WINDOW_SPEC
305
select
306 307 308
  c, max(pk) as m, rank() over w1 as r,
    rank() over (partition by m order by r)
from t1
309 310 311
  group by c
window w1 as (order by m);

312
--error ER_WINDOW_FUNCTION_IN_WINDOW_SPEC
313
select
314 315
  c, max(pk) as m, rank() over w1 as r, dense_rank() over w2 as dr
from t1
316 317 318 319
  group by c
window w1 as (order by m), w2 as (partition by r order by m);

--error ER_NOT_ALLOWED_WINDOW_FRAME
320 321 322 323
select
  pk, c,
  row_number() over (partition by c order by pk
                     range between unbounded preceding and current row) as r
324 325 326
from t1;

--error ER_NOT_ALLOWED_WINDOW_FRAME
327 328
select
  pk, c,
329 330
  rank() over w1 as r
from t1
331
window w1 as (partition by c order by pk
332 333 334
              rows between 2 preceding and 2 following);

--error ER_NOT_ALLOWED_WINDOW_FRAME
335 336
select
  pk, c,
337 338 339 340 341
  dense_rank() over (partition by c order by pk
                    rows between 1 preceding and 1 following) as r
from t1;

--error ER_NO_ORDER_LIST_IN_WINDOW_SPEC
342 343
select
  pk, c,
344 345 346 347 348
  rank() over w1 as r
from t1
window w1 as (partition by c);

--error ER_NO_ORDER_LIST_IN_WINDOW_SPEC
349 350
select
  pk, c,
351 352 353 354 355 356 357 358 359 360 361 362
  dense_rank() over (partition by c) as r
from t1;

drop table t0,t1;

--echo #
--echo # MDEV-9634: Window function produces incorrect value
--echo #

create table t0 (a int);
insert into t0 values (0),(1),(2),(3),(4),(5),(6),(7),(8),(9);
create table t2 (part_id int, pk int, a int);
363
insert into t2 select
364 365 366
  if(a<5, 0, 1), a, if(a<5, NULL, 1) from t0;
select * from t2;

367
select
368 369 370 371 372 373 374 375
  part_id, pk, a,
  count(a) over (partition by part_id order by pk
                 rows between 1 preceding and 1 following) as CNT
from t2;

drop table t0, t2;

--echo #
376
--echo #  RANGE-type bounds
377 378 379
--echo #

create table t3 (
380
  pk int,
381 382 383 384 385 386 387 388 389 390 391 392
  val int
);

insert into t3 values
(0, 1),
(1, 1),
(2, 1),
(3, 2),
(4, 2),
(5, 2),
(6, 2);

393 394
select
  pk,
395
  val,
396 397 398
  count(val) over (order by val
                 range between current row and
                               current row)
399 400 401
  as CNT
from t3;

402
insert into t3 values
403 404 405
(7, 3),
(8, 3);

406 407
select
  pk,
408
  val,
409 410 411
  count(val) over (order by val
                 range between current row and
                               current row)
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
  as CNT
from t3;

drop table t3;

--echo # Now, check with PARTITION BY
create table t4 (
  part_id int,
  pk int,
  val int
);

insert into t4 values
(1234, 100, 1),
(1234, 101, 1),
(1234, 102, 1),
(1234, 103, 2),
(1234, 104, 2),
(1234, 105, 2),
(1234, 106, 2),
(1234, 107, 3),
(1234, 108, 3),

(5678, 200, 1),
(5678, 201, 1),
(5678, 202, 1),
(5678, 203, 2),
(5678, 204, 2),
(5678, 205, 2),
(5678, 206, 2),
(5678, 207, 3),
(5678, 208, 3);

445
select
446
  part_id,
447
  pk,
448 449
  val,
  count(val) over (partition by part_id
450 451 452
                   order by val
                   range between current row and
                               current row)
453 454 455 456 457 458
  as CNT
from t4;

--echo #
--echo # Try RANGE UNBOUNDED PRECEDING | FOLLOWING
--echo #
459
select
460
  part_id,
461
  pk,
462 463
  val,
  count(val) over (partition by part_id
464 465 466
                   order by val
                   range between unbounded preceding and
                               current row)
467 468 469
  as CNT
from t4;

470
select
471
  part_id,
472
  pk,
473 474
  val,
  count(val) over (partition by part_id
475 476 477
                   order by val
                   range between current row and
                                 unbounded following)
478 479 480
  as CNT
from t4;

481
select
482
  part_id,
483
  pk,
484 485
  val,
  count(val) over (partition by part_id
486 487 488
                   order by val
                   range between unbounded preceding and
                                 unbounded following)
489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506
  as CNT
from t4;

drop table t4;

--echo #
--echo # MDEV-9695: Wrong window frame when using RANGE BETWEEN N FOLLOWING AND PRECEDING
--echo #
create table t1 (pk int, a int, b int);
insert into t1 values
( 1 , 0, 1),
( 2 , 0, 2),
( 3 , 1, 4),
( 4 , 1, 8),
( 5 , 2, 32),
( 6 , 2, 64),
( 7 , 2, 128),
( 8 , 2, 16);
507

508 509 510 511
select pk, a, b,
bit_or(b) over (partition by a order by pk ROWS BETWEEN 1 PRECEDING AND 1 FOLLOWING) as bit_or
from t1;

512
--echo # Extra ROWS n PRECEDING tests
513 514 515 516 517 518 519
select pk, a, b,
bit_or(b) over (partition by a order by pk ROWS BETWEEN 1 PRECEDING AND 1 PRECEDING) as bit_or
from t1;
drop table t1;


create table t2 (
520 521
  pk int,
  a int,
522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550
  b int
);

insert into t2 values
( 1, 0, 1),
( 2, 0, 2),
( 3, 0, 4),
( 4, 0, 8),
( 5, 1, 16),
( 6, 1, 32),
( 7, 1, 64),
( 8, 1, 128),
( 9, 2, 256),
(10, 2, 512),
(11, 2, 1024),
(12, 2, 2048);

select pk, a, b,
bit_or(b) over (partition by a order by pk ROWS BETWEEN 1 PRECEDING AND 1 PRECEDING) as bit_or
from t2;

select pk, a, b,
bit_or(b) over (partition by a order by pk ROWS BETWEEN 2 PRECEDING AND 2 PRECEDING) as bit_or
from t2;

select pk, a, b,
bit_or(b) over (partition by a order by pk ROWS BETWEEN 2 PRECEDING AND 1 PRECEDING) as bit_or
from t2;

551
--echo # Check CURRENT ROW
552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567

select pk, a, b,
bit_or(b) over (partition by a order by pk ROWS BETWEEN CURRENT ROW AND CURRENT ROW) as bit_or
from t2;

drop table t2;

--echo #
--echo # Try RANGE PRECEDING|FOLLWING n
--echo #
create table t1 (
  part_id int,
  pk int,
  a int
);

568
insert into t1 values
569 570 571 572
(10, 1, 1),
(10, 2, 2),
(10, 3, 4),
(10, 4, 8),
573
(10, 5,26),
574 575 576 577 578 579 580 581
(10, 6,27),
(10, 7,40),
(10, 8,71),
(10, 9,72);

select
  pk, a,
  count(a) over (ORDER BY a
582
                 RANGE BETWEEN UNBOUNDED PRECEDING
583 584 585 586 587 588
                 AND 10 FOLLOWING) as cnt
from t1;

select
  pk, a,
  count(a) over (ORDER BY a DESC
589
                 RANGE BETWEEN UNBOUNDED PRECEDING
590 591 592 593 594 595
                 AND 10 FOLLOWING) as cnt
from t1;

select
  pk, a,
  count(a) over (ORDER BY a
596
                 RANGE BETWEEN UNBOUNDED PRECEDING
597 598 599 600 601 602
                 AND 1 FOLLOWING) as cnt
from t1;

select
  pk, a,
  count(a) over (ORDER BY a
603
                 RANGE BETWEEN UNBOUNDED PRECEDING
604 605 606 607 608 609
                 AND 10 PRECEDING) as cnt
from t1;

select
  pk, a,
  count(a) over (ORDER BY a DESC
610
                 RANGE BETWEEN UNBOUNDED PRECEDING
611 612 613 614 615 616
                 AND 10 PRECEDING) as cnt
from t1;

select
  pk, a,
  count(a) over (ORDER BY a
617
                 RANGE BETWEEN UNBOUNDED PRECEDING
618 619 620
                 AND 1 PRECEDING) as cnt
from t1;

621
# Try bottom bound
622 623 624
select
  pk, a,
  count(a) over (ORDER BY a
625
                 RANGE BETWEEN 1 PRECEDING
626 627 628 629 630 631
                 AND CURRENT ROW) as cnt
from t1;

select
  pk, a,
  count(a) over (ORDER BY a DESC
632
                 RANGE BETWEEN 1 PRECEDING
633 634 635 636 637 638
                 AND CURRENT ROW) as cnt
from t1;

select
  pk, a,
  count(a) over (ORDER BY a
639
                 RANGE BETWEEN 1 FOLLOWING
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
                 AND 3 FOLLOWING) as cnt
from t1;

--echo # Try CURRENT ROW with[out] DESC
select
  pk, a,
  count(a) over (ORDER BY a
                 RANGE BETWEEN CURRENT ROW
                 AND 1 FOLLOWING) as cnt
from t1;

select
  pk, a,
  count(a) over (order by a desc
                 range between current row
                 and 1 following) as cnt
from t1;


# Try with partitions
insert into t1 select 22, pk, a from t1;
select
  part_id, pk, a,
  count(a) over (PARTITION BY part_id
                 ORDER BY a
665
                 RANGE BETWEEN UNBOUNDED PRECEDING
666 667 668 669 670 671 672
                 AND 10 FOLLOWING) as cnt
from t1;

select
  pk, a,
  count(a) over (PARTITION BY part_id
                 ORDER BY a
673
                 RANGE BETWEEN UNBOUNDED PRECEDING
674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693
                 AND 1 PRECEDING) as cnt
from t1;

drop table t1;

--echo # Try a RANGE frame over non-integer datatype:

create table t1 (
  col1 int,
  a decimal(5,3)
);

insert into t1 values (1, 0.45);
insert into t1 values (1, 0.5);
insert into t1 values (1, 0.55);
insert into t1 values (1, 1.21);
insert into t1 values (1, 1.22);
insert into t1 values (1, 3.33);

select
694 695 696 697
  a,
  count(col1) over (order by a
                    range between 0.1 preceding
                                  and 0.1 following)
698 699 700 701
from t1;

drop table t1;

702 703 704
--echo #
--echo # RANGE-type frames and NULL values
--echo #
705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720
create table t1 (
 pk int,
 a int,
 b int
);

insert into t1 values (1, NULL,1);
insert into t1 values (2, NULL,1);
insert into t1 values (3, NULL,1);
insert into t1 values (4, 10 ,1);
insert into t1 values (5, 11 ,1);
insert into t1 values (6, 12 ,1);
insert into t1 values (7, 13 ,1);
insert into t1 values (8, 14 ,1);


721 722 723 724
select
 pk, a,
 count(b) over (order by a
                range between 2 preceding
725 726 727 728
                          and 2 following) as CNT
from t1;
drop table t1;

729
--echo #
730
--echo #  Try ranges that have bound1 > bound2.  The standard actually allows them
731
--echo #
732 733 734 735 736 737 738 739 740 741 742 743 744 745

create table t0 (a int);
insert into t0 values (0),(1),(2),(3),(4),(5),(6),(7),(8),(9);

create table t1 (pk int, c int);
insert into t1 select a+1,1 from t0;
update t1 set c=2 where pk not in (1,2,3,4);
select * from t1;

select
  pk, c,
  count(*) over (partition by c
                 order by pk
                 rows between 1 preceding
746
                          and 2 preceding)
747 748 749
                as cnt
from t1;

750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767
select
  pk, c,
  sum(c) over (partition by c
                 order by pk
                 rows between 1 preceding
                          and 2 preceding)
                as sum
from t1;

select
  pk, c,
  sum(c) over (partition by c
                 order by pk
                 rows between 2 following
                          and 1 following)
                as sum
from t1;

768 769 770 771 772 773

select
  pk, c,
  count(*) over (partition by c
                 order by pk
                 range between 1 preceding
774
                           and 2 preceding)
775 776 777 778 779 780 781 782 783 784 785 786
                as cnt
from t1;
drop table t0, t1;

--echo #
--echo # Error checking for frame bounds
--echo #

create table t1 (a int, b int, c varchar(32));
insert into t1 values (1,1,'foo');
insert into t1 values (2,2,'bar');
--error ER_RANGE_FRAME_NEEDS_SIMPLE_ORDERBY
787
select
788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823
  count(*) over (order by a,b
                 range between unbounded preceding and current row)
from t1;

--error ER_WRONG_TYPE_FOR_RANGE_FRAME
select
  count(*) over (order by c
                 range between unbounded preceding and current row)
from t1;

--error ER_WRONG_TYPE_FOR_RANGE_FRAME
select
  count(*) over (order by a
                 range between 'abcd' preceding and current row)
from t1;

--error ER_WRONG_TYPE_FOR_RANGE_FRAME
select
  count(*) over (order by a
                 range between current row and 'foo' following)
from t1;

--echo # Try range frame with invalid bounds
--error ER_WRONG_TYPE_FOR_ROWS_FRAME
select
  count(*) over (order by a
                 rows between 0.5 preceding and current row)
from t1;

--error ER_WRONG_TYPE_FOR_ROWS_FRAME
select
  count(*) over (order by a
                 rows between current row and 3.14 following)
from t1;

--echo #
824
--echo # EXCLUDE clause is parsed but not supported
825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856
--echo #

--error ER_FRAME_EXCLUSION_NOT_SUPPORTED
select
  count(*) over (order by a
                 rows between 1 preceding and 1 following
                 exclude current row)
from t1;

--error ER_FRAME_EXCLUSION_NOT_SUPPORTED
select
  count(*) over (order by a
                 range between 1 preceding and 1 following
                 exclude ties)
from t1;

--error ER_FRAME_EXCLUSION_NOT_SUPPORTED
select
  count(*) over (order by a
                 range between 1 preceding and 1 following
                 exclude group)
from t1;

# EXCLUDE NO OTHERS means 'don't exclude anything'
select
  count(*) over (order by a
                 rows between 1 preceding and 1 following
                 exclude no others)
from t1;

drop table t1;

857 858 859
--echo #
--echo #  Window function in grouping query
--echo #
860 861 862 863 864

create table t1 (
  username  varchar(32),
  amount int
);
865

866 867 868 869 870 871 872
insert into t1 values
('user1',1),
('user1',5),
('user1',3),
('user2',10),
('user2',20),
('user2',30);
873 874

select
875
  username,
876
  sum(amount) as s,
877 878 879 880 881 882
  rank() over (order by s desc)
from t1
group by username;

drop table t1;

883 884 885
--echo #
--echo #  mdev-9719: Window function in prepared statement
--echo #
886 887 888 889 890 891 892

create table t1(a int, b int, x char(32));
insert into t1 values (2, 10, 'xx');
insert into t1 values (2, 10, 'zz');
insert into t1 values (2, 20, 'yy');
insert into t1 values (3, 10, 'xxx');
insert into t1 values (3, 20, 'vvv');
893

894 895 896 897 898 899
prepare stmt from 'select a, row_number() over (partition by a order by b) from t1';
--sorted_result
execute stmt;

drop table t1;

900 901 902
--echo #
--echo #  mdev-9754: Window name resolution in prepared statement
--echo #
903 904 905

create table t0 (a int);
insert into t0 values (0),(1),(2),(3),(4),(5),(6),(7),(8),(9);
906

907 908 909 910
create table t1 (pk int, c int);
insert into t1 select a+1,1 from t0;
update t1 set c=2 where pk not in (1,2,3,4);
select * from t1;
911 912 913 914

prepare stmt from
'select
  pk, c,
915 916
  count(*) over w1 as CNT
from t1
917
window w1 as (partition by c order by pk
918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003
              rows between 2 preceding and 2 following)';
execute stmt;

drop table t0,t1;

--echo #
--echo # EXPLAIN FORMAT=JSON support for window functions
--echo #
create table t0 (a int);
insert into t0 values (0),(1),(2),(3),(4),(5),(6),(7),(8),(9);

explain format=json select rank() over (order by a) from t0;

create table t1 (a int, b int, c int);
insert into t1 select a,a,a from t0;

explain format=json
select
  a,
  rank() over (order by sum(b))
from t1
group by a;

explain format=json
select
  a,
  rank() over (order by sum(b))
from t1
group by a
order by null;

--echo #
--echo # Check how window function works together with GROUP BY and HAVING
--echo #

select b,max(a) as MX, rank() over (order by b) from t1 group by b having MX in (3,5,7);
explain format=json
select b,max(a) as MX, rank() over (order by b) from t1 group by b having MX in (3,5,7);

drop table t1;
drop table t0;

--echo #
--echo # Building ordering index for window functions
--echo #

create table t1 (
  pk int primary key,
  a int,
  b int,
  c  int
);

insert into t1 values
(101 , 0, 10, 1),
(102 , 0, 10, 2),
(103 , 1, 10, 3),
(104 , 1, 10, 4),
(108 , 2, 10, 5),
(105 , 2, 20, 6),
(106 , 2, 20, 7),
(107 , 2, 20, 8),
(109 , 4, 20, 9),
(110 , 4, 20, 10),
(111 , 5, NULL, 11),
(112 , 5, 1, 12),
(113 , 5, NULL, 13),
(114 , 5, NULL, 14),
(115 , 5, NULL, 15),
(116 , 6, 1, NULL),
(117 , 6, 1, 10),
(118 , 6, 1, 1),
(119 , 6, 1, NULL),
(120 , 6, 1, NULL),
(121 , 6, 1, NULL),
(122 , 6, 1, 2),
(123 , 6, 1, 20),
(124 , 6, 1, -10),
(125 , 6, 1, NULL),
(126 , 6, 1, NULL),
(127 , 6, 1, NULL);

--sorted_result
select sum(b) over (partition by a order by b,pk
                    rows between unbounded preceding  and current row) as c1,
       avg(b) over (w1 rows between 1 preceding and 1 following) as c2,
1004
       sum(c) over (w2 rows between 1 preceding and 1 following) as c5,
1005 1006
       avg(b) over (w1 rows between 5 preceding and 5 following) as c3,
       sum(b) over (w1 rows between 1 preceding and 1 following) as c4
1007
from t1
1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018
window w1 as (partition by a order by b,pk),
       w2 as (partition by b order by c,pk);

drop table t1;


--echo #
--echo # MDEV-9848: Window functions: reuse sorting and/or scanning
--echo #

create table t1 (a int, b int, c int);
1019
insert into t1 values
1020 1021 1022 1023 1024 1025
(1,3,1),
(2,2,1),
(3,1,1);

--echo #  Check using counters
flush status;
1026
select
1027 1028 1029 1030 1031 1032
  rank() over (partition by c order by a),
  rank() over (partition by c order by b)
from t1;
show status like '%sort%';

flush status;
1033
select
1034 1035 1036 1037 1038
  rank() over (partition by c order by a),
  rank() over (partition by c order by a)
from t1;
show status like '%sort%';

1039
# Check using EXPLAIN FORMAT=JSON
1040
explain format=json
1041
select
1042 1043 1044 1045 1046
  rank() over (partition by c order by a),
  rank() over (partition by c order by a)
from t1;

explain format=json
1047
select
1048 1049 1050 1051 1052
  rank() over (order by a),
  row_number() over (order by a)
from t1;

explain format=json
1053
select
1054 1055 1056 1057 1058
  rank() over (partition by c order by a),
  count(*) over (partition by c)
from t1;

explain format=json
1059
select
1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098
  count(*) over (partition by c),
  rank() over (partition by c order by a)
from t1;

drop table t1;


--echo #
--echo # MDEV-9847: Window functions: crash with big_tables=1
--echo #
create table t1(a int);
insert into t1 values (0),(1),(2),(3),(4),(5),(6),(7),(8),(9);
set @tmp=@@big_tables;
set big_tables=1;
select rank() over (order by a) from t1;
set big_tables=@tmp;
drop table t1;

--echo #
--echo # Check if "ORDER BY window_func" works
--echo #

create table t1 (s1 int, s2 char(5));
insert into t1 values (1,'a');
insert into t1 values (null,null);
insert into t1 values (1,null);
insert into t1 values (null,'a');
insert into t1 values (2,'b');
insert into t1 values (-1,'');

explain format=json
select *, row_number() over (order by s1, s2) as X from t1 order by X desc;
select *, row_number() over (order by s1, s2) as X from t1 order by X desc;
drop table t1;

--echo #
--echo # Try window functions that are not directly present in the select list
--echo #
create table t1 (a int, b int);
1099
insert into t1 values
1100 1101 1102 1103
  (1,3),
  (2,2),
  (3,1);

1104 1105 1106 1107
select
  a, b,
  rank() over (order by a), rank() over (order by b),
  rank() over (order by a) - rank() over (order by b) as diff
1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132
from
  t1;

drop table t1;

create table t1 (i int);
insert into t1 values (1),(2);
SELECT MAX(i) OVER (PARTITION BY (i)) FROM t1;
drop table t1;

--echo #
--echo # Check the 0 in ROWS 0 PRECEDING
--echo #

create table t1 (
  part_id int,
  pk int,
  a int
);

insert into t1 values (1, 1, 1);
insert into t1 values (1, 2, 2);
insert into t1 values (1, 3, 4);
insert into t1 values (1, 4, 8);

1133
select
1134 1135 1136 1137
  pk, a,
  sum(a) over (order by pk rows between 0 preceding and current row)
from t1;

1138 1139
select
  pk, a,
1140 1141 1142 1143 1144 1145 1146
  sum(a) over (order by pk rows between 1 preceding and 0 preceding)
from t1;

insert into t1 values (200, 1, 1);
insert into t1 values (200, 2, 2);
insert into t1 values (200, 3, 4);
insert into t1 values (200, 4, 8);
1147
select
1148 1149 1150 1151
  part_id, pk, a,
  sum(a) over (partition by part_id order by pk rows between 0 preceding and current row)
from t1;

1152 1153
select
  part_id, pk, a,
1154 1155 1156 1157 1158 1159
  sum(a) over (partition by part_id order by pk rows between 1 preceding and 0 preceding)
from t1;

drop table t1;
--echo #
--echo # MDEV-9780, The "DISTINCT must not bet converted into GROUP BY when
1160
--echo #                 window functions are present" part
1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182
--echo #

create table t1 (part_id int, a int);
insert into t1 values
(100, 1),
(100, 2),
(100, 2),
(100, 3),
(2000, 1),
(2000, 2),
(2000, 3),
(2000, 3),
(2000, 3);

select          rank() over (partition by part_id order by a) from t1;
select distinct rank() over (partition by part_id order by a) from t1;
explain format=json
select distinct rank() over (partition by part_id order by a) from t1;

drop table t1;

--echo #
1183 1184
--echo # MDEV-9893: Window functions with different ORDER BY lists,
--echo #            one of these lists containing an expression
1185 1186 1187 1188 1189 1190 1191 1192 1193
--echo #

create table t1 (s1 int, s2 char(5));
insert into t1 values (1,'a');
insert into t1 values (null,null);
insert into t1 values (3,null);
insert into t1 values (4,'a');
insert into t1 values (2,'b');
insert into t1 values (-1,'');
1194 1195

select
1196 1197
  *,
  ROW_NUMBER() OVER (order by s1),
1198
  CUME_DIST() OVER (order by -s1)
1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218
from t1;

drop table t1;


--echo #
--echo # MDEV-9925: Wrong result with aggregate function as a window function
--echo #
create table t1 (i int);
insert into t1 values (1),(2);
select i, sum(i) over (partition by i) from t1;
drop table t1;

--echo #
--echo # MDEV-9922: Assertion `!join->only_const_tables() && fsort' failed in int create_sort_index
--echo #
create view v1 as select 1 as i;
select rank() over (order by i) from v1;
drop view v1;

1219 1220 1221 1222 1223 1224 1225
--echo #
--echo # MDEV-10097: Assertion `count > 0' failed in Item_sum_sum::add_helper(bool)
--echo #
CREATE TABLE `orders` (
        `o_orderkey` int(11) NOT NULL,
        `o_custkey` int(11) DEFAULT NULL,
        PRIMARY KEY (`o_orderkey`)
1226
        ) DEFAULT CHARSET=latin1;
1227 1228 1229 1230 1231 1232 1233 1234 1235

INSERT INTO `orders` VALUES (59908,242);
INSERT INTO `orders` VALUES (59940,238);

SELECT o_custkey, avg(o_custkey) OVER (PARTITION BY abs(o_custkey)
                                       ORDER BY o_custkey
                                       RANGE BETWEEN 15 FOLLOWING
                                                 AND 15 FOLLOWING) from orders;
DROP table orders;
Igor Babaev's avatar
Igor Babaev committed
1236 1237

--echo #
1238
--echo # MDEV-10842: window functions with the same order column
Igor Babaev's avatar
Igor Babaev committed
1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267
--echo #             but different directions
--echo #

create table t1 (
  pk int primary key,
  a int,
  b int,
  c char(10)
);

insert into t1 values
( 1, 0, 1, 'one'),
( 2, 0, 2, 'two'),
( 3, 0, 3, 'three'),
( 4, 1, 1, 'one'),
( 5, 1, 1, 'two'),
( 6, 1, 2, 'three'),
( 7, 2, NULL, 'n_one'),
( 8, 2, 1,    'n_two'),
( 9, 2, 2,    'n_three'),
(10, 2, 0,    'n_four'),
(11, 2, 10,   NULL);

select pk,
      row_number() over (order by pk desc) as r_desc,
      row_number() over (order by pk asc) as r_asc
from t1;

drop table t1;
Igor Babaev's avatar
Igor Babaev committed
1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294 1295 1296 1297 1298 1299 1300 1301 1302 1303 1304 1305 1306 1307 1308 1309 1310 1311 1312 1313 1314 1315

--echo #
--echo # MDEV-10874: two window functions with ccompatible sorting 
--echo #

create table t1 (
pk int primary key,
a int,
b int,
c char(10),
d decimal(10, 3),
e real
);
insert into t1 values
( 1, 0, 1,    'one',    0.1,  0.001),
( 2, 0, 2,    'two',    0.2,  0.002),
( 3, 0, 3,    'three',  0.3,  0.003),
( 4, 1, 2,    'three',  0.4,  0.004),
( 5, 1, 1,    'two',    0.5,  0.005),
( 6, 1, 1,    'one',    0.6,  0.006),
( 7, 2, NULL, 'n_one',  0.5,  0.007),
( 8, 2, 1,    'n_two',  NULL, 0.008),
( 9, 2, 2,    NULL,     0.7,  0.009),
(10, 2, 0,    'n_four', 0.8,  0.010),
(11, 2, 10,   NULL,     0.9,  NULL);

select pk, a, d,
       sum(d) over (partition by a order by pk
                    ROWS between 1 preceding and current row) as sum_1, 
       sum(d) over (order by a 
                    ROWS BETWEEN 1 preceding and 2 following) as sum_2
from t1;
explain format=json
select pk, a, d,
       sum(d) over (partition by a order by pk
                    ROWS between 1 preceding and current row) as sum_1, 
       sum(d) over (order by a 
                    ROWS BETWEEN 1 preceding and 2 following) as sum_2
from t1;

select pk, a, d,
       sum(d) over (partition by a order by pk desc
                    ROWS between 1 preceding and current row) as sum_1, 
       sum(d) over (order by a 
                    ROWS BETWEEN 1 preceding and 2 following) as sum_2
from t1;

drop table t1;
1316 1317 1318 1319 1320 1321 1322 1323 1324 1325 1326 1327 1328 1329 1330 1331 1332 1333 1334 1335 1336 1337 1338 1339 1340 1341 1342

--echo #
--echo # MDEV-9941: two window functions with compatible partitions 
--echo #

create table t1 (
  a int,
  b int,
  c int
);
 
insert into t1 values 
 (10, 1, 1),
 (10, 3, 10),
 (10, 1, 10),
 (10, 3, 100),
 (10, 5, 1000),
 (10, 1, 100);

explain format=json
select 
  a,b,c,
  row_number() over (partition by a),
  row_number() over (partition by a, b)
from t1;

drop table t1;
1343 1344 1345 1346 1347 1348 1349 1350 1351 1352 1353 1354 1355 1356 1357 1358 1359 1360 1361

--echo #
--echo # MDEV-10815: Window Function Expressions Wrong Results
--echo #
create table t(a decimal(35,10), b int);
insert into t(a,b) values(1,1);
insert into t(a,b) values(2,1);
insert into t(a,b) values(0,1);
insert into t(a,b) values(1,  2);
insert into t(a,b) values(1.5,2);
insert into t(a,b) values(3,  2);
insert into t(a,b) values(4.5,2);
select a, b,
       sum(t.a) over (partition by t.b order by a) as simple_sum,
       sum(t.a) over (partition by t.b order by a) + 1 as sum_and_const,
       sum(t.b) over (partition by t.b order by a) + sum(t.a) over (partition by t.b order by a) as sum_and_sum
from t
order by t.b, t.a;
drop table t;
1362 1363 1364 1365 1366 1367 1368 1369 1370 1371 1372 1373 1374 1375

--echo #
--echo # MDEV-10669: Crash in SELECT with window function used
--echo #
create table t(a decimal(35,10), b int);
insert into t(a,b) values(1,1);
insert into t(a,b) values(2,1);
insert into t(a,b) values(0,1);
SELECT (CASE WHEN sum(t.a) over (partition by t.b)=0 THEN null ELSE null END) AS a FROM t;
SELECT ifnull(((t.a) / CASE WHEN sum(t.a) over(partition by t.b) =0 then null else null end) ,0) from t;
SELECT sum(t.a) over (partition by t.b order by a),
       sqrt(ifnull((sum(t.a) over (partition by t.b order by a)), 0))
from t;
drop table t;
Igor Babaev's avatar
Igor Babaev committed
1376 1377 1378 1379 1380 1381 1382 1383 1384 1385 1386 1387 1388 1389 1390 1391 1392 1393 1394 1395 1396 1397 1398 1399 1400 1401 1402 1403 1404 1405 1406 1407 1408 1409 1410 1411 1412 1413 1414 1415 1416

--echo #
--echo # MDEV-10868: view definitions with window functions
--echo #

create table t0 (a int);
insert into t0 values (0),(1),(2),(3),(4),(5),(6),(7),(8),(9);

create table t1 (pk int, c int);
insert into t1 select a+1,1 from t0;
update t1 set c=2 where pk not in (1,2,3,4);
select * from t1;

let $q=
select pk, c, c/count(*) over (partition by c order by pk
                               rows between 1 preceding and 2 following) as CNT
from t1;

eval $q;
eval create view v1 as $q;
show create view v1;
select * from v1;

let $q=
select pk, c, c/count(*) over w1 as CNT from t1
  window w1 as (partition by c order by pk rows between 1 preceding and 2 following);

eval $q;
eval create view v2 as $q;
show create view v2;
select * from v2;

let $q=
select pk, c, c/count(*) over w1 as CNT from t1
  window w1 as (partition by c order by pk rows unbounded preceding);

eval $q;
eval create view v3 as $q;
show create view v3;
select * from v3;

1417 1418 1419 1420 1421 1422 1423 1424 1425 1426 1427
let $q=
select pk, c, c/count(*) over (partition by c order by pk
                               range between 3 preceding and current row) as CNT
from t1;

eval $q;
eval create view v4 as $q;
show create view v4;
select * from v4;

drop view v1,v2,v3,v4;
Igor Babaev's avatar
Igor Babaev committed
1428
drop table t0,t1;
Igor Babaev's avatar
Igor Babaev committed
1429 1430 1431 1432 1433 1434 1435 1436 1437 1438 1439 1440 1441 1442 1443 1444

--echo #
--echo # MDEV-10875: window function in subquery
--echo #

CREATE TABLE t1 (i INT);
INSERT INTO t1 VALUES (3),(1);

CREATE TABLE t2 (c VARCHAR(8));
INSERT INTO t2 VALUES ('foo'),('bar'),('foo');

SELECT COUNT(*) OVER (PARTITION BY c) FROM t2;

SELECT * FROM t1 WHERE i IN ( SELECT COUNT(*) OVER (PARTITION BY c) FROM t2 );

DROP TABLE t1, t2;
Igor Babaev's avatar
Igor Babaev committed
1445 1446 1447 1448 1449 1450 1451 1452 1453 1454 1455 1456 1457 1458 1459 1460 1461 1462 1463 1464 1465

--echo #
--echo # MDEV-9976: window function without PARTITION BY and ORDER BY
--echo #

CREATE TABLE t1  (id int, a int);
INSERT INTO t1 VALUES
 (1,1000), (2,1100), (3,1800), (4,1500), (5,1700), (6,1200),
 (7,2000), (8,2100), (9,1600);

--sorted_result
SELECT id, sum(a) OVER (PARTITION BY id 
                       ROWS BETWEEN CURRENT ROW AND UNBOUNDED FOLLOWING)
FROM t1;

--sorted_result
SELECT id, sum(a) OVER (ROWS BETWEEN CURRENT ROW AND UNBOUNDED FOLLOWING)
FROM t1;

DROP TABLE t1;

Igor Babaev's avatar
Igor Babaev committed
1466 1467 1468 1469 1470 1471 1472 1473 1474 1475 1476 1477 1478 1479 1480 1481 1482 1483 1484 1485 1486 1487 1488 1489 1490 1491 1492 1493
--echo #
--echo # MDEV-11867: window function with aggregation
--echo #             over the result of grouping
--echo #

create table t1 (
  username  varchar(32),
  amount int
);

insert into t1 values
('user1',1),
('user1',5),
('user1',3),
('user2',10),
('user2',20),
('user2',30);

select username, sum(amount) as s, avg(sum(amount)) over (order by s desc)
  from t1
group by username;

select username, sum(amount), avg(sum(amount)) over (order by sum(amount) desc)
  from t1
group by username;

drop table t1;

Igor Babaev's avatar
Igor Babaev committed
1494 1495 1496 1497
--echo #
--echo # MDEV-11594: window function over implicit grouping
--echo #

Igor Babaev's avatar
Igor Babaev committed
1498 1499
create table t1 (id int);
insert into t1 values (1), (2), (3), (2);
Igor Babaev's avatar
Igor Babaev committed
1500 1501 1502 1503 1504 1505

select sum(id) over (order by sum(id)) from t1;

select sum(sum(id)) over (order by sum(id)) from t1;

drop table t1;
Igor Babaev's avatar
Igor Babaev committed
1506

Igor Babaev's avatar
Igor Babaev committed
1507 1508 1509 1510 1511
--echo #
--echo # MDEV-9923: integer constant in order by list 
--echo #            of window specification
--echo #

Igor Babaev's avatar
Igor Babaev committed
1512 1513
create table t1 (id int);
insert into t1 values (1), (2), (3), (2);
Igor Babaev's avatar
Igor Babaev committed
1514 1515 1516 1517 1518 1519

select rank() over (order by 1) from t1;
select rank() over (order by 2) from t1;
select rank() over (partition by id order by 2) from t1;

drop table t1;
Igor Babaev's avatar
Igor Babaev committed
1520 1521 1522 1523 1524 1525 1526 1527 1528 1529 1530 1531 1532 1533 1534 1535 1536 1537

--echo #
--echo # MDEV-10660: view using a simple window function 
--echo #

create table t1 (id int);
insert into t1 values (1), (2), (3), (2);

create view v1(id,rnk) as
  select id, rank() over (order by id) from t1;

show create view v1;

select id, rank() over (order by id) from t1;
select * from v1;

drop view v1;
drop table t1;
Igor Babaev's avatar
Igor Babaev committed
1538 1539 1540 1541 1542 1543 1544 1545 1546 1547 1548 1549

--echo #
--echo # MDEV-11138: window function in the query without tables 
--echo #

select row_number() over ();
select count(*) over ();
select sum(5) over ();
select row_number() over (), sum(5) over ();
select row_number() over (order by 2);
select row_number() over (partition by 2);
select row_number() over (partition by 4 order by 1+2);
Igor Babaev's avatar
Igor Babaev committed
1550 1551 1552 1553 1554 1555 1556 1557 1558 1559 1560 1561

--echo #
--echo # MDEV-11999: execution of prepared statement for  
--echo #             tableless query with window functions
--echo #

prepare stmt from
"select row_number() over (partition by 4 order by 1+2)";
execute stmt;
execute stmt;
deallocate prepare stmt;

Igor Babaev's avatar
Igor Babaev committed
1562 1563 1564 1565 1566 1567 1568 1569 1570 1571 1572 1573 1574 1575 1576 1577 1578 1579 1580 1581 1582 1583 1584 1585 1586 1587 1588 1589 1590 1591 1592 1593 1594 1595 1596 1597 1598 1599 1600 1601 1602 1603 1604 1605 1606 1607 1608 1609 1610 1611 1612 1613 1614 1615 1616 1617 1618 1619 1620 1621 1622 1623 1624 1625 1626 1627 1628 1629 1630 1631 1632 1633 1634 1635 1636 1637 1638
--echo #
--echo # MDEV-11745: window function with min/max 
--echo #

create table t1 (i int, b int);
insert into t1 values
  (1,1),(2,1),(3,1),(4,4),(5,4),(6,4),(7,8),(8,8),(9,8),(10,8);

select b, min(i) over (partition by b) as f 
  from t1 as tt
order by i;

select b, min(i) over (partition by b) as f 
  from (select * from t1) as tt
order by i;

select b, min(i+10) over (partition by b) as f 
  from t1 as tt
order by i;

select b, min(i) over (partition by b) as f 
  from (select i+10 as i, b from t1) as tt
order by i;

select b, min(i+20) over (partition by b) as f 
  from (select i+10 as i, b from t1) as tt
order by i;

select b, max(i) over (partition by b) as f 
  from t1 as tt
order by i;

select b, max(i) over (partition by b) as f 
  from (select * from t1) as tt
order by i;

select b, max(i+10) over (partition by b) as f 
  from t1 as tt
order by i;

select b, max(i) over (partition by b) as f 
  from (select i+10 as i, b from t1) as tt
order by i;

select b, max(i+20) over (partition by b) as f 
  from (select i+10 as i, b from t1) as tt
order by i;

select max(i), max(i), sum(i), count(i)
  from t1 as tt
group by b;

select max(i), min(sum(i)) over (partition by count(i)) f
  from t1 as tt
group by b;

select max(i), min(sum(i)) over (partition by count(i)) f
  from (select * from t1) as tt
group by b;

select max(i+10), min(sum(i)+10) over (partition by count(i)) f
  from t1 as tt
group by b;

select max(i), max(i), sum(i), count(i)
  from (select i+10 as i, b from t1) as tt
group by b;

select max(i), min(sum(i)) over (partition by count(i)) f
  from (select i+10 as i, b from t1) as tt
group by b;

select max(i), min(i), min(max(i)-min(i)) over (partition by count(i)) f
  from (select i+10 as i, b from t1) as tt
group by b;

drop table t1;
Igor Babaev's avatar
Igor Babaev committed
1639 1640 1641 1642 1643 1644 1645 1646 1647 1648 1649 1650 1651 1652 1653

--echo #
--echo # MDEV-12015: window function over select with WHERE  
--echo #             that is always FALSE
--echo #

CREATE TABLE t1 (i INT);
INSERT INTO t1 VALUES (3), (1), (2);

SELECT i, ROW_NUMBER() OVER () FROM t1 WHERE 1 = 2;

SELECT i, COUNT(*) OVER () FROM t1 WHERE 1 = 2;

DROP TABLE t1;

1654 1655 1656 1657 1658 1659 1660 1661 1662 1663 1664 1665 1666 1667 1668 1669 1670 1671 1672 1673 1674 1675 1676 1677 1678 1679 1680 1681 1682 1683 1684 1685 1686 1687 1688 1689 1690 1691 1692 1693 1694 1695 1696 1697 1698 1699 1700 1701 1702 1703 1704 1705 1706 1707 1708 1709 1710 1711 1712 1713
--echo #
--echo # MDEV-12051: window function in query with implicit grouping 
--echo #             on always empty set
--echo #

create table t1 (a int, b varchar(8));
insert into t1 values (1,'foo'),(2,'bar');

select max(a), row_number() over () from t1 where a > 10;
select max(a), sum(max(a)) over () from t1 where a > 10;
select max(a), sum(max(a)) over (partition by max(a)) from t1 where a > 10;

select max(a), row_number() over () from t1 where 1 = 2;
select max(a), sum(max(a)) over () from t1 where 1 = 2;
select max(a), sum(max(a)) over (partition by max(a)) from t1 where 1 = 2;

select max(a), row_number() over () from t1 where 1 = 2
  having max(a) is not null;
select max(a), sum(max(a)) over () from t1 where 1 = 2
  having max(a) is not null;

drop table t1;

--echo #
--echo # MDEV-10885: window function in query with implicit grouping 
--echo #             with constant condition evaluated to false
--echo #

CREATE TABLE t1 (a INT, b VARCHAR(8));
INSERT INTO t1 VALUES (1,'foo'),(2,'bar');

CREATE TABLE t2 (c INT);
INSERT INTO t2 VALUES (3),(4);
 
CREATE TABLE t3 (d INT);
INSERT INTO t3 VALUES (5),(6);

SELECT MAX(a), ROW_NUMBER() OVER (PARTITION BY MAX(a)) FROM t1
WHERE EXISTS ( SELECT * FROM t2 WHERE c IN ( SELECT MAX(d) FROM t3 ) );

SELECT MAX(a), COUNT(MAX(a)) OVER (PARTITION BY MAX(a)) FROM t1
WHERE EXISTS ( SELECT * FROM t2 WHERE c IN ( SELECT MAX(d) FROM t3 ) );

SELECT MAX(a), SUM(MAX(a)) OVER (PARTITION BY MAX(a)) FROM t1
WHERE EXISTS ( SELECT * FROM t2 WHERE c IN ( SELECT MAX(d) FROM t3 ) );

SELECT MAX(a), ROW_NUMBER() OVER (PARTITION BY MAX(a)) FROM t1
WHERE EXISTS ( SELECT * FROM t2 WHERE c IN ( SELECT MAX(d) FROM t3 ) )
HAVING MAX(a) IS NOT NULL;

SELECT a, MAX(a), ROW_NUMBER() OVER (PARTITION BY b) FROM t1
WHERE EXISTS ( SELECT * FROM t2 WHERE c IN ( SELECT MAX(d) FROM t3 ) );

SELECT a, COUNT(a), AVG(a) OVER (PARTITION BY b) FROM t1
WHERE EXISTS ( SELECT * FROM t2 WHERE c IN ( SELECT MAX(d) FROM t3 ) );

SELECT a, MAX(a), AVG(a) OVER (PARTITION BY b) FROM t1
WHERE EXISTS ( SELECT * FROM t2 WHERE c IN ( SELECT MAX(d) FROM t3 ) );

DROP TABLE t1,t2,t3;
1714 1715 1716 1717 1718 1719 1720 1721 1722 1723 1724 1725 1726 1727 1728 1729 1730 1731 1732 1733 1734 1735 1736

--echo #
--echo # MDEV-10859: Wrong result of aggregate window function in query
--echo #                  with HAVING and no ORDER BY
--echo #

create table empsalary (depname varchar(32), empno smallint primary key, salary int);
insert into empsalary values
  ('develop', 1, 5000), ('develop', 2, 4000),('sales', 3, '6000'),('sales', 4, 5000);

--sorted_result
SELECT depname, empno, salary, avg(salary) OVER (PARTITION BY depname) FROM empsalary;
--sorted_result
SELECT depname, empno, salary, avg(salary) OVER (PARTITION BY depname) FROM empsalary ORDER BY depname;
--echo #
--echo # These last 2 should have the same row results, ignoring order.
--echo #
--sorted_result
SELECT depname, empno, salary, avg(salary) OVER (PARTITION BY depname) FROM empsalary HAVING empno > 1;
--sorted_result
SELECT depname, empno, salary, avg(salary) OVER (PARTITION BY depname) FROM empsalary HAVING empno > 1 ORDER BY depname;

drop table empsalary;
1737 1738 1739 1740 1741 1742 1743 1744 1745 1746 1747 1748 1749 1750 1751 1752 1753 1754 1755 1756 1757 1758 1759 1760 1761 1762 1763 1764 1765 1766 1767 1768 1769 1770 1771 1772 1773 1774 1775 1776

--echo #
--echo # MDEV-11868: min(distinct) over () returns wrong value
--echo #

create table TDEC (CDEC int, RNUM int);
create view VDEC as select * from TDEC;
insert into TDEC (CDEC) values (null),(-1),(0),(1),(0),(10);
select TDEC.CDEC, min(TDEC.CDEC) over () from TDEC;
select VDEC.CDEC, min(VDEC.CDEC) over () from VDEC;
select TDEC.CDEC, max(TDEC.CDEC) over () from TDEC;
select VDEC.CDEC, max(VDEC.CDEC) over () from VDEC;

select TDEC.CDEC, min(distinct TDEC.CDEC) over () from TDEC;
select VDEC.CDEC, min(distinct VDEC.CDEC) over () from VDEC;
select TDEC.CDEC, max(distinct TDEC.CDEC) over () from TDEC;
select VDEC.CDEC, max(distinct VDEC.CDEC) over () from VDEC;

--echo #
--echo # These should be removed once support for them is added.
--echo #
--error ER_NOT_SUPPORTED_YET
select TDEC.CDEC, count(distinct TDEC.CDEC) over () from TDEC;
--error ER_NOT_SUPPORTED_YET
select VDEC.CDEC, count(distinct VDEC.CDEC) over () from VDEC;
--error ER_NOT_SUPPORTED_YET
select TDEC.CDEC, sum(distinct TDEC.CDEC) over () from TDEC;
--error ER_NOT_SUPPORTED_YET
select VDEC.CDEC, sum(distinct VDEC.CDEC) over () from VDEC;
--error ER_NOT_SUPPORTED_YET
select TDEC.CDEC, avg(distinct TDEC.CDEC) over () from TDEC;
--error ER_NOT_SUPPORTED_YET
select VDEC.CDEC, avg(distinct VDEC.CDEC) over () from VDEC;
--error ER_NOT_SUPPORTED_YET
select TDEC.CDEC, GROUP_CONCAT(TDEC.CDEC) over () from TDEC;
--error ER_NOT_SUPPORTED_YET
select VDEC.CDEC, GROUP_CONCAT(distinct VDEC.CDEC) over () from VDEC;

drop table TDEC;
drop view VDEC;
1777 1778 1779 1780 1781 1782 1783 1784 1785 1786 1787 1788 1789 1790 1791 1792 1793 1794 1795

--echo #
--echo # MDEV-10700: 10.2.2 windowing function returns incorrect result
--echo #
create table t(a int,b int, c int , d int);
insert into t(a,b,c,d) values(1, rand(10)*1000, rand(10)*1000, rand(10)*1000);
insert into t(a,b,c,d) values(1, rand(10)*1000, rand(10)*1000, rand(10)*1000);
replace into t(a,b,c,d) select 1, rand(10)*1000, rand(10)*1000, rand(10)*1000 from t t1, t t2, t t3, t t4, t t5, t t6, t t7, t t8, t t9, t t10, t t11, t t12, t t13, t t14, t t15, t t16, t t17;

select count(distinct s) from (select sum(d) over(partition by a,b,c) as s from t) Z where s > 0;
select count(distinct s) from (select sum(d) as s from t group by a,b,c) Z where s > 0;

select count(distinct s) from (select sum(d) over(partition by a,b) as s from t) Z where s > 0;
select count(distinct s) from (select sum(d) as s from t group by a,b) Z where s > 0;

select count(distinct s) from (select sum(d) over(partition by a) as s from t) Z where s > 0;
select count(distinct s) from (select sum(d) as s from t group by a) Z where s > 0;

drop table t;
Igor Babaev's avatar
Igor Babaev committed
1796 1797 1798 1799 1800 1801 1802 1803 1804 1805 1806 1807 1808 1809 1810 1811

--echo #
--echo # MDEV-9924: window function in query with group by optimized away 
--echo #

create table t1 (i int);
insert into t1 values (2),(3),(1);

select row_number() over () from t1 group by 1+2;
select max(i), row_number() over () from t1 group by 1+2;
select rank() over (order by max(i)) from t1 group by 1+2;

select i, row_number() over () from t1 group by 1+2;
select i, rank() over (order by i) rnk from t1 group by 1+2;

drop table t1;
Igor Babaev's avatar
Igor Babaev committed
1812 1813 1814 1815 1816 1817 1818 1819 1820 1821 1822 1823 1824 1825 1826 1827 1828 1829 1830 1831 1832

--echo #
--echo # MDEV-11907: window function as the second operand of division
--echo #

create table t1 (pk int, c int);
insert into t1 values (1,1),(2,1),(3,1),(4,1),(5,2);

set @sql_mode_save= @@sql_mode;
set sql_mode='ERROR_FOR_DIVISION_BY_ZERO';

select pk, c, c/count(*) over 
           (partition by c order by pk
            rows between 1 preceding and 2 following) as CNT
from t1;
show warnings;

set sql_mode=@sql_mode_save;

drop table t1;

Igor Babaev's avatar
Igor Babaev committed
1833 1834 1835 1836 1837 1838 1839 1840 1841 1842 1843 1844 1845 1846 1847 1848 1849 1850 1851 1852 1853 1854 1855 1856 1857 1858
--echo #
--echo # MDEV-12336: several functions over a window function 
--echo #
  
create table t1 (name varchar(10), cnt int);
insert into t1 values ('Fred', 23), ('Fred', 35), ('Joe', 10);

select q.name, q.row_cnt, 
       round( 100 * ( q.row_cnt /
                      sum(q.row_cnt) over
                        ( 
                          order by q.name 
                          rows between
                            unbounded preceding and
                            unbounded following
                        )
                    ),2
            ) pct_of_total
from 
( 
  select name, count(*) row_cnt, sum(cnt) sum_cnt 
  from t1 
  group by 1
) q;

drop table t1;
Igor Babaev's avatar
Igor Babaev committed
1859 1860 1861 1862 1863 1864 1865 1866 1867 1868 1869 1870 1871 1872 1873 1874 1875 1876 1877 1878 1879

--echo #
--echo # MDEV-11990: window function over min/max aggregation
--echo #

create table t1 (id int);
insert into t1 values (1), (2), (3), (2), (4), (2);

select sum(max(id)) over (order by max(id)) from t1;
explain
select sum(max(id)) over (order by max(id)) from t1;

create index idx on t1(id);
select sum(max(id)) over (order by max(id)) from t1;
explain
select sum(max(id)) over (order by max(id)) from t1;
select sum(max(id)) over (order by max(id)) from t1 where id < 3;
select count(max(id)) over (order by max(id)) from t1 where id < 3;
select max(id), rank() over (order by max(id)) from t1 where id < 3;

drop table t1;
1880 1881 1882 1883 1884 1885 1886 1887 1888 1889

--echo #
--echo # main.win failure post MDEV-12336
--echo #
create table t(a decimal(35,10), b int);
insert into t values (1, 10), (2, 20), (3, 30);

prepare stmt from "SELECT (CASE WHEN sum(t.a) over (partition by t.b)=1 THEN 1000 ELSE 300 END) AS a FROM t";
execute stmt;
drop table t;
1890 1891 1892 1893 1894 1895 1896 1897 1898 1899 1900 1901 1902 1903 1904 1905 1906 1907 1908 1909 1910 1911 1912 1913 1914 1915 1916

--echo #
--echo # MDEV-12851 case with window functions query crashes server
--echo #

create table t1(dt datetime);
insert into t1 values ('2017-05-17'), ('2017-05-18');
select dt,
       case when (max(dt) over (order by dt rows between 1 following and 1 following) is null)
       then '9999-12-31 12:00:00'
       else max(dt) over (order by dt rows between 1 following and 1 following)
       end x,
       case when (max(dt) over (order by dt rows between 1 following and 1 following) is not null)
       then '9999-12-31 12:00:00'
       else max(dt) over (order by dt rows between 1 following and 1 following)
       end x
from t1;

drop table t1;

create table t1(i int);
insert into t1 values (null),(1),(2);
select max(i) over (order by i),
       max(i) over (order by i) is null,
       max(i) over (order by i) is not null
from t1;
drop table t1;
1917 1918 1919 1920 1921 1922 1923 1924 1925

--echo #
--echo # MDEV-13189: Window functions crash when using INTERVAL function
--echo #
create table t1(i int);
insert into t1 values (1),(2),(10),(20),(30);
select sum(i) over (order by i), interval(sum(i) over (order by i), 10, 20)
from t1;
drop table t1;
1926 1927 1928 1929 1930 1931 1932 1933 1934 1935

--echo #
--echo # MDEV-13352: Server crashes in st_join_table::remove_duplicates
--echo #
CREATE TABLE t1 (i INT);
INSERT INTO t1 VALUES (1),(2);
SELECT DISTINCT ROW_NUMBER() OVER(), i FROM t1 WHERE 0;
SELECT ROW_NUMBER() OVER(), i FROM t1 WHERE 0;
DROP TABLE t1;

1936 1937 1938 1939 1940 1941 1942 1943 1944
--echo #
--echo # MDEV-13344: Server crashes in in AGGR_OP::put_record on subquery 
--echo #             with window function and constant table
--echo # (Testcase only)
--echo #
CREATE TABLE t1 (c CHAR(8)) ENGINE=MyISAM;
INSERT IGNORE INTO t1 VALUES ('foo');
SELECT ('bar',1) IN ( SELECT c, ROW_NUMBER() OVER (PARTITION BY c) FROM t1);
DROP TABLE t1;
1945