win.test 25.4 KB
Newer Older
1 2 3 4 5 6 7 8
#
#  Window Functions Tests
#

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

9 10 11
--echo # ########################################################################
--echo # # Parser tests
--echo # ########################################################################
12 13 14 15 16 17 18 19 20 21
--echo #
--echo #  Check what happens when one attempts to use window function without OVER clause 
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;

22
--echo # Attempt to use window function in the WHERE clause
23
--error ER_WRONG_PLACEMENT_OF_WINDOW_FUNCTION 
24
select * from t1 where 1=rank() over (order by a);
25
--error ER_WRONG_PLACEMENT_OF_WINDOW_FUNCTION
26
select * from t1 where 1>row_number() over (partition by b order by a);
27 28
drop table t1;

29 30 31
--echo # ########################################################################
--echo # # Functionality tests
--echo # ########################################################################
32
--echo #
33
--echo #  Check if ROW_NUMBER() works in basic cases
34 35 36 37 38 39 40
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');

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

44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61
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);
insert into t1 values 
  (1, 10, 22),
  (2, 11, 21),
  (3, 12, 20),
  (4, 13, 19),
  (5, 14, 18);

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

62
drop table t1;
63

64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84
--echo #
--echo # Try RANK() function
--echo #
create table t2 (
  pk int primary key, 
  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);

select pk, a, rank() over (order by a) from t2;
85
select pk, a, rank() over (order by a desc) from t2;
86 87 88

drop table t2;

89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112
--echo #
--echo # Try DENSE_RANK() function
--echo #
create table t3 (
  pk int primary key,
  a int,
  b int
);

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

select pk, a, b, rank() over (order by a), dense_rank() over (order by a) from t3;
select pk, a, b, rank() over (partition by b order by a), dense_rank() over (partition by b order by a) from t3;

Vicențiu Ciorbaru's avatar
Vicențiu Ciorbaru committed
113
drop table t3;
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

--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;

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

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

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

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

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

select
  pk, c,  
  count(*) over (partition by c order by pk
                 rows between current row and 1 following) as CNT 
from t1;
Igor Babaev's avatar
Igor Babaev committed
161

162 163 164 165 166 167 168
--echo # Check ORDER BY DESC
select 
  pk, c, 
  count(*) over (partition by c order by pk desc
                 rows between 2 preceding and 2 following) as CNT
from t1;

Igor Babaev's avatar
Igor Babaev committed
169 170 171 172 173 174 175 176 177 178 179 180 181 182 183
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;

select 
184
  pk, c,
Igor Babaev's avatar
Igor Babaev committed
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 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 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291
  count(*) over w1 as CNT
from t1
window w1 as (partition by c order by pk 
              rows between 2 preceding and 2 following);

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

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

select 
  pk, c, 
  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);

select 
  pk, c, 
  count(*) over w3 as CNT
from t1
window
  w1 as (partition by c), 
  w2 as (w1 order by pk),
  w3 as (w2 rows between 2 preceding and 2 following);

--error ER_WRONG_WINDOW_SPEC_NAME
select 
  pk, c, 
  count(*) over w as CNT
from t1
window w1 as (partition by c order by pk 
              rows between 2 preceding and 2 following);

--error ER_DUP_WINDOW_NAME
select 
  pk, c, 
  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
select 
  pk, c, 
  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
select 
  pk, c, 
  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
select 
  pk, c, 
  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
select 
  pk, c, 
  count(*) over w3 as CNT
from t1
window
  w1 as (partition by c), 
  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
select 
  pk, c, 
  count(*) over w1 as CNT
from t1
window w1 as (partition by c order by pk 
              rows between unbounded following and 2 following);

--error ER_BAD_COMBINATION_OF_WINDOW_FRAME_BOUND_SPECS
select 
  pk, c, 
  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
select 
  pk, c, 
  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
select 
  pk, c, 
  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);

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
--error ER_WRONG_PLACEMENT_OF_WINDOW_FUNCTION 
select 
  pk, c 
from t1 where rank() over w1 > 2
window w1 as (partition by c order by pk);

--error ER_WRONG_PLACEMENT_OF_WINDOW_FUNCTION 
select
  c, max(pk) as m
from t1 
  group by c + rank() over w1
window w1 as (order by m);

--error ER_WRONG_PLACEMENT_OF_WINDOW_FUNCTION 
select
  c, max(pk) as m, rank() over w1 as r 
from t1 
  group by c+r
window w1 as (order by m);

--error ER_WRONG_PLACEMENT_OF_WINDOW_FUNCTION 
select
  c, max(pk) as m, rank() over w1 as r
from t1 
  group by c having c+r > 3
window w1 as (order by m);

--error ER_WINDOW_FUNCTION_IN_WINDOW_SPEC 
select
  c, max(pk) as m, rank() over w1 as r, 
    rank() over (partition by r+1 order by m) 
from t1 
  group by c
window w1 as (order by m);

--error ER_WINDOW_FUNCTION_IN_WINDOW_SPEC 
select
  c, max(pk) as m, rank() over w1 as r, 
    rank() over (partition by m order by r) 
from t1 
  group by c
window w1 as (order by m);

--error ER_WINDOW_FUNCTION_IN_WINDOW_SPEC 
select
  c, max(pk) as m, rank() over w1 as r, dense_rank() over w2 as dr  
from t1 
  group by c
window w1 as (order by m), w2 as (partition by r order by m);

342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362
--error ER_NOT_ALLOWED_WINDOW_FRAME
select 
  pk, c, 
  row_number() over (partition by c order by pk 
                     range between unbounded preceding and current row) as r 
from t1;

--error ER_NOT_ALLOWED_WINDOW_FRAME
select 
  pk, c, 
  rank() over w1 as r
from t1
window w1 as (partition by c order by pk 
              rows between 2 preceding and 2 following);

--error ER_NOT_ALLOWED_WINDOW_FRAME
select 
  pk, c, 
  dense_rank() over (partition by c order by pk
                    rows between 1 preceding and 1 following) as r
from t1;
Igor Babaev's avatar
Igor Babaev committed
363

364 365 366 367 368 369 370 371 372 373 374 375 376
--error ER_NO_ORDER_LIST_IN_WINDOW_SPEC
select 
  pk, c, 
  rank() over w1 as r
from t1
window w1 as (partition by c);

--error ER_NO_ORDER_LIST_IN_WINDOW_SPEC
select 
  pk, c, 
  dense_rank() over (partition by c) as r
from t1;

377 378
drop table t0,t1;

379 380 381
--echo #
--echo # MDEV-9634: Window function produces incorrect value
--echo #
Igor Babaev's avatar
Igor Babaev committed
382

383 384 385 386 387 388
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);
insert into t2 select 
  if(a<5, 0, 1), a, if(a<5, NULL, 1) from t0;
select * from t2;
Igor Babaev's avatar
Igor Babaev committed
389

390 391 392 393 394
select 
  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;
Igor Babaev's avatar
Igor Babaev committed
395

396
drop table t0, t2;
Igor Babaev's avatar
Igor Babaev committed
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 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478
--echo #
--echo #  RANGE-type bounds 
--echo #

create table t3 (
  pk int, 
  val int
);

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

select 
  pk, 
  val,
  count(val) over (order by val 
                 range between current row and 
                               current row) 
  as CNT
from t3;

insert into t3 values 
(7, 3),
(8, 3);

select 
  pk, 
  val,
  count(val) over (order by val 
                 range between current row and 
                               current row) 
  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);

select 
  part_id,
  pk, 
  val,
  count(val) over (partition by part_id
                   order by val 
                   range between current row and 
                               current row) 
  as CNT
from t4;

479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514
--echo #
--echo # Try RANGE UNBOUNDED PRECEDING | FOLLOWING
--echo #
select 
  part_id,
  pk, 
  val,
  count(val) over (partition by part_id
                   order by val 
                   range between unbounded preceding and 
                               current row) 
  as CNT
from t4;

select 
  part_id,
  pk, 
  val,
  count(val) over (partition by part_id
                   order by val 
                   range between current row and 
                                 unbounded following) 
  as CNT
from t4;

select 
  part_id,
  pk, 
  val,
  count(val) over (partition by part_id
                   order by val 
                   range between unbounded preceding and 
                                 unbounded following) 
  as CNT
from t4;

515 516
drop table t4;

517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534
--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);
 
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;

535 536 537 538
--echo # Extra ROWS n PRECEDING tests 
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;
539
drop table t1;
540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581


create table t2 (
  pk int, 
  a int, 
  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;

--echo # Check CURRENT ROW 

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;

582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608
--echo #
--echo # Try RANGE PRECEDING|FOLLWING n
--echo #
create table t1 (
  part_id int,
  pk int,
  a int
);

insert into t1 values 
(10, 1, 1),
(10, 2, 2),
(10, 3, 4),
(10, 4, 8),
(10, 5,26), 
(10, 6,27),
(10, 7,40),
(10, 8,71),
(10, 9,72);

select
  pk, a,
  count(a) over (ORDER BY a
                 RANGE BETWEEN UNBOUNDED PRECEDING 
                 AND 10 FOLLOWING) as cnt
from t1;

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

616 617 618 619 620 621 622 623 624 625 626 627 628 629
select
  pk, a,
  count(a) over (ORDER BY a
                 RANGE BETWEEN UNBOUNDED PRECEDING 
                 AND 1 FOLLOWING) as cnt
from t1;

select
  pk, a,
  count(a) over (ORDER BY a
                 RANGE BETWEEN UNBOUNDED PRECEDING 
                 AND 10 PRECEDING) as cnt
from t1;

630 631 632 633 634 635 636
select
  pk, a,
  count(a) over (ORDER BY a DESC
                 RANGE BETWEEN UNBOUNDED PRECEDING 
                 AND 10 PRECEDING) as cnt
from t1;

637 638 639 640 641 642 643 644 645 646 647 648 649 650 651
select
  pk, a,
  count(a) over (ORDER BY a
                 RANGE BETWEEN UNBOUNDED PRECEDING 
                 AND 1 PRECEDING) as cnt
from t1;

# Try bottom bound 
select
  pk, a,
  count(a) over (ORDER BY a
                 RANGE BETWEEN 1 PRECEDING 
                 AND CURRENT ROW) as cnt
from t1;

652 653 654 655 656 657 658
select
  pk, a,
  count(a) over (ORDER BY a DESC
                 RANGE BETWEEN 1 PRECEDING 
                 AND CURRENT ROW) as cnt
from t1;

659 660 661 662 663 664 665
select
  pk, a,
  count(a) over (ORDER BY a
                 RANGE BETWEEN 1 FOLLOWING 
                 AND 3 FOLLOWING) as cnt
from t1;

666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681
--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;


682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701
# 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
                 RANGE BETWEEN UNBOUNDED PRECEDING 
                 AND 10 FOLLOWING) as cnt
from t1;

select
  pk, a,
  count(a) over (PARTITION BY part_id
                 ORDER BY a
                 RANGE BETWEEN UNBOUNDED PRECEDING 
                 AND 1 PRECEDING) as cnt
from t1;

drop table t1;

702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723
--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
  a, 
  count(col1) over (order by a 
                    range between 0.1 preceding 
                                  and 0.1 following) 
from t1;

drop table t1;
724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750

--echo # 
--echo # RANGE-type frames and NULL values 
--echo # 
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);


select 
 pk, a, 
 count(b) over (order by a 
                range between 2 preceding 
                          and 2 following) as CNT
from t1;
drop table t1;
751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783

--echo # 
--echo #  Try ranges that have bound1 > bound2.  The standard actually allows them
--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;

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


select
  pk, c,
  count(*) over (partition by c
                 order by pk
                 range between 1 preceding
                           and 2 preceding) 
                as cnt
from t1;
drop table t0, t1;

784 785 786 787 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 824 825 826 827
--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
select 
  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;

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 857 858 859
--echo #
--echo # EXCLUDE clause is parsed but not supported 
--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;

860
drop table t1;
861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903

--echo # 
--echo #  Window function in grouping query 
--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, 
  rank() over (order by s desc)
from t1
group by username;

drop table t1;

--echo # 
--echo #  mdev-9719: Window function in prepared statement 
--echo # 

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');
 
prepare stmt from 'select a, row_number() over (partition by a order by b) from t1';
execute stmt;

drop table t1;

Igor Babaev's avatar
Igor Babaev committed
904 905 906
--echo # 
--echo #  mdev-9754: Window name resolution in prepared statement 
--echo # 
907

Igor Babaev's avatar
Igor Babaev committed
908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925
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;
 
prepare stmt from 
'select 
  pk, c, 
  count(*) over w1 as CNT
from t1
window w1 as (partition by c order by pk 
              rows between 2 preceding and 2 following)';
execute stmt;

drop table t0,t1;
926

927 928 929 930 931
--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);
932

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

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

938 939 940 941 942 943 944 945 946 947 948 949 950 951 952
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;

953 954 955 956 957 958 959 960
--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);

961 962
drop table t1;
drop table t0;
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 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016
--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,
       sum(c) over (w2 rows between 1 preceding and 1 following) as c5,  
       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
from t1 
window w1 as (partition by a order by b,pk),
       w2 as (partition by b order by c,pk);

drop table t1;

1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060

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

create table t1 (a int, b int, c int);
insert into t1 values 
(1,3,1),
(2,2,1),
(3,1,1);

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

flush status;
select 
  rank() over (partition by c order by a),
  rank() over (partition by c order by a)
from t1;
show status like '%sort%';

# Check using EXPLAIN FORMAT=JSON 
explain format=json
select 
  rank() over (partition by c order by a),
  rank() over (partition by c order by a)
from t1;

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

explain format=json
select 
  rank() over (partition by c order by a),
  count(*) over (partition by c)
from t1;
1061 1062 1063 1064 1065 1066

explain format=json
select 
  count(*) over (partition by c),
  rank() over (partition by c order by a)
from t1;
1067 1068 1069

drop table t1;

1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081

--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;

1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094
--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
1095 1096
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;
1097 1098
drop table t1;

1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115
--echo #
--echo # Try window functions that are not directly present in the select list
--echo #
create table t1 (a int, b int);
insert into t1 values 
  (1,3),
  (2,2),
  (3,1);

select 
  rank() over (order by a) -
  rank() over (order by b)
from
  t1;

drop table t1;

1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126
--echo #
--echo # MDEV-9894: Assertion `0' failed in Window_func_runner::setup
--echo #  return ER_NOT_SUPPORTED_YET for aggregates that are not yet supported
--echo #  as window functions.
--echo #
create table t1 (i int);
insert into t1 values (1),(2);
--error ER_NOT_SUPPORTED_YET
SELECT MAX(i) OVER (PARTITION BY (i)) FROM t1;
drop table t1;

1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166
--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);

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

select 
  pk, a, 
  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);
select 
  part_id, pk, a,
  sum(a) over (partition by part_id order by pk rows between 0 preceding and current row)
from t1;

select 
  part_id, pk, a, 
  sum(a) over (partition by part_id order by pk rows between 1 preceding and 0 preceding)
from t1;

drop table t1;
1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189
--echo #
--echo # MDEV-9780, The "DISTINCT must not bet converted into GROUP BY when
--echo #                 window functions are present" part 
--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;
1190