1# Test of SQL window functions. 2# ---------------------------------------------------------------------- 3SET NAMES utf8; 4Warnings: 5Warning 3719 'utf8' is currently an alias for the character set UTF8MB3, but will be an alias for UTF8MB4 in a future release. Please consider using UTF8MB4 in order to be unambiguous. 6 7Flag unsupported aggregates as window functions 8 9select group_concat('3') over (); 10ERROR 42000: This version of MySQL doesn't yet support 'group_concat as window function' 11Single window function (plus ORDER BY). 12CREATE TABLE t(i INT, j INT); 13INSERT INTO t VALUES (1,1); 14INSERT INTO t VALUES (1,4); 15INSERT INTO t VALUES (1,2); 16INSERT INTO t VALUES (1,4); 17ANALYZE TABLE t; 18Table Op Msg_type Msg_text 19test.t analyze status OK 20Single partition, no sorting 21SELECT i, j, SUM(i+j) OVER (ROWS UNBOUNDED PRECEDING) foo FROM t; 22i j foo 231 1 2 241 4 7 251 2 10 261 4 15 27SELECT i, j, SUM(i+j) OVER (ROWS BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW) foo FROM t; 28i j foo 291 1 2 301 4 7 311 2 10 321 4 15 33SELECT i, j, SUM(i+j) OVER (ROWS UNBOUNDED PRECEDING) foo FROM t ORDER BY foo; 34i j foo 351 1 2 361 4 7 371 2 10 381 4 15 39SELECT i, j, SUM(i+j) OVER (ROWS UNBOUNDED PRECEDING) foo FROM t ORDER BY foo DESC; 40i j foo 411 4 15 421 2 10 431 4 7 441 1 2 45Check that we eliminate redundant sorting in ORDER BY even with wfs 46Also check that EXPLAIN prints the right number of "using_temporary_table" 47FLUSH STATUS; 48SELECT i, j, SUM(i+j) OVER (ROWS UNBOUNDED PRECEDING) foo FROM t ORDER BY NULL DESC; 49i j foo 501 1 2 511 4 7 521 2 10 531 4 15 54SHOW STATUS LIKE 'Created_tmp_tables'; 55Variable_name Value 56Created_tmp_tables 0 57EXPLAIN FORMAT=JSON SELECT i, j, SUM(i+j) OVER (ROWS UNBOUNDED PRECEDING) foo FROM t ORDER BY NULL DESC; 58EXPLAIN 59{ 60 "query_block": { 61 "select_id": 1, 62 "cost_info": { 63 "query_cost": "0.65" 64 }, 65 "ordering_operation": { 66 "using_filesort": false, 67 "windowing": { 68 "windows": [ 69 { 70 "name": "<unnamed window>", 71 "functions": [ 72 "sum" 73 ] 74 } 75 ], 76 "table": { 77 "table_name": "t", 78 "access_type": "ALL", 79 "rows_examined_per_scan": 4, 80 "rows_produced_per_join": 4, 81 "filtered": "100.00", 82 "cost_info": { 83 "read_cost": "0.25", 84 "eval_cost": "0.40", 85 "prefix_cost": "0.65", 86 "data_read_per_join": "64" 87 }, 88 "used_columns": [ 89 "i", 90 "j" 91 ] 92 } 93 } 94 } 95 } 96} 97Warnings: 98Note 1003 /* select#1 */ select `test`.`t`.`i` AS `i`,`test`.`t`.`j` AS `j`,sum((`test`.`t`.`i` + `test`.`t`.`j`)) OVER (ROWS BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW) AS `foo` from `test`.`t` order by NULL desc 99With LIMIT 100SELECT i, j, SUM(i+j) OVER (ROWS UNBOUNDED PRECEDING) foo FROM t ORDER BY foo DESC LIMIT 3; 101i j foo 1021 4 15 1031 2 10 1041 4 7 105With LIMIT when last tmp file step is optimized away 106CREATE TABLE t1 (i INT) ; 107INSERT INTO t1 (i) VALUES (1); 108INSERT INTO t1 (i) VALUES (2); 109INSERT INTO t1 (i) VALUES (3); 110INSERT INTO t1 (i) VALUES (4); 111INSERT INTO t1 (i) VALUES (5); 112SELECT i, SUM(i) OVER (ORDER BY i ROWS BETWEEN 2 PRECEDING AND 2 FOLLOWING) FROM t1 LIMIT 3; 113i SUM(i) OVER (ORDER BY i ROWS BETWEEN 2 PRECEDING AND 2 FOLLOWING) 1141 6 1152 10 1163 15 117DROP TABLE t1; 118Single ordered partition 119SELECT i, j, SUM(i+j) OVER (ORDER BY j ROWS UNBOUNDED PRECEDING) foo FROM t; 120i j foo 1211 1 2 1221 2 5 1231 4 10 1241 4 15 125SELECT i, j, SUM(i+j) OVER (ORDER BY j ROWS UNBOUNDED PRECEDING) foo FROM t ORDER BY foo; 126i j foo 1271 1 2 1281 2 5 1291 4 10 1301 4 15 131SELECT i, j, SUM(i+j) OVER (ORDER BY j ROWS UNBOUNDED PRECEDING) foo FROM t ORDER BY foo DESC; 132i j foo 1331 4 15 1341 4 10 1351 2 5 1361 1 2 137SELECT i, j, SUM(i+j) OVER (ORDER BY j DESC ROWS UNBOUNDED PRECEDING) foo FROM t; 138i j foo 1391 4 5 1401 4 10 1411 2 13 1421 1 15 143SELECT i, j, SUM(i+j) OVER (ORDER BY jj DESC ROWS UNBOUNDED PRECEDING) foo FROM t; 144ERROR 42S22: Unknown column 'jj' in 'window order by' 145View with window function 146CREATE VIEW v AS 147SELECT i, j, SUM(i+j) OVER (ORDER BY j DESC ROWS UNBOUNDED PRECEDING) foo FROM t; 148SHOW CREATE VIEW v; 149View Create View character_set_client collation_connection 150v CREATE ALGORITHM=UNDEFINED DEFINER=`root`@`localhost` SQL SECURITY DEFINER VIEW `v` AS select `t`.`i` AS `i`,`t`.`j` AS `j`,sum((`t`.`i` + `t`.`j`)) OVER (ORDER BY `t`.`j` desc ROWS BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW) AS `foo` from `t` utf8 utf8_general_ci 151SELECT * FROM v; 152i j foo 1531 4 5 1541 4 10 1551 2 13 1561 1 15 157DROP VIEW v; 158SELECT i, j, SUM(i+j) OVER (ORDER BY j DESC ROWS UNBOUNDED PRECEDING) foo FROM t ORDER BY foo; 159i j foo 1601 4 5 1611 4 10 1621 2 13 1631 1 15 164SELECT i, j, SUM(i+j) OVER (ORDER BY j DESC ROWS UNBOUNDED PRECEDING) foo FROM t ORDER BY foo DESC; 165i j foo 1661 1 15 1671 2 13 1681 4 10 1691 4 5 170TRUNCATE TABLE t; 171Check my_decimal bug: no warning if c=a+b and c is one of a,b... just fails over 9 digits 172INSERT INTO t VALUES (999961560, DEFAULT); 173INSERT INTO t VALUES (44721, DEFAULT); 174SELECT SUM(i) OVER () FROM t; 175SUM(i) OVER () 1761000006281 1771000006281 178DROP TABLE t; 179CREATE TABLE t(i INT, j INT, k INT); 180INSERT INTO t VALUES (1,1,1); 181INSERT INTO t VALUES (1,4,1); 182INSERT INTO t VALUES (1,2,1); 183INSERT INTO t VALUES (1,4,1); 184INSERT INTO t VALUES (1,1,2); 185INSERT INTO t VALUES (1,4,2); 186INSERT INTO t VALUES (1,2,2); 187INSERT INTO t VALUES (1,4,2); 188INSERT INTO t VALUES (1,1,3); 189INSERT INTO t VALUES (1,4,3); 190INSERT INTO t VALUES (1,2,3); 191INSERT INTO t VALUES (1,4,3); 192INSERT INTO t VALUES (1,1,4); 193INSERT INTO t VALUES (1,4,4); 194INSERT INTO t VALUES (1,2,4); 195INSERT INTO t VALUES (1,4,4); 196---------------------------------------------------------------------- 197- Combination with GROUP BY 198---------------------------------------------------------------------- 199Show difference in binding of colums 200CREATE TABLE tb(a INT, b INT); 201Grouping version of SUM can refer to ungrouped column 202SELECT a, SUM(b) FROM tb GROUP BY a; 203a SUM(b) 204Windowing version of SUM cannot refer to ungrouped column since the 205grouping has already happened by the the the windowing starts 206and each row now represents multiple aggregated values of b 207SELECT a, SUM(b) OVER () FROM tb GROUP BY a; 208ERROR 42000: Expression #2 of SELECT list is not in GROUP BY clause and contains nonaggregated column 'test.tb.b' which is not functionally dependent on columns in GROUP BY clause; this is incompatible with sql_mode=only_full_group_by 209DROP TABLE tb; 210SELECT k, SUM(k) OVER (ROWS UNBOUNDED PRECEDING) wf FROM t; 211k wf 2121 1 2131 2 2141 3 2151 4 2162 6 2172 8 2182 10 2192 12 2203 15 2213 18 2223 21 2233 24 2244 28 2254 32 2264 36 2274 40 228SELECT k, MIN(i), SUM(j), SUM(k) OVER (ROWS UNBOUNDED PRECEDING) wf FROM t GROUP BY (k); 229k MIN(i) SUM(j) wf 2301 1 11 1 2312 1 11 3 2323 1 11 6 2334 1 11 10 234SELECT k, MIN(i), SUM(j), SUM(k) OVER (ROWS UNBOUNDED PRECEDING) wf FROM t GROUP BY (k) ORDER BY wf DESC; 235k MIN(i) SUM(j) wf 2364 1 11 10 2373 1 11 6 2382 1 11 3 2391 1 11 1 240SELECT k, GROUP_CONCAT(j ORDER BY j), SUM(k) OVER (ROWS UNBOUNDED PRECEDING) foo FROM t GROUP BY (k); 241k GROUP_CONCAT(j ORDER BY j) foo 2421 1,2,4,4 1 2432 1,2,4,4 3 2443 1,2,4,4 6 2454 1,2,4,4 10 246SELECT k, AVG(DISTINCT j), SUM(k) OVER (ROWS UNBOUNDED PRECEDING) foo FROM t GROUP BY (k); 247k AVG(DISTINCT j) foo 2481 2.3333 1 2492 2.3333 3 2503 2.3333 6 2514 2.3333 10 252SELECT k, GROUP_CONCAT(j ORDER BY j), SUM(k+1) OVER (ROWS UNBOUNDED PRECEDING) foo FROM t GROUP BY (k); 253k GROUP_CONCAT(j ORDER BY j) foo 2541 1,2,4,4 2 2552 1,2,4,4 5 2563 1,2,4,4 9 2574 1,2,4,4 14 258SELECT k, GROUP_CONCAT(j ORDER BY j), SUM(k+1) OVER (ORDER BY k DESC ROWS UNBOUNDED PRECEDING) foo FROM t GROUP BY (k); 259k GROUP_CONCAT(j ORDER BY j) foo 2604 1,2,4,4 5 2613 1,2,4,4 9 2622 1,2,4,4 12 2631 1,2,4,4 14 264SELECT i/SUM(j) OVER (PARTITION BY k) AS x FROM t GROUP BY x; 265ERROR 42000: Can't group on 'x' 266SELECT i/SUM(j) OVER (PARTITION BY kk) AS x FROM t; 267ERROR 42S22: Unknown column 'kk' in 'window partition by' 268SELECT i/SUM(j) OVER (PARTITION BY 1) AS x FROM t; 269ERROR HY000: Window '<unnamed window>': ORDER BY or PARTITION BY uses legacy position indication which is not supported, use expression. 270CREATE TABLE t1 (id INTEGER, sex CHAR(1)); 271INSERT INTO t1 VALUES (1, 'M'); 272INSERT INTO t1 VALUES (2, 'F'); 273INSERT INTO t1 VALUES (3, 'F'); 274INSERT INTO t1 VALUES (4, 'F'); 275INSERT INTO t1 VALUES (5, 'M'); 276INSERT INTO t1 VALUES (10, NULL); 277INSERT INTO t1 VALUES (11, NULL); 278CREATE TABLE ss(c CHAR(1)); 279INSERT INTO ss VALUES ('M'); 280This is grouped aggregate in conjunction with a streaming wfs 281SELECT sex, AVG(id), ROW_NUMBER() OVER w FROM t1 282GROUP BY sex 283WINDOW w AS () ORDER BY sex DESC; 284sex AVG(id) ROW_NUMBER() OVER w 285M 3.0000 1 286F 3.0000 2 287NULL 10.5000 3 288SELECT sex, AVG(id), SUM(AVG(id)) OVER w FROM t1 289GROUP BY sex 290WINDOW w AS (ROWS UNBOUNDED PRECEDING) ORDER BY sex DESC; 291sex AVG(id) SUM(AVG(id)) OVER w 292M 3.0000 3.0000 293F 3.0000 6.0000 294NULL 10.5000 16.5000 295This is grouped aggregate with HAVING in conjunction with a streaming wf 296which will initially adds a dummy window. However, the HAVING is pushed 297to a table condition: This exercises the try_remove_dummy_windowing_step 298which gets called from make_tmp_table_info, so make sure it works on 299second execution since we change initial windowing decision made in 300setup_windows only during prepare. 301PREPARE p FROM "SELECT sex, AVG(id), ROW_NUMBER() OVER w FROM t1 302 GROUP BY sex HAVING sex='M' OR sex IS NULL 303 WINDOW w AS () ORDER BY sex DESC"; 304EXECUTE p; 305sex AVG(id) ROW_NUMBER() OVER w 306M 3.0000 1 307NULL 10.5000 2 308EXECUTE p; 309sex AVG(id) ROW_NUMBER() OVER w 310M 3.0000 1 311NULL 10.5000 2 312DROP PREPARE p; 313SELECT sex, AVG(id), ROW_NUMBER() OVER w FROM t1 314GROUP BY sex HAVING sex='M' OR sex IS NULL 315WINDOW w AS () ORDER BY sex DESC; 316sex AVG(id) ROW_NUMBER() OVER w 317M 3.0000 1 318NULL 10.5000 2 319SELECT sex, AVG(id), SUM(AVG(id)) OVER w FROM t1 320GROUP BY sex HAVING sex='M' OR sex='F' OR sex IS NULL 321WINDOW w AS (ROWS UNBOUNDED PRECEDING) ORDER BY sex DESC; 322sex AVG(id) SUM(AVG(id)) OVER w 323M 3.0000 3.0000 324F 3.0000 6.0000 325NULL 10.5000 16.5000 326Ditto, but HAVING using subquery 327SELECT sex, AVG(id), ROW_NUMBER() OVER w FROM t1 328GROUP BY sex HAVING sex=(SELECT c FROM ss LIMIT 1) OR sex IS NULL 329WINDOW w AS () ORDER BY sex DESC; 330sex AVG(id) ROW_NUMBER() OVER w 331M 3.0000 1 332NULL 10.5000 2 333SELECT sex, AVG(id), SUM(AVG(id)) OVER w FROM t1 334GROUP BY sex HAVING sex=(SELECT c FROM ss LIMIT 1) OR sex='F' OR sex IS NULL 335WINDOW w AS (ROWS UNBOUNDED PRECEDING) ORDER BY sex DESC; 336sex AVG(id) SUM(AVG(id)) OVER w 337M 3.0000 3.0000 338F 3.0000 6.0000 339NULL 10.5000 16.5000 340This is a grouped aggregate in conjunction with a buffered wf 341which generates an extra tmp file step 342SELECT sex, AVG(id), NTILE(2) OVER w FROM t1 343GROUP BY sex 344WINDOW w AS (ORDER BY sex) ORDER BY sex DESC; 345sex AVG(id) NTILE(2) OVER w 346M 3.0000 2 347F 3.0000 1 348NULL 10.5000 1 349SELECT sex, AVG(id), SUM(AVG(id)) OVER w, NTILE(2) OVER w FROM t1 350GROUP BY sex 351WINDOW w AS (ORDER BY sex ROWS UNBOUNDED PRECEDING) ORDER BY sex DESC; 352sex AVG(id) SUM(AVG(id)) OVER w NTILE(2) OVER w 353M 3.0000 16.5000 2 354F 3.0000 13.5000 1 355NULL 10.5000 10.5000 1 356This is a grouped aggregate with HAVING in conjunction with a buffered wf 357which generates an extra tmp file step 358SELECT sex, AVG(id), NTILE(2) OVER w FROM t1 359GROUP BY sex HAVING sex=(SELECT c FROM ss LIMIT 1) OR sex IS NULL 360WINDOW w AS (ORDER BY sex) ORDER BY sex DESC; 361sex AVG(id) NTILE(2) OVER w 362M 3.0000 2 363NULL 10.5000 1 364SELECT sex, AVG(id), SUM(AVG(id)) OVER w, NTILE(2) OVER w FROM t1 365GROUP BY sex HAVING sex=(SELECT c FROM ss LIMIT 1) OR sex='F' OR sex IS NULL 366WINDOW w AS (ORDER BY sex ROWS UNBOUNDED PRECEDING) ORDER BY sex DESC; 367sex AVG(id) SUM(AVG(id)) OVER w NTILE(2) OVER w 368M 3.0000 16.5000 2 369F 3.0000 13.5000 1 370NULL 10.5000 10.5000 1 371Pure HAVING: In absence of filtering in the grouping sort step, 372make sure we filter before windowing steps 373SELECT sex, NTILE(2) OVER w, SUM(ASCII(sex)) OVER w s FROM t1 374HAVING sex=(SELECT c FROM ss LIMIT 1) 375WINDOW w AS (ORDER BY sex ROWS UNBOUNDED PRECEDING); 376sex NTILE(2) OVER w s 377M 1 77 378M 2 154 379Bug fix for prepared statements 380PREPARE p FROM "SELECT sex, AVG(id), SUM(AVG(id)) OVER w, NTILE(2) OVER w FROM t1 381 GROUP BY sex HAVING sex=(SELECT c FROM ss LIMIT 1) OR sex='F' OR sex IS NULL 382 WINDOW w AS (ORDER BY sex ROWS UNBOUNDED PRECEDING) ORDER BY sex DESC"; 383EXECUTE p; 384sex AVG(id) SUM(AVG(id)) OVER w NTILE(2) OVER w 385M 3.0000 16.5000 2 386F 3.0000 13.5000 1 387NULL 10.5000 10.5000 1 388EXECUTE p; 389sex AVG(id) SUM(AVG(id)) OVER w NTILE(2) OVER w 390M 3.0000 16.5000 2 391F 3.0000 13.5000 1 392NULL 10.5000 10.5000 1 393DROP PREPARE p; 394Tests with ROLLUP 395SELECT k, MIN(i), SUM(j), SUM(k) OVER (ROWS UNBOUNDED PRECEDING) wf FROM t 396GROUP BY (k) WITH ROLLUP; 397k MIN(i) SUM(j) wf 3981 1 11 1 3992 1 11 3 4003 1 11 6 4014 1 11 10 402NULL 1 44 10 403SELECT MIN(i), SUM(j), SUM(k) OVER (ROWS UNBOUNDED PRECEDING) wf FROM t 404GROUP BY (k) WITH ROLLUP; 405MIN(i) SUM(j) wf 4061 11 1 4071 11 3 4081 11 6 4091 11 10 4101 44 10 411SELECT k, MIN(i), SUM(j), SUM(k) OVER (ROWS UNBOUNDED PRECEDING) wf FROM t 412GROUP BY (k) WITH ROLLUP ORDER BY wf DESC; 413k MIN(i) SUM(j) wf 4141 1 11 1 4152 1 11 3 4163 1 11 6 4174 1 11 10 418NULL 1 44 10 419SELECT k, MIN(i), SUM(j), SUM(k) OVER (ROWS UNBOUNDED PRECEDING) wf FROM t 420GROUP BY k,j WITH ROLLUP; 421k MIN(i) SUM(j) wf 4221 1 1 1 4231 1 2 2 4241 1 8 3 4251 1 11 4 4262 1 1 6 4272 1 2 8 4282 1 8 10 4292 1 11 12 4303 1 1 15 4313 1 2 18 4323 1 8 21 4333 1 11 24 4344 1 1 28 4354 1 2 32 4364 1 8 36 4374 1 11 40 438NULL 1 44 40 439SELECT MIN(i), SUM(j), SUM(k) OVER (ROWS UNBOUNDED PRECEDING) wf FROM t 440GROUP BY k,j WITH ROLLUP; 441MIN(i) SUM(j) wf 4421 1 1 4431 2 2 4441 8 3 4451 11 4 4461 1 6 4471 2 8 4481 8 10 4491 11 12 4501 1 15 4511 2 18 4521 8 21 4531 11 24 4541 1 28 4551 2 32 4561 8 36 4571 11 40 4581 44 40 459SELECT sex, AVG(id), ROW_NUMBER() OVER w FROM t1 460GROUP BY sex WITH ROLLUP WINDOW w AS (); 461sex AVG(id) ROW_NUMBER() OVER w 462NULL 10.5000 1 463F 3.0000 2 464M 3.0000 3 465NULL 5.1429 4 466SELECT sex, AVG(id), ROW_NUMBER() OVER w FROM t1 467GROUP BY sex WITH ROLLUP HAVING sex='M' OR sex IS NULL 468WINDOW w AS (); 469sex AVG(id) ROW_NUMBER() OVER w 470NULL 10.5000 1 471M 3.0000 2 472NULL 5.1429 3 473SELECT sex, AVG(id) FROM t1 474GROUP BY sex WITH ROLLUP 475HAVING (sex='M' OR sex IS NULL) AND AVG(id)=3.0 476ORDER BY GROUPING(sex), sex; 477sex AVG(id) 478M 3.0000 479SELECT sex, AVG(id), ROW_NUMBER() OVER w FROM t1 480GROUP BY sex WITH ROLLUP 481HAVING (sex='M' OR sex IS NULL) AND AVG(id)=3.0 482WINDOW w AS (); 483sex AVG(id) ROW_NUMBER() OVER w 484M 3.0000 1 485 486Bug#25756549 487 488SELECT id, FIRST_VALUE(id) OVER w first, LAST_VALUE(id) OVER w last, sex FROM t1 489WINDOW w AS (PARTITION BY sex 490ORDER BY id ROWS BETWEEN 1 FOLLOWING AND 2 FOLLOWING); 491id first last sex 49210 11 11 NULL 49311 NULL NULL NULL 4942 3 4 F 4953 4 4 F 4964 NULL NULL F 4971 5 5 M 4985 NULL NULL M 499SELECT id, FIRST_VALUE(id) OVER w first, LAST_VALUE(id) OVER w last, sex FROM t1 500WINDOW w AS (PARTITION BY sex 501ORDER BY id RANGE BETWEEN 1 FOLLOWING AND 2 FOLLOWING); 502id first last sex 50310 11 11 NULL 50411 NULL NULL NULL 5052 3 4 F 5063 4 4 F 5074 NULL NULL F 5081 NULL NULL M 5095 NULL NULL M 510Subquery which causes reuse of window requiring state reset, 511cf. Window::reset_round. 512CREATE TABLE t_a (a INT, b INT); 513INSERT INTO t_a VALUES (4, 40), (1, 10), (2, 20), (2, 20), (3, 30); 514CREATE TABLE t_b SELECT DISTINCT a FROM t_a; 515SELECT (SELECT SUM(t_b.a) OVER () FROM t_b WHERE t_b.a = t_a.a) aa, b FROM t_a GROUP BY aa, b; 516aa b 5174 40 5181 10 5192 20 5203 30 521DROP TABLE t_a, t_b; 522---------------------------------------------------------------------- 523Test of legal frame border value, including prepared statement and dynamic ? 524parameters 525---------------------------------------------------------------------- 526Static version of checking is caught at parsing time, unless we have INTERVAL. 527For subquery in border, cf. test cases in Bug#25907777 528SELECT sex, COUNT(id) OVER (ORDER BY id RANGE -1 PRECEDING) FROM t1; 529ERROR 42000: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '-1 PRECEDING) FROM t1' at line 1 530SELECT sex, COUNT(id) OVER (ORDER BY id RANGE BETWEEN -1 PRECEDING and 2 PRECEDING) FROM t1; 531ERROR 42000: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '-1 PRECEDING and 2 PRECEDING) FROM t1' at line 1 532SELECT sex, COUNT(id) OVER (ORDER BY id RANGE BETWEEN 1 PRECEDING and -1 PRECEDING) FROM t1; 533ERROR 42000: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '-1 PRECEDING) FROM t1' at line 1 534OK, even if empty frame 535SELECT sex, COUNT(id) OVER (ORDER BY id RANGE BETWEEN 1 PRECEDING and 2 PRECEDING) FROM t1; 536sex COUNT(id) OVER (ORDER BY id RANGE BETWEEN 1 PRECEDING and 2 PRECEDING) 537M 0 538F 0 539F 0 540F 0 541M 0 542NULL 0 543NULL 0 544CREATE TABLE t_time(t TIME, ts TIMESTAMP); 545INSERT INTO t_time VALUES ('12:30', '2016-07-05 08:30:42'); 546SELECT t, FIRST_VALUE(t) OVER (ORDER BY t ) FROM t_time; 547t FIRST_VALUE(t) OVER (ORDER BY t ) 54812:30:00 12:30:00 549coverage for ::get_time 550SELECT ADDTIME(FIRST_VALUE(time'18:00:00') OVER (ORDER BY NULL), '01:00:00'); 551ADDTIME(FIRST_VALUE(time'18:00:00') OVER (ORDER BY NULL), '01:00:00') 55219:00:00 553SELECT ADDTIME(NTH_VALUE(time'18:00:00', 1) OVER (ORDER BY NULL), '01:00:00'); 554ADDTIME(NTH_VALUE(time'18:00:00', 1) OVER (ORDER BY NULL), '01:00:00') 55519:00:00 556SELECT t, FIRST_VALUE(t) OVER (ORDER BY t RANGE INTERVAL -1 HOUR PRECEDING) FROM t_time; 557ERROR HY000: Window '<unnamed window>': frame start or end is negative, NULL or of non-integral type 558SELECT t, FIRST_VALUE(t) OVER (ORDER BY t RANGE INTERVAL NULL HOUR PRECEDING) FROM t_time; 559ERROR HY000: Window '<unnamed window>': frame start or end is negative, NULL or of non-integral type 560SELECT t, FIRST_VALUE(t) OVER (ORDER BY t RANGE BETWEEN INTERVAL -1 HOUR PRECEDING AND INTERVAL 2 HOUR PRECEDING) FROM t_time; 561ERROR HY000: Window '<unnamed window>': frame start or end is negative, NULL or of non-integral type 562SELECT t, FIRST_VALUE(t) OVER (ORDER BY t RANGE BETWEEN INTERVAL 1 HOUR PRECEDING AND INTERVAL -2 HOUR PRECEDING) FROM t_time; 563ERROR HY000: Window '<unnamed window>': frame start or end is negative, NULL or of non-integral type 564SELECT t, FIRST_VALUE(t) OVER (ORDER BY t RANGE BETWEEN INTERVAL 1 HOUR PRECEDING AND INTERVAL '-2:2' HOUR_MINUTE PRECEDING) FROM t_time; 565ERROR HY000: Window '<unnamed window>': frame start or end is negative, NULL or of non-integral type 566SELECT t, FIRST_VALUE(t) OVER (ORDER BY t RANGE BETWEEN INTERVAL 1 HOUR PRECEDING AND INTERVAL NULL HOUR PRECEDING) FROM t_time; 567ERROR HY000: Window '<unnamed window>': frame start or end is negative, NULL or of non-integral type 568SELECT t, FIRST_VALUE(t) OVER (ORDER BY t RANGE BETWEEN INTERVAL NULL HOUR PRECEDING AND INTERVAL 1 HOUR PRECEDING) FROM t_time; 569ERROR HY000: Window '<unnamed window>': frame start or end is negative, NULL or of non-integral type 570Dynamic ? parameter checking 571PREPARE p FROM "SELECT sex, COUNT(id) OVER (ORDER BY id ROWS BETWEEN ? PRECEDING and ? PRECEDING) FROM t1"; 572SET @p1= 1; 573SET @p2= 2; 574Empty frame is legal according to standard 575EXECUTE p USING @p1, @p2; 576sex COUNT(id) OVER (ORDER BY id ROWS BETWEEN ? PRECEDING and ? PRECEDING) 577M 0 578F 0 579F 0 580F 0 581M 0 582NULL 0 583NULL 0 584OK, not empty frame 585EXECUTE p USING @p2, @p1; 586sex COUNT(id) OVER (ORDER BY id ROWS BETWEEN ? PRECEDING and ? PRECEDING) 587M 0 588F 1 589F 2 590F 2 591M 2 592NULL 2 593NULL 2 594DROP PREPARE p; 595PREPARE p FROM "SELECT sex, COUNT(id) OVER (ORDER BY id ROWS BETWEEN ? FOLLOWING and ? FOLLOWING) FROM t1"; 596SET @p1= 1; 597SET @p2= 2; 598Empty frame is legal according to standard 599EXECUTE p USING @p2, @p1; 600sex COUNT(id) OVER (ORDER BY id ROWS BETWEEN ? FOLLOWING and ? FOLLOWING) 601M 0 602F 0 603F 0 604F 0 605M 0 606NULL 0 607NULL 0 608OK, not empty frame 609EXECUTE p USING @p1, @p2; 610sex COUNT(id) OVER (ORDER BY id ROWS BETWEEN ? FOLLOWING and ? FOLLOWING) 611M 2 612F 2 613F 2 614F 2 615M 2 616NULL 1 617NULL 0 618DROP PREPARE p; 619PREPARE p FROM "SELECT sex, COUNT(id) OVER (ORDER BY id ROWS BETWEEN ? FOLLOWING and ? FOLLOWING) FROM t1"; 620SET @p1= -1; 621SET @p2= 2; 622EXECUTE p USING @p2, @p1; 623ERROR HY000: Window '<unnamed window>': frame start or end is negative, NULL or of non-integral type 624EXECUTE p USING @p1, @p2; 625ERROR HY000: Window '<unnamed window>': frame start or end is negative, NULL or of non-integral type 626DROP PREPARE p; 627PREPARE p FROM "SELECT sex, COUNT(id) OVER (ORDER BY id ROWS ? PRECEDING) FROM t1"; 628SET @p1= -1; 629EXECUTE p USING @p1; 630ERROR HY000: Window '<unnamed window>': frame start or end is negative, NULL or of non-integral type 631DROP PREPARE p; 632PREPARE p FROM "SELECT sex, COUNT(id) OVER (ORDER BY id ROWS BETWEEN CURRENT ROW AND ? FOLLOWING) FROM t1"; 633SET @p1= -1; 634EXECUTE p USING @p1; 635ERROR HY000: Window '<unnamed window>': frame start or end is negative, NULL or of non-integral type 636DROP PREPARE p; 637PREPARE p FROM "SELECT t, FIRST_VALUE(t) OVER (ORDER BY t RANGE INTERVAL ? HOUR PRECEDING) FROM t_time"; 638SET @p1= -1; 639EXECUTE p USING @p1; 640ERROR HY000: Window '<unnamed window>': frame start or end is negative, NULL or of non-integral type 641PREPARE p FROM "SELECT t, FIRST_VALUE(t) OVER (ORDER BY t RANGE BETWEEN INTERVAL ? HOUR PRECEDING AND INTERVAL ? HOUR PRECEDING) FROM t_time"; 642SET @p2= 2; 643EXECUTE p USING @p1, @p2; 644ERROR HY000: Window '<unnamed window>': frame start or end is negative, NULL or of non-integral type 645EXECUTE p USING @p2, @p1; 646ERROR HY000: Window '<unnamed window>': frame start or end is negative, NULL or of non-integral type 647SET @p1= NULL; 648EXECUTE p USING @p2, @p1; 649ERROR HY000: Window '<unnamed window>': frame start or end is negative, NULL or of non-integral type 650PREPARE p FROM "SELECT t, FIRST_VALUE(t) OVER (ORDER BY t RANGE BETWEEN INTERVAL ? HOUR PRECEDING AND INTERVAL ? HOUR_MINUTE PRECEDING) FROM t_time"; 651SET @p1= '-2:2'; 652EXECUTE p USING @p2, @p1; 653ERROR HY000: Window '<unnamed window>': frame start or end is negative, NULL or of non-integral type 654SET @p1= '2:2'; 655EXECUTE p USING @p2, @p1; 656t FIRST_VALUE(t) OVER (ORDER BY t RANGE BETWEEN INTERVAL ? HOUR PRECEDING AND INTERVAL ? HOUR_MINUTE PRECEDING) 65712:30:00 NULL 658DROP TABLE t, t1, ss, t_time; 659---------------------------------------------------------------------- 660- Some RANK, DENSE_RANK, PERCENT_RANK, CUME_DIST tests 661---------------------------------------------------------------------- 662CREATE TABLE t1 (id INTEGER, sex CHAR(1)); 663INSERT INTO t1 VALUES (1, 'M'); 664INSERT INTO t1 VALUES (2, 'F'); 665INSERT INTO t1 VALUES (3, 'F'); 666INSERT INTO t1 VALUES (4, 'F'); 667INSERT INTO t1 VALUES (5, 'M'); 668CREATE TABLE t2 (user_id INTEGER NOT NULL, date DATE); 669INSERT INTO t2 VALUES (1, '2002-06-09'); 670INSERT INTO t2 VALUES (2, '2002-06-09'); 671INSERT INTO t2 VALUES (1, '2002-06-09'); 672INSERT INTO t2 VALUES (3, '2002-06-09'); 673INSERT INTO t2 VALUES (4, '2002-06-09'); 674INSERT INTO t2 VALUES (4, '2002-06-09'); 675INSERT INTO t2 VALUES (5, '2002-06-09'); 676SELECT RANK() OVER (ORDER BY user_id) r FROM t2; 677r 6781 6791 6803 6814 6825 6835 6847 685SELECT DENSE_RANK() OVER (ORDER BY user_id) r FROM t2; 686r 6871 6881 6892 6903 6914 6924 6935 694SELECT PERCENT_RANK() OVER (ORDER BY user_id) r FROM t2; 695r 6960 6970 6980.3333333333333333 6990.5 7000.6666666666666666 7010.6666666666666666 7021 703SELECT CUME_DIST() OVER (ORDER BY user_id) cd FROM t2; 704cd 7050.2857142857142857 7060.2857142857142857 7070.42857142857142855 7080.5714285714285714 7090.8571428571428571 7100.8571428571428571 7111 712SELECT RANK() OVER () r FROM t2; 713r 7141 7151 7161 7171 7181 7191 7201 721SELECT DENSE_RANK() OVER () r FROM t2; 722r 7231 7241 7251 7261 7271 7281 7291 730SELECT PERCENT_RANK() OVER () r FROM t2; 731r 7320 7330 7340 7350 7360 7370 7380 739SELECT CUME_DIST() OVER () cd FROM t2; 740cd 7411 7421 7431 7441 7451 7461 7471 748SELECT sex, SUM(DISTINCT id) AS uids FROM t1 u, t2 749WHERE t2.user_id = u.id GROUP BY sex ORDER BY uids; 750sex uids 751M 6 752F 9 753SELECT id, sex, RANK() OVER (ORDER BY sex) FROM t1 ORDER BY id; 754id sex RANK() OVER (ORDER BY sex) 7551 M 4 7562 F 1 7573 F 1 7584 F 1 7595 M 4 760SELECT id, sex, DENSE_RANK() OVER (ORDER BY sex) FROM t1 ORDER BY id; 761id sex DENSE_RANK() OVER (ORDER BY sex) 7621 M 2 7632 F 1 7643 F 1 7654 F 1 7665 M 2 767SELECT id, sex, PERCENT_RANK() OVER (ORDER BY sex) FROM t1 ORDER BY id; 768id sex PERCENT_RANK() OVER (ORDER BY sex) 7691 M 0.75 7702 F 0 7713 F 0 7724 F 0 7735 M 0.75 774SELECT id, sex, CUME_DIST() OVER (ORDER BY sex) FROM t1 ORDER BY id; 775id sex CUME_DIST() OVER (ORDER BY sex) 7761 M 1 7772 F 0.6 7783 F 0.6 7794 F 0.6 7805 M 1 781SELECT sex, RANK() OVER (ORDER BY sex DESC) `rank`, AVG(DISTINCT id) AS uids FROM t1 u, t2 782WHERE t2.user_id = u.id GROUP BY sex ORDER BY sex; 783sex rank uids 784F 2 3.0000 785M 1 3.0000 786SELECT sex, PERCENT_RANK() OVER (ORDER BY sex DESC) `rank`, AVG(DISTINCT id) AS uids FROM t1 u, t2 787WHERE t2.user_id = u.id GROUP BY sex ORDER BY sex; 788sex rank uids 789F 1 3.0000 790M 0 3.0000 791SELECT sex, CUME_DIST() OVER (ORDER BY sex DESC) `cume_dist`, AVG(DISTINCT id) AS uids FROM t1 u, t2 792WHERE t2.user_id = u.id GROUP BY sex ORDER BY sex; 793sex cume_dist uids 794F 1 3.0000 795M 0.5 3.0000 796Explicit window definition, WINDOW DESC ordering by GROUP BY 797SELECT sex, AVG(id) AS uids, RANK() OVER w `rank` FROM t1 u, t2 798WHERE t2.user_id = u.id GROUP BY sex 799WINDOW w AS (ORDER BY AVG(id)); 800sex uids rank 801M 2.3333 1 802F 3.2500 2 803SELECT sex, AVG(id) AS uids, PERCENT_RANK() OVER w `p_rank` FROM t1 u, t2 804WHERE t2.user_id = u.id GROUP BY sex 805WINDOW w AS (ORDER BY AVG(id)); 806sex uids p_rank 807M 2.3333 0 808F 3.2500 1 809SELECT sex, AVG(id) AS uids, CUME_DIST() OVER w `c_dist` FROM t1 u, t2 810WHERE t2.user_id = u.id GROUP BY sex 811WINDOW w AS (ORDER BY AVG(id)); 812sex uids c_dist 813M 2.3333 0.5 814F 3.2500 1 815Explicit window definition, window ordering by DISTINCT GROUP BY 816SELECT sex, AVG(DISTINCT id) AS uids, RANK() OVER w `rank` FROM t1 u, t2 817WHERE t2.user_id = u.id GROUP BY sex 818WINDOW w AS (ORDER BY AVG(DISTINCT id) DESC) ORDER BY sex; 819sex uids rank 820F 3.0000 1 821M 3.0000 1 822SELECT sex, AVG(DISTINCT id) AS uids, PERCENT_RANK() OVER w `p_rank` FROM t1 u, t2 823WHERE t2.user_id = u.id GROUP BY sex 824WINDOW w AS (ORDER BY AVG(DISTINCT id) DESC) ORDER BY sex; 825sex uids p_rank 826F 3.0000 0 827M 3.0000 0 828SELECT sex, AVG(DISTINCT id) AS uids, CUME_DIST() OVER w `c_dist` FROM t1 u, t2 829WHERE t2.user_id = u.id GROUP BY sex 830WINDOW w AS (ORDER BY AVG(DISTINCT id) DESC) ORDER BY sex; 831sex uids c_dist 832F 3.0000 1 833M 3.0000 1 834Explicit window definition, window ordering by GROUP BY, final ORDER BY 835SELECT sex, AVG(id) AS uids, RANK() OVER w `rank` FROM t1 u, t2 836WHERE t2.user_id = u.id GROUP BY sex 837WINDOW w AS (ORDER BY AVG(id) DESC) 838ORDER BY `rank` DESC; 839sex uids rank 840M 2.3333 2 841F 3.2500 1 842SELECT sex, AVG(id) AS uids, PERCENT_RANK() OVER w `p_rank`, CUME_DIST() OVER w `c_dist` 843 FROM t1 u, t2 844WHERE t2.user_id = u.id GROUP BY sex 845WINDOW w AS (ORDER BY AVG(id) DESC) 846ORDER BY `p_rank` DESC; 847sex uids p_rank c_dist 848F 3.2500 0 0.5 849M 2.3333 1 1 850With NULLs 851INSERT INTO t1 VALUES (10, NULL); 852INSERT INTO t1 VALUES (11, NULL); 853SELECT id, sex, RANK() OVER w, DENSE_RANK() OVER w FROM t1 854WINDOW w AS (ORDER BY sex) ORDER BY id; 855id sex RANK() OVER w DENSE_RANK() OVER w 8561 M 6 3 8572 F 3 2 8583 F 3 2 8594 F 3 2 8605 M 6 3 86110 NULL 1 1 86211 NULL 1 1 863SELECT id, sex, PERCENT_RANK() OVER w, CUME_DIST() OVER w FROM t1 864WINDOW w AS (ORDER BY sex) ORDER BY id; 865id sex PERCENT_RANK() OVER w CUME_DIST() OVER w 8661 M 0.8333333333333334 1 8672 F 0.3333333333333333 0.7142857142857143 8683 F 0.3333333333333333 0.7142857142857143 8694 F 0.3333333333333333 0.7142857142857143 8705 M 0.8333333333333334 1 87110 NULL 0 0.2857142857142857 87211 NULL 0 0.2857142857142857 873SELECT id, sex, RANK() OVER (ORDER BY sex DESC) FROM t1 ORDER BY id; 874id sex RANK() OVER (ORDER BY sex DESC) 8751 M 1 8762 F 3 8773 F 3 8784 F 3 8795 M 1 88010 NULL 6 88111 NULL 6 882SELECT id, sex, PERCENT_RANK() OVER (ORDER BY sex DESC) FROM t1 ORDER BY id; 883id sex PERCENT_RANK() OVER (ORDER BY sex DESC) 8841 M 0 8852 F 0.3333333333333333 8863 F 0.3333333333333333 8874 F 0.3333333333333333 8885 M 0 88910 NULL 0.8333333333333334 89011 NULL 0.8333333333333334 891SELECT id, sex, CUME_DIST() OVER (ORDER BY sex DESC) FROM t1 ORDER BY id; 892id sex CUME_DIST() OVER (ORDER BY sex DESC) 8931 M 0.2857142857142857 8942 F 0.7142857142857143 8953 F 0.7142857142857143 8964 F 0.7142857142857143 8975 M 0.2857142857142857 89810 NULL 1 89911 NULL 1 900SELECT id value, 901SUM(id) OVER (ROWS UNBOUNDED PRECEDING) 902FROM t1 u, t2 WHERE t2.user_id = u.id; 903value SUM(id) OVER (ROWS UNBOUNDED PRECEDING) 9041 1 9052 3 9061 4 9073 7 9084 11 9094 15 9105 20 911Aggregate with GROUP BY arguments to window function 912SELECT AVG(id) average, 913SUM(AVG(id)) OVER (ROWS UNBOUNDED PRECEDING) 914FROM t1 u, t2 WHERE t2.user_id = u.id GROUP BY sex; 915average SUM(AVG(id)) OVER (ROWS UNBOUNDED PRECEDING) 9162.3333 2.3333 9173.2500 5.5833 918Aggregate with GROUP BY in window's ORDER BY clause, with aggregate present in 919SELECT list or not. 920SELECT sex, AVG(id), RANK() OVER (ORDER BY AVG(id) DESC) FROM t1 GROUP BY sex ORDER BY sex; 921sex AVG(id) RANK() OVER (ORDER BY AVG(id) DESC) 922NULL 10.5000 1 923F 3.0000 2 924M 3.0000 2 925SELECT sex, PERCENT_RANK() OVER (ORDER BY AVG(id) DESC) FROM t1 GROUP BY sex ORDER BY sex; 926sex PERCENT_RANK() OVER (ORDER BY AVG(id) DESC) 927NULL 0 928F 0.5 929M 0.5 930SELECT sex, CUME_DIST() OVER (ORDER BY AVG(id) DESC) FROM t1 GROUP BY sex ORDER BY sex; 931sex CUME_DIST() OVER (ORDER BY AVG(id) DESC) 932NULL 0.3333333333333333 933F 1 934M 1 935SELECT sex, RANK() OVER (ORDER BY AVG(id) DESC) FROM t1 GROUP BY sex ORDER BY sex; 936sex RANK() OVER (ORDER BY AVG(id) DESC) 937NULL 1 938F 2 939M 2 940SELECT sex, CUME_DIST() OVER (ORDER BY AVG(id) DESC) FROM t1 GROUP BY sex ORDER BY sex; 941sex CUME_DIST() OVER (ORDER BY AVG(id) DESC) 942NULL 0.3333333333333333 943F 1 944M 1 945Implicit group aggregate arguments to window function and in 946window's ORDER BY clause 947SELECT RANK() OVER (ORDER BY AVG(id)) FROM t1; 948RANK() OVER (ORDER BY AVG(id)) 9491 950SELECT PERCENT_RANK() OVER (ORDER BY AVG(id)) FROM t1; 951PERCENT_RANK() OVER (ORDER BY AVG(id)) 9520 953SELECT CUME_DIST() OVER (ORDER BY AVG(id)) FROM t1; 954CUME_DIST() OVER (ORDER BY AVG(id)) 9551 956SELECT AVG(id), RANK() OVER (ORDER BY AVG(id)) FROM t1; 957AVG(id) RANK() OVER (ORDER BY AVG(id)) 9585.1429 1 959SELECT AVG(id), PERCENT_RANK() OVER (ORDER BY AVG(id)) FROM t1; 960AVG(id) PERCENT_RANK() OVER (ORDER BY AVG(id)) 9615.1429 0 962SELECT AVG(id), CUME_DIST() OVER (ORDER BY AVG(id)) FROM t1; 963AVG(id) CUME_DIST() OVER (ORDER BY AVG(id)) 9645.1429 1 965SELECT AVG(id), SUM(AVG(id)) OVER (ORDER BY AVG(id) ROWS UNBOUNDED PRECEDING) FROM t1; 966AVG(id) SUM(AVG(id)) OVER (ORDER BY AVG(id) ROWS UNBOUNDED PRECEDING) 9675.1429 5.1429 968Several partitions, several window functions over the same window 969SELECT sex, id, RANK() OVER (PARTITION BY sex ORDER BY id DESC) FROM t1; 970sex id RANK() OVER (PARTITION BY sex ORDER BY id DESC) 971NULL 11 1 972NULL 10 2 973F 4 1 974F 3 2 975F 2 3 976M 5 1 977M 1 2 978SELECT sex, id, PERCENT_RANK() OVER (PARTITION BY sex ORDER BY id DESC) FROM t1; 979sex id PERCENT_RANK() OVER (PARTITION BY sex ORDER BY id DESC) 980NULL 11 0 981NULL 10 1 982F 4 0 983F 3 0.5 984F 2 1 985M 5 0 986M 1 1 987SELECT sex, id, CUME_DIST() OVER (PARTITION BY sex ORDER BY id DESC) FROM t1; 988sex id CUME_DIST() OVER (PARTITION BY sex ORDER BY id DESC) 989NULL 11 0.5 990NULL 10 1 991F 4 0.3333333333333333 992F 3 0.6666666666666666 993F 2 1 994M 5 0.5 995M 1 1 996SELECT sex, id, RANK() OVER (PARTITION BY sex ORDER BY id ASC) FROM t1; 997sex id RANK() OVER (PARTITION BY sex ORDER BY id ASC) 998NULL 10 1 999NULL 11 2 1000F 2 1 1001F 3 2 1002F 4 3 1003M 1 1 1004M 5 2 1005SELECT sex, id, PERCENT_RANK() OVER (PARTITION BY sex ORDER BY id ASC) FROM t1; 1006sex id PERCENT_RANK() OVER (PARTITION BY sex ORDER BY id ASC) 1007NULL 10 0 1008NULL 11 1 1009F 2 0 1010F 3 0.5 1011F 4 1 1012M 1 0 1013M 5 1 1014SELECT sex, id, CUME_DIST() OVER (PARTITION BY sex ORDER BY id ASC) FROM t1; 1015sex id CUME_DIST() OVER (PARTITION BY sex ORDER BY id ASC) 1016NULL 10 0.5 1017NULL 11 1 1018F 2 0.3333333333333333 1019F 3 0.6666666666666666 1020F 4 1 1021M 1 0.5 1022M 5 1 1023SELECT sex, id, SUM(id) OVER w summ, RANK() OVER w `rank` FROM t1 1024WINDOW w AS (PARTITION BY sex ORDER BY id ASC ROWS UNBOUNDED PRECEDING); 1025sex id summ rank 1026NULL 10 10 1 1027NULL 11 21 2 1028F 2 2 1 1029F 3 5 2 1030F 4 9 3 1031M 1 1 1 1032M 5 6 2 1033SELECT sex, id, SUM(id) OVER w summ, PERCENT_RANK() OVER w `p_rank`, 1034CUME_DIST() OVER w `c_dist` FROM t1 1035WINDOW w AS (PARTITION BY sex ORDER BY id ASC ROWS UNBOUNDED PRECEDING); 1036sex id summ p_rank c_dist 1037NULL 10 10 0 0.5 1038NULL 11 21 1 1 1039F 2 2 0 0.3333333333333333 1040F 3 5 0.5 0.6666666666666666 1041F 4 9 1 1 1042M 1 1 0 0.5 1043M 5 6 1 1 1044SELECT sex, id, SUM(id) OVER w summ, RANK() OVER w `rank` FROM t1 1045WINDOW w AS (PARTITION BY sex ORDER BY id ASC ROWS UNBOUNDED PRECEDING) ORDER BY summ; 1046sex id summ rank 1047M 1 1 1 1048F 2 2 1 1049F 3 5 2 1050M 5 6 2 1051F 4 9 3 1052NULL 10 10 1 1053NULL 11 21 2 1054SELECT sex, id, SUM(id) OVER w summ, PERCENT_RANK() OVER w `p_rank`, 1055CUME_DIST() OVER w `c_dist` FROM t1 1056WINDOW w AS (PARTITION BY sex ORDER BY id ASC ROWS UNBOUNDED PRECEDING) ORDER BY summ; 1057sex id summ p_rank c_dist 1058M 1 1 0 0.5 1059F 2 2 0 0.3333333333333333 1060F 3 5 0.5 0.6666666666666666 1061M 5 6 1 1 1062F 4 9 1 1 1063NULL 10 10 0 0.5 1064NULL 11 21 1 1 1065SQL 2011 7.11 <window clause>, SR 4. Window specification's ORDER BY or 1066PARTITION BY cannot reference SELECT list aliases 1067SELECT sex, AVG(DISTINCT id), 1068RANK() OVER w `uids` 1069 FROM t1 u, t2 WHERE t2.user_id = u.id GROUP BY sex 1070WINDOW w AS (ORDER BY uids DESC) ORDER BY sex; 1071ERROR 42S22: Unknown column 'uids' in 'window order by' 1072SELECT sex, AVG(DISTINCT id), 1073RANK() OVER (ORDER BY uids DESC) `uids` 1074 FROM t1 u, t2 WHERE t2.user_id = u.id 1075GROUP BY sex ORDER BY sex; 1076ERROR 42S22: Unknown column 'uids' in 'window order by' 1077CREATE TABLE t(d decimal(10,2), date DATE); 1078INSERT INTO t values (10.4, '2002-06-09'); 1079INSERT INTO t values (20.5, '2002-06-09'); 1080INSERT INTO t values (10.4, '2002-06-10'); 1081INSERT INTO t values (3, '2002-06-09'); 1082INSERT INTO t values (40.2, '2015-08-01'); 1083INSERT INTO t values (40.2, '2002-06-09'); 1084INSERT INTO t values (5, '2015-08-01'); 1085SELECT * FROM (SELECT RANK() OVER (ORDER BY d) AS `rank`, d, date FROM t) alias ORDER BY `rank`, d, date; 1086rank d date 10871 3.00 2002-06-09 10882 5.00 2015-08-01 10893 10.40 2002-06-09 10903 10.40 2002-06-10 10915 20.50 2002-06-09 10926 40.20 2002-06-09 10936 40.20 2015-08-01 1094SELECT * FROM (SELECT PERCENT_RANK() OVER (ORDER BY d) AS `p_rank`, d, date FROM t) alias ORDER BY `p_rank`, d, date; 1095p_rank d date 10960 3.00 2002-06-09 10970.16666666666666666 5.00 2015-08-01 10980.3333333333333333 10.40 2002-06-09 10990.3333333333333333 10.40 2002-06-10 11000.6666666666666666 20.50 2002-06-09 11010.8333333333333334 40.20 2002-06-09 11020.8333333333333334 40.20 2015-08-01 1103SELECT * FROM (SELECT CUME_DIST() OVER (ORDER BY d) AS `c_dist`, d, date FROM t) alias ORDER BY `c_dist`, d, date; 1104c_dist d date 11050.14285714285714285 3.00 2002-06-09 11060.2857142857142857 5.00 2015-08-01 11070.5714285714285714 10.40 2002-06-09 11080.5714285714285714 10.40 2002-06-10 11090.7142857142857143 20.50 2002-06-09 11101 40.20 2002-06-09 11111 40.20 2015-08-01 1112SELECT * FROM (SELECT RANK() OVER (ORDER BY date) AS `rank`, date, d FROM t) alias ORDER BY `rank`, d DESC; 1113rank date d 11141 2002-06-09 40.20 11151 2002-06-09 20.50 11161 2002-06-09 10.40 11171 2002-06-09 3.00 11185 2002-06-10 10.40 11196 2015-08-01 40.20 11206 2015-08-01 5.00 1121SELECT * FROM (SELECT PERCENT_RANK() OVER (ORDER BY date) AS `p_rank`, date, d FROM t) alias ORDER BY `p_rank`, d DESC; 1122p_rank date d 11230 2002-06-09 40.20 11240 2002-06-09 20.50 11250 2002-06-09 10.40 11260 2002-06-09 3.00 11270.6666666666666666 2002-06-10 10.40 11280.8333333333333334 2015-08-01 40.20 11290.8333333333333334 2015-08-01 5.00 1130SELECT * FROM (SELECT CUME_DIST() OVER (ORDER BY date) AS `c_dist`, date, d FROM t) alias ORDER BY `c_dist`, d DESC; 1131c_dist date d 11320.5714285714285714 2002-06-09 40.20 11330.5714285714285714 2002-06-09 20.50 11340.5714285714285714 2002-06-09 10.40 11350.5714285714285714 2002-06-09 3.00 11360.7142857142857143 2002-06-10 10.40 11371 2015-08-01 40.20 11381 2015-08-01 5.00 1139DROP TABLE t; 1140Check that SUM stays that same when it sees NULL values 1141CREATE TABLE t(i INT, j INT); 1142INSERT INTO t VALUES (1,NULL); 1143INSERT INTO t VALUES (1,NULL); 1144INSERT INTO t VALUES (1,1); 1145INSERT INTO t VALUES (1,NULL); 1146INSERT INTO t VALUES (1,2); 1147INSERT INTO t VALUES (2,1); 1148INSERT INTO t VALUES (2,2); 1149INSERT INTO t VALUES (2,NULL); 1150INSERT INTO t VALUES (2,NULL); 1151SELECT i, j, SUM(j) OVER (PARTITION BY i ORDER BY j ROWS UNBOUNDED PRECEDING) FROM t; 1152i j SUM(j) OVER (PARTITION BY i ORDER BY j ROWS UNBOUNDED PRECEDING) 11531 NULL NULL 11541 NULL NULL 11551 NULL NULL 11561 1 1 11571 2 3 11582 NULL NULL 11592 NULL NULL 11602 1 1 11612 2 3 1162SELECT SUM(id), SUM(SUM(id)) OVER (ORDER BY sex ROWS UNBOUNDED PRECEDING) FROM t1,t2 WHERE t1.id=t2.user_id GROUP BY sex; 1163SUM(id) SUM(SUM(id)) OVER (ORDER BY sex ROWS UNBOUNDED PRECEDING) 116413 13 11657 20 1166SELECT id, SUM(SUM(id)) OVER (ORDER BY sex ROWS UNBOUNDED PRECEDING) FROM t1,t2 WHERE t1.id=t2.user_id GROUP BY sex; 1167ERROR 42000: Expression #1 of SELECT list is not in GROUP BY clause and contains nonaggregated column 'test.t1.id' which is not functionally dependent on columns in GROUP BY clause; this is incompatible with sql_mode=only_full_group_by 1168SELECT SUM(id) OVER (ORDER BY sex ROWS UNBOUNDED PRECEDING) FROM t1,t2 WHERE t1.id=t2.user_id GROUP BY sex; 1169ERROR 42000: Expression #1 of SELECT list is not in GROUP BY clause and contains nonaggregated column 'test.t1.id' which is not functionally dependent on columns in GROUP BY clause; this is incompatible with sql_mode=only_full_group_by 1170SELECT RANK() OVER w FROM t1,t2 WHERE t1.id=t2.user_id WINDOW w AS (PARTITION BY id ORDER BY sex); 1171RANK() OVER w 11721 11731 11741 11751 11761 11771 11781 1179SELECT PERCENT_RANK() OVER w FROM t1,t2 WHERE t1.id=t2.user_id WINDOW w AS (PARTITION BY id ORDER BY sex); 1180PERCENT_RANK() OVER w 11810 11820 11830 11840 11850 11860 11870 1188SELECT CUME_DIST() OVER w FROM t1,t2 WHERE t1.id=t2.user_id WINDOW w AS (PARTITION BY id ORDER BY sex); 1189CUME_DIST() OVER w 11901 11911 11921 11931 11941 11951 11961 1197SELECT RANK() OVER w FROM (SELECT * FROM t1,t2 WHERE t1.id=t2.user_id) t WINDOW w AS (PARTITION BY id ORDER BY sex); 1198RANK() OVER w 11991 12001 12011 12021 12031 12041 12051 1206SELECT PERCENT_RANK() OVER w FROM (SELECT * FROM t1,t2 WHERE t1.id=t2.user_id) t WINDOW w AS (PARTITION BY id ORDER BY sex); 1207PERCENT_RANK() OVER w 12080 12090 12100 12110 12120 12130 12140 1215SELECT CUME_DIST() OVER w FROM (SELECT * FROM t1,t2 WHERE t1.id=t2.user_id) t WINDOW w AS (PARTITION BY id ORDER BY sex); 1216CUME_DIST() OVER w 12171 12181 12191 12201 12211 12221 12231 1224Two more tests related to fix_fields on arguments and frame clause 1225in prepared statements. 1226SELECT NTH_VALUE(id, id) OVER w FROM (SELECT * FROM t1,t2 WHERE t1.id=t2.user_id) t WINDOW w AS (PARTITION BY id ORDER BY sex); 1227ERROR HY000: Incorrect arguments to nth_value 1228SELECT SUM(1) OVER w FROM (SELECT * FROM t1,t2 WHERE t1.id=t2.user_id) t 1229WINDOW w AS (PARTITION BY id ORDER BY sex ROWS id PRECEDING); 1230ERROR 42000: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'id PRECEDING)' at line 2 1231Check that aggregate window functions that reference columns not in the SELECT list work 1232SELECT SUM(id) OVER (PARTITION BY sex ORDER BY id ROWS UNBOUNDED PRECEDING) summ, sex FROM t1; 1233summ sex 123410 NULL 123521 NULL 12362 F 12375 F 12389 F 12391 M 12406 M 1241CREATE TABLE t3(t3_id INT, k INT); 1242INSERT INTO t3 VALUES (0, 0); 1243INSERT INTO t3 VALUES (0, 0); 1244INSERT INTO t3 VALUES (2, 0); 1245INSERT INTO t3 VALUES (2, 0); 1246INSERT INTO t3 VALUES (4, 0); 1247INSERT INTO t3 VALUES (4, 0); 1248INSERT INTO t3 VALUES (6, 0); 1249INSERT INTO t3 VALUES (6, 0); 1250INSERT INTO t3 VALUES (8, 0); 1251INSERT INTO t3 VALUES (8, 0); 1252INSERT INTO t3 VALUES (1, 1); 1253INSERT INTO t3 VALUES (1, 1); 1254INSERT INTO t3 VALUES (3, 1); 1255INSERT INTO t3 VALUES (3, 1); 1256INSERT INTO t3 VALUES (5, 1); 1257INSERT INTO t3 VALUES (5, 1); 1258INSERT INTO t3 VALUES (7, 1); 1259INSERT INTO t3 VALUES (7, 1); 1260INSERT INTO t3 VALUES (9, 1); 1261INSERT INTO t3 VALUES (9, 1); 1262Broke initial CUME_DIST frame buffer positioning locality (DBUG assert) 1263SELECT t3_id, AVG(t3_id) OVER w, 1264CUME_DIST() OVER w, 1265k FROM t3 1266WINDOW w AS (PARTITION BY k ORDER BY t3_id 1267RANGE BETWEEN 4 PRECEDING AND 2 PRECEDING); 1268t3_id AVG(t3_id) OVER w CUME_DIST() OVER w k 12690 NULL 0.2 0 12700 NULL 0.2 0 12712 0.0000 0.4 0 12722 0.0000 0.4 0 12734 1.0000 0.6 0 12744 1.0000 0.6 0 12756 3.0000 0.8 0 12766 3.0000 0.8 0 12778 5.0000 1 0 12788 5.0000 1 0 12791 NULL 0.2 1 12801 NULL 0.2 1 12813 1.0000 0.4 1 12823 1.0000 0.4 1 12835 2.0000 0.6 1 12845 2.0000 0.6 1 12857 4.0000 0.8 1 12867 4.0000 0.8 1 12879 6.0000 1 1 12889 6.0000 1 1 1289Reuse of already evaluated peer of current row due to dynamic_aggregate present 1290SELECT t3_id, SUM(t3_id) OVER w, 1291CUME_DIST() OVER w, 1292LEAD(t3_id, 2) OVER w `lead2`, 1293NTH_VALUE(t3_id, 3) OVER w `nth`, 1294k FROM t3 1295WINDOW w AS (PARTITION BY k ORDER BY t3_id); 1296t3_id SUM(t3_id) OVER w CUME_DIST() OVER w lead2 nth k 12970 0 0.2 2 NULL 0 12980 0 0.2 2 NULL 0 12992 4 0.4 4 2 0 13002 4 0.4 4 2 0 13014 12 0.6 6 2 0 13024 12 0.6 6 2 0 13036 24 0.8 8 2 0 13046 24 0.8 8 2 0 13058 40 1 NULL 2 0 13068 40 1 NULL 2 0 13071 2 0.2 3 NULL 1 13081 2 0.2 3 NULL 1 13093 8 0.4 5 3 1 13103 8 0.4 5 3 1 13115 18 0.6 7 3 1 13125 18 0.6 7 3 1 13137 32 0.8 9 3 1 13147 32 0.8 9 3 1 13159 50 1 NULL 3 1 13169 50 1 NULL 3 1 1317Same semantics without reuse p.t. 1318SELECT t3_id, SUM(t3_id) OVER w, 1319CUME_DIST() OVER w, 1320LEAD(t3_id, 2) OVER w `lead2`, 1321NTH_VALUE(t3_id, 3) OVER w `nth`, 1322k FROM t3 1323WINDOW w AS (PARTITION BY k ORDER BY t3_id RANGE UNBOUNDED PRECEDING); 1324t3_id SUM(t3_id) OVER w CUME_DIST() OVER w lead2 nth k 13250 0 0.2 2 NULL 0 13260 0 0.2 2 NULL 0 13272 4 0.4 4 2 0 13282 4 0.4 4 2 0 13294 12 0.6 6 2 0 13304 12 0.6 6 2 0 13316 24 0.8 8 2 0 13326 24 0.8 8 2 0 13338 40 1 NULL 2 0 13348 40 1 NULL 2 0 13351 2 0.2 3 NULL 1 13361 2 0.2 3 NULL 1 13373 8 0.4 5 3 1 13383 8 0.4 5 3 1 13395 18 0.6 7 3 1 13405 18 0.6 7 3 1 13417 32 0.8 9 3 1 13427 32 0.8 9 3 1 13439 50 1 NULL 3 1 13449 50 1 NULL 3 1 1345Followup to Bug#25756549 1346SELECT t3_id, LAST_VALUE(t3_id) OVER w, k FROM t3 1347WINDOW w AS (PARTITION BY k ORDER BY t3_id RANGE UNBOUNDED PRECEDING); 1348t3_id LAST_VALUE(t3_id) OVER w k 13490 0 0 13500 0 0 13512 2 0 13522 2 0 13534 4 0 13544 4 0 13556 6 0 13566 6 0 13578 8 0 13588 8 0 13591 1 1 13601 1 1 13613 3 1 13623 3 1 13635 5 1 13645 5 1 13657 7 1 13667 7 1 13679 9 1 13689 9 1 1369SELECT t3_id, LAST_VALUE(t3_id) OVER w, k FROM t3 1370WINDOW w AS (PARTITION BY k ORDER BY t3_id RANGE 2 PRECEDING); 1371t3_id LAST_VALUE(t3_id) OVER w k 13720 0 0 13730 0 0 13742 2 0 13752 2 0 13764 4 0 13774 4 0 13786 6 0 13796 6 0 13808 8 0 13818 8 0 13821 1 1 13831 1 1 13843 3 1 13853 3 1 13865 5 1 13875 5 1 13887 7 1 13897 7 1 13909 9 1 13919 9 1 1392DROP TABLE t3; 1393---------------------------------------------------------------------- 1394- Some ROW_NUMBER tests 1395---------------------------------------------------------------------- 1396SELECT user_id, ROW_NUMBER() OVER (PARTITION BY user_id) FROM t2 t1; 1397user_id ROW_NUMBER() OVER (PARTITION BY user_id) 13981 1 13991 2 14002 1 14013 1 14024 1 14034 2 14045 1 1405SELECT * FROM t1,t2 WHERE t1.id=t2.user_id; 1406id sex user_id date 14071 M 1 2002-06-09 14081 M 1 2002-06-09 14092 F 2 2002-06-09 14103 F 3 2002-06-09 14114 F 4 2002-06-09 14124 F 4 2002-06-09 14135 M 5 2002-06-09 1414SELECT sex, id, date, ROW_NUMBER() OVER w AS row_no, RANK() OVER w AS `rank` FROM t1,t2 1415WHERE t1.id=t2.user_id 1416WINDOW w AS (PARTITION BY id ORDER BY sex); 1417sex id date row_no rank 1418M 1 2002-06-09 1 1 1419M 1 2002-06-09 2 1 1420F 2 2002-06-09 1 1 1421F 3 2002-06-09 1 1 1422F 4 2002-06-09 1 1 1423F 4 2002-06-09 2 1 1424M 5 2002-06-09 1 1 1425SELECT sex, id, date, ROW_NUMBER() OVER w AS row_no, RANK() OVER w AS `rank` FROM t1,t2 1426WHERE t1.id=t2.user_id 1427WINDOW w AS (PARTITION BY date ORDER BY id); 1428sex id date row_no rank 1429M 1 2002-06-09 1 1 1430M 1 2002-06-09 2 1 1431F 2 2002-06-09 3 3 1432F 3 2002-06-09 4 4 1433F 4 2002-06-09 5 5 1434F 4 2002-06-09 6 5 1435M 5 2002-06-09 7 7 1436Coverage for ::val_str 1437SELECT CONCAT(ROW_NUMBER() OVER (), 1); 1438CONCAT(ROW_NUMBER() OVER (), 1) 143911 1440SELECT CONCAT(RANK() OVER (ORDER BY NULL), 1); 1441CONCAT(RANK() OVER (ORDER BY NULL), 1) 144211 1443SELECT CONCAT(CUME_DIST() OVER (ORDER BY NULL), 1); 1444CONCAT(CUME_DIST() OVER (ORDER BY NULL), 1) 144511 1446SELECT CONCAT(PERCENT_RANK() OVER (ORDER BY NULL), 1); 1447CONCAT(PERCENT_RANK() OVER (ORDER BY NULL), 1) 144801 1449SELECT CONCAT(NTILE(3) OVER (ORDER BY NULL), 1); 1450CONCAT(NTILE(3) OVER (ORDER BY NULL), 1) 145111 1452---------------------------------------------------------------------- 1453- Window function in subquery 1454---------------------------------------------------------------------- 1455SELECT date,id, RANK() OVER w AS `rank` FROM t1,t2 WINDOW w AS (PARTITION BY date ORDER BY id); 1456date id rank 14572002-06-09 1 1 14582002-06-09 1 1 14592002-06-09 1 1 14602002-06-09 1 1 14612002-06-09 1 1 14622002-06-09 1 1 14632002-06-09 1 1 14642002-06-09 2 8 14652002-06-09 2 8 14662002-06-09 2 8 14672002-06-09 2 8 14682002-06-09 2 8 14692002-06-09 2 8 14702002-06-09 2 8 14712002-06-09 3 15 14722002-06-09 3 15 14732002-06-09 3 15 14742002-06-09 3 15 14752002-06-09 3 15 14762002-06-09 3 15 14772002-06-09 3 15 14782002-06-09 4 22 14792002-06-09 4 22 14802002-06-09 4 22 14812002-06-09 4 22 14822002-06-09 4 22 14832002-06-09 4 22 14842002-06-09 4 22 14852002-06-09 5 29 14862002-06-09 5 29 14872002-06-09 5 29 14882002-06-09 5 29 14892002-06-09 5 29 14902002-06-09 5 29 14912002-06-09 5 29 14922002-06-09 10 36 14932002-06-09 10 36 14942002-06-09 10 36 14952002-06-09 10 36 14962002-06-09 10 36 14972002-06-09 10 36 14982002-06-09 10 36 14992002-06-09 11 43 15002002-06-09 11 43 15012002-06-09 11 43 15022002-06-09 11 43 15032002-06-09 11 43 15042002-06-09 11 43 15052002-06-09 11 43 1506SELECT * from (SELECT date,id, RANK() OVER w AS `rank` FROM t1,t2 WINDOW w AS (PARTITION BY date ORDER BY id)) t; 1507date id rank 15082002-06-09 1 1 15092002-06-09 1 1 15102002-06-09 1 1 15112002-06-09 1 1 15122002-06-09 1 1 15132002-06-09 1 1 15142002-06-09 1 1 15152002-06-09 2 8 15162002-06-09 2 8 15172002-06-09 2 8 15182002-06-09 2 8 15192002-06-09 2 8 15202002-06-09 2 8 15212002-06-09 2 8 15222002-06-09 3 15 15232002-06-09 3 15 15242002-06-09 3 15 15252002-06-09 3 15 15262002-06-09 3 15 15272002-06-09 3 15 15282002-06-09 3 15 15292002-06-09 4 22 15302002-06-09 4 22 15312002-06-09 4 22 15322002-06-09 4 22 15332002-06-09 4 22 15342002-06-09 4 22 15352002-06-09 4 22 15362002-06-09 5 29 15372002-06-09 5 29 15382002-06-09 5 29 15392002-06-09 5 29 15402002-06-09 5 29 15412002-06-09 5 29 15422002-06-09 5 29 15432002-06-09 10 36 15442002-06-09 10 36 15452002-06-09 10 36 15462002-06-09 10 36 15472002-06-09 10 36 15482002-06-09 10 36 15492002-06-09 10 36 15502002-06-09 11 43 15512002-06-09 11 43 15522002-06-09 11 43 15532002-06-09 11 43 15542002-06-09 11 43 15552002-06-09 11 43 15562002-06-09 11 43 1557SELECT * from (SELECT date,id, PERCENT_RANK() OVER w AS `p_rank`, CUME_DIST() OVER w as `c_dist` FROM t1,t2 WINDOW w AS (PARTITION BY date ORDER BY id)) t; 1558date id p_rank c_dist 15592002-06-09 1 0 0.14285714285714285 15602002-06-09 1 0 0.14285714285714285 15612002-06-09 1 0 0.14285714285714285 15622002-06-09 1 0 0.14285714285714285 15632002-06-09 1 0 0.14285714285714285 15642002-06-09 1 0 0.14285714285714285 15652002-06-09 1 0 0.14285714285714285 15662002-06-09 2 0.14583333333333334 0.2857142857142857 15672002-06-09 2 0.14583333333333334 0.2857142857142857 15682002-06-09 2 0.14583333333333334 0.2857142857142857 15692002-06-09 2 0.14583333333333334 0.2857142857142857 15702002-06-09 2 0.14583333333333334 0.2857142857142857 15712002-06-09 2 0.14583333333333334 0.2857142857142857 15722002-06-09 2 0.14583333333333334 0.2857142857142857 15732002-06-09 3 0.2916666666666667 0.42857142857142855 15742002-06-09 3 0.2916666666666667 0.42857142857142855 15752002-06-09 3 0.2916666666666667 0.42857142857142855 15762002-06-09 3 0.2916666666666667 0.42857142857142855 15772002-06-09 3 0.2916666666666667 0.42857142857142855 15782002-06-09 3 0.2916666666666667 0.42857142857142855 15792002-06-09 3 0.2916666666666667 0.42857142857142855 15802002-06-09 4 0.4375 0.5714285714285714 15812002-06-09 4 0.4375 0.5714285714285714 15822002-06-09 4 0.4375 0.5714285714285714 15832002-06-09 4 0.4375 0.5714285714285714 15842002-06-09 4 0.4375 0.5714285714285714 15852002-06-09 4 0.4375 0.5714285714285714 15862002-06-09 4 0.4375 0.5714285714285714 15872002-06-09 5 0.5833333333333334 0.7142857142857143 15882002-06-09 5 0.5833333333333334 0.7142857142857143 15892002-06-09 5 0.5833333333333334 0.7142857142857143 15902002-06-09 5 0.5833333333333334 0.7142857142857143 15912002-06-09 5 0.5833333333333334 0.7142857142857143 15922002-06-09 5 0.5833333333333334 0.7142857142857143 15932002-06-09 5 0.5833333333333334 0.7142857142857143 15942002-06-09 10 0.7291666666666666 0.8571428571428571 15952002-06-09 10 0.7291666666666666 0.8571428571428571 15962002-06-09 10 0.7291666666666666 0.8571428571428571 15972002-06-09 10 0.7291666666666666 0.8571428571428571 15982002-06-09 10 0.7291666666666666 0.8571428571428571 15992002-06-09 10 0.7291666666666666 0.8571428571428571 16002002-06-09 10 0.7291666666666666 0.8571428571428571 16012002-06-09 11 0.875 1 16022002-06-09 11 0.875 1 16032002-06-09 11 0.875 1 16042002-06-09 11 0.875 1 16052002-06-09 11 0.875 1 16062002-06-09 11 0.875 1 16072002-06-09 11 0.875 1 1608---------------------------------------------------------------------- 1609- Window function in parent and subquery 1610---------------------------------------------------------------------- 1611SELECT t.*, SUM(t.`rank`) OVER (ROWS UNBOUNDED PRECEDING) FROM 1612(SELECT sex, id, date, ROW_NUMBER() OVER w AS row_no, RANK() OVER w AS `rank` FROM t1,t2 1613WHERE t1.id=t2.user_id 1614WINDOW w AS (PARTITION BY date ORDER BY id) 1615) AS t; 1616sex id date row_no rank SUM(t.`rank`) OVER (ROWS UNBOUNDED PRECEDING) 1617M 1 2002-06-09 1 1 1 1618M 1 2002-06-09 2 1 2 1619F 2 2002-06-09 3 3 5 1620F 3 2002-06-09 4 4 9 1621F 4 2002-06-09 5 5 14 1622F 4 2002-06-09 6 5 19 1623M 5 2002-06-09 7 7 26 1624SELECT t.*, SUM(t.`p_rank`) OVER (ROWS UNBOUNDED PRECEDING) FROM 1625(SELECT sex, id, date, ROW_NUMBER() OVER w AS row_no, PERCENT_RANK() OVER w AS `p_rank`, 1626CUME_DIST() OVER w as `c_dist` FROM t1,t2 1627WHERE t1.id=t2.user_id 1628WINDOW w AS (PARTITION BY date ORDER BY id) 1629) AS t; 1630sex id date row_no p_rank c_dist SUM(t.`p_rank`) OVER (ROWS UNBOUNDED PRECEDING) 1631M 1 2002-06-09 1 0 0.2857142857142857 0 1632M 1 2002-06-09 2 0 0.2857142857142857 0 1633F 2 2002-06-09 3 0.3333333333333333 0.42857142857142855 0.3333333333333333 1634F 3 2002-06-09 4 0.5 0.5714285714285714 0.8333333333333333 1635F 4 2002-06-09 5 0.6666666666666666 0.8571428571428571 1.5 1636F 4 2002-06-09 6 0.6666666666666666 0.8571428571428571 2.1666666666666665 1637M 5 2002-06-09 7 1 1 3.1666666666666665 1638---------------------------------------------------------------------- 1639- Multiple windows 1640---------------------------------------------------------------------- 1641SELECT t1.*, RANK() OVER (ORDER BY sex), SUM(id) OVER (ORDER BY sex,id ROWS UNBOUNDED PRECEDING) FROM t1; 1642id sex RANK() OVER (ORDER BY sex) SUM(id) OVER (ORDER BY sex,id ROWS UNBOUNDED PRECEDING) 164310 NULL 1 10 164411 NULL 1 21 16452 F 3 23 16463 F 3 26 16474 F 3 30 16481 M 6 31 16495 M 6 36 1650SELECT t1.*, PERCENT_RANK() OVER (ORDER BY sex), SUM(id) OVER (ORDER BY sex,id ROWS UNBOUNDED PRECEDING) FROM t1; 1651id sex PERCENT_RANK() OVER (ORDER BY sex) SUM(id) OVER (ORDER BY sex,id ROWS UNBOUNDED PRECEDING) 165210 NULL 0 10 165311 NULL 0 21 16542 F 0.3333333333333333 23 16553 F 0.3333333333333333 26 16564 F 0.3333333333333333 30 16571 M 0.8333333333333334 31 16585 M 0.8333333333333334 36 1659SELECT t1.*, CUME_DIST() OVER (ORDER BY sex), SUM(id) OVER (ORDER BY sex,id ROWS UNBOUNDED PRECEDING) FROM t1; 1660id sex CUME_DIST() OVER (ORDER BY sex) SUM(id) OVER (ORDER BY sex,id ROWS UNBOUNDED PRECEDING) 166110 NULL 0.2857142857142857 10 166211 NULL 0.2857142857142857 21 16632 F 0.7142857142857143 23 16643 F 0.7142857142857143 26 16654 F 0.7142857142857143 30 16661 M 1 31 16675 M 1 36 1668SELECT * from (SELECT t1.*, SUM(id) OVER (ROWS UNBOUNDED PRECEDING), RANK() OVER (ORDER BY sex) FROM t1) alias ORDER BY id; 1669id sex SUM(id) OVER (ROWS UNBOUNDED PRECEDING) RANK() OVER (ORDER BY sex) 16701 M 1 6 16712 F 3 3 16723 F 6 3 16734 F 10 3 16745 M 15 6 167510 NULL 25 1 167611 NULL 36 1 1677SELECT * from (SELECT t1.*, SUM(id) OVER (ROWS UNBOUNDED PRECEDING), PERCENT_RANK() OVER (ORDER BY sex) FROM t1) alias ORDER BY id; 1678id sex SUM(id) OVER (ROWS UNBOUNDED PRECEDING) PERCENT_RANK() OVER (ORDER BY sex) 16791 M 1 0.8333333333333334 16802 F 3 0.3333333333333333 16813 F 6 0.3333333333333333 16824 F 10 0.3333333333333333 16835 M 15 0.8333333333333334 168410 NULL 25 0 168511 NULL 36 0 1686SELECT * from (SELECT t1.*, SUM(id) OVER (ROWS UNBOUNDED PRECEDING), CUME_DIST() OVER (ORDER BY sex) FROM t1) alias ORDER BY id; 1687id sex SUM(id) OVER (ROWS UNBOUNDED PRECEDING) CUME_DIST() OVER (ORDER BY sex) 16881 M 1 1 16892 F 3 0.7142857142857143 16903 F 6 0.7142857142857143 16914 F 10 0.7142857142857143 16925 M 15 1 169310 NULL 25 0.2857142857142857 169411 NULL 36 0.2857142857142857 1695SELECT t1.*, SUM(id) OVER (ORDER BY id ROWS UNBOUNDED PRECEDING), 1696RANK() OVER (ORDER BY sex,id), 1697ROW_NUMBER() OVER (ORDER BY sex,id) 1698FROM t1; 1699id sex SUM(id) OVER (ORDER BY id ROWS UNBOUNDED PRECEDING) RANK() OVER (ORDER BY sex,id) ROW_NUMBER() OVER (ORDER BY sex,id) 170010 NULL 25 1 1 170111 NULL 36 2 2 17022 F 3 3 3 17033 F 6 4 4 17044 F 10 5 5 17051 M 1 6 6 17065 M 15 7 7 1707SELECT t1.*, SUM(id) OVER (ORDER BY id ROWS UNBOUNDED PRECEDING), 1708PERCENT_RANK() OVER (ORDER BY sex,id), 1709CUME_DIST() OVER (ORDER BY sex,id), 1710ROW_NUMBER() OVER (ORDER BY sex,id) 1711FROM t1; 1712id sex SUM(id) OVER (ORDER BY id ROWS UNBOUNDED PRECEDING) PERCENT_RANK() OVER (ORDER BY sex,id) CUME_DIST() OVER (ORDER BY sex,id) ROW_NUMBER() OVER (ORDER BY sex,id) 171310 NULL 25 0 0.14285714285714285 1 171411 NULL 36 0.16666666666666666 0.2857142857142857 2 17152 F 3 0.3333333333333333 0.42857142857142855 3 17163 F 6 0.5 0.5714285714285714 4 17174 F 10 0.6666666666666666 0.7142857142857143 5 17181 M 1 0.8333333333333334 0.8571428571428571 6 17195 M 15 1 1 7 1720a little more windows + subquery 1721SELECT t.*, SUM(id + r00 + r01) OVER (ORDER BY id ROWS UNBOUNDED PRECEDING) AS s FROM ( 1722SELECT t1.*, 1723RANK() OVER (ORDER BY sex, id) AS r00, 1724RANK() OVER (ORDER BY sex, id DESC) AS r01, 1725RANK() OVER (ORDER BY sex, id DESC) AS r02, 1726RANK() OVER (PARTITION BY id ORDER BY sex) AS r03, 1727RANK() OVER (ORDER BY sex,id) AS r04, 1728RANK() OVER (ORDER BY sex,id) AS r05, 1729RANK() OVER (ORDER BY sex, id) AS r06, 1730RANK() OVER (ORDER BY sex, id) AS r07, 1731RANK() OVER (ORDER BY sex, id) AS r08, 1732RANK() OVER (ORDER BY sex, id) AS r09, 1733RANK() OVER (ORDER BY sex, id) AS r10, 1734RANK() OVER (ORDER BY sex, id) AS r11, 1735RANK() OVER (ORDER BY sex, id) AS r12, 1736RANK() OVER (ORDER BY sex, id) AS r13, 1737RANK() OVER (ORDER BY sex, id) AS r14 1738FROM t1) t; 1739id sex r00 r01 r02 r03 r04 r05 r06 r07 r08 r09 r10 r11 r12 r13 r14 s 17401 M 6 7 7 1 6 6 6 6 6 6 6 6 6 6 6 14 17412 F 3 5 5 1 3 3 3 3 3 3 3 3 3 3 3 24 17423 F 4 4 4 1 4 4 4 4 4 4 4 4 4 4 4 35 17434 F 5 3 3 1 5 5 5 5 5 5 5 5 5 5 5 47 17445 M 7 6 6 1 7 7 7 7 7 7 7 7 7 7 7 65 174510 NULL 1 2 2 1 1 1 1 1 1 1 1 1 1 1 1 78 174611 NULL 2 1 1 1 2 2 2 2 2 2 2 2 2 2 2 92 1747With LIMIT 1748SELECT t.*, SUM(id + r00 + r01) OVER (ORDER BY id ROWS UNBOUNDED PRECEDING) AS s FROM ( 1749SELECT t1.*, 1750RANK() OVER (ORDER BY sex, id) AS r00, 1751RANK() OVER (ORDER BY sex DESC, id) AS r01, 1752RANK() OVER (ORDER BY sex, id DESC) AS r02, 1753RANK() OVER (PARTITION BY id ORDER BY sex) AS r03, 1754RANK() OVER (ORDER BY sex, id) AS r04, 1755RANK() OVER (ORDER BY sex, id) AS r05, 1756RANK() OVER (ORDER BY sex, id) AS r06, 1757RANK() OVER (ORDER BY sex, id) AS r07, 1758RANK() OVER (ORDER BY sex, id) AS r08, 1759RANK() OVER (ORDER BY sex, id) AS r09, 1760RANK() OVER (ORDER BY sex, id) AS r10, 1761RANK() OVER (ORDER BY sex, id) AS r11, 1762RANK() OVER (ORDER BY sex, id) AS r12, 1763RANK() OVER (ORDER BY sex, id) AS r13, 1764RANK() OVER (ORDER BY sex, id) AS r14 1765FROM t1 LIMIT 4) t; 1766id sex r00 r01 r02 r03 r04 r05 r06 r07 r08 r09 r10 r11 r12 r13 r14 s 17673 F 4 4 4 1 4 4 4 4 4 4 4 4 4 4 4 11 17684 F 5 5 3 1 5 5 5 5 5 5 5 5 5 5 5 25 176910 NULL 1 6 2 1 1 1 1 1 1 1 1 1 1 1 1 42 177011 NULL 2 7 1 1 2 2 2 2 2 2 2 2 2 2 2 62 1771SELECT t.*, SUM(id + r00 + r01) OVER (ORDER BY id ROWS UNBOUNDED PRECEDING) AS s FROM ( 1772SELECT t1.*, 1773PERCENT_RANK() OVER (ORDER BY sex, id) AS r00, 1774PERCENT_RANK() OVER (ORDER BY sex DESC, id) AS r01, 1775PERCENT_RANK() OVER (ORDER BY sex, id DESC) AS r02, 1776PERCENT_RANK() OVER (PARTITION BY id ORDER BY sex) AS r03, 1777PERCENT_RANK() OVER (ORDER BY sex, id) AS r04, 1778PERCENT_RANK() OVER (ORDER BY sex, id) AS r05, 1779PERCENT_RANK() OVER (ORDER BY sex, id) AS r06, 1780PERCENT_RANK() OVER (ORDER BY sex, id) AS r07, 1781PERCENT_RANK() OVER (ORDER BY sex, id) AS r08, 1782PERCENT_RANK() OVER (ORDER BY sex, id) AS r09, 1783PERCENT_RANK() OVER (ORDER BY sex, id) AS r10, 1784PERCENT_RANK() OVER (ORDER BY sex, id) AS r11, 1785PERCENT_RANK() OVER (ORDER BY sex, id) AS r12, 1786PERCENT_RANK() OVER (ORDER BY sex, id) AS r13, 1787PERCENT_RANK() OVER (ORDER BY sex, id) AS r14 1788FROM t1) t; 1789id sex r00 r01 r02 r03 r04 r05 r06 r07 r08 r09 r10 r11 r12 r13 r14 s 17901 M 0.8333333333333334 0 1 0 0.8333333333333334 0.8333333333333334 0.8333333333333334 0.8333333333333334 0.8333333333333334 0.8333333333333334 0.8333333333333334 0.8333333333333334 0.8333333333333334 0.8333333333333334 0.8333333333333334 1.8333333333333335 17912 F 0.3333333333333333 0.3333333333333333 0.6666666666666666 0 0.3333333333333333 0.3333333333333333 0.3333333333333333 0.3333333333333333 0.3333333333333333 0.3333333333333333 0.3333333333333333 0.3333333333333333 0.3333333333333333 0.3333333333333333 0.3333333333333333 4.5 17923 F 0.5 0.5 0.5 0 0.5 0.5 0.5 0.5 0.5 0.5 0.5 0.5 0.5 0.5 0.5 8.5 17934 F 0.6666666666666666 0.6666666666666666 0.3333333333333333 0 0.6666666666666666 0.6666666666666666 0.6666666666666666 0.6666666666666666 0.6666666666666666 0.6666666666666666 0.6666666666666666 0.6666666666666666 0.6666666666666666 0.6666666666666666 0.6666666666666666 13.833333333333334 17945 M 1 0.16666666666666666 0.8333333333333334 0 1 1 1 1 1 1 1 1 1 1 1 20 179510 NULL 0 0.8333333333333334 0.16666666666666666 0 0 0 0 0 0 0 0 0 0 0 0 30.833333333333336 179611 NULL 0.16666666666666666 1 0 0 0.16666666666666666 0.16666666666666666 0.16666666666666666 0.16666666666666666 0.16666666666666666 0.16666666666666666 0.16666666666666666 0.16666666666666666 0.16666666666666666 0.16666666666666666 0.16666666666666666 43 1797SELECT t.*, SUM(id + r00 + r01) OVER (ORDER BY id ROWS UNBOUNDED PRECEDING) AS s FROM ( 1798SELECT t1.*, 1799CUME_DIST() OVER (ORDER BY sex, id) AS r00, 1800CUME_DIST() OVER (ORDER BY sex DESC, id) AS r01, 1801CUME_DIST() OVER (ORDER BY sex, id DESC) AS r02, 1802CUME_DIST() OVER (PARTITION BY id ORDER BY sex) AS r03, 1803CUME_DIST() OVER (ORDER BY sex, id) AS r04, 1804CUME_DIST() OVER (ORDER BY sex, id) AS r05, 1805CUME_DIST() OVER (ORDER BY sex, id) AS r06, 1806CUME_DIST() OVER (ORDER BY sex, id) AS r07, 1807CUME_DIST() OVER (ORDER BY sex, id) AS r08, 1808CUME_DIST() OVER (ORDER BY sex, id) AS r09, 1809CUME_DIST() OVER (ORDER BY sex, id) AS r10, 1810CUME_DIST() OVER (ORDER BY sex, id) AS r11, 1811CUME_DIST() OVER (ORDER BY sex, id) AS r12, 1812CUME_DIST() OVER (ORDER BY sex, id) AS r13, 1813CUME_DIST() OVER (ORDER BY sex, id) AS r14 1814FROM t1) t; 1815id sex r00 r01 r02 r03 r04 r05 r06 r07 r08 r09 r10 r11 r12 r13 r14 s 18161 M 0.8571428571428571 0.14285714285714285 1 1 0.8571428571428571 0.8571428571428571 0.8571428571428571 0.8571428571428571 0.8571428571428571 0.8571428571428571 0.8571428571428571 0.8571428571428571 0.8571428571428571 0.8571428571428571 0.8571428571428571 2 18172 F 0.42857142857142855 0.42857142857142855 0.7142857142857143 1 0.42857142857142855 0.42857142857142855 0.42857142857142855 0.42857142857142855 0.42857142857142855 0.42857142857142855 0.42857142857142855 0.42857142857142855 0.42857142857142855 0.42857142857142855 0.42857142857142855 4.857142857142857 18183 F 0.5714285714285714 0.5714285714285714 0.5714285714285714 1 0.5714285714285714 0.5714285714285714 0.5714285714285714 0.5714285714285714 0.5714285714285714 0.5714285714285714 0.5714285714285714 0.5714285714285714 0.5714285714285714 0.5714285714285714 0.5714285714285714 9 18194 F 0.7142857142857143 0.7142857142857143 0.42857142857142855 1 0.7142857142857143 0.7142857142857143 0.7142857142857143 0.7142857142857143 0.7142857142857143 0.7142857142857143 0.7142857142857143 0.7142857142857143 0.7142857142857143 0.7142857142857143 0.7142857142857143 14.428571428571429 18205 M 1 0.2857142857142857 0.8571428571428571 1 1 1 1 1 1 1 1 1 1 1 1 20.714285714285715 182110 NULL 0.14285714285714285 0.8571428571428571 0.2857142857142857 1 0.14285714285714285 0.14285714285714285 0.14285714285714285 0.14285714285714285 0.14285714285714285 0.14285714285714285 0.14285714285714285 0.14285714285714285 0.14285714285714285 0.14285714285714285 0.14285714285714285 31.714285714285715 182211 NULL 0.2857142857142857 1 0.14285714285714285 1 0.2857142857142857 0.2857142857142857 0.2857142857142857 0.2857142857142857 0.2857142857142857 0.2857142857142857 0.2857142857142857 0.2857142857142857 0.2857142857142857 0.2857142857142857 0.2857142857142857 44 1823FLUSH STATUS; 1824SELECT t.*, SUM(id + r00 + r01) OVER (ORDER BY id ROWS UNBOUNDED PRECEDING) AS s FROM ( 1825SELECT t1.*, 1826RANK() OVER w00 AS r00, 1827RANK() OVER w01 AS r01, 1828RANK() OVER w02 AS r02, 1829RANK() OVER w03 AS r03, 1830RANK() OVER w04 AS r04, 1831RANK() OVER w05 AS r05, 1832RANK() OVER w06 AS r06, 1833RANK() OVER w07 AS r07, 1834RANK() OVER w08 AS r08, 1835RANK() OVER w09 AS r09, 1836RANK() OVER w10 AS r10, 1837RANK() OVER w11 AS r11, 1838RANK() OVER w12 AS r12, 1839RANK() OVER w13 AS r13, 1840RANK() OVER w14 AS r14 1841FROM t1 1842WINDOW w00 AS (ORDER BY sex), 1843w01 AS (ORDER BY sex DESC), 1844w02 AS (ORDER BY sex, id DESC), 1845w03 AS (PARTITION BY id ORDER BY sex), 1846w04 AS (ORDER BY sex), 1847w05 AS (ORDER BY sex), 1848w06 AS (ORDER BY sex), 1849w07 AS (ORDER BY sex), 1850w08 AS (ORDER BY sex), 1851w09 AS (ORDER BY sex), 1852w10 AS (ORDER BY sex), 1853w11 AS (ORDER BY sex), 1854w12 AS (ORDER BY sex), 1855w13 AS (ORDER BY sex), 1856w14 AS (ORDER BY sex)) t; 1857id sex r00 r01 r02 r03 r04 r05 r06 r07 r08 r09 r10 r11 r12 r13 r14 s 18581 M 6 1 7 1 6 6 6 6 6 6 6 6 6 6 6 8 18592 F 3 3 5 1 3 3 3 3 3 3 3 3 3 3 3 16 18603 F 3 3 4 1 3 3 3 3 3 3 3 3 3 3 3 25 18614 F 3 3 3 1 3 3 3 3 3 3 3 3 3 3 3 35 18625 M 6 1 6 1 6 6 6 6 6 6 6 6 6 6 6 47 186310 NULL 1 6 2 1 1 1 1 1 1 1 1 1 1 1 1 64 186411 NULL 1 6 1 1 1 1 1 1 1 1 1 1 1 1 1 82 1865SHOW STATUS LIKE 'Created_tmp_tables'; 1866Variable_name Value 1867Created_tmp_tables 15 1868Show sort elimination for the above 1869EXPLAIN FORMAT=JSON SELECT t.*, SUM(id + r00 + r01) OVER (ORDER BY id ROWS UNBOUNDED PRECEDING) AS s FROM ( 1870SELECT t1.*, 1871RANK() OVER w00 AS r00, 1872RANK() OVER w01 AS r01, 1873RANK() OVER w02 AS r02, 1874RANK() OVER w03 AS r03, 1875RANK() OVER w04 AS r04, 1876RANK() OVER w05 AS r05, 1877RANK() OVER w06 AS r06, 1878RANK() OVER w07 AS r07, 1879RANK() OVER w08 AS r08, 1880RANK() OVER w09 AS r09, 1881RANK() OVER w10 AS r10, 1882RANK() OVER w11 AS r11, 1883RANK() OVER w12 AS r12, 1884RANK() OVER w13 AS r13, 1885RANK() OVER w14 AS r14 1886FROM t1 1887WINDOW w00 AS (ORDER BY sex), 1888w01 AS (ORDER BY sex DESC), 1889w02 AS (ORDER BY sex, id DESC), 1890w03 AS (PARTITION BY id ORDER BY sex), 1891w04 AS (ORDER BY sex), 1892w05 AS (ORDER BY sex), 1893w06 AS (ORDER BY sex), 1894w07 AS (ORDER BY sex), 1895w08 AS (ORDER BY sex), 1896w09 AS (ORDER BY sex), 1897w10 AS (ORDER BY sex), 1898w11 AS (ORDER BY sex), 1899w12 AS (ORDER BY sex), 1900w13 AS (ORDER BY sex), 1901w14 AS (ORDER BY sex)) t; 1902EXPLAIN 1903{ 1904 "query_block": { 1905 "select_id": 1, 1906 "cost_info": { 1907 "query_cost": "10.29" 1908 }, 1909 "windowing": { 1910 "windows": [ 1911 { 1912 "name": "<unnamed window>", 1913 "using_filesort": true, 1914 "filesort_key": [ 1915 "`id`" 1916 ], 1917 "functions": [ 1918 "sum" 1919 ] 1920 } 1921 ], 1922 "cost_info": { 1923 "sort_cost": "7.00" 1924 }, 1925 "table": { 1926 "table_name": "t", 1927 "access_type": "ALL", 1928 "rows_examined_per_scan": 7, 1929 "rows_produced_per_join": 7, 1930 "filtered": "100.00", 1931 "cost_info": { 1932 "read_cost": "2.59", 1933 "eval_cost": "0.70", 1934 "prefix_cost": "3.29", 1935 "data_read_per_join": "952" 1936 }, 1937 "used_columns": [ 1938 "id", 1939 "sex", 1940 "r00", 1941 "r01", 1942 "r02", 1943 "r03", 1944 "r04", 1945 "r05", 1946 "r06", 1947 "r07", 1948 "r08", 1949 "r09", 1950 "r10", 1951 "r11", 1952 "r12", 1953 "r13", 1954 "r14" 1955 ], 1956 "materialized_from_subquery": { 1957 "using_temporary_table": true, 1958 "dependent": false, 1959 "cacheable": true, 1960 "query_block": { 1961 "select_id": 2, 1962 "cost_info": { 1963 "query_cost": "28.95" 1964 }, 1965 "windowing": { 1966 "windows": [ 1967 { 1968 "name": "w00", 1969 "definition_position": 1, 1970 "using_temporary_table": true, 1971 "using_filesort": true, 1972 "filesort_key": [ 1973 "`sex`" 1974 ], 1975 "functions": [ 1976 "rank" 1977 ] 1978 }, 1979 { 1980 "name": "w04", 1981 "definition_position": 5, 1982 "using_temporary_table": true, 1983 "functions": [ 1984 "rank" 1985 ] 1986 }, 1987 { 1988 "name": "w05", 1989 "definition_position": 6, 1990 "using_temporary_table": true, 1991 "functions": [ 1992 "rank" 1993 ] 1994 }, 1995 { 1996 "name": "w06", 1997 "definition_position": 7, 1998 "using_temporary_table": true, 1999 "functions": [ 2000 "rank" 2001 ] 2002 }, 2003 { 2004 "name": "w07", 2005 "definition_position": 8, 2006 "using_temporary_table": true, 2007 "functions": [ 2008 "rank" 2009 ] 2010 }, 2011 { 2012 "name": "w08", 2013 "definition_position": 9, 2014 "using_temporary_table": true, 2015 "functions": [ 2016 "rank" 2017 ] 2018 }, 2019 { 2020 "name": "w09", 2021 "definition_position": 10, 2022 "using_temporary_table": true, 2023 "functions": [ 2024 "rank" 2025 ] 2026 }, 2027 { 2028 "name": "w10", 2029 "definition_position": 11, 2030 "using_temporary_table": true, 2031 "functions": [ 2032 "rank" 2033 ] 2034 }, 2035 { 2036 "name": "w11", 2037 "definition_position": 12, 2038 "using_temporary_table": true, 2039 "functions": [ 2040 "rank" 2041 ] 2042 }, 2043 { 2044 "name": "w12", 2045 "definition_position": 13, 2046 "using_temporary_table": true, 2047 "functions": [ 2048 "rank" 2049 ] 2050 }, 2051 { 2052 "name": "w13", 2053 "definition_position": 14, 2054 "using_temporary_table": true, 2055 "functions": [ 2056 "rank" 2057 ] 2058 }, 2059 { 2060 "name": "w14", 2061 "definition_position": 15, 2062 "using_temporary_table": true, 2063 "functions": [ 2064 "rank" 2065 ] 2066 }, 2067 { 2068 "name": "w03", 2069 "definition_position": 4, 2070 "using_temporary_table": true, 2071 "using_filesort": true, 2072 "filesort_key": [ 2073 "`id`", 2074 "`sex`" 2075 ], 2076 "functions": [ 2077 "rank" 2078 ] 2079 }, 2080 { 2081 "name": "w01", 2082 "definition_position": 2, 2083 "using_temporary_table": true, 2084 "using_filesort": true, 2085 "filesort_key": [ 2086 "`sex` desc" 2087 ], 2088 "functions": [ 2089 "rank" 2090 ] 2091 }, 2092 { 2093 "name": "w02", 2094 "definition_position": 3, 2095 "last_executed_window": true, 2096 "using_filesort": true, 2097 "filesort_key": [ 2098 "`sex`", 2099 "`id` desc" 2100 ], 2101 "functions": [ 2102 "rank" 2103 ] 2104 } 2105 ], 2106 "cost_info": { 2107 "sort_cost": "28.00" 2108 }, 2109 "table": { 2110 "table_name": "t1", 2111 "access_type": "ALL", 2112 "rows_examined_per_scan": 7, 2113 "rows_produced_per_join": 7, 2114 "filtered": "100.00", 2115 "cost_info": { 2116 "read_cost": "0.25", 2117 "eval_cost": "0.70", 2118 "prefix_cost": "0.95", 2119 "data_read_per_join": "112" 2120 }, 2121 "used_columns": [ 2122 "id", 2123 "sex" 2124 ] 2125 } 2126 } 2127 } 2128 } 2129 } 2130 } 2131 } 2132} 2133Warnings: 2134Note 1003 /* select#1 */ select `t`.`id` AS `id`,`t`.`sex` AS `sex`,`t`.`r00` AS `r00`,`t`.`r01` AS `r01`,`t`.`r02` AS `r02`,`t`.`r03` AS `r03`,`t`.`r04` AS `r04`,`t`.`r05` AS `r05`,`t`.`r06` AS `r06`,`t`.`r07` AS `r07`,`t`.`r08` AS `r08`,`t`.`r09` AS `r09`,`t`.`r10` AS `r10`,`t`.`r11` AS `r11`,`t`.`r12` AS `r12`,`t`.`r13` AS `r13`,`t`.`r14` AS `r14`,sum(((`t`.`id` + `t`.`r00`) + `t`.`r01`)) OVER (ORDER BY `t`.`id` ROWS BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW) AS `s` from (/* select#2 */ select `test`.`t1`.`id` AS `id`,`test`.`t1`.`sex` AS `sex`,rank() OVER `w00` AS `r00`,rank() OVER `w01` AS `r01`,rank() OVER `w02` AS `r02`,rank() OVER `w03` AS `r03`,rank() OVER `w04` AS `r04`,rank() OVER `w05` AS `r05`,rank() OVER `w06` AS `r06`,rank() OVER `w07` AS `r07`,rank() OVER `w08` AS `r08`,rank() OVER `w09` AS `r09`,rank() OVER `w10` AS `r10`,rank() OVER `w11` AS `r11`,rank() OVER `w12` AS `r12`,rank() OVER `w13` AS `r13`,rank() OVER `w14` AS `r14` from `test`.`t1` window `w00` AS (ORDER BY `test`.`t1`.`sex` ) , `w04` AS (ORDER BY `test`.`t1`.`sex` ) , `w05` AS (ORDER BY `test`.`t1`.`sex` ) , `w06` AS (ORDER BY `test`.`t1`.`sex` ) , `w07` AS (ORDER BY `test`.`t1`.`sex` ) , `w08` AS (ORDER BY `test`.`t1`.`sex` ) , `w09` AS (ORDER BY `test`.`t1`.`sex` ) , `w10` AS (ORDER BY `test`.`t1`.`sex` ) , `w11` AS (ORDER BY `test`.`t1`.`sex` ) , `w12` AS (ORDER BY `test`.`t1`.`sex` ) , `w13` AS (ORDER BY `test`.`t1`.`sex` ) , `w14` AS (ORDER BY `test`.`t1`.`sex` ) , `w03` AS (PARTITION BY `test`.`t1`.`id` ORDER BY `test`.`t1`.`sex` ) , `w01` AS (ORDER BY `test`.`t1`.`sex` desc ) , `w02` AS (ORDER BY `test`.`t1`.`sex`,`test`.`t1`.`id` desc ) ) `t` 2135---------------------------------------------------------------------- 2136- SUM, AVG, COUNT with frames 2137---------------------------------------------------------------------- 2138SELECT SUM(id) OVER w * 2, AVG(id) OVER w, COUNT(id) OVER w FROM t1 2139WINDOW w AS (PARTITION BY sex); 2140SUM(id) OVER w * 2 AVG(id) OVER w COUNT(id) OVER w 214142 10.5000 2 214242 10.5000 2 214318 3.0000 3 214418 3.0000 3 214518 3.0000 3 214612 3.0000 2 214712 3.0000 2 2148SELECT * FROM ( 2149SELECT id, SUM(id) OVER w, COUNT(*) OVER w, sex FROM t1 2150WINDOW w AS (PARTITION BY sex) 2151) alias ORDER BY id; 2152id SUM(id) OVER w COUNT(*) OVER w sex 21531 6 2 M 21542 9 3 F 21553 9 3 F 21564 9 3 F 21575 6 2 M 215810 21 2 NULL 215911 21 2 NULL 2160SELECT SUM(id) OVER w FROM t1 WINDOW w AS (PARTITION BY sex); 2161SUM(id) OVER w 216221 216321 21649 21659 21669 21676 21686 2169SELECT id, SUM(id) OVER w, sex FROM t1 2170WINDOW w AS (PARTITION BY sex ORDER BY id 2171ROWS BETWEEN 2 PRECEDING AND 1 PRECEDING); 2172id SUM(id) OVER w sex 217310 NULL NULL 217411 10 NULL 21752 NULL F 21763 2 F 21774 5 F 21781 NULL M 21795 1 M 2180try the same as a view 2181CREATE VIEW v AS 2182SELECT id, SUM(id) OVER w, sex FROM t1 2183WINDOW w AS (PARTITION BY sex ORDER BY id 2184ROWS BETWEEN 2 PRECEDING AND 1 PRECEDING); 2185SHOW CREATE VIEW v; 2186View Create View character_set_client collation_connection 2187v CREATE ALGORITHM=UNDEFINED DEFINER=`root`@`localhost` SQL SECURITY DEFINER VIEW `v` AS select `t1`.`id` AS `id`,sum(`t1`.`id`) OVER `w` AS `SUM(id) OVER w`,`t1`.`sex` AS `sex` from `t1` window `w` AS (PARTITION BY `t1`.`sex` ORDER BY `t1`.`id` ROWS BETWEEN 2 PRECEDING AND 1 PRECEDING) utf8 utf8_general_ci 2188SELECT * FROM v; 2189id SUM(id) OVER w sex 219010 NULL NULL 219111 10 NULL 21922 NULL F 21933 2 F 21944 5 F 21951 NULL M 21965 1 M 2197DROP VIEW v; 2198SELECT SUM(id) OVER w FROM t1 2199WINDOW w AS (PARTITION BY sex ORDER BY id 2200ROWS BETWEEN 2 PRECEDING AND 1 PRECEDING); 2201SUM(id) OVER w 2202NULL 220310 2204NULL 22052 22065 2207NULL 22081 2209SELECT id, SUM(id) OVER w, sex FROM t1 2210WINDOW w AS (PARTITION BY sex ORDER BY id 2211ROWS BETWEEN 1 FOLLOWING AND 2 FOLLOWING); 2212id SUM(id) OVER w sex 221310 11 NULL 221411 NULL NULL 22152 7 F 22163 4 F 22174 NULL F 22181 5 M 22195 NULL M 2220SELECT SUM(id) OVER w, COUNT(*) OVER w FROM t1 2221WINDOW w AS (PARTITION BY sex ORDER BY id 2222ROWS BETWEEN 1 FOLLOWING AND 2 FOLLOWING); 2223SUM(id) OVER w COUNT(*) OVER w 222411 1 2225NULL 0 22267 2 22274 1 2228NULL 0 22295 1 2230NULL 0 2231SELECT id, AVG(id) OVER (ROWS UNBOUNDED PRECEDING) FROM t1; 2232id AVG(id) OVER (ROWS UNBOUNDED PRECEDING) 22331 1.0000 22342 1.5000 22353 2.0000 22364 2.5000 22375 3.0000 223810 4.1667 223911 5.1429 2240SELECT id, AVG(id) OVER w, COUNT(id) OVER w FROM t1 2241WINDOW w AS (ORDER BY id ROWS BETWEEN 1 PRECEDING AND 1 FOLLOWING); 2242id AVG(id) OVER w COUNT(id) OVER w 22431 1.5000 2 22442 2.0000 3 22453 3.0000 3 22464 4.0000 3 22475 6.3333 3 224810 8.6667 3 224911 10.5000 2 2250AVG, SUM with double type 2251CREATE TABLE td(d DOUBLE); 2252INSERT INTO td VALUES (2); 2253INSERT INTO td VALUES (2); 2254INSERT INTO td VALUES (3); 2255INSERT INTO td VALUES (1); 2256INSERT INTO td VALUES (1.2); 2257INSERT INTO td VALUES (NULL); 2258SELECT d, SUM(d) OVER (ORDER BY d), AVG(d) OVER (ORDER BY d) FROM td; 2259d SUM(d) OVER (ORDER BY d) AVG(d) OVER (ORDER BY d) 2260NULL NULL NULL 22611 1 1 22621.2 2.2 1.1 22632 6.2 1.55 22642 6.2 1.55 22653 9.2 1.8399999999999999 2266SELECT d, SUM(d) OVER (ORDER BY d), AVG(d) OVER () FROM td; 2267d SUM(d) OVER (ORDER BY d) AVG(d) OVER () 2268NULL NULL 1.8399999999999999 22691 1 1.8399999999999999 22701.2 2.2 1.8399999999999999 22712 6.2 1.8399999999999999 22722 6.2 1.8399999999999999 22733 9.2 1.8399999999999999 2274SELECT d, SUM(d) OVER (ORDER BY d), AVG(d) OVER (ORDER BY d ROWS BETWEEN 1 PRECEDING AND 1 FOLLOWING) FROM td; 2275d SUM(d) OVER (ORDER BY d) AVG(d) OVER (ORDER BY d ROWS BETWEEN 1 PRECEDING AND 1 FOLLOWING) 2276NULL NULL 1 22771 1 1.1 22781.2 2.2 1.4000000000000001 22792 6.2 1.7333333333333334 22802 6.2 2.3333333333333335 22813 9.2 2.5 2282Check system variable "windowing_use_high_precision" 2283TRUNCATE td; 2284INSERT INTO td VALUES (1.7976931348623157E+307); 2285INSERT INTO td VALUES (1); 2286should be default off: 2287SHOW VARIABLES LIKE 'windowing_use_high_precision'; 2288Variable_name Value 2289windowing_use_high_precision ON 2290SELECT d, SUM(d) OVER (ROWS BETWEEN CURRENT ROW AND 1 FOLLOWING) FROM td; 2291d SUM(d) OVER (ROWS BETWEEN CURRENT ROW AND 1 FOLLOWING) 22921.7976931348623158e307 1.7976931348623158e307 22931 1 2294allow unsafe optimization: result changes 2295SET SESSION windowing_use_high_precision=FALSE; 2296SELECT d, SUM(d) OVER (ROWS BETWEEN CURRENT ROW AND 1 FOLLOWING) FROM td; 2297d SUM(d) OVER (ROWS BETWEEN CURRENT ROW AND 1 FOLLOWING) 22981.7976931348623158e307 1.7976931348623158e307 22991 0 2300SET SESSION windowing_use_high_precision=TRUE; 2301bugfix: AVG for moving range frame 2302TRUNCATE td; 2303INSERT INTO td VALUES (10); 2304INSERT INTO td VALUES (1); 2305INSERT INTO td VALUES (2); 2306INSERT INTO td VALUES (3); 2307INSERT INTO td VALUES (4); 2308INSERT INTO td VALUES (5); 2309INSERT INTO td VALUES (6); 2310INSERT INTO td VALUES (7); 2311INSERT INTO td VALUES (8); 2312INSERT INTO td VALUES (9); 2313SELECT d, SUM(d) OVER w, AVG(d) OVER w FROM td 2314WINDOW w AS (ORDER BY d RANGE BETWEEN 2 PRECEDING AND CURRENT ROW); 2315d SUM(d) OVER w AVG(d) OVER w 23161 1 1 23172 3 1.5 23183 6 2 23194 9 3 23205 12 4 23216 15 5 23227 18 6 23238 21 7 23249 24 8 232510 27 9 2326SELECT d, SUM(d) OVER w, AVG(d) OVER w FROM td 2327WINDOW w AS (ORDER BY d RANGE BETWEEN 2 PRECEDING AND 2 FOLLOWING); 2328d SUM(d) OVER w AVG(d) OVER w 23291 6 2 23302 10 2.5 23313 15 3 23324 20 4 23335 25 5 23346 30 6 23357 35 7 23368 40 8 23379 34 8.5 233810 27 9 2339SELECT d, SUM(d) OVER w, AVG(d) OVER w FROM td 2340WINDOW w AS (ORDER BY d RANGE BETWEEN CURRENT ROW AND 2 FOLLOWING); 2341d SUM(d) OVER w AVG(d) OVER w 23421 6 2 23432 9 3 23443 12 4 23454 15 5 23465 18 6 23476 21 7 23487 24 8 23498 27 9 23509 19 9.5 235110 10 10 2352SET SESSION windowing_use_high_precision=FALSE; 2353SELECT d, SUM(d) OVER w, AVG(d) OVER w FROM td 2354WINDOW w AS (ORDER BY d RANGE BETWEEN 2 PRECEDING AND CURRENT ROW); 2355d SUM(d) OVER w AVG(d) OVER w 23561 1 1 23572 3 1.5 23583 6 2 23594 9 3 23605 12 4 23616 15 5 23627 18 6 23638 21 7 23649 24 8 236510 27 9 2366SELECT d, SUM(d) OVER w, AVG(d) OVER w FROM td 2367WINDOW w AS (ORDER BY d RANGE BETWEEN 2 PRECEDING AND 2 FOLLOWING); 2368d SUM(d) OVER w AVG(d) OVER w 23691 6 2 23702 10 2.5 23713 15 3 23724 20 4 23735 25 5 23746 30 6 23757 35 7 23768 40 8 23779 34 8.5 237810 27 9 2379SELECT d, SUM(d) OVER w, AVG(d) OVER w FROM td 2380WINDOW w AS (ORDER BY d RANGE BETWEEN CURRENT ROW AND 2 FOLLOWING); 2381d SUM(d) OVER w AVG(d) OVER w 23821 6 2 23832 9 3 23843 12 4 23854 15 5 23865 18 6 23876 21 7 23887 24 8 23898 27 9 23909 19 9.5 239110 10 10 2392SET SESSION windowing_use_high_precision=TRUE; 2393INSERT INTO td SELECT * FROM td; 2394SELECT d, SUM(d) OVER w, AVG(d) OVER w FROM td 2395WINDOW w AS (ORDER BY d RANGE BETWEEN 2 PRECEDING AND CURRENT ROW); 2396d SUM(d) OVER w AVG(d) OVER w 23971 2 1 23981 2 1 23992 6 1.5 24002 6 1.5 24013 12 2 24023 12 2 24034 18 3 24044 18 3 24055 24 4 24065 24 4 24076 30 5 24086 30 5 24097 36 6 24107 36 6 24118 42 7 24128 42 7 24139 48 8 24149 48 8 241510 54 9 241610 54 9 2417SELECT d, SUM(d) OVER w, AVG(d) OVER w FROM td 2418WINDOW w AS (ORDER BY d RANGE BETWEEN 2 PRECEDING AND 2 FOLLOWING); 2419d SUM(d) OVER w AVG(d) OVER w 24201 12 2 24211 12 2 24222 20 2.5 24232 20 2.5 24243 30 3 24253 30 3 24264 40 4 24274 40 4 24285 50 5 24295 50 5 24306 60 6 24316 60 6 24327 70 7 24337 70 7 24348 80 8 24358 80 8 24369 68 8.5 24379 68 8.5 243810 54 9 243910 54 9 2440SELECT d, SUM(d) OVER w, AVG(d) OVER w FROM td 2441WINDOW w AS (ORDER BY d RANGE BETWEEN CURRENT ROW AND 2 FOLLOWING); 2442d SUM(d) OVER w AVG(d) OVER w 24431 12 2 24441 12 2 24452 18 3 24462 18 3 24473 24 4 24483 24 4 24494 30 5 24504 30 5 24515 36 6 24525 36 6 24536 42 7 24546 42 7 24557 48 8 24567 48 8 24578 54 9 24588 54 9 24599 38 9.5 24609 38 9.5 246110 20 10 246210 20 10 2463SET SESSION windowing_use_high_precision=FALSE; 2464SELECT d, SUM(d) OVER w, AVG(d) OVER w FROM td 2465WINDOW w AS (ORDER BY d RANGE BETWEEN 2 PRECEDING AND CURRENT ROW); 2466d SUM(d) OVER w AVG(d) OVER w 24671 2 1 24681 2 1 24692 6 1.5 24702 6 1.5 24713 12 2 24723 12 2 24734 18 3 24744 18 3 24755 24 4 24765 24 4 24776 30 5 24786 30 5 24797 36 6 24807 36 6 24818 42 7 24828 42 7 24839 48 8 24849 48 8 248510 54 9 248610 54 9 2487SELECT d, SUM(d) OVER w, AVG(d) OVER w FROM td 2488WINDOW w AS (ORDER BY d RANGE BETWEEN 2 PRECEDING AND 2 FOLLOWING); 2489d SUM(d) OVER w AVG(d) OVER w 24901 12 2 24911 12 2 24922 20 2.5 24932 20 2.5 24943 30 3 24953 30 3 24964 40 4 24974 40 4 24985 50 5 24995 50 5 25006 60 6 25016 60 6 25027 70 7 25037 70 7 25048 80 8 25058 80 8 25069 68 8.5 25079 68 8.5 250810 54 9 250910 54 9 2510SELECT d, SUM(d) OVER w, AVG(d) OVER w FROM td 2511WINDOW w AS (ORDER BY d RANGE BETWEEN CURRENT ROW AND 2 FOLLOWING); 2512d SUM(d) OVER w AVG(d) OVER w 25131 12 2 25141 12 2 25152 18 3 25162 18 3 25173 24 4 25183 24 4 25194 30 5 25204 30 5 25215 36 6 25225 36 6 25236 42 7 25246 42 7 25257 48 8 25267 48 8 25278 54 9 25288 54 9 25299 38 9.5 25309 38 9.5 253110 20 10 253210 20 10 2533SET SESSION windowing_use_high_precision=TRUE; 2534DROP TABLE td; 2535---------------------------------------------------------------------- 2536- NTILE (requires two passes over partition). 2537- Currently suboptimal in that it causes N*N reads of tmp buffer 2538---------------------------------------------------------------------- 2539SELECT id, NTILE(0) OVER w FROM t1 WINDOW w AS (); 2540ERROR HY000: Incorrect arguments to ntile 2541SELECT id, NTILE(NULL) OVER w FROM t1 WINDOW w AS (ORDER BY id); 2542id NTILE(NULL) OVER w 25431 NULL 25442 NULL 25453 NULL 25464 NULL 25475 NULL 254810 NULL 254911 NULL 2550SELECT id, NTILE(1) OVER w FROM t1 WINDOW w AS (); 2551id NTILE(1) OVER w 25521 1 25532 1 25543 1 25554 1 25565 1 255710 1 255811 1 2559SELECT id, NTILE(5) OVER w FROM t1 WINDOW w AS (); 2560id NTILE(5) OVER w 25611 1 25622 1 25633 2 25644 2 25655 3 256610 4 256711 5 2568SELECT id, NTILE(1) OVER w FROM t1 WINDOW w AS (ORDER BY id); 2569id NTILE(1) OVER w 25701 1 25712 1 25723 1 25734 1 25745 1 257510 1 257611 1 2577SELECT id, NTILE(2) OVER w FROM t1 WINDOW w AS (ORDER BY id); 2578id NTILE(2) OVER w 25791 1 25802 1 25813 1 25824 1 25835 2 258410 2 258511 2 2586SELECT id, NTILE(5) OVER w FROM t1 WINDOW w AS (ORDER BY id); 2587id NTILE(5) OVER w 25881 1 25892 1 25903 2 25914 2 25925 3 259310 4 259411 5 2595SELECT id, NTILE(11) OVER w FROM t1 WINDOW w AS (ORDER BY id); 2596id NTILE(11) OVER w 25971 1 25982 2 25993 3 26004 4 26015 5 260210 6 260311 7 2604combo with frame 2605SELECT id, ROW_NUMBER() OVER w, NTILE(4) OVER w, SUM(id) OVER w FROM t1 2606WINDOW w AS (ORDER BY id ROWS 1 PRECEDING); 2607id ROW_NUMBER() OVER w NTILE(4) OVER w SUM(id) OVER w 26081 1 1 1 26092 2 1 3 26103 3 2 5 26114 4 2 7 26125 5 3 9 261310 6 3 15 261411 7 4 21 2615Try one where there are no extras 2616DELETE FROM t1 WHERE id=11; 2617SELECT id, NTILE(3) OVER w FROM t1 WINDOW w AS (ORDER BY id); 2618id NTILE(3) OVER w 26191 1 26202 1 26213 2 26224 2 26235 3 262410 3 2625INSERT INTO t1 VALUES (11, NULL); 2626PREPARE p FROM "SELECT id, NTILE(?) OVER w FROM t1 WINDOW w AS (ORDER BY id)"; 2627SET @p1= 3; 2628EXECUTE p USING @p1; 2629id NTILE(?) OVER w 26301 1 26312 1 26323 1 26334 2 26345 2 263510 3 263611 3 2637SET @p1= '1'; 2638EXECUTE p USING @p1; 2639ERROR HY000: Incorrect arguments to ntile 2640SET @p1= NULL; 2641EXECUTE p USING @p1; 2642id NTILE(?) OVER w 26431 NULL 26442 NULL 26453 NULL 26464 NULL 26475 NULL 264810 NULL 264911 NULL 2650DROP PREPARE p; 2651Simulated NTILE via other SQL window functions. Exercises an 2652an expression containing window functions defined on different 2653windows 2654SELECT (ROW_NUMBER() OVER w1 * 5 - 1) DIV (COUNT(*) OVER w2) + 1 AS cnt 2655FROM t1 WINDOW w1 AS (ORDER BY id ASC), 2656w2 AS (); 2657cnt 26581 26592 26603 26613 26624 26635 26645 2665SELECT (ROW_NUMBER() OVER w1 * 5 - 1) DIV (COUNT(*) OVER w2) + 1 AS ntile_manually, 2666COUNT(*) OVER w3 2667FROM t1 WINDOW w1 AS (ORDER BY id ASC), 2668w2 AS (), w3 AS (); 2669ntile_manually COUNT(*) OVER w3 26701 7 26712 7 26723 7 26733 7 26744 7 26755 7 26765 7 2677NTILE in combination with a frame that doesn't cover current row (was bug) 2678SELECT id, ROW_NUMBER() OVER w, SUM(id) OVER w, NTILE(5) OVER w FROM t1 2679WINDOW w AS (ORDER BY id ROWS BETWEEN UNBOUNDED PRECEDING AND 2 PRECEDING); 2680id ROW_NUMBER() OVER w SUM(id) OVER w NTILE(5) OVER w 26811 1 NULL 1 26822 2 NULL 1 26833 3 1 2 26844 4 3 2 26855 5 6 3 268610 6 10 4 268711 7 15 5 2688---------------------------------------------------------------------- 2689- SUM with frames in combination with non-framing window functions 2690- ROW_NUMBER and RANK 2691---------------------------------------------------------------------- 2692SELECT ROW_NUMBER() OVER w, id, SUM(id) OVER w, sex FROM t1 2693WINDOW w AS (PARTITION BY sex ORDER BY id ROWS BETWEEN UNBOUNDED PRECEDING AND UNBOUNDED FOLLOWING); 2694ROW_NUMBER() OVER w id SUM(id) OVER w sex 26951 10 21 NULL 26962 11 21 NULL 26971 2 9 F 26982 3 9 F 26993 4 9 F 27001 1 6 M 27012 5 6 M 2702SELECT ROW_NUMBER() OVER w, SUM(id) OVER w FROM t1 2703WINDOW w AS (PARTITION BY sex ORDER BY id 2704ROWS BETWEEN 1 FOLLOWING AND 2 FOLLOWING); 2705ROW_NUMBER() OVER w SUM(id) OVER w 27061 11 27072 NULL 27081 7 27092 4 27103 NULL 27111 5 27122 NULL 2713INSERT INTO t1 VALUES (10, NULL); 2714SELECT RANK() OVER w, id, SUM(id) OVER w, sex FROM t1 2715WINDOW w AS (PARTITION BY sex ORDER BY id); 2716RANK() OVER w id SUM(id) OVER w sex 27171 10 20 NULL 27181 10 20 NULL 27193 11 31 NULL 27201 2 2 F 27212 3 5 F 27223 4 9 F 27231 1 1 M 27242 5 6 M 2725SELECT RANK() OVER w, SUM(id) OVER w FROM t1 2726WINDOW w AS (PARTITION BY sex ORDER BY id 2727ROWS BETWEEN 1 FOLLOWING AND 2 FOLLOWING); 2728RANK() OVER w SUM(id) OVER w 27291 21 27301 11 27313 NULL 27321 7 27332 4 27343 NULL 27351 5 27362 NULL 2737SELECT id, sex, SUM(id) OVER w, 2738ROW_NUMBER() OVER w, 2739RANK() OVER w FROM t1 2740WINDOW w AS (PARTITION BY sex ORDER BY id ROWS BETWEEN 2 PRECEDING AND 1 PRECEDING); 2741id sex SUM(id) OVER w ROW_NUMBER() OVER w RANK() OVER w 274210 NULL NULL 1 1 274310 NULL 10 2 1 274411 NULL 20 3 3 27452 F NULL 1 1 27463 F 2 2 2 27474 F 5 3 3 27481 M NULL 1 1 27495 M 1 2 2 2750SELECT id, sex, SUM(id) OVER w, 2751ROW_NUMBER() OVER w, 2752CUME_DIST() OVER w FROM t1 2753WINDOW w AS (PARTITION BY sex ORDER BY id ROWS BETWEEN 2 PRECEDING AND 1 PRECEDING); 2754id sex SUM(id) OVER w ROW_NUMBER() OVER w CUME_DIST() OVER w 275510 NULL NULL 1 0.6666666666666666 275610 NULL 10 2 0.6666666666666666 275711 NULL 20 3 1 27582 F NULL 1 0.3333333333333333 27593 F 2 2 0.6666666666666666 27604 F 5 3 1 27611 M NULL 1 0.5 27625 M 1 2 1 2763Bug: if we have *any* window frame buffers, we need to add any fields 2764only referenced in expressions to the select list, so they get stored and 2765restored to/from the window frame buffer; which only uses copy_field. 2766Unfortunately, we don't know whther we have frame buffer at the time of 2767setup_fields so we must presume they are always used. 2768 2769Used to work (no frame buffer) 2770SELECT id+2, ROW_NUMBER() OVER () FROM t1; 2771id+2 ROW_NUMBER() OVER () 27723 1 27734 2 27745 3 27756 4 27767 5 277712 6 277813 7 277912 8 2780Used to work 2781SELECT id+2, FIRST_VALUE(sex) OVER (ORDER BY sex ROWS BETWEEN 1 PRECEDING AND 1 FOLLOWING) FROM t1; 2782id+2 FIRST_VALUE(sex) OVER (ORDER BY sex ROWS BETWEEN 1 PRECEDING AND 1 FOLLOWING) 278312 NULL 278413 NULL 278512 NULL 27864 NULL 27875 F 27886 F 27893 F 27907 M 2791Used to fail 2792SELECT id+2, NTILE(2) OVER (ORDER BY sex) FROM t1; 2793id+2 NTILE(2) OVER (ORDER BY sex) 279412 1 279513 1 279612 1 27974 1 27985 2 27996 2 28003 2 28017 2 2802SELECT NTILE(2) OVER (ORDER BY sex) FROM t1 ORDER BY id+2 DESC; 2803NTILE(2) OVER (ORDER BY sex) 28041 28051 28061 28072 28082 28092 28101 28112 2812---------------------------------------------------------------------- 2813- FIRST_VALUE 2814---------------------------------------------------------------------- 2815INSERT INTO t1 VALUES (NULL, 'M'); 2816SELECT FIRST_VALUE(6) OVER (); 2817FIRST_VALUE(6) OVER () 28186 2819SELECT FIRST_VALUE(6.0) OVER (); 2820FIRST_VALUE(6.0) OVER () 28216.0 2822SELECT FIRST_VALUE(CAST(6.0 AS DECIMAL(4,2))) OVER (); 2823FIRST_VALUE(CAST(6.0 AS DECIMAL(4,2))) OVER () 28246.00 2825SELECT FIRST_VALUE('6') OVER (); 2826FIRST_VALUE('6') OVER () 28276 2828SELECT FIRST_VALUE(NULL) OVER (); 2829FIRST_VALUE(NULL) OVER () 2830NULL 2831SELECT FIRST_VALUE(6) OVER (ROWS BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW); 2832FIRST_VALUE(6) OVER (ROWS BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW) 28336 2834SELECT FIRST_VALUE(NULL) OVER (ROWS BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW); 2835FIRST_VALUE(NULL) OVER (ROWS BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW) 2836NULL 2837SELECT LAST_VALUE(6) OVER (); 2838LAST_VALUE(6) OVER () 28396 2840SELECT LAST_VALUE(NULL) OVER (); 2841LAST_VALUE(NULL) OVER () 2842NULL 2843SELECT LAST_VALUE(6) OVER (ROWS BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW); 2844LAST_VALUE(6) OVER (ROWS BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW) 28456 2846SELECT LAST_VALUE(NULL) OVER (ROWS BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW); 2847LAST_VALUE(NULL) OVER (ROWS BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW) 2848NULL 2849SELECT id, FIRST_VALUE(id) OVER w FROM t1 WINDOW w AS (); 2850id FIRST_VALUE(id) OVER w 28511 1 28522 1 28533 1 28544 1 28555 1 285610 1 285711 1 285810 1 2859NULL 1 2860select id, FIRST_VALUE(id) OVER (ROWS UNBOUNDED PRECEDING) FROM t1; 2861id FIRST_VALUE(id) OVER (ROWS UNBOUNDED PRECEDING) 28621 1 28632 1 28643 1 28654 1 28665 1 286710 1 286811 1 286910 1 2870NULL 1 2871SELECT id, FIRST_VALUE(id) OVER w FROM t1 WINDOW w AS (ORDER BY id); 2872id FIRST_VALUE(id) OVER w 2873NULL NULL 28741 NULL 28752 NULL 28763 NULL 28774 NULL 28785 NULL 287910 NULL 288010 NULL 288111 NULL 2882SELECT id, FIRST_VALUE(id) OVER w FROM t1 WINDOW w AS (PARTITION BY sex ORDER BY id); 2883id FIRST_VALUE(id) OVER w 288410 10 288510 10 288611 10 28872 2 28883 2 28894 2 2890NULL NULL 28911 NULL 28925 NULL 2893SELECT id, FIRST_VALUE(id) OVER w FROM t1 WINDOW w AS (ORDER BY id DESC); 2894id FIRST_VALUE(id) OVER w 289511 11 289610 11 289710 11 28985 11 28994 11 29003 11 29012 11 29021 11 2903NULL 11 2904SELECT id, FIRST_VALUE(id) OVER w FROM t1 WINDOW w AS (ORDER BY id ROWS 2 PRECEDING); 2905id FIRST_VALUE(id) OVER w 2906NULL NULL 29071 NULL 29082 NULL 29093 1 29104 2 29115 3 291210 4 291310 5 291411 10 2915SELECT id, FIRST_VALUE(id) OVER w FROM t1 WINDOW w AS (ORDER BY id RANGE 2 PRECEDING); 2916id FIRST_VALUE(id) OVER w 2917NULL NULL 29181 1 29192 1 29203 1 29214 2 29225 3 292310 10 292410 10 292511 10 2926SELECT id, FIRST_VALUE(id) OVER w FROM t1 WINDOW w AS (ORDER BY id ROWS BETWEEN 2 PRECEDING AND 1 PRECEDING); 2927id FIRST_VALUE(id) OVER w 2928NULL NULL 29291 NULL 29302 NULL 29313 1 29324 2 29335 3 293410 4 293510 5 293611 10 2937SELECT id, FIRST_VALUE(id) OVER w FROM t1 WINDOW w AS (ORDER BY id RANGE BETWEEN 2 PRECEDING AND 1 PRECEDING); 2938id FIRST_VALUE(id) OVER w 2939NULL NULL 29401 NULL 29412 1 29423 1 29434 2 29445 3 294510 NULL 294610 NULL 294711 10 2948SELECT id, FIRST_VALUE(id) OVER w FROM t1 WINDOW w AS (ORDER BY id ROWS BETWEEN CURRENT ROW AND UNBOUNDED FOLLOWING); 2949id FIRST_VALUE(id) OVER w 2950NULL NULL 29511 1 29522 2 29533 3 29544 4 29555 5 295610 10 295710 10 295811 11 2959SELECT id, FIRST_VALUE(id) OVER w FROM t1 WINDOW w AS (ORDER BY id RANGE BETWEEN CURRENT ROW AND UNBOUNDED FOLLOWING); 2960id FIRST_VALUE(id) OVER w 2961NULL NULL 29621 1 29632 2 29643 3 29654 4 29665 5 296710 10 296810 10 296911 11 2970CREATE VIEW v AS 2971SELECT id, FIRST_VALUE(id) OVER w FROM t1 WINDOW w AS (ORDER BY id RANGE BETWEEN CURRENT ROW AND UNBOUNDED FOLLOWING); 2972SHOW CREATE VIEW v; 2973View Create View character_set_client collation_connection 2974v CREATE ALGORITHM=UNDEFINED DEFINER=`root`@`localhost` SQL SECURITY DEFINER VIEW `v` AS select `t1`.`id` AS `id`,first_value(`t1`.`id`) OVER `w` AS `FIRST_VALUE(id) OVER w` from `t1` window `w` AS (ORDER BY `t1`.`id` RANGE BETWEEN CURRENT ROW AND UNBOUNDED FOLLOWING) utf8 utf8_general_ci 2975SELECT * FROM v; 2976id FIRST_VALUE(id) OVER w 2977NULL NULL 29781 1 29792 2 29803 3 29814 4 29825 5 298310 10 298410 10 298511 11 2986DROP VIEW v; 2987SELECT id, FIRST_VALUE(id) OVER w FROM t1 WINDOW w AS (ORDER BY id ROWS BETWEEN 2 FOLLOWING AND 3 FOLLOWING); 2988id FIRST_VALUE(id) OVER w 2989NULL 2 29901 3 29912 4 29923 5 29934 10 29945 10 299510 11 299610 NULL 299711 NULL 2998SELECT id, FIRST_VALUE(id) OVER w FROM t1 WINDOW w AS (ORDER BY id RANGE BETWEEN 2 FOLLOWING AND 3 FOLLOWING); 2999id FIRST_VALUE(id) OVER w 3000NULL NULL 30011 3 30022 4 30033 5 30044 NULL 30055 NULL 300610 NULL 300710 NULL 300811 NULL 3009CREATE TABLE td1 (id DOUBLE, sex CHAR(1)); 3010INSERT INTO td1 SELECT * FROM t1; 3011SELECT id, FIRST_VALUE(id) OVER w FROM td1 WINDOW w AS (); 3012id FIRST_VALUE(id) OVER w 30131 1 30142 1 30153 1 30164 1 30175 1 301810 1 301911 1 302010 1 3021NULL 1 3022SELECT id, FIRST_VALUE(id) OVER w FROM td1 WINDOW w AS (ORDER BY id); 3023id FIRST_VALUE(id) OVER w 3024NULL NULL 30251 NULL 30262 NULL 30273 NULL 30284 NULL 30295 NULL 303010 NULL 303110 NULL 303211 NULL 3033SELECT id, FIRST_VALUE(id) OVER w FROM td1 WINDOW w AS (PARTITION BY sex ORDER BY id); 3034id FIRST_VALUE(id) OVER w 303510 10 303610 10 303711 10 30382 2 30393 2 30404 2 3041NULL NULL 30421 NULL 30435 NULL 3044SELECT id, FIRST_VALUE(id) OVER w FROM td1 WINDOW w AS (ORDER BY id DESC); 3045id FIRST_VALUE(id) OVER w 304611 11 304710 11 304810 11 30495 11 30504 11 30513 11 30522 11 30531 11 3054NULL 11 3055SELECT id, FIRST_VALUE(id) OVER w FROM td1 WINDOW w AS (ORDER BY id ROWS 2 PRECEDING); 3056id FIRST_VALUE(id) OVER w 3057NULL NULL 30581 NULL 30592 NULL 30603 1 30614 2 30625 3 306310 4 306410 5 306511 10 3066SELECT id, FIRST_VALUE(id) OVER w FROM td1 WINDOW w AS (ORDER BY id RANGE 2 PRECEDING); 3067id FIRST_VALUE(id) OVER w 3068NULL NULL 30691 1 30702 1 30713 1 30724 2 30735 3 307410 10 307510 10 307611 10 3077SELECT id, FIRST_VALUE(id) OVER w FROM td1 WINDOW w AS (ORDER BY id ROWS BETWEEN 2 PRECEDING AND 1 PRECEDING); 3078id FIRST_VALUE(id) OVER w 3079NULL NULL 30801 NULL 30812 NULL 30823 1 30834 2 30845 3 308510 4 308610 5 308711 10 3088SELECT id, FIRST_VALUE(id) OVER w FROM td1 WINDOW w AS (ORDER BY id RANGE BETWEEN 2 PRECEDING AND 1 PRECEDING); 3089id FIRST_VALUE(id) OVER w 3090NULL NULL 30911 NULL 30922 1 30933 1 30944 2 30955 3 309610 NULL 309710 NULL 309811 10 3099SELECT id, FIRST_VALUE(id) OVER w FROM td1 WINDOW w AS (ORDER BY id ROWS BETWEEN CURRENT ROW AND UNBOUNDED FOLLOWING); 3100id FIRST_VALUE(id) OVER w 3101NULL NULL 31021 1 31032 2 31043 3 31054 4 31065 5 310710 10 310810 10 310911 11 3110SELECT id, FIRST_VALUE(id) OVER w FROM td1 WINDOW w AS (ORDER BY id RANGE BETWEEN CURRENT ROW AND UNBOUNDED FOLLOWING); 3111id FIRST_VALUE(id) OVER w 3112NULL NULL 31131 1 31142 2 31153 3 31164 4 31175 5 311810 10 311910 10 312011 11 3121SELECT id, FIRST_VALUE(id) OVER w FROM td1 WINDOW w AS (ORDER BY id ROWS BETWEEN 2 FOLLOWING AND 3 FOLLOWING); 3122id FIRST_VALUE(id) OVER w 3123NULL 2 31241 3 31252 4 31263 5 31274 10 31285 10 312910 11 313010 NULL 313111 NULL 3132SELECT id, FIRST_VALUE(id) OVER w, CUME_DIST() OVER w FROM td1 WINDOW w AS (ORDER BY id ROWS BETWEEN 2 FOLLOWING AND 3 FOLLOWING); 3133id FIRST_VALUE(id) OVER w CUME_DIST() OVER w 3134NULL 2 0.1111111111111111 31351 3 0.2222222222222222 31362 4 0.3333333333333333 31373 5 0.4444444444444444 31384 10 0.5555555555555556 31395 10 0.6666666666666666 314010 11 0.8888888888888888 314110 NULL 0.8888888888888888 314211 NULL 1 3143SELECT id, FIRST_VALUE(id) OVER w FROM td1 WINDOW w AS (ORDER BY id RANGE BETWEEN 2 FOLLOWING AND 3 FOLLOWING); 3144id FIRST_VALUE(id) OVER w 3145NULL NULL 31461 3 31472 4 31483 5 31494 NULL 31505 NULL 315110 NULL 315210 NULL 315311 NULL 3154SELECT id, FIRST_VALUE(id) OVER w, CUME_DIST() OVER w FROM td1 WINDOW w AS (ORDER BY id RANGE BETWEEN 2 FOLLOWING AND 3 FOLLOWING); 3155id FIRST_VALUE(id) OVER w CUME_DIST() OVER w 3156NULL NULL 0.1111111111111111 31571 3 0.2222222222222222 31582 4 0.3333333333333333 31593 5 0.4444444444444444 31604 NULL 0.5555555555555556 31615 NULL 0.6666666666666666 316210 NULL 0.8888888888888888 316310 NULL 0.8888888888888888 316411 NULL 1 3165DROP TABLE td1; 3166CREATE TABLE td_dec (id DECIMAL(10,2), sex CHAR(1)); 3167INSERT INTO td_dec SELECT * FROM t1; 3168SELECT id, FIRST_VALUE(id) OVER w FROM td_dec WINDOW w AS (); 3169id FIRST_VALUE(id) OVER w 31701.00 1.00 31712.00 1.00 31723.00 1.00 31734.00 1.00 31745.00 1.00 317510.00 1.00 317611.00 1.00 317710.00 1.00 3178NULL 1.00 3179SELECT id, FIRST_VALUE(id) OVER w FROM td_dec WINDOW w AS (ORDER BY id); 3180id FIRST_VALUE(id) OVER w 3181NULL NULL 31821.00 NULL 31832.00 NULL 31843.00 NULL 31854.00 NULL 31865.00 NULL 318710.00 NULL 318810.00 NULL 318911.00 NULL 3190SELECT id, FIRST_VALUE(id) OVER w FROM td_dec WINDOW w AS (PARTITION BY sex ORDER BY id); 3191id FIRST_VALUE(id) OVER w 319210.00 10.00 319310.00 10.00 319411.00 10.00 31952.00 2.00 31963.00 2.00 31974.00 2.00 3198NULL NULL 31991.00 NULL 32005.00 NULL 3201SELECT id, FIRST_VALUE(id) OVER w FROM td_dec WINDOW w AS (ORDER BY id DESC); 3202id FIRST_VALUE(id) OVER w 320311.00 11.00 320410.00 11.00 320510.00 11.00 32065.00 11.00 32074.00 11.00 32083.00 11.00 32092.00 11.00 32101.00 11.00 3211NULL 11.00 3212SELECT id, FIRST_VALUE(id) OVER w FROM td_dec WINDOW w AS (ORDER BY id ROWS 2 PRECEDING); 3213id FIRST_VALUE(id) OVER w 3214NULL NULL 32151.00 NULL 32162.00 NULL 32173.00 1.00 32184.00 2.00 32195.00 3.00 322010.00 4.00 322110.00 5.00 322211.00 10.00 3223SELECT id, FIRST_VALUE(id) OVER w FROM td_dec WINDOW w AS (ORDER BY id RANGE 2 PRECEDING); 3224id FIRST_VALUE(id) OVER w 3225NULL NULL 32261.00 1.00 32272.00 1.00 32283.00 1.00 32294.00 2.00 32305.00 3.00 323110.00 10.00 323210.00 10.00 323311.00 10.00 3234SELECT id, FIRST_VALUE(id) OVER w FROM td_dec WINDOW w AS (ORDER BY id ROWS BETWEEN 2 PRECEDING AND 1 PRECEDING); 3235id FIRST_VALUE(id) OVER w 3236NULL NULL 32371.00 NULL 32382.00 NULL 32393.00 1.00 32404.00 2.00 32415.00 3.00 324210.00 4.00 324310.00 5.00 324411.00 10.00 3245SELECT id, FIRST_VALUE(id) OVER w FROM td_dec WINDOW w AS (ORDER BY id RANGE BETWEEN 2 PRECEDING AND 1 PRECEDING); 3246id FIRST_VALUE(id) OVER w 3247NULL NULL 32481.00 NULL 32492.00 1.00 32503.00 1.00 32514.00 2.00 32525.00 3.00 325310.00 NULL 325410.00 NULL 325511.00 10.00 3256SELECT id, FIRST_VALUE(id) OVER w FROM td_dec WINDOW w AS (ORDER BY id ROWS BETWEEN CURRENT ROW AND UNBOUNDED FOLLOWING); 3257id FIRST_VALUE(id) OVER w 3258NULL NULL 32591.00 1.00 32602.00 2.00 32613.00 3.00 32624.00 4.00 32635.00 5.00 326410.00 10.00 326510.00 10.00 326611.00 11.00 3267SELECT id, FIRST_VALUE(id) OVER w FROM td_dec WINDOW w AS (ORDER BY id RANGE BETWEEN CURRENT ROW AND UNBOUNDED FOLLOWING); 3268id FIRST_VALUE(id) OVER w 3269NULL NULL 32701.00 1.00 32712.00 2.00 32723.00 3.00 32734.00 4.00 32745.00 5.00 327510.00 10.00 327610.00 10.00 327711.00 11.00 3278SELECT id, FIRST_VALUE(id) OVER w FROM td_dec WINDOW w AS (ORDER BY id ROWS BETWEEN 2 FOLLOWING AND 3 FOLLOWING); 3279id FIRST_VALUE(id) OVER w 3280NULL 2.00 32811.00 3.00 32822.00 4.00 32833.00 5.00 32844.00 10.00 32855.00 10.00 328610.00 11.00 328710.00 NULL 328811.00 NULL 3289SELECT id, FIRST_VALUE(id) OVER w FROM td_dec WINDOW w AS (ORDER BY id RANGE BETWEEN 2 FOLLOWING AND 3 FOLLOWING); 3290id FIRST_VALUE(id) OVER w 3291NULL NULL 32921.00 3.00 32932.00 4.00 32943.00 5.00 32954.00 NULL 32965.00 NULL 329710.00 NULL 329810.00 NULL 329911.00 NULL 3300DROP TABLE td_dec; 3301CREATE TABLE td_str (id VARCHAR(20), sex CHAR(1)); 3302INSERT INTO td_str SELECT * FROM t1; 3303SELECT id, FIRST_VALUE(id) OVER w FROM td_str WINDOW w AS (); 3304id FIRST_VALUE(id) OVER w 33051 1 33062 1 33073 1 33084 1 33095 1 331010 1 331111 1 331210 1 3313NULL 1 3314SELECT id, FIRST_VALUE(id) OVER w FROM td_str WINDOW w AS (ORDER BY id); 3315id FIRST_VALUE(id) OVER w 3316NULL NULL 33171 NULL 331810 NULL 331910 NULL 332011 NULL 33212 NULL 33223 NULL 33234 NULL 33245 NULL 3325SELECT id, FIRST_VALUE(id) OVER w FROM td_str WINDOW w AS (PARTITION BY sex ORDER BY id); 3326id FIRST_VALUE(id) OVER w 332710 10 332810 10 332911 10 33302 2 33313 2 33324 2 3333NULL NULL 33341 NULL 33355 NULL 3336SELECT id, FIRST_VALUE(id) OVER w FROM td_str WINDOW w AS (ORDER BY id DESC); 3337id FIRST_VALUE(id) OVER w 33385 5 33394 5 33403 5 33412 5 334211 5 334310 5 334410 5 33451 5 3346NULL 5 3347SELECT id, FIRST_VALUE(id) OVER w FROM td_str WINDOW w AS (ORDER BY id ROWS 2 PRECEDING); 3348id FIRST_VALUE(id) OVER w 3349NULL NULL 33501 NULL 335110 NULL 335210 1 335311 10 33542 10 33553 11 33564 2 33575 3 3358SELECT id, FIRST_VALUE(id) OVER w FROM td_str WINDOW w AS (ORDER BY id RANGE 2 PRECEDING); 3359ERROR HY000: Window 'w' with RANGE N PRECEDING/FOLLOWING frame requires exactly one ORDER BY expression, of numeric or temporal type 3360SELECT id, FIRST_VALUE(id) OVER w FROM td_str WINDOW w AS (ORDER BY id ROWS BETWEEN 2 PRECEDING AND 1 PRECEDING); 3361id FIRST_VALUE(id) OVER w 3362NULL NULL 33631 NULL 336410 NULL 336510 1 336611 10 33672 10 33683 11 33694 2 33705 3 3371SELECT id, FIRST_VALUE(id) OVER w FROM td_str WINDOW w AS (ORDER BY id RANGE BETWEEN 2 PRECEDING AND 1 PRECEDING); 3372ERROR HY000: Window 'w' with RANGE N PRECEDING/FOLLOWING frame requires exactly one ORDER BY expression, of numeric or temporal type 3373SELECT id, FIRST_VALUE(id) OVER w FROM td_str WINDOW w AS (ORDER BY id ROWS BETWEEN CURRENT ROW AND UNBOUNDED FOLLOWING); 3374id FIRST_VALUE(id) OVER w 3375NULL NULL 33761 1 337710 10 337810 10 337911 11 33802 2 33813 3 33824 4 33835 5 3384SELECT id, FIRST_VALUE(id) OVER w FROM td_str WINDOW w AS (ORDER BY id RANGE BETWEEN CURRENT ROW AND UNBOUNDED FOLLOWING); 3385id FIRST_VALUE(id) OVER w 3386NULL NULL 33871 1 338810 10 338910 10 339011 11 33912 2 33923 3 33934 4 33945 5 3395SELECT id, FIRST_VALUE(id) OVER w FROM td_str WINDOW w AS (ORDER BY id ROWS BETWEEN 2 FOLLOWING AND 3 FOLLOWING); 3396id FIRST_VALUE(id) OVER w 3397NULL 10 33981 10 339910 11 340010 2 340111 3 34022 4 34033 5 34044 NULL 34055 NULL 3406SELECT id, FIRST_VALUE(id) OVER w FROM td_str WINDOW w AS (ORDER BY id RANGE BETWEEN 2 FOLLOWING AND 3 FOLLOWING); 3407ERROR HY000: Window 'w' with RANGE N PRECEDING/FOLLOWING frame requires exactly one ORDER BY expression, of numeric or temporal type 3408DROP TABLE td_str; 3409CREATE TABLE t_date(id DATE); 3410INSERT INTO t_date VALUES ('2002-06-09'); 3411INSERT INTO t_date VALUES ('2002-06-09'); 3412INSERT INTO t_date VALUES ('2002-06-10'); 3413INSERT INTO t_date VALUES ('2002-06-09'); 3414INSERT INTO t_date VALUES ('2015-08-01'); 3415INSERT INTO t_date VALUES ('2002-06-09'); 3416INSERT INTO t_date VALUES ('2015-08-01'); 3417SELECT id, FIRST_VALUE(id) OVER w FROM t_date WINDOW w AS (); 3418id FIRST_VALUE(id) OVER w 34192002-06-09 2002-06-09 34202002-06-09 2002-06-09 34212002-06-10 2002-06-09 34222002-06-09 2002-06-09 34232015-08-01 2002-06-09 34242002-06-09 2002-06-09 34252015-08-01 2002-06-09 3426SELECT id, FIRST_VALUE(id) OVER w FROM t_date WINDOW w AS (ORDER BY id); 3427id FIRST_VALUE(id) OVER w 34282002-06-09 2002-06-09 34292002-06-09 2002-06-09 34302002-06-09 2002-06-09 34312002-06-09 2002-06-09 34322002-06-10 2002-06-09 34332015-08-01 2002-06-09 34342015-08-01 2002-06-09 3435SELECT id, FIRST_VALUE(id) OVER w FROM t_date WINDOW w AS (ORDER BY id DESC); 3436id FIRST_VALUE(id) OVER w 34372015-08-01 2015-08-01 34382015-08-01 2015-08-01 34392002-06-10 2015-08-01 34402002-06-09 2015-08-01 34412002-06-09 2015-08-01 34422002-06-09 2015-08-01 34432002-06-09 2015-08-01 3444SELECT id, FIRST_VALUE(id) OVER w FROM t_date WINDOW w AS (ORDER BY id ROWS 2 PRECEDING); 3445id FIRST_VALUE(id) OVER w 34462002-06-09 2002-06-09 34472002-06-09 2002-06-09 34482002-06-09 2002-06-09 34492002-06-09 2002-06-09 34502002-06-10 2002-06-09 34512015-08-01 2002-06-09 34522015-08-01 2002-06-10 3453SELECT id, FIRST_VALUE(id) OVER w FROM t_date WINDOW w AS (ORDER BY id RANGE INTERVAL 2 DAY PRECEDING); 3454id FIRST_VALUE(id) OVER w 34552002-06-09 2002-06-09 34562002-06-09 2002-06-09 34572002-06-09 2002-06-09 34582002-06-09 2002-06-09 34592002-06-10 2002-06-09 34602015-08-01 2015-08-01 34612015-08-01 2015-08-01 3462SELECT id, FIRST_VALUE(id) OVER w FROM t_date WINDOW w AS (ORDER BY id ROWS BETWEEN 2 PRECEDING AND 1 PRECEDING); 3463id FIRST_VALUE(id) OVER w 34642002-06-09 NULL 34652002-06-09 2002-06-09 34662002-06-09 2002-06-09 34672002-06-09 2002-06-09 34682002-06-10 2002-06-09 34692015-08-01 2002-06-09 34702015-08-01 2002-06-10 3471SELECT id, FIRST_VALUE(id) OVER w FROM t_date WINDOW w AS (ORDER BY id RANGE BETWEEN INTERVAL 2 DAY PRECEDING AND INTERVAL 1 DAY PRECEDING); 3472id FIRST_VALUE(id) OVER w 34732002-06-09 NULL 34742002-06-09 NULL 34752002-06-09 NULL 34762002-06-09 NULL 34772002-06-10 2002-06-09 34782015-08-01 NULL 34792015-08-01 NULL 3480SELECT id, FIRST_VALUE(id) OVER w FROM t_date WINDOW w AS (ORDER BY id ROWS BETWEEN CURRENT ROW AND UNBOUNDED FOLLOWING); 3481id FIRST_VALUE(id) OVER w 34822002-06-09 2002-06-09 34832002-06-09 2002-06-09 34842002-06-09 2002-06-09 34852002-06-09 2002-06-09 34862002-06-10 2002-06-10 34872015-08-01 2015-08-01 34882015-08-01 2015-08-01 3489SELECT id, FIRST_VALUE(id) OVER w FROM t_date WINDOW w AS (ORDER BY id RANGE BETWEEN CURRENT ROW AND UNBOUNDED FOLLOWING); 3490id FIRST_VALUE(id) OVER w 34912002-06-09 2002-06-09 34922002-06-09 2002-06-09 34932002-06-09 2002-06-09 34942002-06-09 2002-06-09 34952002-06-10 2002-06-10 34962015-08-01 2015-08-01 34972015-08-01 2015-08-01 3498CREATE VIEW v AS 3499SELECT id, FIRST_VALUE(id) OVER w FROM t_date WINDOW w AS (ORDER BY id RANGE BETWEEN CURRENT ROW AND UNBOUNDED FOLLOWING); 3500SHOW CREATE VIEW v; 3501View Create View character_set_client collation_connection 3502v CREATE ALGORITHM=UNDEFINED DEFINER=`root`@`localhost` SQL SECURITY DEFINER VIEW `v` AS select `t_date`.`id` AS `id`,first_value(`t_date`.`id`) OVER `w` AS `FIRST_VALUE(id) OVER w` from `t_date` window `w` AS (ORDER BY `t_date`.`id` RANGE BETWEEN CURRENT ROW AND UNBOUNDED FOLLOWING) utf8 utf8_general_ci 3503SELECT * FROM v; 3504id FIRST_VALUE(id) OVER w 35052002-06-09 2002-06-09 35062002-06-09 2002-06-09 35072002-06-09 2002-06-09 35082002-06-09 2002-06-09 35092002-06-10 2002-06-10 35102015-08-01 2015-08-01 35112015-08-01 2015-08-01 3512DROP VIEW v; 3513SELECT id, FIRST_VALUE(id) OVER w FROM t_date WINDOW w AS (ORDER BY id ROWS BETWEEN 2 FOLLOWING AND 3 FOLLOWING); 3514id FIRST_VALUE(id) OVER w 35152002-06-09 2002-06-09 35162002-06-09 2002-06-09 35172002-06-09 2002-06-10 35182002-06-09 2015-08-01 35192002-06-10 2015-08-01 35202015-08-01 NULL 35212015-08-01 NULL 3522SELECT id, FIRST_VALUE(id) OVER w FROM t_date WINDOW w AS (ORDER BY id RANGE BETWEEN INTERVAL 2 DAY FOLLOWING AND INTERVAL 3 DAY FOLLOWING); 3523id FIRST_VALUE(id) OVER w 35242002-06-09 NULL 35252002-06-09 NULL 35262002-06-09 NULL 35272002-06-09 NULL 35282002-06-10 NULL 35292015-08-01 NULL 35302015-08-01 NULL 3531CREATE VIEW v AS 3532SELECT id, FIRST_VALUE(id) OVER w FROM t_date WINDOW w AS (ORDER BY id RANGE BETWEEN INTERVAL 2 DAY FOLLOWING AND INTERVAL 3 DAY FOLLOWING); 3533SHOW CREATE VIEW v; 3534View Create View character_set_client collation_connection 3535v CREATE ALGORITHM=UNDEFINED DEFINER=`root`@`localhost` SQL SECURITY DEFINER VIEW `v` AS select `t_date`.`id` AS `id`,first_value(`t_date`.`id`) OVER `w` AS `FIRST_VALUE(id) OVER w` from `t_date` window `w` AS (ORDER BY `t_date`.`id` RANGE BETWEEN INTERVAL 2 day FOLLOWING AND INTERVAL 3 day FOLLOWING) utf8 utf8_general_ci 3536SELECT * FROM v; 3537id FIRST_VALUE(id) OVER w 35382002-06-09 NULL 35392002-06-09 NULL 35402002-06-09 NULL 35412002-06-09 NULL 35422002-06-10 NULL 35432015-08-01 NULL 35442015-08-01 NULL 3545DROP VIEW v; 3546DROP TABLE t_date; 3547CREATE TABLE t_time(t TIME, ts TIMESTAMP); 3548INSERT INTO t_time VALUES ('12:30', '2016-07-05 08:30:42'); 3549INSERT INTO t_time VALUES ('22:30', '2015-07-05 08:30:43'); 3550INSERT INTO t_time VALUES ('13:30', '2014-07-05 08:30:44'); 3551INSERT INTO t_time VALUES ('01:30', '2013-07-05 08:30:45'); 3552INSERT INTO t_time VALUES ('15:30', '2016-08-05 08:31:42'); 3553INSERT INTO t_time VALUES ('20:30', '2016-09-05 08:32:42'); 3554INSERT INTO t_time VALUES ('04:30', '2016-10-05 08:33:42'); 3555INSERT INTO t_time VALUES ('06:30', '2016-11-05 08:34:42'); 3556INSERT INTO t_time VALUES ('18:30', '2016-07-05 09:30:42'); 3557INSERT INTO t_time VALUES ('21:30', '2016-07-06 10:30:42'); 3558INSERT INTO t_time VALUES ('00:30', '2016-07-07 11:30:42'); 3559INSERT INTO t_time VALUES ('00:31', '2016-07-08 12:30:42'); 3560CREATE TABLE t_time2(t TIME, ts TIMESTAMP, p INTEGER DEFAULT 1); 3561INSERT INTO t_time2(t, ts) SELECT * FROM t_time; 3562UPDATE t_time2 SET p=p+1; 3563INSERT INTO t_time2(t, ts) SELECT * FROM t_time; 3564SELECT t, FIRST_VALUE(t) OVER w FROM t_time WINDOW w AS (); 3565t FIRST_VALUE(t) OVER w 356612:30:00 12:30:00 356722:30:00 12:30:00 356813:30:00 12:30:00 356901:30:00 12:30:00 357015:30:00 12:30:00 357120:30:00 12:30:00 357204:30:00 12:30:00 357306:30:00 12:30:00 357418:30:00 12:30:00 357521:30:00 12:30:00 357600:30:00 12:30:00 357700:31:00 12:30:00 3578SELECT t, FIRST_VALUE(t) OVER w FROM t_time WINDOW w AS (ORDER BY t); 3579t FIRST_VALUE(t) OVER w 358000:30:00 00:30:00 358100:31:00 00:30:00 358201:30:00 00:30:00 358304:30:00 00:30:00 358406:30:00 00:30:00 358512:30:00 00:30:00 358613:30:00 00:30:00 358715:30:00 00:30:00 358818:30:00 00:30:00 358920:30:00 00:30:00 359021:30:00 00:30:00 359122:30:00 00:30:00 3592SELECT t, FIRST_VALUE(t) OVER w FROM t_time WINDOW w AS (ORDER BY t DESC); 3593t FIRST_VALUE(t) OVER w 359422:30:00 22:30:00 359521:30:00 22:30:00 359620:30:00 22:30:00 359718:30:00 22:30:00 359815:30:00 22:30:00 359913:30:00 22:30:00 360012:30:00 22:30:00 360106:30:00 22:30:00 360204:30:00 22:30:00 360301:30:00 22:30:00 360400:31:00 22:30:00 360500:30:00 22:30:00 3606SELECT t, FIRST_VALUE(t) OVER w FROM t_time WINDOW w AS (ORDER BY t ROWS 2 PRECEDING); 3607t FIRST_VALUE(t) OVER w 360800:30:00 00:30:00 360900:31:00 00:30:00 361001:30:00 00:30:00 361104:30:00 00:31:00 361206:30:00 01:30:00 361312:30:00 04:30:00 361413:30:00 06:30:00 361515:30:00 12:30:00 361618:30:00 13:30:00 361720:30:00 15:30:00 361821:30:00 18:30:00 361922:30:00 20:30:00 3620SELECT t, FIRST_VALUE(t) OVER w FROM t_time WINDOW w AS (ORDER BY t RANGE INTERVAL 2 HOUR PRECEDING); 3621t FIRST_VALUE(t) OVER w 362200:30:00 00:30:00 362300:31:00 00:30:00 362401:30:00 00:30:00 362504:30:00 04:30:00 362606:30:00 04:30:00 362712:30:00 12:30:00 362813:30:00 12:30:00 362915:30:00 13:30:00 363018:30:00 18:30:00 363120:30:00 18:30:00 363221:30:00 20:30:00 363322:30:00 20:30:00 3634SELECT t, FIRST_VALUE(t) OVER w FROM t_time WINDOW w AS (ORDER BY t ROWS BETWEEN 2 PRECEDING AND 1 PRECEDING); 3635t FIRST_VALUE(t) OVER w 363600:30:00 NULL 363700:31:00 00:30:00 363801:30:00 00:30:00 363904:30:00 00:31:00 364006:30:00 01:30:00 364112:30:00 04:30:00 364213:30:00 06:30:00 364315:30:00 12:30:00 364418:30:00 13:30:00 364520:30:00 15:30:00 364621:30:00 18:30:00 364722:30:00 20:30:00 3648SELECT t, FIRST_VALUE(t) OVER w FROM t_time WINDOW w AS (ORDER BY t RANGE BETWEEN INTERVAL 2 HOUR PRECEDING AND INTERVAL 1 HOUR PRECEDING); 3649t FIRST_VALUE(t) OVER w 365000:30:00 NULL 365100:31:00 NULL 365201:30:00 00:30:00 365304:30:00 NULL 365406:30:00 04:30:00 365512:30:00 NULL 365613:30:00 12:30:00 365715:30:00 13:30:00 365818:30:00 NULL 365920:30:00 18:30:00 366021:30:00 20:30:00 366122:30:00 20:30:00 3662SELECT t, FIRST_VALUE(t) OVER w FROM t_time WINDOW w AS (ORDER BY t ROWS BETWEEN CURRENT ROW AND UNBOUNDED FOLLOWING); 3663t FIRST_VALUE(t) OVER w 366400:30:00 00:30:00 366500:31:00 00:31:00 366601:30:00 01:30:00 366704:30:00 04:30:00 366806:30:00 06:30:00 366912:30:00 12:30:00 367013:30:00 13:30:00 367115:30:00 15:30:00 367218:30:00 18:30:00 367320:30:00 20:30:00 367421:30:00 21:30:00 367522:30:00 22:30:00 3676SELECT t, FIRST_VALUE(t) OVER w FROM t_time WINDOW w AS (ORDER BY t RANGE BETWEEN CURRENT ROW AND UNBOUNDED FOLLOWING); 3677t FIRST_VALUE(t) OVER w 367800:30:00 00:30:00 367900:31:00 00:31:00 368001:30:00 01:30:00 368104:30:00 04:30:00 368206:30:00 06:30:00 368312:30:00 12:30:00 368413:30:00 13:30:00 368515:30:00 15:30:00 368618:30:00 18:30:00 368720:30:00 20:30:00 368821:30:00 21:30:00 368922:30:00 22:30:00 3690SELECT t, FIRST_VALUE(t) OVER w FROM t_time WINDOW w AS (ORDER BY t ROWS BETWEEN 2 FOLLOWING AND 3 FOLLOWING); 3691t FIRST_VALUE(t) OVER w 369200:30:00 01:30:00 369300:31:00 04:30:00 369401:30:00 06:30:00 369504:30:00 12:30:00 369606:30:00 13:30:00 369712:30:00 15:30:00 369813:30:00 18:30:00 369915:30:00 20:30:00 370018:30:00 21:30:00 370120:30:00 22:30:00 370221:30:00 NULL 370322:30:00 NULL 3704SELECT t, FIRST_VALUE(t) OVER w FROM t_time WINDOW w AS (ORDER BY t RANGE BETWEEN INTERVAL 2 HOUR FOLLOWING AND INTERVAL 3 HOUR FOLLOWING); 3705t FIRST_VALUE(t) OVER w 370600:30:00 NULL 370700:31:00 NULL 370801:30:00 04:30:00 370904:30:00 06:30:00 371006:30:00 NULL 371112:30:00 15:30:00 371213:30:00 15:30:00 371315:30:00 18:30:00 371418:30:00 20:30:00 371520:30:00 22:30:00 371621:30:00 NULL 371722:30:00 NULL 3718SELECT p, t, FIRST_VALUE(t) OVER w FROM t_time2 WINDOW w AS (PARTITION by p ); 3719p t FIRST_VALUE(t) OVER w 37201 12:30:00 12:30:00 37211 22:30:00 12:30:00 37221 13:30:00 12:30:00 37231 01:30:00 12:30:00 37241 15:30:00 12:30:00 37251 20:30:00 12:30:00 37261 04:30:00 12:30:00 37271 06:30:00 12:30:00 37281 18:30:00 12:30:00 37291 21:30:00 12:30:00 37301 00:30:00 12:30:00 37311 00:31:00 12:30:00 37322 12:30:00 12:30:00 37332 22:30:00 12:30:00 37342 13:30:00 12:30:00 37352 01:30:00 12:30:00 37362 15:30:00 12:30:00 37372 20:30:00 12:30:00 37382 04:30:00 12:30:00 37392 06:30:00 12:30:00 37402 18:30:00 12:30:00 37412 21:30:00 12:30:00 37422 00:30:00 12:30:00 37432 00:31:00 12:30:00 3744SELECT p, t, FIRST_VALUE(t) OVER w FROM t_time2 WINDOW w AS (PARTITION by p ORDER BY t); 3745p t FIRST_VALUE(t) OVER w 37461 00:30:00 00:30:00 37471 00:31:00 00:30:00 37481 01:30:00 00:30:00 37491 04:30:00 00:30:00 37501 06:30:00 00:30:00 37511 12:30:00 00:30:00 37521 13:30:00 00:30:00 37531 15:30:00 00:30:00 37541 18:30:00 00:30:00 37551 20:30:00 00:30:00 37561 21:30:00 00:30:00 37571 22:30:00 00:30:00 37582 00:30:00 00:30:00 37592 00:31:00 00:30:00 37602 01:30:00 00:30:00 37612 04:30:00 00:30:00 37622 06:30:00 00:30:00 37632 12:30:00 00:30:00 37642 13:30:00 00:30:00 37652 15:30:00 00:30:00 37662 18:30:00 00:30:00 37672 20:30:00 00:30:00 37682 21:30:00 00:30:00 37692 22:30:00 00:30:00 3770SELECT p, t, FIRST_VALUE(t) OVER w FROM t_time2 WINDOW w AS (PARTITION by p ORDER BY t DESC); 3771p t FIRST_VALUE(t) OVER w 37721 22:30:00 22:30:00 37731 21:30:00 22:30:00 37741 20:30:00 22:30:00 37751 18:30:00 22:30:00 37761 15:30:00 22:30:00 37771 13:30:00 22:30:00 37781 12:30:00 22:30:00 37791 06:30:00 22:30:00 37801 04:30:00 22:30:00 37811 01:30:00 22:30:00 37821 00:31:00 22:30:00 37831 00:30:00 22:30:00 37842 22:30:00 22:30:00 37852 21:30:00 22:30:00 37862 20:30:00 22:30:00 37872 18:30:00 22:30:00 37882 15:30:00 22:30:00 37892 13:30:00 22:30:00 37902 12:30:00 22:30:00 37912 06:30:00 22:30:00 37922 04:30:00 22:30:00 37932 01:30:00 22:30:00 37942 00:31:00 22:30:00 37952 00:30:00 22:30:00 3796SELECT p, t, FIRST_VALUE(t) OVER w FROM t_time2 WINDOW w AS (PARTITION by p ORDER BY t ROWS 2 PRECEDING); 3797p t FIRST_VALUE(t) OVER w 37981 00:30:00 00:30:00 37991 00:31:00 00:30:00 38001 01:30:00 00:30:00 38011 04:30:00 00:31:00 38021 06:30:00 01:30:00 38031 12:30:00 04:30:00 38041 13:30:00 06:30:00 38051 15:30:00 12:30:00 38061 18:30:00 13:30:00 38071 20:30:00 15:30:00 38081 21:30:00 18:30:00 38091 22:30:00 20:30:00 38102 00:30:00 00:30:00 38112 00:31:00 00:30:00 38122 01:30:00 00:30:00 38132 04:30:00 00:31:00 38142 06:30:00 01:30:00 38152 12:30:00 04:30:00 38162 13:30:00 06:30:00 38172 15:30:00 12:30:00 38182 18:30:00 13:30:00 38192 20:30:00 15:30:00 38202 21:30:00 18:30:00 38212 22:30:00 20:30:00 3822SELECT p, t, FIRST_VALUE(t) OVER w FROM t_time2 WINDOW w AS (PARTITION by p ORDER BY t RANGE INTERVAL 2 HOUR PRECEDING); 3823p t FIRST_VALUE(t) OVER w 38241 00:30:00 00:30:00 38251 00:31:00 00:30:00 38261 01:30:00 00:30:00 38271 04:30:00 04:30:00 38281 06:30:00 04:30:00 38291 12:30:00 12:30:00 38301 13:30:00 12:30:00 38311 15:30:00 13:30:00 38321 18:30:00 18:30:00 38331 20:30:00 18:30:00 38341 21:30:00 20:30:00 38351 22:30:00 20:30:00 38362 00:30:00 00:30:00 38372 00:31:00 00:30:00 38382 01:30:00 00:30:00 38392 04:30:00 04:30:00 38402 06:30:00 04:30:00 38412 12:30:00 12:30:00 38422 13:30:00 12:30:00 38432 15:30:00 13:30:00 38442 18:30:00 18:30:00 38452 20:30:00 18:30:00 38462 21:30:00 20:30:00 38472 22:30:00 20:30:00 3848SELECT p, t, FIRST_VALUE(t) OVER w FROM t_time2 WINDOW w AS (PARTITION by p ORDER BY t ROWS BETWEEN 2 PRECEDING AND 1 PRECEDING); 3849p t FIRST_VALUE(t) OVER w 38501 00:30:00 NULL 38511 00:31:00 00:30:00 38521 01:30:00 00:30:00 38531 04:30:00 00:31:00 38541 06:30:00 01:30:00 38551 12:30:00 04:30:00 38561 13:30:00 06:30:00 38571 15:30:00 12:30:00 38581 18:30:00 13:30:00 38591 20:30:00 15:30:00 38601 21:30:00 18:30:00 38611 22:30:00 20:30:00 38622 00:30:00 NULL 38632 00:31:00 00:30:00 38642 01:30:00 00:30:00 38652 04:30:00 00:31:00 38662 06:30:00 01:30:00 38672 12:30:00 04:30:00 38682 13:30:00 06:30:00 38692 15:30:00 12:30:00 38702 18:30:00 13:30:00 38712 20:30:00 15:30:00 38722 21:30:00 18:30:00 38732 22:30:00 20:30:00 3874SELECT p, t, FIRST_VALUE(t) OVER w FROM t_time2 WINDOW w AS (PARTITION by p ORDER BY t RANGE BETWEEN INTERVAL 2 HOUR PRECEDING AND INTERVAL 1 HOUR PRECEDING); 3875p t FIRST_VALUE(t) OVER w 38761 00:30:00 NULL 38771 00:31:00 NULL 38781 01:30:00 00:30:00 38791 04:30:00 NULL 38801 06:30:00 04:30:00 38811 12:30:00 NULL 38821 13:30:00 12:30:00 38831 15:30:00 13:30:00 38841 18:30:00 NULL 38851 20:30:00 18:30:00 38861 21:30:00 20:30:00 38871 22:30:00 20:30:00 38882 00:30:00 NULL 38892 00:31:00 NULL 38902 01:30:00 00:30:00 38912 04:30:00 NULL 38922 06:30:00 04:30:00 38932 12:30:00 NULL 38942 13:30:00 12:30:00 38952 15:30:00 13:30:00 38962 18:30:00 NULL 38972 20:30:00 18:30:00 38982 21:30:00 20:30:00 38992 22:30:00 20:30:00 3900SELECT p, t, FIRST_VALUE(t) OVER w FROM t_time2 WINDOW w AS (PARTITION by p ORDER BY t ROWS BETWEEN CURRENT ROW AND UNBOUNDED FOLLOWING); 3901p t FIRST_VALUE(t) OVER w 39021 00:30:00 00:30:00 39031 00:31:00 00:31:00 39041 01:30:00 01:30:00 39051 04:30:00 04:30:00 39061 06:30:00 06:30:00 39071 12:30:00 12:30:00 39081 13:30:00 13:30:00 39091 15:30:00 15:30:00 39101 18:30:00 18:30:00 39111 20:30:00 20:30:00 39121 21:30:00 21:30:00 39131 22:30:00 22:30:00 39142 00:30:00 00:30:00 39152 00:31:00 00:31:00 39162 01:30:00 01:30:00 39172 04:30:00 04:30:00 39182 06:30:00 06:30:00 39192 12:30:00 12:30:00 39202 13:30:00 13:30:00 39212 15:30:00 15:30:00 39222 18:30:00 18:30:00 39232 20:30:00 20:30:00 39242 21:30:00 21:30:00 39252 22:30:00 22:30:00 3926SELECT p, t, FIRST_VALUE(t) OVER w FROM t_time2 WINDOW w AS (PARTITION by p ORDER BY t RANGE BETWEEN CURRENT ROW AND UNBOUNDED FOLLOWING); 3927p t FIRST_VALUE(t) OVER w 39281 00:30:00 00:30:00 39291 00:31:00 00:31:00 39301 01:30:00 01:30:00 39311 04:30:00 04:30:00 39321 06:30:00 06:30:00 39331 12:30:00 12:30:00 39341 13:30:00 13:30:00 39351 15:30:00 15:30:00 39361 18:30:00 18:30:00 39371 20:30:00 20:30:00 39381 21:30:00 21:30:00 39391 22:30:00 22:30:00 39402 00:30:00 00:30:00 39412 00:31:00 00:31:00 39422 01:30:00 01:30:00 39432 04:30:00 04:30:00 39442 06:30:00 06:30:00 39452 12:30:00 12:30:00 39462 13:30:00 13:30:00 39472 15:30:00 15:30:00 39482 18:30:00 18:30:00 39492 20:30:00 20:30:00 39502 21:30:00 21:30:00 39512 22:30:00 22:30:00 3952SELECT p, t, FIRST_VALUE(t) OVER w FROM t_time2 WINDOW w AS (PARTITION by p ORDER BY t ROWS BETWEEN 2 FOLLOWING AND 3 FOLLOWING); 3953p t FIRST_VALUE(t) OVER w 39541 00:30:00 01:30:00 39551 00:31:00 04:30:00 39561 01:30:00 06:30:00 39571 04:30:00 12:30:00 39581 06:30:00 13:30:00 39591 12:30:00 15:30:00 39601 13:30:00 18:30:00 39611 15:30:00 20:30:00 39621 18:30:00 21:30:00 39631 20:30:00 22:30:00 39641 21:30:00 NULL 39651 22:30:00 NULL 39662 00:30:00 01:30:00 39672 00:31:00 04:30:00 39682 01:30:00 06:30:00 39692 04:30:00 12:30:00 39702 06:30:00 13:30:00 39712 12:30:00 15:30:00 39722 13:30:00 18:30:00 39732 15:30:00 20:30:00 39742 18:30:00 21:30:00 39752 20:30:00 22:30:00 39762 21:30:00 NULL 39772 22:30:00 NULL 3978SELECT p, t, FIRST_VALUE(t) OVER w FROM t_time2 WINDOW w AS (PARTITION by p ORDER BY t RANGE BETWEEN INTERVAL 2 HOUR FOLLOWING AND INTERVAL 3 HOUR FOLLOWING); 3979p t FIRST_VALUE(t) OVER w 39801 00:30:00 NULL 39811 00:31:00 NULL 39821 01:30:00 04:30:00 39831 04:30:00 06:30:00 39841 06:30:00 NULL 39851 12:30:00 15:30:00 39861 13:30:00 15:30:00 39871 15:30:00 18:30:00 39881 18:30:00 20:30:00 39891 20:30:00 22:30:00 39901 21:30:00 NULL 39911 22:30:00 NULL 39922 00:30:00 NULL 39932 00:31:00 NULL 39942 01:30:00 04:30:00 39952 04:30:00 06:30:00 39962 06:30:00 NULL 39972 12:30:00 15:30:00 39982 13:30:00 15:30:00 39992 15:30:00 18:30:00 40002 18:30:00 20:30:00 40012 20:30:00 22:30:00 40022 21:30:00 NULL 40032 22:30:00 NULL 4004DROP TABLE t_time, t_time2; 4005---------------------------------------------------------------------- 4006- Aggregates with RANGE frame specification 4007---------------------------------------------------------------------- 4008SELECT * FROM t1; 4009id sex 40101 M 40112 F 40123 F 40134 F 40145 M 401510 NULL 401611 NULL 401710 NULL 4018NULL M 4019Make t11 a clone of t1 but with an extra partitioning column, but other values 4020repeated, so we can test it the same frames work on more than one partition 4021CREATE TABLE t11 (id INTEGER, sex CHAR(1), p INTEGER DEFAULT 1); 4022INSERT INTO t11(id, sex) SELECT * FROM t1; 4023UPDATE t11 SET p=p+1; 4024INSERT INTO t11(id, sex) SELECT * FROM t1; 4025Make t22 a clone of t2 but with an extra partitioning column, but other values 4026repeated, so we can test it the same frames work on more than one partition 4027CREATE TABLE t22 (user_id INTEGER NOT NULL, date DATE, p INTEGER DEFAULT 1); 4028INSERT INTO t22(user_id, date) SELECT * FROM t2; 4029UPDATE t22 SET p=p+1; 4030INSERT INTO t22(user_id, date) SELECT * FROM t2; 4031SELECT id, SUM(id) OVER (ORDER BY id RANGE 2 PRECEDING) FROM t1 ORDER BY id; 4032id SUM(id) OVER (ORDER BY id RANGE 2 PRECEDING) 4033NULL NULL 40341 1 40352 3 40363 6 40374 9 40385 12 403910 20 404010 20 404111 31 4042SELECT id, SUM(id) OVER (ORDER BY id RANGE BETWEEN 1 PRECEDING AND 1 FOLLOWING) FROM t1 ORDER BY id; 4043id SUM(id) OVER (ORDER BY id RANGE BETWEEN 1 PRECEDING AND 1 FOLLOWING) 4044NULL NULL 40451 3 40462 6 40473 9 40484 12 40495 9 405010 31 405110 31 405211 31 4053SELECT id, SUM(id) OVER (ORDER BY id RANGE UNBOUNDED PRECEDING) FROM t1 ORDER BY id; 4054id SUM(id) OVER (ORDER BY id RANGE UNBOUNDED PRECEDING) 4055NULL NULL 40561 1 40572 3 40583 6 40594 10 40605 15 406110 35 406210 35 406311 46 4064SELECT p, id, SUM(id) OVER (PARTITION BY p ORDER BY id RANGE 2 PRECEDING) FROM t11 ORDER BY p,id; 4065p id SUM(id) OVER (PARTITION BY p ORDER BY id RANGE 2 PRECEDING) 40661 NULL NULL 40671 1 1 40681 2 3 40691 3 6 40701 4 9 40711 5 12 40721 10 20 40731 10 20 40741 11 31 40752 NULL NULL 40762 1 1 40772 2 3 40782 3 6 40792 4 9 40802 5 12 40812 10 20 40822 10 20 40832 11 31 4084SELECT p, id, SUM(id) OVER (PARTITION BY p ORDER BY id RANGE BETWEEN 1 PRECEDING AND 1 FOLLOWING) FROM t11 ORDER BY p,id; 4085p id SUM(id) OVER (PARTITION BY p ORDER BY id RANGE BETWEEN 1 PRECEDING AND 1 FOLLOWING) 40861 NULL NULL 40871 1 3 40881 2 6 40891 3 9 40901 4 12 40911 5 9 40921 10 31 40931 10 31 40941 11 31 40952 NULL NULL 40962 1 3 40972 2 6 40982 3 9 40992 4 12 41002 5 9 41012 10 31 41022 10 31 41032 11 31 4104SELECT p, id, SUM(id) OVER (PARTITION BY p ORDER BY id RANGE UNBOUNDED PRECEDING) FROM t11 ORDER BY p,id; 4105p id SUM(id) OVER (PARTITION BY p ORDER BY id RANGE UNBOUNDED PRECEDING) 41061 NULL NULL 41071 1 1 41081 2 3 41091 3 6 41101 4 10 41111 5 15 41121 10 35 41131 10 35 41141 11 46 41152 NULL NULL 41162 1 1 41172 2 3 41182 3 6 41192 4 10 41202 5 15 41212 10 35 41222 10 35 41232 11 46 4124Implicit frame due to ORDER BY, with last in peer group as upper bound 4125SELECT user_id, SUM(user_id) OVER w, AVG(user_id) OVER w FROM t2 WINDOW w AS (ORDER BY user_id); 4126user_id SUM(user_id) OVER w AVG(user_id) OVER w 41271 2 1.0000 41281 2 1.0000 41292 4 1.3333 41303 7 1.7500 41314 15 2.5000 41324 15 2.5000 41335 20 2.8571 4134SELECT p, user_id, SUM(user_id) OVER w, AVG(user_id) OVER w FROM t22 WINDOW w AS (PARTITION BY p ORDER BY user_id) ORDER BY p; 4135p user_id SUM(user_id) OVER w AVG(user_id) OVER w 41361 1 2 1.0000 41371 1 2 1.0000 41381 2 4 1.3333 41391 3 7 1.7500 41401 4 15 2.5000 41411 4 15 2.5000 41421 5 20 2.8571 41432 1 2 1.0000 41442 1 2 1.0000 41452 2 4 1.3333 41462 3 7 1.7500 41472 4 15 2.5000 41482 4 15 2.5000 41492 5 20 2.8571 4150Window function use of same field in different windows, both of which 4151need buffering. In this case we need subsequent rewrites of arg fields 4152Field pointer in tmp files for window 2..n The intervening internal 4153window buffering in each step used to mess that up. 4154SELECT user_id, SUM(user_id) OVER w, AVG(user_id) OVER w1 FROM t2 4155WINDOW w AS (ORDER BY user_id), w1 AS (ORDER BY user_id); 4156user_id SUM(user_id) OVER w AVG(user_id) OVER w1 41571 2 1.0000 41581 2 1.0000 41592 4 1.3333 41603 7 1.7500 41614 15 2.5000 41624 15 2.5000 41635 20 2.8571 4164Check descending order by with RANGE: 2 PRECEDING in this case means larger than 4165current row. 4166SELECT NTILE(5) OVER w, ROW_NUMBER() OVER w, id, SUM(id) OVER w FROM t1 4167WINDOW w AS (ORDER BY id DESC RANGE 2 PRECEDING); 4168NTILE(5) OVER w ROW_NUMBER() OVER w id SUM(id) OVER w 41691 1 11 11 41701 2 10 31 41712 3 10 31 41722 4 5 5 41733 5 4 9 41743 6 3 12 41754 7 2 9 41764 8 1 6 41775 9 NULL NULL 4178SELECT p, NTILE(5) OVER w, ROW_NUMBER() OVER w, id, SUM(id) OVER w FROM t11 4179WINDOW w AS (PARTITION BY p ORDER BY id DESC RANGE 2 PRECEDING); 4180p NTILE(5) OVER w ROW_NUMBER() OVER w id SUM(id) OVER w 41811 1 1 11 11 41821 1 2 10 31 41831 2 3 10 31 41841 2 4 5 5 41851 3 5 4 9 41861 3 6 3 12 41871 4 7 2 9 41881 4 8 1 6 41891 5 9 NULL NULL 41902 1 1 11 11 41912 1 2 10 31 41922 2 3 10 31 41932 2 4 5 5 41942 3 5 4 9 41952 3 6 3 12 41962 4 7 2 9 41972 4 8 1 6 41982 5 9 NULL NULL 4199SELECT NTILE(5) OVER w, ROW_NUMBER() OVER w, id, SUM(id) OVER w FROM t1 4200WINDOW w AS (ORDER BY id DESC RANGE INTERVAL 2 MONTH PRECEDING); 4201ERROR HY000: Window 'w' with RANGE frame has ORDER BY expression of numeric type, INTERVAL bound value not allowed. 4202update t2 set date=date + user_id; 4203SELECT user_id, date, COUNT(*) OVER (ORDER BY date RANGE INTERVAL 1 DAY PRECEDING) FROM t2; 4204user_id date COUNT(*) OVER (ORDER BY date RANGE INTERVAL 1 DAY PRECEDING) 42051 2002-06-10 2 42061 2002-06-10 2 42072 2002-06-11 3 42083 2002-06-12 2 42094 2002-06-13 3 42104 2002-06-13 3 42115 2002-06-14 3 4212SELECT user_id, date, COUNT(*) OVER (ORDER BY date RANGE 1 PRECEDING) FROM t2; 4213ERROR HY000: Window '<unnamed window>' with RANGE frame has ORDER BY expression of datetime type. Only INTERVAL bound value allowed. 4214CREATE TABLE t3(d DOUBLE); 4215INSERT INTO t3 VALUES (1.1); 4216INSERT INTO t3 VALUES (1.9); 4217INSERT INTO t3 VALUES (4.0); 4218INSERT INTO t3 VALUES (8.3); 4219INSERT INTO t3 VALUES (16.0); 4220INSERT INTO t3 VALUES (24.0); 4221INSERT INTO t3 VALUES (20.1); 4222INSERT INTO t3 VALUES (22.0); 4223INSERT INTO t3 VALUES (23.0); 4224SELECT d, SUM(d) OVER w, COUNT(*) OVER w FROM t3 WINDOW w AS (ORDER BY d RANGE BETWEEN 2.1 PRECEDING AND 1.1 FOLLOWING); 4225d SUM(d) OVER w COUNT(*) OVER w 42261.1 3 2 42271.9 3 2 42284 5.9 2 42298.3 8.3 1 423016 16 1 423120.1 20.1 1 423222 65.1 3 423323 69 3 423424 69 3 4235Illegal range ORDER BY type, cf. SQL 2011 7.11 <window clause>, SR 11.a.ii 4236CREATE TABLE t4(c VARCHAR(30), i INT, j INT); 4237SELECT COUNT(*) OVER (ORDER BY c RANGE 3 PRECEDING) FROM t4; 4238ERROR HY000: Window '<unnamed window>' with RANGE N PRECEDING/FOLLOWING frame requires exactly one ORDER BY expression, of numeric or temporal type 4239SELECT COUNT(*) OVER (ORDER BY i,j RANGE 3 PRECEDING) FROM t4; 4240ERROR HY000: Window '<unnamed window>' with RANGE N PRECEDING/FOLLOWING frame requires exactly one ORDER BY expression, of numeric or temporal type 4241---------------------------------------------------------------------- 4242- wf over JSON 4243---------------------------------------------------------------------- 4244CREATE TABLE tj(j JSON, i INT DEFAULT 7); 4245INSERT INTO tj(j) VALUES ('1'); 4246INSERT INTO tj(j) VALUES ('2'); 4247INSERT INTO tj(j) VALUES ('3'); 4248INSERT INTO tj(j) VALUES ('4'); 4249INSERT INTO tj(j) VALUES ('5'); 4250INSERT INTO tj(j) VALUES (NULL); 4251INSERT INTO tj(j) VALUES ('3.14'); 4252INSERT INTO tj(j) VALUES ('[1,2,3]'); 4253SELECT CAST(SUM(j) OVER () AS JSON) FROM tj; 4254CAST(SUM(j) OVER () AS JSON) 425518.14 425618.14 425718.14 425818.14 425918.14 426018.14 426118.14 426218.14 4263Warnings: 4264Warning 3156 Invalid JSON value for CAST to DOUBLE from column j at row 1 4265---------------------------------------------------------------------- 4266- SELECT DISTINCT 4267---------------------------------------------------------------------- 4268One window 4269SELECT DISTINCT i,COUNT(*) OVER () FROM tj; 4270i COUNT(*) OVER () 42717 8 4272Several windows with final ORDER BY also 4273SELECT DISTINCT i,NTILE(3) OVER (ORDER BY i), SUM(i) OVER (), COUNT(*) OVER () FROM tj ORDER BY NTILE(3) OVER (ORDER BY i); 4274i NTILE(3) OVER (ORDER BY i) SUM(i) OVER () COUNT(*) OVER () 42757 1 56 8 42767 2 56 8 42777 3 56 8 4278UPDATE tj SET i=i+CASE WHEN JSON_TYPE(j) = 'ARRAY' THEN 1 ELSE j END; 4279UPDATE tj SET i=7 where i=8 AND JSON_TYPE(j) != 'ARRAY'; 4280CREATE TABLE tj2 AS SELECT * FROM tj; 4281UPDATE tj2 SET i=MOD(i,3); 4282SELECT * FROM tj2; 4283j i 42841 1 42852 0 42863 1 42874 2 42885 0 4289NULL NULL 42903.14 1 4291[1, 2, 3] 2 4292With GROUP BY 4293SELECT COUNT(*) OVER (), MOD(SUM(i),2) FROM tj2 GROUP BY i; 4294COUNT(*) OVER () MOD(SUM(i),2) 42954 1 42964 0 42974 0 42984 NULL 4299SELECT DISTINCT COUNT(*) OVER (), MOD(SUM(i),2) FROM tj2 GROUP BY i; 4300COUNT(*) OVER () MOD(SUM(i),2) 43014 1 43024 0 43034 NULL 4304Bug fix GROUP BY with window function referring column used in grouping expression 4305SELECT i, SUM(i) OVER (), MOD(SUM(i),2) FROM tj2 GROUP BY i; 4306i SUM(i) OVER () MOD(SUM(i),2) 43071 3 1 43080 3 0 43092 3 0 4310NULL 3 NULL 4311SELECT i, SUM(SUM(i)) OVER (), SUM(i) OVER (ORDER BY i), MOD(SUM(i),2), SUM(i) FROM tj2 GROUP BY i; 4312i SUM(SUM(i)) OVER () SUM(i) OVER (ORDER BY i) MOD(SUM(i),2) SUM(i) 4313NULL 7 NULL NULL NULL 43140 7 0 0 0 43151 7 1 1 3 43162 7 3 0 4 4317DROP TABLE tj2; 4318---------------------------------------------------------------------- 4319- Bug fixes 4320---------------------------------------------------------------------- 4321Bug fix for FIRST_VALUE, LAST_VALUE when not buffered processing 4322SELECT LAST_VALUE(j) OVER w, FIRST_VALUE(j) OVER w FROM tj WINDOW w AS (PARTITION BY i ORDER BY j ROWS UNBOUNDED PRECEDING); 4323LAST_VALUE(j) OVER w FIRST_VALUE(j) OVER w 4324NULL NULL 43251 1 4326[1, 2, 3] [1, 2, 3] 43272 2 43283 3 43293.14 3 43304 4 43315 5 4332Warnings: 4333Warning 1235 This version of MySQL doesn't yet support 'sorting of non-scalar JSON values' 4334Bug missing hidden column (j) induction to select list: FIRST_VALUE/LAST_VALUE 4335SELECT i, LAST_VALUE((CAST(j AS UNSIGNED))) OVER w, FIRST_VALUE(CAST(j AS UNSIGNED)) OVER w FROM tj 4336WINDOW w AS (PARTITION BY i ORDER BY CAST(j AS UNSIGNED) RANGE UNBOUNDED PRECEDING); 4337i LAST_VALUE((CAST(j AS UNSIGNED))) OVER w FIRST_VALUE(CAST(j AS UNSIGNED)) OVER w 4338NULL NULL NULL 43397 1 1 43408 0 0 43419 2 2 434210 3 3 434310 3 3 434411 4 4 434512 5 5 4346Warnings: 4347Warning 3156 Invalid JSON value for CAST to INTEGER from column j at row 1 4348Warning 3156 Invalid JSON value for CAST to INTEGER from column j at row 2 4349Warning 3156 Invalid JSON value for CAST to INTEGER from column j at row 2 4350Warning 3156 Invalid JSON value for CAST to INTEGER from column j at row 2 4351Fix for lineno in warnings buffered and unbuffered windows 4352SELECT j,CAST(SUM(j) OVER (PARTITION BY i) AS JSON), CAST(SUM(j) OVER () AS JSON) FROM tj; 4353j CAST(SUM(j) OVER (PARTITION BY i) AS JSON) CAST(SUM(j) OVER () AS JSON) 4354NULL NULL 18.14 43551 1.0 18.14 4356[1, 2, 3] 0.0 18.14 43572 2.0 18.14 43583 6.140000000000001 18.14 43593.14 6.140000000000001 18.14 43604 4.0 18.14 43615 5.0 18.14 4362Warnings: 4363Warning 3156 Invalid JSON value for CAST to DOUBLE from column j at row 1 4364Warning 3156 Invalid JSON value for CAST to DOUBLE from column j at row 1 4365SELECT j,CAST(SUM(j) OVER (PARTITION BY i ROWS UNBOUNDED PRECEDING) AS JSON), CAST(SUM(j) OVER (PARTITION BY i ROWS UNBOUNDED PRECEDING) AS JSON) FROM tj; 4366j CAST(SUM(j) OVER (PARTITION BY i ROWS UNBOUNDED PRECEDING) AS JSON) CAST(SUM(j) OVER (PARTITION BY i ROWS UNBOUNDED PRECEDING) AS JSON) 4367NULL NULL NULL 43681 1.0 1.0 4369[1, 2, 3] 0.0 0.0 43702 2.0 2.0 43713 3.0 3.0 43723.14 6.140000000000001 6.140000000000001 43734 4.0 4.0 43745 5.0 5.0 4375Warnings: 4376Warning 3156 Invalid JSON value for CAST to DOUBLE from column j at row 1 4377Warning 3156 Invalid JSON value for CAST to DOUBLE from column j at row 3 4378Bug fix for UNION 4379SELECT i, ROW_NUMBER() OVER () FROM tj UNION ALL SELECT i, ROW_NUMBER() OVER () FROM tj; 4380i ROW_NUMBER() OVER () 43817 1 43829 2 438310 3 438411 4 438512 5 4386NULL 6 438710 7 43888 8 43897 1 43909 2 439110 3 439211 4 439312 5 4394NULL 6 439510 7 43968 8 4397SELECT * FROM (SELECT i, j, ROW_NUMBER() OVER (ORDER BY j) FROM tj UNION SELECT i, j, ROW_NUMBER() OVER (ORDER BY j) FROM tj) alias; 4398i j ROW_NUMBER() OVER (ORDER BY j) 4399NULL NULL 1 44007 1 2 44019 2 3 440210 3 4 440310 3.14 5 440411 4 6 440512 5 7 44068 [1, 2, 3] 8 4407Warnings: 4408Warning 1235 This version of MySQL doesn't yet support 'sorting of non-scalar JSON values' 4409Warning 1235 This version of MySQL doesn't yet support 'sorting of non-scalar JSON values' 4410Bug fixes after field_type refactorings on trunk triggered an error in 4411the above statements. The WFs didn't all have m_data_type set up. 4412SELECT i, RANK() OVER (ORDER BY i) FROM tj UNION ALL SELECT i, RANK() OVER (ORDER BY i) FROM tj; 4413i RANK() OVER (ORDER BY i) 4414NULL 1 44157 2 44168 3 44179 4 441810 5 441910 5 442011 7 442112 8 4422NULL 1 44237 2 44248 3 44259 4 442610 5 442710 5 442811 7 442912 8 4430SELECT i, DENSE_RANK() OVER (ORDER BY i) FROM tj UNION ALL SELECT i, DENSE_RANK() OVER (ORDER BY i) FROM tj; 4431i DENSE_RANK() OVER (ORDER BY i) 4432NULL 1 44337 2 44348 3 44359 4 443610 5 443710 5 443811 6 443912 7 4440NULL 1 44417 2 44428 3 44439 4 444410 5 444510 5 444611 6 444712 7 4448SELECT i, CUME_DIST() OVER (ORDER BY i) FROM tj UNION ALL SELECT i, CUME_DIST() OVER (ORDER BY i) FROM tj; 4449i CUME_DIST() OVER (ORDER BY i) 4450NULL 0.125 44517 0.25 44528 0.375 44539 0.5 445410 0.75 445510 0.75 445611 0.875 445712 1 4458NULL 0.125 44597 0.25 44608 0.375 44619 0.5 446210 0.75 446310 0.75 446411 0.875 446512 1 4466SELECT i, PERCENT_RANK() OVER (ORDER BY i) FROM tj UNION ALL SELECT i, PERCENT_RANK() OVER (ORDER BY i) FROM tj; 4467i PERCENT_RANK() OVER (ORDER BY i) 4468NULL 0 44697 0.14285714285714285 44708 0.2857142857142857 44719 0.42857142857142855 447210 0.5714285714285714 447310 0.5714285714285714 447411 0.8571428571428571 447512 1 4476NULL 0 44777 0.14285714285714285 44788 0.2857142857142857 44799 0.42857142857142855 448010 0.5714285714285714 448110 0.5714285714285714 448211 0.8571428571428571 448312 1 4484SELECT i, NTILE(3) OVER (ORDER BY i) FROM tj UNION ALL SELECT i, NTILE(3) OVER (ORDER BY i) FROM tj; 4485i NTILE(3) OVER (ORDER BY i) 4486NULL 1 44877 1 44888 1 44899 2 449010 2 449110 2 449211 3 449312 3 4494NULL 1 44957 1 44968 1 44979 2 449810 2 449910 2 450011 3 450112 3 4502SELECT i, SUM(i) OVER (ORDER BY i) FROM tj UNION ALL SELECT i, SUM(i) OVER (ORDER BY i) FROM tj; 4503i SUM(i) OVER (ORDER BY i) 4504NULL NULL 45057 7 45068 15 45079 24 450810 44 450910 44 451011 55 451112 67 4512NULL NULL 45137 7 45148 15 45159 24 451610 44 451710 44 451811 55 451912 67 4520SELECT i, NTH_VALUE(i,3) OVER (ORDER BY i) FROM tj UNION ALL SELECT i, NTH_VALUE(i, 3) OVER (ORDER BY i) FROM tj; 4521i NTH_VALUE(i,3) OVER (ORDER BY i) 4522NULL NULL 45237 NULL 45248 8 45259 8 452610 8 452710 8 452811 8 452912 8 4530NULL NULL 45317 NULL 45328 8 45339 8 453410 8 453510 8 453611 8 453712 8 4538SELECT i, NTH_VALUE(i + 3,3) OVER (ORDER BY i) FROM tj UNION ALL SELECT i, NTH_VALUE(i + 3, 3) OVER (ORDER BY i) FROM tj; 4539i NTH_VALUE(i + 3,3) OVER (ORDER BY i) 4540NULL NULL 45417 NULL 45428 11 45439 11 454410 11 454510 11 454611 11 454712 11 4548NULL NULL 45497 NULL 45508 11 45519 11 455210 11 455310 11 455411 11 455512 11 4556SELECT i, LEAD(i,3) OVER (ORDER BY i) FROM tj UNION ALL SELECT i, LEAD(i, 3) OVER (ORDER BY i) FROM tj; 4557i LEAD(i,3) OVER (ORDER BY i) 4558NULL 9 45597 10 45608 10 45619 11 456210 12 456310 NULL 456411 NULL 456512 NULL 4566NULL 9 45677 10 45688 10 45699 11 457010 12 457110 NULL 457211 NULL 457312 NULL 4574SELECT i, FIRST_VALUE(i) OVER (ORDER BY i) FROM tj UNION ALL SELECT i, FIRST_VALUE(i) OVER (ORDER BY i) FROM tj; 4575i FIRST_VALUE(i) OVER (ORDER BY i) 4576NULL NULL 45777 NULL 45788 NULL 45799 NULL 458010 NULL 458110 NULL 458211 NULL 458312 NULL 4584NULL NULL 45857 NULL 45868 NULL 45879 NULL 458810 NULL 458910 NULL 459011 NULL 459112 NULL 4592SELECT i, LAST_VALUE(i) OVER (ORDER BY i) FROM tj UNION ALL SELECT i, LAST_VALUE(i) OVER (ORDER BY i) FROM tj; 4593i LAST_VALUE(i) OVER (ORDER BY i) 4594NULL NULL 45957 7 45968 8 45979 9 459810 10 459910 10 460011 11 460112 12 4602NULL NULL 46037 7 46048 8 46059 9 460610 10 460710 10 460811 11 460912 12 4610DROP TABLE tj; 4611---------------------------------------------------------------------- 4612- More JSON 4613---------------------------------------------------------------------- 4614CREATE TABLE tj(j JSON); 4615INSERT INTO tj VALUES ('1'); 4616INSERT INTO tj VALUES ('2'); 4617INSERT INTO tj VALUES ('3'); 4618INSERT INTO tj VALUES ('4'); 4619INSERT INTO tj VALUES ('5'); 4620INSERT INTO tj VALUES (NULL); 4621SELECT j, JSON_TYPE(j), SUM(j) OVER (ORDER BY j ROWS 3 PRECEDING) FROM tj; 4622j JSON_TYPE(j) SUM(j) OVER (ORDER BY j ROWS 3 PRECEDING) 4623NULL NULL NULL 46241 INTEGER 1 46252 INTEGER 3 46263 INTEGER 6 46274 INTEGER 10 46285 INTEGER 14 4629SELECT j, JSON_TYPE(j), SUM(j) OVER (ORDER BY j RANGE 3 PRECEDING) FROM tj; 4630ERROR HY000: Window '<unnamed window>' with RANGE N PRECEDING/FOLLOWING frame requires exactly one ORDER BY expression, of numeric or temporal type 4631INSERT INTO tj VALUES ('3.14'); 4632SELECT j, JSON_TYPE(j), SUM(j) OVER (ORDER BY j ROWS 3 PRECEDING) FROM tj; 4633j JSON_TYPE(j) SUM(j) OVER (ORDER BY j ROWS 3 PRECEDING) 4634NULL NULL NULL 46351 INTEGER 1 46362 INTEGER 3 46373 INTEGER 6 46383.14 DOUBLE 9.14 46394 INTEGER 12.14 46405 INTEGER 15.14 4641INSERT INTO tj VALUES ('[1,2,3]'); 4642SELECT j, 4643JSON_TYPE(j), 4644SUM(CASE WHEN JSON_TYPE(j) = 'ARRAY' THEN j->"$[0]" ELSE j END) 4645OVER (ORDER BY j ROWS 3 PRECEDING) 4646FROM tj; 4647j JSON_TYPE(j) SUM(CASE WHEN JSON_TYPE(j) = 'ARRAY' THEN j->"$[0]" ELSE j END) 4648OVER (ORDER BY j ROWS 3 PRECEDING) 4649NULL NULL NULL 46501 INTEGER 1 46512 INTEGER 3 46523 INTEGER 6 46533.14 DOUBLE 9.14 46544 INTEGER 12.14 46555 INTEGER 15.14 4656[1, 2, 3] ARRAY 13.14 4657Warnings: 4658Warning 1235 This version of MySQL doesn't yet support 'sorting of non-scalar JSON values' 4659CREATE TABLE t5(b BIGINT UNSIGNED); 4660INSERT INTO t5 VALUES (1); 4661INSERT INTO t5 VALUES (2); 4662INSERT INTO t5 VALUES (3); 4663INSERT INTO t5 VALUES (4); 4664INSERT INTO t5 VALUES (5); 4665INSERT INTO t5 VALUES (6); 4666INSERT INTO t5 VALUES (7); 4667last row should have COUNT(*) == 0 , not 1 (bug fix) 4668SELECT b, COUNT(*) OVER (ORDER BY b RANGE BETWEEN 1 FOLLOWING AND 100 FOLLOWING) bb FROM t5; 4669b bb 46701 6 46712 5 46723 4 46734 3 46745 2 46756 1 46767 0 4677CREATE TABLE t6(t TIME, ts TIMESTAMP); 4678INSERT INTO t6 VALUES ('12:30', '2016-07-05 08:30:42'); 4679INSERT INTO t6 VALUES ('22:30', '2015-07-05 08:30:43'); 4680INSERT INTO t6 VALUES ('13:30', '2014-07-05 08:30:44'); 4681INSERT INTO t6 VALUES ('01:30', '2013-07-05 08:30:45'); 4682INSERT INTO t6 VALUES ('15:30', '2016-08-05 08:31:42'); 4683INSERT INTO t6 VALUES ('20:30', '2016-09-05 08:32:42'); 4684INSERT INTO t6 VALUES ('04:30', '2016-10-05 08:33:42'); 4685INSERT INTO t6 VALUES ('06:30', '2016-11-05 08:34:42'); 4686INSERT INTO t6 VALUES ('18:30', '2016-07-05 09:30:42'); 4687INSERT INTO t6 VALUES ('21:30', '2016-07-06 10:30:42'); 4688INSERT INTO t6 VALUES ('00:30', '2016-07-07 11:30:42'); 4689INSERT INTO t6 VALUES ('00:31', '2016-07-08 12:30:42'); 4690INTERVAL specified with string as below failed 4691SELECT t, COUNT(*) OVER (ORDER BY t RANGE 4692BETWEEN INTERVAL 1 HOUR PRECEDING AND INTERVAL '2:2' MINUTE_SECOND FOLLOWING) AS cnt FROM t6; 4693t cnt 469400:30:00 2 469500:31:00 2 469601:30:00 3 469704:30:00 1 469806:30:00 1 469912:30:00 1 470013:30:00 2 470115:30:00 1 470218:30:00 1 470320:30:00 1 470421:30:00 2 470522:30:00 2 4706---------------------------------------------------------------------- 4707- Window spec inheritance 4708---------------------------------------------------------------------- 4709SELECT COUNT(*) OVER w0, 4710COUNT(*) OVER w, 4711COUNT(*) OVER w1 FROM t6 4712WINDOW w0 AS (), 4713w AS (w0 ORDER BY t), 4714w1 AS (w RANGE BETWEEN INTERVAL 24 HOUR PRECEDING AND 4715INTERVAL '2:2' MINUTE_SECOND FOLLOWING); 4716COUNT(*) OVER w0 COUNT(*) OVER w COUNT(*) OVER w1 471712 1 2 471812 2 2 471912 3 3 472012 4 4 472112 5 5 472212 6 6 472312 7 7 472412 8 8 472512 9 9 472612 10 10 472712 11 11 472812 12 12 4729SELECT t, COUNT(t) OVER w1 FROM t6 4730WINDOW w1 AS (), 4731w2 AS (w1 PARTITION BY t); 4732ERROR HY000: A window which depends on another cannot define partitioning. 4733SELECT t, COUNT(t) OVER w1 FROM t6 4734WINDOW w1 AS (ORDER BY t), 4735w2 AS (w1 ORDER BY t); 4736ERROR HY000: Window 'w2' cannot inherit 'w1' since both contain an ORDER BY clause. 4737SELECT t, COUNT(t) OVER w1 FROM t6 4738WINDOW w1 AS (ORDER BY t RANGE BETWEEN CURRENT ROW AND 4739INTERVAL 1 DAY FOLLOWING), 4740w2 AS (w1); 4741ERROR HY000: Window 'w1' has a frame definition, so cannot be referenced by another window. 4742CREATE VIEW v AS 4743SELECT COUNT(*) OVER w0, 4744COUNT(*) OVER w, 4745COUNT(*) OVER w1 FROM t6 4746WINDOW w0 AS (), 4747w AS (w0 ORDER BY t), 4748w1 AS (w RANGE BETWEEN INTERVAL 24 HOUR PRECEDING AND 4749INTERVAL '2:2' MINUTE_SECOND FOLLOWING); 4750SHOW CREATE VIEW v; 4751View Create View character_set_client collation_connection 4752v CREATE ALGORITHM=UNDEFINED DEFINER=`root`@`localhost` SQL SECURITY DEFINER VIEW `v` AS select count(0) OVER `w0` AS `COUNT(*) OVER w0`,count(0) OVER `w` AS `COUNT(*) OVER w`,count(0) OVER `w1` AS `COUNT(*) OVER w1` from `t6` window `w0` AS () , `w` AS (`w0` ORDER BY `t6`.`t` ) , `w1` AS (`w` RANGE BETWEEN INTERVAL 24 hour PRECEDING AND INTERVAL '2:2' minute_second FOLLOWING) utf8 utf8_general_ci 4753SELECT * FROM v; 4754COUNT(*) OVER w0 COUNT(*) OVER w COUNT(*) OVER w1 475512 1 2 475612 2 2 475712 3 3 475812 4 4 475912 5 5 476012 6 6 476112 7 7 476212 8 8 476312 9 9 476412 10 10 476512 11 11 476612 12 12 4767DROP VIEW v; 4768---------------------------------------------------------------------- 4769- Bugs with induction of hidden fields from window function also used 4770- in ORDER BY/PARTITION BY 4771---------------------------------------------------------------------- 4772SELECT id, AVG(id) OVER (PARTITION BY id) summ FROM t1; 4773id summ 4774NULL NULL 47751 1.0000 47762 2.0000 47773 3.0000 47784 4.0000 47795 5.0000 478010 10.0000 478110 10.0000 478211 11.0000 4783SELECT AVG(id) OVER (PARTITION BY id) summ FROM t1; 4784summ 4785NULL 47861.0000 47872.0000 47883.0000 47894.0000 47905.0000 479110.0000 479210.0000 479311.0000 4794SELECT id, AVG(id) OVER (PARTITION BY id) summ, 4795AVG(id) OVER (PARTITION BY id) summ2 FROM t1; 4796id summ summ2 4797NULL NULL NULL 47981 1.0000 1.0000 47992 2.0000 2.0000 48003 3.0000 3.0000 48014 4.0000 4.0000 48025 5.0000 5.0000 480310 10.0000 10.0000 480410 10.0000 10.0000 480511 11.0000 11.0000 4806SELECT AVG(id) OVER (PARTITION BY id) summ, 4807AVG(id) OVER (PARTITION BY id) summ2 FROM t1; 4808summ summ2 4809NULL NULL 48101.0000 1.0000 48112.0000 2.0000 48123.0000 3.0000 48134.0000 4.0000 48145.0000 5.0000 481510.0000 10.0000 481610.0000 10.0000 481711.0000 11.0000 4818Bug for AVG in presence of several NULLs 4819INSERT INTO t1 VALUES (NULL, 'F'); 4820SELECT COUNT(id) OVER w, id, AVG(id) OVER w, SUM(id) OVER w, FIRST_VALUE(id) OVER w FROM t1 4821WINDOW w AS (ORDER BY id RANGE 1 PRECEDING); 4822COUNT(id) OVER w id AVG(id) OVER w SUM(id) OVER w FIRST_VALUE(id) OVER w 48230 NULL NULL NULL NULL 48240 NULL NULL NULL NULL 48251 1 1.0000 1 1 48262 2 1.5000 3 1 48272 3 2.5000 5 2 48282 4 3.5000 7 3 48292 5 4.5000 9 4 48302 10 10.0000 20 10 48312 10 10.0000 20 10 48323 11 10.3333 31 10 4833Repeat previous test, just with REAL to get coverage 4834CREATE TABLE t1r (id REAL, sex CHAR(1)); 4835INSERT INTO t1r VALUES (1.0, 'M'); 4836INSERT INTO t1r VALUES (2.0, 'F'); 4837INSERT INTO t1r VALUES (3.0, 'F'); 4838INSERT INTO t1r VALUES (4.0, 'F'); 4839INSERT INTO t1r VALUES (5.0, 'M'); 4840INSERT INTO t1r VALUES (10.0, NULL); 4841INSERT INTO t1r VALUES (11.0, NULL); 4842INSERT INTO t1r VALUES (10.0, NULL); 4843INSERT INTO t1r VALUES (NULL, 'M'); 4844INSERT INTO t1r VALUES (NULL, 'F'); 4845SET windowing_use_high_precision= OFF; 4846SELECT COUNT(id) OVER w, id, AVG(id) OVER w, SUM(id) OVER w, FIRST_VALUE(id) OVER w FROM t1r 4847WINDOW w AS (ORDER BY id RANGE 1 PRECEDING); 4848COUNT(id) OVER w id AVG(id) OVER w SUM(id) OVER w FIRST_VALUE(id) OVER w 48490 NULL NULL NULL NULL 48500 NULL NULL NULL NULL 48511 1 1 1 1 48522 2 1.5 3 1 48532 3 2.5 5 2 48542 4 3.5 7 3 48552 5 4.5 9 4 48562 10 10 20 10 48572 10 10 20 10 48583 11 10.333333333333334 31 10 4859SET windowing_use_high_precision= ON; 4860DROP TABLE t1r; 4861Check frame size, COUNT(*) vs COUNT(<column>) in frames with NULLs 4862SELECT id, count(id) over w, count(*) over w, FIRST_VALUE(id) OVER w FROM t1 4863WINDOW w AS (ORDER BY id ASC RANGE BETWEEN 2 PRECEDING AND 1 PRECEDING); 4864id count(id) over w count(*) over w FIRST_VALUE(id) OVER w 4865NULL 0 2 NULL 4866NULL 0 2 NULL 48671 0 0 NULL 48682 1 1 1 48693 2 2 1 48704 2 2 2 48715 2 2 3 487210 0 0 NULL 487310 0 0 NULL 487411 2 2 10 4875SELECT id, count(id) over w, count(*) over w, FIRST_VALUE(id) OVER w FROM t1 4876WINDOW w AS (ORDER BY id DESC RANGE BETWEEN 2 PRECEDING AND 1 PRECEDING); 4877id count(id) over w count(*) over w FIRST_VALUE(id) OVER w 487811 0 0 NULL 487910 1 1 11 488010 1 1 11 48815 0 0 NULL 48824 1 1 5 48833 2 2 5 48842 2 2 4 48851 2 2 3 4886NULL 0 2 NULL 4887NULL 0 2 NULL 4888DROP TABLE t1, t11, t2, t22, t3, t4, t5, t6, tj; 4889DROP TABLE t; 4890---------------------------------------------------------------------- 4891- Test NULL handling with RANGE 4892---------------------------------------------------------------------- 4893CREATE TABLE t(i INT); 4894INSERT INTO t VALUES (NULL), (NULL), (1), (2), (3), (4), (5); 4895SELECT COUNT(*) OVER (ORDER BY i RANGE BETWEEN 1 FOLLOWING AND UNBOUNDED FOLLOWING) FROM t; 4896COUNT(*) OVER (ORDER BY i RANGE BETWEEN 1 FOLLOWING AND UNBOUNDED FOLLOWING) 48977 48987 48994 49003 49012 49021 49030 4904SELECT COUNT(*) OVER (ORDER BY i RANGE BETWEEN 1 FOLLOWING AND 2 FOLLOWING) FROM t; 4905COUNT(*) OVER (ORDER BY i RANGE BETWEEN 1 FOLLOWING AND 2 FOLLOWING) 49062 49072 49082 49092 49102 49111 49120 4913SELECT COUNT(*) OVER (ORDER BY i RANGE BETWEEN 1 PRECEDING AND 2 FOLLOWING) FROM t; 4914COUNT(*) OVER (ORDER BY i RANGE BETWEEN 1 PRECEDING AND 2 FOLLOWING) 49152 49162 49173 49184 49194 49203 49212 4922SELECT COUNT(*) OVER (ORDER BY i RANGE BETWEEN 1 PRECEDING AND UNBOUNDED FOLLOWING) FROM t; 4923COUNT(*) OVER (ORDER BY i RANGE BETWEEN 1 PRECEDING AND UNBOUNDED FOLLOWING) 49247 49257 49265 49275 49284 49293 49302 4931SELECT COUNT(*) OVER (ORDER BY i RANGE BETWEEN 2 PRECEDING AND 1 PRECEDING) FROM t; 4932COUNT(*) OVER (ORDER BY i RANGE BETWEEN 2 PRECEDING AND 1 PRECEDING) 49332 49342 49350 49361 49372 49382 49392 4940SELECT COUNT(*) OVER (ORDER BY i RANGE BETWEEN UNBOUNDED PRECEDING AND UNBOUNDED FOLLOWING) FROM t; 4941COUNT(*) OVER (ORDER BY i RANGE BETWEEN UNBOUNDED PRECEDING AND UNBOUNDED FOLLOWING) 49427 49437 49447 49457 49467 49477 49487 4949SELECT COUNT(*) OVER (ORDER BY i RANGE BETWEEN UNBOUNDED PRECEDING AND 2 PRECEDING) FROM t; 4950COUNT(*) OVER (ORDER BY i RANGE BETWEEN UNBOUNDED PRECEDING AND 2 PRECEDING) 49512 49522 49532 49542 49553 49564 49575 4958SELECT COUNT(*) OVER (ORDER BY i RANGE BETWEEN UNBOUNDED PRECEDING AND 2 FOLLOWING) FROM t; 4959COUNT(*) OVER (ORDER BY i RANGE BETWEEN UNBOUNDED PRECEDING AND 2 FOLLOWING) 49602 49612 49625 49636 49647 49657 49667 4967SELECT COUNT(*) OVER (ORDER BY i DESC RANGE BETWEEN 1 FOLLOWING AND UNBOUNDED FOLLOWING) FROM t; 4968COUNT(*) OVER (ORDER BY i DESC RANGE BETWEEN 1 FOLLOWING AND UNBOUNDED FOLLOWING) 49696 49705 49714 49723 49732 49742 49752 4976SELECT COUNT(*) OVER (ORDER BY i DESC RANGE BETWEEN 1 FOLLOWING AND 2 FOLLOWING) FROM t; 4977COUNT(*) OVER (ORDER BY i DESC RANGE BETWEEN 1 FOLLOWING AND 2 FOLLOWING) 49782 49792 49802 49811 49820 49832 49842 4985SELECT COUNT(*) OVER (ORDER BY i DESC RANGE BETWEEN 1 PRECEDING AND 2 FOLLOWING) FROM t; 4986COUNT(*) OVER (ORDER BY i DESC RANGE BETWEEN 1 PRECEDING AND 2 FOLLOWING) 49873 49884 49894 49903 49912 49922 49932 4994SELECT COUNT(*) OVER (ORDER BY i DESC RANGE BETWEEN 1 PRECEDING AND UNBOUNDED FOLLOWING) FROM t; 4995COUNT(*) OVER (ORDER BY i DESC RANGE BETWEEN 1 PRECEDING AND UNBOUNDED FOLLOWING) 49967 49977 49986 49995 50004 50012 50022 5003SELECT COUNT(*) OVER (ORDER BY i DESC RANGE BETWEEN 2 PRECEDING AND 1 PRECEDING) FROM t; 5004COUNT(*) OVER (ORDER BY i DESC RANGE BETWEEN 2 PRECEDING AND 1 PRECEDING) 50050 50061 50072 50082 50092 50102 50112 5012SELECT COUNT(*) OVER (ORDER BY i DESC RANGE BETWEEN UNBOUNDED PRECEDING AND UNBOUNDED FOLLOWING) FROM t; 5013COUNT(*) OVER (ORDER BY i DESC RANGE BETWEEN UNBOUNDED PRECEDING AND UNBOUNDED FOLLOWING) 50147 50157 50167 50177 50187 50197 50207 5021SELECT COUNT(*) OVER (ORDER BY i DESC RANGE BETWEEN UNBOUNDED PRECEDING AND 2 PRECEDING) FROM t; 5022COUNT(*) OVER (ORDER BY i DESC RANGE BETWEEN UNBOUNDED PRECEDING AND 2 PRECEDING) 50230 50240 50251 50262 50273 50287 50297 5030SELECT COUNT(*) OVER (ORDER BY i DESC RANGE BETWEEN UNBOUNDED PRECEDING AND 2 FOLLOWING) FROM t; 5031COUNT(*) OVER (ORDER BY i DESC RANGE BETWEEN UNBOUNDED PRECEDING AND 2 FOLLOWING) 50323 50334 50345 50355 50365 50377 50387 5039DROP TABLE t; 5040---------------------------------------------------------------------- 5041- ORDER BY + RANK with more than one ordering expression 5042---------------------------------------------------------------------- 5043CREATE TABLE t(i INT, j INT, k INT); 5044INSERT INTO t VALUES (1,1,1); 5045INSERT INTO t VALUES (1,1,2); 5046INSERT INTO t VALUES (1,1,2); 5047INSERT INTO t VALUES (1,2,1); 5048INSERT INTO t VALUES (1,2,2); 5049INSERT INTO t VALUES (2,1,1); 5050INSERT INTO t VALUES (2,1,1); 5051INSERT INTO t VALUES (2,1,2); 5052INSERT INTO t VALUES (2,2,1); 5053INSERT INTO t VALUES (2,2,2); 5054SELECT *, RANK() OVER (ORDER BY i,j,k) AS O_IJK, 5055RANK() OVER (ORDER BY j) AS O_J, 5056RANK() OVER (ORDER BY k,j) AS O_KJ FROM t ORDER BY i,j,k; 5057i j k O_IJK O_J O_KJ 50581 1 1 1 1 1 50591 1 2 2 1 6 50601 1 2 2 1 6 50611 2 1 4 7 4 50621 2 2 5 7 9 50632 1 1 6 1 1 50642 1 1 6 1 1 50652 1 2 8 1 6 50662 2 1 9 7 4 50672 2 2 10 7 9 5068DROP TABLE t; 5069---------------------------------------------------------------------- 5070- Gulutzan's sanity tests in 5071- http://ocelot.ca/blog/blog/2016/04/18/mariadb-10-2-window-functions/ 5072- His comments are quoted. 5073---------------------------------------------------------------------- 5074CREATE TABLE t1 (s1 INT, s2 CHAR(5)); 5075INSERT INTO t1 VALUES (1, 'a'); 5076INSERT INTO t1 VALUES (NULL, NULL); 5077INSERT INTO t1 VALUES (1, NULL); 5078INSERT INTO t1 VALUES (NULL, 'a'); 5079INSERT INTO t1 VALUES (2, 'b'); 5080INSERT INTO t1 VALUES (-1, ''); 5081"The following statements all cause the MariaDB server to crash" 5082MySQL doesn't crash 5083SELECT ROW_NUMBER() OVER (); 5084ROW_NUMBER() OVER () 50851 5086SELECT 1 AS a, ROW_NUMBER() OVER (ORDER BY a) FROM dual; 5087ERROR 42S22: Unknown column 'a' in 'window order by' 5088SELECT *, ABS(ROW_NUMBER() OVER (ORDER BY s1,s2)) 5089- ROW_NUMBER() OVER (ORDER BY s1,s2) AS X FROM t1; 5090s1 s2 X 5091NULL NULL 0 5092NULL a 0 5093-1 0 50941 NULL 0 50951 a 0 50962 b 0 5097SELECT RANK() OVER (ORDER BY AVG(s1)) FROM t1; 5098RANK() OVER (ORDER BY AVG(s1)) 50991 5100"The following statements all give the wrong answers with MariaDB" 5101Correct with MySQL. 5102SELECT COUNT(*) OVER (ORDER BY s2) FROM t1 WHERE s2 IS NULL; 5103COUNT(*) OVER (ORDER BY s2) 51042 51052 5106SELECT * FROM ( 5107SELECT *,DENSE_RANK() OVER (ORDER BY s2 DESC), 5108DENSE_RANK() OVER (ORDER BY s2) FROM t1 5109) alias ORDER BY s1,s2; 5110s1 s2 DENSE_RANK() OVER (ORDER BY s2 DESC) DENSE_RANK() OVER (ORDER BY s2) 5111NULL NULL 4 1 5112NULL a 2 3 5113-1 3 2 51141 NULL 4 1 51151 a 2 3 51162 b 1 4 5117SELECT * FROM ( 5118SELECT *, SUM(s1) OVER (ORDER BY s1) FROM t1 ORDER BY s1 5119) alias ORDER BY s1,s2; 5120s1 s2 SUM(s1) OVER (ORDER BY s1) 5121NULL NULL NULL 5122NULL a NULL 5123-1 -1 51241 NULL 1 51251 a 1 51262 b 3 5127SELECT AVG(s1), RANK() OVER (ORDER BY s1) FROM t1; 5128ERROR 42000: In aggregated query without GROUP BY, expression #1 of PARTITION BY or ORDER BY clause of window '<unnamed window>' contains nonaggregated column 'test.t1.s1'; this is incompatible with sql_mode=only_full_group_by 5129"The following statement causes the client to hang (it loops in 5130mysql_store_result, I think this is the first time I've seen this type of 5131error)" 5132No issue with MySQL 5133SELECT *, AVG(s1) OVER () FROM t1; 5134s1 s2 AVG(s1) OVER () 51351 a 0.7500 5136NULL NULL 0.7500 51371 NULL 0.7500 5138NULL a 0.7500 51392 b 0.7500 5140-1 0.7500 5141SELECT *, AVG(s1) OVER (ROWS UNBOUNDED PRECEDING) FROM t1; 5142s1 s2 AVG(s1) OVER (ROWS UNBOUNDED PRECEDING) 51431 a 1.0000 5144NULL NULL 1.0000 51451 NULL 1.0000 5146NULL a 1.0000 51472 b 1.3333 5148-1 0.7500 5149DROP TABLE t1; 5150Some negative tests (from Srikanth) 5151CREATE TABLE t (a INT, b INT, c INT); 5152INSERT INTO t VALUES (1,1,1); 5153INSERT INTO t VALUES (1,1,2); 5154INSERT INTO t VALUES (1,1,3); 5155INSERT INTO t VALUES (1,2,1); 5156INSERT INTO t VALUES (1,2,2); 5157INSERT INTO t VALUES (1,2,3); 5158INSERT INTO t VALUES (1,3,1); 5159INSERT INTO t VALUES (1,3,2); 5160INSERT INTO t VALUES (1,3,3); 5161INSERT INTO t VALUES (2,1,1); 5162INSERT INTO t VALUES (2,1,2); 5163INSERT INTO t VALUES (2,1,3); 5164INSERT INTO t VALUES (2,2,1); 5165INSERT INTO t VALUES (2,2,2); 5166INSERT INTO t VALUES (2,2,3); 5167INSERT INTO t VALUES (2,3,1); 5168INSERT INTO t VALUES (2,3,2); 5169INSERT INTO t VALUES (2,3,3); 5170Wfs OK in ORDER BY, but not in WHERE or HAVING clauses 5171SELECT * FROM t ORDER BY RANK() OVER (ORDER BY a DESC,b,c); 5172a b c 51732 1 1 51742 1 2 51752 1 3 51762 2 1 51772 2 2 51782 2 3 51792 3 1 51802 3 2 51812 3 3 51821 1 1 51831 1 2 51841 1 3 51851 2 1 51861 2 2 51871 2 3 51881 3 1 51891 3 2 51901 3 3 5191SELECT *, RANK() OVER (ORDER BY a DESC,b,c) AS `rank` FROM t ORDER BY `rank`; 5192a b c rank 51932 1 1 1 51942 1 2 2 51952 1 3 3 51962 2 1 4 51972 2 2 5 51982 2 3 6 51992 3 1 7 52002 3 2 8 52012 3 3 9 52021 1 1 10 52031 1 2 11 52041 1 3 12 52051 2 1 13 52061 2 2 14 52071 2 3 15 52081 3 1 16 52091 3 2 17 52101 3 3 18 5211SELECT * FROM t WHERE 1 = RANK() OVER (ORDER BY a); 5212ERROR HY000: You cannot use the window function 'rank' in this context.' 5213SELECT * FROM t HAVING 1 = rank() OVER (ORDER BY a); 5214ERROR HY000: You cannot use the window function 'rank' in this context.' 5215SELECT 1 FROM t HAVING 1=(SELECT 1 FROM (SELECT 1) foo) AND COUNT(a) OVER (); 5216ERROR HY000: You cannot use the window function 'count' in this context.' 5217# 5218# Bug#26502118: WINDOW FUNCTIONS: CRASH AND LARGE MEMORY ALLOCATION, 5219# FIRST_VALUE, BLOBS 5220# 5221# This was really missing error detection 5222# 5223CREATE TABLE tb(a LONGBLOB NOT NULL); 5224INSERT INTO tb VALUES ('1'), ('2'), ('3'), ('4'), ('5'); 5225SELECT (SELECT 1 FROM tb WHERE (SELECT 1 FROM tb WHERE FIRST_VALUE(a) OVER())); 5226ERROR HY000: You cannot use the window function 'first_value' in this context.' 5227DROP TABLE tb; 5228Windows should only be allowed in order by of a simple table query 5229(select a from t) union (select a from t) order by (row_number() over ()); 5230ERROR HY000: Expression #1 of ORDER BY contains aggregate function and applies to a UNION 5231(select a from t) union (select a from t) order by (1+row_number() over ()); 5232ERROR HY000: Expression #1 of ORDER BY contains aggregate function and applies to a UNION 5233This is legal, though: 5234(select a from t) union (select a from t order by (row_number() over ())); 5235a 52361 52372 5238Non constants as frame bounds 5239SELECT a AS foo, SUM(a) OVER (ORDER BY a ROWS foo PRECEDING) FROM t; 5240ERROR 42000: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'foo PRECEDING) FROM t' at line 1 5241SELECT a, SUM(a) OVER (ORDER BY a ROWS a PRECEDING) FROM t; 5242ERROR 42000: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'a PRECEDING) FROM t' at line 1 5243Non-unique window name 5244SELECT count(*) OVER w FROM t WINDOW w AS (ORDER BY a), w AS (ORDER BY b); 5245ERROR HY000: Window 'w' is defined twice. 5246Illegal legacy position indication in window's ORDER BY clause 5247SELECT RANK() OVER (ORDER BY 1) FROM t; 5248ERROR HY000: Window '<unnamed window>': ORDER BY or PARTITION BY uses legacy position indication which is not supported, use expression. 5249SELECT * FROM ( 5250SELECT a,b,c, RANK() OVER (ORDER BY 1*1) FROM t 5251) alias ORDER BY a,b,c; 5252a b c RANK() OVER (ORDER BY 1*1) 52531 1 1 1 52541 1 2 1 52551 1 3 1 52561 2 1 1 52571 2 2 1 52581 2 3 1 52591 3 1 1 52601 3 2 1 52611 3 3 1 52622 1 1 1 52632 1 2 1 52642 1 3 1 52652 2 1 1 52662 2 2 1 52672 2 3 1 52682 3 1 1 52692 3 2 1 52702 3 3 1 5271Crashed: more than one window in subquery 5272SELECT * FROM (SELECT count(*) OVER (), sum(c) OVER () AS sum1, a from t) as alias; 5273count(*) OVER () sum1 a 527418 36 1 527518 36 1 527618 36 1 527718 36 1 527818 36 1 527918 36 1 528018 36 1 528118 36 1 528218 36 1 528318 36 2 528418 36 2 528518 36 2 528618 36 2 528718 36 2 528818 36 2 528918 36 2 529018 36 2 529118 36 2 5292Crashed: expression containing window function(s) in subquery 5293SELECT * FROM (SELECT count(*) OVER () + sum(c) OVER () AS sum1, a from t) as alias; 5294sum1 a 529554 1 529654 1 529754 1 529854 1 529954 1 530054 1 530154 1 530254 1 530354 1 530454 2 530554 2 530654 2 530754 2 530854 2 530954 2 531054 2 531154 2 531254 2 5313Wrong result if subquery window function referenced another column in the select list 5314This was OK, but: 5315SELECT * FROM (SELECT SUM(b) OVER (), a FROM t) AS alias; 5316SUM(b) OVER () a 531736 1 531836 1 531936 1 532036 1 532136 1 532236 1 532336 1 532436 1 532536 1 532636 2 532736 2 532836 2 532936 2 533036 2 533136 2 533236 2 533336 2 533436 2 5335this one failed with NULL as sum 5336SELECT * FROM (SELECT SUM(b) OVER (), b FROM t) AS alias; 5337SUM(b) OVER () b 533836 1 533936 1 534036 1 534136 2 534236 2 534336 2 534436 3 534536 3 534636 3 534736 1 534836 1 534936 1 535036 2 535136 2 535236 2 535336 3 535436 3 535536 3 5356Crash due to unguarded access for window name string for an unnamed 5357window while producing the error message 5358SELECT a, b, c, rank() OVER ( w ORDER BY c DESC ) FROM t WINDOW w AS (ORDER BY a); 5359ERROR HY000: Window '<unnamed window>' cannot inherit 'w' since both contain an ORDER BY clause. 5360Check that DISTINCT is not allowed in wfs 5361SELECT SUM(DISTINCT b) OVER () FROM t; 5362ERROR 42000: This version of MySQL doesn't yet support '<window function>(DISTINCT ..)' 5363Check that GROUPS bounds unit is not supported yet 5364SELECT SUM(b) OVER (ORDER by a GROUPS 2 PRECEDING) FROM t; 5365ERROR 42000: This version of MySQL doesn't yet support 'GROUPS' 5366UPDATE t set a= SUM(b) OVER (); 5367ERROR HY000: You cannot use the window function 'sum' in this context.' 5368DELETE FROM t WHERE SUM(b) OVER () = 10; 5369ERROR HY000: You cannot use the window function 'sum' in this context.' 5370Check that EXCLUDE in frames is not supported yet 5371SELECT SUM(b) OVER (ORDER by a ROWS 2 PRECEDING EXCLUDE CURRENT ROW) FROM t; 5372ERROR 42000: This version of MySQL doesn't yet support 'EXCLUDE' 5373SELECT SUM(b) OVER (ORDER by a ROWS 2 PRECEDING EXCLUDE TIES) FROM t; 5374ERROR 42000: This version of MySQL doesn't yet support 'EXCLUDE' 5375SELECT SUM(b) OVER (ORDER by a ROWS 2 PRECEDING EXCLUDE GROUP) FROM t; 5376ERROR 42000: This version of MySQL doesn't yet support 'EXCLUDE' 5377SELECT SUM(b) OVER (ORDER by a ROWS 2 PRECEDING EXCLUDE NO OTHERS) FROM t; 5378ERROR 42000: This version of MySQL doesn't yet support 'EXCLUDE' 5379Check Nested wfs 5380SELECT a, b, FIRST_VALUE(SUM(a+b) OVER()) OVER () AS sum FROM t; 5381ERROR HY000: You cannot use the window function 'sum' in this context.' 5382SELECT a, b, FIRST_VALUE(1+SUM(a+b) OVER()) OVER () AS sum FROM t; 5383ERROR HY000: You cannot use the window function 'sum' in this context.' 5384SELECT a, b, SUM(1+SUM(a+b) OVER()) OVER () AS sum FROM t; 5385ERROR HY000: You cannot use the window function 'sum' in this context.' 5386SELECT a, b, FIRST_VALUE(a) OVER (PARTITION BY ROW_NUMBER() OVER ()) AS sum FROM t; 5387ERROR HY000: You cannot nest a window function in the specification of window '<unnamed window>'. 5388SELECT a, b, FIRST_VALUE(a) OVER (PARTITION BY 1+ROW_NUMBER() OVER ()) AS sum FROM t; 5389ERROR HY000: You cannot nest a window function in the specification of window '<unnamed window>'. 5390SELECT a, b, FIRST_VALUE(a) OVER (ORDER BY ROW_NUMBER() OVER ()) AS sum FROM t; 5391ERROR HY000: You cannot nest a window function in the specification of window '<unnamed window>'. 5392CREATE TABLE t_time(t TIME, ts TIMESTAMP); 5393SELECT t, FIRST_VALUE(t) OVER (ORDER BY t RANGE INTERVAL ROW_NUMBER() OVER () HOUR PRECEDING) FROM t_time; 5394ERROR HY000: You cannot use the window function 'row_number' in this context.' 5395SELECT t, FIRST_VALUE(t) OVER (ORDER BY t RANGE INTERVAL ABS(t) HOUR PRECEDING) FROM t_time; 5396ERROR HY000: Window '<unnamed window>' has a non-constant frame bound. 5397SELECT t, FIRST_VALUE(t) OVER (ORDER BY t RANGE BETWEEN INTERVAL 3 HOUR PRECEDING AND INTERVAL ABS(t) HOUR FOLLOWING) FROM t_time; 5398ERROR HY000: Window '<unnamed window>' has a non-constant frame bound. 5399DROP TABLE t, t_time; 5400Crash report (Srikanth) 5401CREATE TABLE t(a int, b int); 5402INSERT INTO t VALUES (1,1); 5403INSERT INTO t VALUES (2,1); 5404INSERT INTO t VALUES (3,2); 5405INSERT INTO t VALUES (4,2); 5406INSERT INTO t VALUES (5,3); 5407INSERT INTO t VALUES (6,3); 5408SELECT SUM(a) OVER (ORDER BY b) FROM t; 5409SUM(a) OVER (ORDER BY b) 54103 54113 541210 541310 541421 541521 5416SELECT COUNT(*) OVER (ORDER BY b) FROM t; 5417COUNT(*) OVER (ORDER BY b) 54182 54192 54204 54214 54226 54236 5424SELECT AVG(b) OVER (ORDER BY b) FROM t; 5425AVG(b) OVER (ORDER BY b) 54261.0000 54271.0000 54281.5000 54291.5000 54302.0000 54312.0000 5432SELECT a,b,LAST_VALUE(a) OVER (ORDER BY b,a) FROM t; 5433a b LAST_VALUE(a) OVER (ORDER BY b,a) 54341 1 1 54352 1 2 54363 2 3 54374 2 4 54385 3 5 54396 3 6 5440SELECT NTILE(2) OVER (ORDER BY b) FROM t; 5441NTILE(2) OVER (ORDER BY b) 54421 54431 54441 54452 54462 54472 5448DROP TABLE t; 5449Wrong result (Srikanth) 5450CREATE TABLE t1(a INT, b INT); 5451INSERT INTO t1 VALUES (1,2); 5452INSERT INTO t1 VALUES (1,3); 5453SELECT a, b, COUNT(a) OVER w count, 5454SUM(a) OVER w sum, 5455AVG(a) over w average, 5456LAST_VALUE(a) OVER w lastval FROM t1 5457WINDOW w as (PARTITION BY a ORDER BY b ROWS BETWEEN 1 PRECEDING AND 1 FOLLOWING); 5458a b count sum average lastval 54591 2 2 2 1.0000 1 54601 3 2 2 1.0000 1 5461INSERT INTO t1 VALUES (1,3); 5462SELECT a, b, COUNT(a) OVER w count, 5463SUM(a) OVER w sum, 5464AVG(a) OVER w average, 5465LAST_VALUE(a) OVER w lastval FROM t1 5466WINDOW w as (PARTITION BY a ORDER BY b ROWS BETWEEN 1 PRECEDING AND 1 FOLLOWING); 5467a b count sum average lastval 54681 2 2 2 1.0000 1 54691 3 3 3 1.0000 1 54701 3 2 2 1.0000 1 5471SELECT a, b, COUNT(a) OVER w count, 5472SUM(a) OVER w sum, 5473AVG(a) OVER w average, 5474LAST_VALUE(a) OVER w lastval FROM t1 5475WINDOW w as (PARTITION BY a ORDER BY b ROWS BETWEEN 1 PRECEDING AND 2 FOLLOWING); 5476a b count sum average lastval 54771 2 3 3 1.0000 1 54781 3 3 3 1.0000 1 54791 3 2 2 1.0000 1 5480DROP TABLE t1; 5481frame buffer navigation assert 5482CREATE TABLE ta (a INT(11) DEFAULT NULL, b INT(11) DEFAULT NULL); 5483Warnings: 5484Warning 1681 Integer display width is deprecated and will be removed in a future release. 5485Warning 1681 Integer display width is deprecated and will be removed in a future release. 5486INSERT INTO ta VALUES (1,1); 5487INSERT INTO ta VALUES (1,2); 5488INSERT INTO ta VALUES (1,3); 5489INSERT INTO ta VALUES (2,1); 5490INSERT INTO ta VALUES (2,2); 5491INSERT INTO ta VALUES (2,3); 5492SELECT last_value(b) OVER (ROWS BETWEEN CURRENT ROW AND UNBOUNDED FOLLOWING) FROM ta; 5493last_value(b) OVER (ROWS BETWEEN CURRENT ROW AND UNBOUNDED FOLLOWING) 54943 54953 54963 54973 54983 54993 5500DROP TABLE ta; 5501Nullability fix bug for COUNT OVER in non optimized eval strategy 5502CREATE TABLE t(d DOUBLE); 5503INSERT INTO t VALUES (1.0); 5504INSERT INTO t VALUES (2.0); 5505INSERT INTO t VALUES (3.0); 5506SELECT SUM(d) OVER w, COUNT(*) OVER w FROM t WINDOW W AS (ROWS BETWEEN 1 FOLLOWING AND 2 FOLLOWING); 5507SUM(d) OVER w COUNT(*) OVER w 55085 2 55093 1 5510NULL 0 5511DROP TABLE t; 5512Bug in inverse logic with initial NULL and RANGE BETWEEN N FOLLOWING AND M FOLLOWING 5513CREATE TABLE t1 (d DOUBLE, id INT, sex CHAR(1), n INT NOT NULL AUTO_INCREMENT, PRIMARY KEY(n)); 5514INSERT INTO t1(d, id, sex) VALUES (1.0, 1, 'M'); 5515INSERT INTO t1(d, id, sex) VALUES (2.0, 2, 'F'); 5516INSERT INTO t1(d, id, sex) VALUES (3.0, 3, 'F'); 5517INSERT INTO t1(d, id, sex) VALUES (4.0, 4, 'F'); 5518INSERT INTO t1(d, id, sex) VALUES (5.0, 5, 'M'); 5519INSERT INTO t1(d, id, sex) VALUES (NULL, NULL, 'M'); 5520INSERT INTO t1(d, id, sex) VALUES (10.0, 10, NULL); 5521INSERT INTO t1(d, id, sex) VALUES (10.0, 10, NULL); 5522INSERT INTO t1(d, id, sex) VALUES (11.0, 11, NULL); 5523SELECT id, AVG(id) over w `avg`, SUM(id) OVER w `sum`, COUNT(*) OVER w cnt FROM t1 WINDOW w as (ORDER BY id RANGE BETWEEN 1 FOLLOWING AND 2 FOLLOWING); 5524id avg sum cnt 5525NULL NULL NULL 1 55261 2.5000 5 2 55272 3.5000 7 2 55283 4.5000 9 2 55294 5.0000 5 1 55305 NULL NULL 0 553110 11.0000 11 1 553210 11.0000 11 1 553311 NULL NULL 0 5534SET windowing_use_high_precision= OFF; 5535SELECT d, AVG(d) over w `avg`, SUM(d) OVER w `sum`, COUNT(*) OVER w cnt FROM t1 WINDOW w as (ORDER BY d RANGE BETWEEN 1 FOLLOWING AND 2 FOLLOWING); 5536d avg sum cnt 5537NULL NULL NULL 1 55381 2.5 5 2 55392 3.5 7 2 55403 4.5 9 2 55414 5 5 1 55425 NULL NULL 0 554310 11 11 1 554410 11 11 1 554511 NULL NULL 0 5546SET windowing_use_high_precision= ON; 5547DROP TABLE t1; 5548Bug in inverse logic with e.g. ROWS BETWEEN UNBOUNDED PRECEDING AND 1 5549FOLLOWING: at end of partition, when no rows are removed or added we 5550lacked initialization of aggregates in optimized mode. 5551CREATE TABLE t (i char(10), j int); 5552INSERT INTO t VALUES('A', 1); 5553INSERT INTO t VALUES('A', 3); 5554INSERT INTO t VALUES('A', 5); 5555INSERT INTO t VALUES('B', 1); 5556INSERT INTO t VALUES('B', 7); 5557SELECT i, j, SUM(j) OVER w FROM t 5558WINDOW w AS (PARTITION BY i ORDER BY j 5559ROWS BETWEEN UNBOUNDED PRECEDING AND 1 FOLLOWING); 5560i j SUM(j) OVER w 5561A 1 4 5562A 3 9 5563A 5 9 5564B 1 8 5565B 7 8 5566DROP TABLE t; 5567Test that we force use of a final temporary table in the last windowing step 5568if SQL_BUFFER_RESULT is set, even if we would otherwise optimize it away 5569CREATE TABLE t1 (t1_id INT) ; 5570INSERT INTO t1 (t1_id) VALUES (1); 5571INSERT INTO t1 (t1_id) VALUES (2); 5572INSERT INTO t1 (t1_id) VALUES (3); 5573INSERT INTO t1 (t1_id) VALUES (4); 5574INSERT INTO t1 (t1_id) VALUES (5); 5575ANALYZE TABLE t1; 5576Table Op Msg_type Msg_text 5577test.t1 analyze status OK 5578Still unset 5579SELECT t1_id, ROW_NUMBER() OVER () FROM t1; 5580t1_id ROW_NUMBER() OVER () 55811 1 55822 2 55833 3 55844 4 55855 5 5586EXPLAIN FORMAT=JSON SELECT t1_id, ROW_NUMBER() OVER () FROM t1; 5587EXPLAIN 5588{ 5589 "query_block": { 5590 "select_id": 1, 5591 "cost_info": { 5592 "query_cost": "0.75" 5593 }, 5594 "windowing": { 5595 "windows": [ 5596 { 5597 "name": "<unnamed window>", 5598 "functions": [ 5599 "row_number" 5600 ] 5601 } 5602 ], 5603 "table": { 5604 "table_name": "t1", 5605 "access_type": "ALL", 5606 "rows_examined_per_scan": 5, 5607 "rows_produced_per_join": 5, 5608 "filtered": "100.00", 5609 "cost_info": { 5610 "read_cost": "0.25", 5611 "eval_cost": "0.50", 5612 "prefix_cost": "0.75", 5613 "data_read_per_join": "40" 5614 }, 5615 "used_columns": [ 5616 "t1_id" 5617 ] 5618 } 5619 } 5620 } 5621} 5622Warnings: 5623Note 1003 /* select#1 */ select `test`.`t1`.`t1_id` AS `t1_id`,row_number() OVER () AS `ROW_NUMBER() OVER ()` from `test`.`t1` 5624Now set 5625SET SQL_BUFFER_RESULT=TRUE; 5626FLUSH STATUS; 5627SELECT t1_id, ROW_NUMBER() OVER () FROM t1; 5628t1_id ROW_NUMBER() OVER () 56291 1 56302 2 56313 3 56324 4 56335 5 5634SHOW STATUS LIKE 'Created_tmp_tables'; 5635Variable_name Value 5636Created_tmp_tables 1 5637EXPLAIN FORMAT=JSON SELECT t1_id, ROW_NUMBER() OVER () FROM t1; 5638EXPLAIN 5639{ 5640 "query_block": { 5641 "select_id": 1, 5642 "cost_info": { 5643 "query_cost": "0.75" 5644 }, 5645 "windowing": { 5646 "windows": [ 5647 { 5648 "name": "<unnamed window>", 5649 "using_temporary_table": true, 5650 "functions": [ 5651 "row_number" 5652 ] 5653 } 5654 ], 5655 "table": { 5656 "table_name": "t1", 5657 "access_type": "ALL", 5658 "rows_examined_per_scan": 5, 5659 "rows_produced_per_join": 5, 5660 "filtered": "100.00", 5661 "cost_info": { 5662 "read_cost": "0.25", 5663 "eval_cost": "0.50", 5664 "prefix_cost": "0.75", 5665 "data_read_per_join": "40" 5666 }, 5667 "used_columns": [ 5668 "t1_id" 5669 ] 5670 } 5671 } 5672 } 5673} 5674Warnings: 5675Note 1003 /* select#1 */ select sql_buffer_result `test`.`t1`.`t1_id` AS `t1_id`,row_number() OVER () AS `ROW_NUMBER() OVER ()` from `test`.`t1` 5676SET SQL_BUFFER_RESULT=FALSE; 5677DROP TABLE t1; 5678Optimization of last tmp file made this query fail before 5679(found by Guilhem during review) 5680CREATE TABLE t1(a INT); 5681INSERT INTO t1 VALUES(1),(1),(2),(2); 5682SELECT (SELECT COUNT(a) OVER (PARTITION BY a) FROM t1) FROM t1; 5683ERROR 21000: Subquery returns more than 1 row 5684DROP TABLE t1; 5685SELECT SUM(1) OVER (); 5686SUM(1) OVER () 56871 5688SELECT SUM(1.2) OVER (); 5689SUM(1.2) OVER () 56901.2 5691SELECT SUM(CAST(4 as JSON)) OVER (); 5692SUM(CAST(4 as JSON)) OVER () 56934 5694SELECT SUM('e') OVER (); 5695SUM('e') OVER () 56960 5697Warnings: 5698Warning 1292 Truncated incorrect DOUBLE value: 'e' 5699SELECT SUM(1) OVER (ROWS BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW); 5700SUM(1) OVER (ROWS BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW) 57011 5702SELECT SUM(1.2) OVER (ROWS BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW); 5703SUM(1.2) OVER (ROWS BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW) 57041.2 5705SELECT SUM(CAST(4 as JSON)) OVER (ROWS BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW); 5706SUM(CAST(4 as JSON)) OVER (ROWS BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW) 57074 5708SELECT SUM('e') OVER (ROWS BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW); 5709SUM('e') OVER (ROWS BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW) 57100 5711Warnings: 5712Warning 1292 Truncated incorrect DOUBLE value: 'e' 5713SELECT AVG(1) OVER (); 5714AVG(1) OVER () 57151.0000 5716SELECT AVG(1.2) OVER (); 5717AVG(1.2) OVER () 57181.20000 5719SELECT AVG(CAST(4 as JSON)) OVER (); 5720AVG(CAST(4 as JSON)) OVER () 57214 5722SELECT AVG('e') OVER (); 5723AVG('e') OVER () 57240 5725Warnings: 5726Warning 1292 Truncated incorrect DOUBLE value: 'e' 5727SELECT AVG(1) OVER (ROWS BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW); 5728AVG(1) OVER (ROWS BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW) 57291.0000 5730SELECT AVG(1.2) OVER (ROWS BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW); 5731AVG(1.2) OVER (ROWS BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW) 57321.20000 5733SELECT AVG(CAST(4 as JSON)) OVER (ROWS BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW); 5734AVG(CAST(4 as JSON)) OVER (ROWS BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW) 57354 5736SELECT AVG('e') OVER (ROWS BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW); 5737AVG('e') OVER (ROWS BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW) 57380 5739Warnings: 5740Warning 1292 Truncated incorrect DOUBLE value: 'e' 5741CREATE TABLE t1 (i INT); 5742INSERT INTO t1 VALUES (1),(2); 5743SELECT i FROM t1 WHERE i IN ( SELECT CAST( SUM(i) OVER (ROWS CURRENT ROW) AS UNSIGNED) FROM t1); 5744i 57451 57462 5747SELECT i FROM t1 WHERE i IN ( SELECT CAST(0+SUM(i) OVER (ROWS CURRENT ROW) AS UNSIGNED) FROM t1); 5748i 57491 57502 5751SELECT FIRST_VALUE(i) IGNORE NULLS OVER () FROM t1; 5752ERROR 42000: This version of MySQL doesn't yet support 'IGNORE NULLS' 5753DROP TABLE t1; 5754Regression bug introduced by the first patch for Bug#25363694 for empty 5755result set in the presence of buffered windowing. 5756CREATE TABLE t1(i INT, j INT); 5757SELECT SUM(i) OVER w FROM t1 5758WINDOW w AS (PARTITION BY j ORDER BY i ROWS BETWEEN 1 PRECEDING AND 1 FOLLOWING); 5759SUM(i) OVER w 5760DROP TABLE t1; 5761CREATE TABLE t1 (pk INT, j INT, PRIMARY KEY (pk), KEY(j)); 5762INSERT INTO t1 values (1,2); 5763SELECT LAST_VALUE(1) OVER (PARTITION BY t1.j), t2.pk FROM t1 LEFT JOIN 5764t1 as t2 ON t1.pk = t2.pk WHERE t1.pk=1; 5765LAST_VALUE(1) OVER (PARTITION BY t1.j) pk 57661 1 5767DROP TABLE t1; 5768CREATE TABLE t1 (a INTEGER); 5769INSERT INTO t1 VALUES (1),(2),(3),(4),(5); 5770ANALYZE TABLE t1; 5771Table Op Msg_type Msg_text 5772test.t1 analyze status OK 5773SELECT a, RANK() OVER (ORDER BY a) rank_asc, 5774RANK() OVER (ORDER BY a desc) rank_desc, 5775RANK() OVER (ORDER BY a) + RANK() OVER (ORDER BY a desc) rank_asc_desc FROM t1; 5776a rank_asc rank_desc rank_asc_desc 57775 5 1 6 57784 4 2 6 57793 3 3 6 57802 2 4 6 57811 1 5 6 5782EXPLAIN FORMAT=JSON SELECT a, RANK() OVER (ORDER BY a) rank_asc, 5783RANK() OVER (ORDER BY a desc) rank_desc, 5784RANK() OVER (ORDER BY a) + RANK() OVER (ORDER BY a desc) rank_asc_desc FROM t1; 5785EXPLAIN 5786{ 5787 "query_block": { 5788 "select_id": 1, 5789 "cost_info": { 5790 "query_cost": "10.75" 5791 }, 5792 "windowing": { 5793 "windows": [ 5794 { 5795 "name": "<unnamed window>", 5796 "definition_position": 1, 5797 "using_temporary_table": true, 5798 "using_filesort": true, 5799 "filesort_key": [ 5800 "`a`" 5801 ], 5802 "functions": [ 5803 "rank" 5804 ] 5805 }, 5806 { 5807 "name": "<unnamed window>", 5808 "definition_position": 3, 5809 "using_temporary_table": true, 5810 "functions": [ 5811 "rank" 5812 ] 5813 }, 5814 { 5815 "name": "<unnamed window>", 5816 "definition_position": 2, 5817 "using_temporary_table": true, 5818 "using_filesort": true, 5819 "filesort_key": [ 5820 "`a` desc" 5821 ], 5822 "functions": [ 5823 "rank" 5824 ] 5825 }, 5826 { 5827 "name": "<unnamed window>", 5828 "definition_position": 4, 5829 "last_executed_window": true, 5830 "functions": [ 5831 "rank" 5832 ] 5833 } 5834 ], 5835 "cost_info": { 5836 "sort_cost": "10.00" 5837 }, 5838 "table": { 5839 "table_name": "t1", 5840 "access_type": "ALL", 5841 "rows_examined_per_scan": 5, 5842 "rows_produced_per_join": 5, 5843 "filtered": "100.00", 5844 "cost_info": { 5845 "read_cost": "0.25", 5846 "eval_cost": "0.50", 5847 "prefix_cost": "0.75", 5848 "data_read_per_join": "40" 5849 }, 5850 "used_columns": [ 5851 "a" 5852 ] 5853 } 5854 } 5855 } 5856} 5857Warnings: 5858Note 1003 /* select#1 */ select `test`.`t1`.`a` AS `a`,rank() OVER (ORDER BY `test`.`t1`.`a` ) AS `rank_asc`,rank() OVER (ORDER BY `test`.`t1`.`a` desc ) AS `rank_desc`,(rank() OVER (ORDER BY `test`.`t1`.`a` ) + rank() OVER (ORDER BY `test`.`t1`.`a` desc ) ) AS `rank_asc_desc` from `test`.`t1` 5859DROP TABLE t1; 5860CREATE TABLE t(a INT); 5861INSERT INTO t VALUES(5); 5862SELECT ROW_NUMBER () OVER (), COUNT(*) FROM t WHERE a < 5; 5863ROW_NUMBER () OVER () COUNT(*) 58641 0 5865DROP TABLE t; 5866CREATE TABLE t1(i INT, j INT, k INT); 5867INSERT INTO t1 VALUES (1,1,1),(2,2,2),(3,3,3),(4,4,4); 5868INSERT INTO t1 SELECT 10*i,j,5*j FROM t1 UNION SELECT 20*i,j,5*j FROM t1 5869UNION SELECT 30*i,j,5*j FROM t1; 5870ANALYZE TABLE t1; 5871Table Op Msg_type Msg_text 5872test.t1 analyze status OK 5873SELECT SUM(i) OVER W FROM t1 WINDOW w AS (PARTITION BY j ORDER BY i) 5874ORDER BY SUM(i) OVER w; 5875SUM(i) OVER W 58761 58772 58783 58794 588011 588122 588231 588333 588444 588561 588662 588793 5888122 5889124 5890183 5891244 5892SELECT SUM(i) OVER W FROM t1 WINDOW w AS (PARTITION BY j ORDER BY i) 5893ORDER BY 1+SUM(i) OVER w; 5894SUM(i) OVER W 58951 58962 58973 58984 589911 590022 590131 590233 590344 590461 590562 590693 5907122 5908124 5909183 5910244 5911SELECT SUM(SUM(i)) OVER W FROM t1 GROUP BY i WINDOW w AS (PARTITION BY i ORDER BY i) 5912ORDER BY SUM(SUM(i)) OVER w; 5913SUM(SUM(i)) OVER W 59141 59152 59163 59174 591810 591940 592060 592180 592280 592390 5924120 5925120 5926SELECT 1+SUM(SUM(i)) OVER W FROM t1 GROUP BY i WINDOW w AS (PARTITION BY i ORDER BY i) 5927ORDER BY 1+SUM(SUM(i)) OVER w; 59281+SUM(SUM(i)) OVER W 59292 59303 59314 59325 593311 593441 593561 593681 593781 593891 5939121 5940121 5941SELECT 1+SUM(i) OVER W FROM t1 WINDOW w AS (PARTITION BY j ORDER BY i) 5942ORDER BY SUM(i) OVER w; 59431+SUM(i) OVER W 59442 59453 59464 59475 594812 594923 595032 595134 595245 595362 595463 595594 5956123 5957125 5958184 5959245 5960SELECT SUM(2+SUM(i)) OVER W FROM t1 GROUP BY j WINDOW w AS (PARTITION BY j ORDER BY j) 5961ORDER BY SUM(2+SUM(i)) OVER w DESC; 5962SUM(2+SUM(i)) OVER W 5963246 5964185 5965124 596663 5967DROP TABLE t1; 5968CREATE TABLE t(a INT); 5969INSERT INTO t VALUES (1),(2),(3); 5970SELECT ROW_NUMBER() OVER () AS num FROM t HAVING (num = '2'); 5971ERROR HY000: You cannot use the alias 'num' of an expression containing a window function in this context.' 5972SELECT ROW_NUMBER() OVER () FROM t HAVING ( ROW_NUMBER() OVER () = '2'); 5973ERROR HY000: You cannot use the window function 'row_number' in this context.' 5974DROP TABLE t; 5975Error due to missing fix_items of window's ORDER BY when 5976eliminating redundant sorts 5977CREATE TABLE t1 (a INT, b INT); 5978INSERT INTO t1 VALUES (1,1),(2,2),(3,3), 5979(3,1),(2,2),(1,3); 5980ANALYZE TABLE t1; 5981Table Op Msg_type Msg_text 5982test.t1 analyze status OK 5983This one was ok 5984SELECT a,b, RANK() OVER (ORDER BY a), RANK() OVER (order BY a) FROM t1; 5985a b RANK() OVER (ORDER BY a) RANK() OVER (order BY a) 59861 1 1 1 59871 3 1 1 59882 2 3 3 59892 2 3 3 59903 3 5 5 59913 1 5 5 5992EXPLAIN FORMAT=JSON SELECT a,b, RANK() OVER (ORDER BY a), RANK() OVER (ORDER BY a) FROM t1; 5993EXPLAIN 5994{ 5995 "query_block": { 5996 "select_id": 1, 5997 "cost_info": { 5998 "query_cost": "6.85" 5999 }, 6000 "windowing": { 6001 "windows": [ 6002 { 6003 "name": "<unnamed window>", 6004 "definition_position": 1, 6005 "using_temporary_table": true, 6006 "using_filesort": true, 6007 "filesort_key": [ 6008 "`a`" 6009 ], 6010 "functions": [ 6011 "rank" 6012 ] 6013 }, 6014 { 6015 "name": "<unnamed window>", 6016 "definition_position": 2, 6017 "last_executed_window": true, 6018 "functions": [ 6019 "rank" 6020 ] 6021 } 6022 ], 6023 "cost_info": { 6024 "sort_cost": "6.00" 6025 }, 6026 "table": { 6027 "table_name": "t1", 6028 "access_type": "ALL", 6029 "rows_examined_per_scan": 6, 6030 "rows_produced_per_join": 6, 6031 "filtered": "100.00", 6032 "cost_info": { 6033 "read_cost": "0.25", 6034 "eval_cost": "0.60", 6035 "prefix_cost": "0.85", 6036 "data_read_per_join": "96" 6037 }, 6038 "used_columns": [ 6039 "a", 6040 "b" 6041 ] 6042 } 6043 } 6044 } 6045} 6046Warnings: 6047Note 1003 /* select#1 */ select `test`.`t1`.`a` AS `a`,`test`.`t1`.`b` AS `b`,rank() OVER (ORDER BY `test`.`t1`.`a` ) AS `RANK() OVER (ORDER BY a)`,rank() OVER (ORDER BY `test`.`t1`.`a` ) AS `RANK() OVER (ORDER BY a)` from `test`.`t1` 6048But here the ORDER BY b was erroneously removed. 6049FLUSH STATUS; 6050SELECT a,b, RANK() OVER (ORDER BY a), RANK() OVER (ORDER BY b) FROM t1; 6051a b RANK() OVER (ORDER BY a) RANK() OVER (ORDER BY b) 60521 1 1 1 60533 1 5 1 60542 2 3 3 60552 2 3 3 60561 3 1 5 60573 3 5 5 6058SHOW STATUS LIKE 'Created_tmp_tables'; 6059Variable_name Value 6060Created_tmp_tables 1 6061EXPLAIN FORMAT=JSON SELECT a,b, RANK() OVER (ORDER BY a), RANK() OVER (ORDER BY b) FROM t1; 6062EXPLAIN 6063{ 6064 "query_block": { 6065 "select_id": 1, 6066 "cost_info": { 6067 "query_cost": "12.85" 6068 }, 6069 "windowing": { 6070 "windows": [ 6071 { 6072 "name": "<unnamed window>", 6073 "definition_position": 1, 6074 "using_temporary_table": true, 6075 "using_filesort": true, 6076 "filesort_key": [ 6077 "`a`" 6078 ], 6079 "functions": [ 6080 "rank" 6081 ] 6082 }, 6083 { 6084 "name": "<unnamed window>", 6085 "definition_position": 2, 6086 "last_executed_window": true, 6087 "using_filesort": true, 6088 "filesort_key": [ 6089 "`b`" 6090 ], 6091 "functions": [ 6092 "rank" 6093 ] 6094 } 6095 ], 6096 "cost_info": { 6097 "sort_cost": "12.00" 6098 }, 6099 "table": { 6100 "table_name": "t1", 6101 "access_type": "ALL", 6102 "rows_examined_per_scan": 6, 6103 "rows_produced_per_join": 6, 6104 "filtered": "100.00", 6105 "cost_info": { 6106 "read_cost": "0.25", 6107 "eval_cost": "0.60", 6108 "prefix_cost": "0.85", 6109 "data_read_per_join": "96" 6110 }, 6111 "used_columns": [ 6112 "a", 6113 "b" 6114 ] 6115 } 6116 } 6117 } 6118} 6119Warnings: 6120Note 1003 /* select#1 */ select `test`.`t1`.`a` AS `a`,`test`.`t1`.`b` AS `b`,rank() OVER (ORDER BY `test`.`t1`.`a` ) AS `RANK() OVER (ORDER BY a)`,rank() OVER (ORDER BY `test`.`t1`.`b` ) AS `RANK() OVER (ORDER BY b)` from `test`.`t1` 6121DROP TABLE t1; 6122 6123Test of SOME/ANY/ALL subqueries. 6124 6125CREATE TABLE t(a INT); 6126INSERT INTO t VALUES (1),(2),(3); 6127ANALYZE TABLE t; 6128Table Op Msg_type Msg_text 6129test.t analyze status OK 6130SELECT * FROM t WHERE a IN (SELECT ROW_NUMBER() OVER () FROM t); 6131a 61321 61332 61343 6135SELECT * FROM t WHERE a IN (SELECT ROW_NUMBER() OVER () + 1 FROM t); 6136a 61372 61383 6139EXPLAIN FORMAT=JSON SELECT * FROM t WHERE a IN (SELECT ROW_NUMBER() OVER () FROM t); 6140EXPLAIN 6141{ 6142 "query_block": { 6143 "select_id": 1, 6144 "cost_info": { 6145 "query_cost": "0.55" 6146 }, 6147 "table": { 6148 "table_name": "t", 6149 "access_type": "ALL", 6150 "rows_examined_per_scan": 3, 6151 "rows_produced_per_join": 3, 6152 "filtered": "100.00", 6153 "cost_info": { 6154 "read_cost": "0.25", 6155 "eval_cost": "0.30", 6156 "prefix_cost": "0.55", 6157 "data_read_per_join": "24" 6158 }, 6159 "used_columns": [ 6160 "a" 6161 ], 6162 "attached_condition": "<in_optimizer>(`test`.`t`.`a`,`test`.`t`.`a` in ( <materialize> (/* select#2 */ select row_number() OVER () from `test`.`t` having true ), <primary_index_lookup>(`test`.`t`.`a` in <temporary table> on <auto_distinct_key> where ((`test`.`t`.`a` = `<materialized_subquery>`.`ROW_NUMBER() OVER ()`)))))", 6163 "attached_subqueries": [ 6164 { 6165 "table": { 6166 "table_name": "<materialized_subquery>", 6167 "access_type": "eq_ref", 6168 "key": "<auto_key>", 6169 "key_length": "8", 6170 "rows_examined_per_scan": 1, 6171 "materialized_from_subquery": { 6172 "using_temporary_table": true, 6173 "dependent": true, 6174 "cacheable": false, 6175 "query_block": { 6176 "select_id": 2, 6177 "cost_info": { 6178 "query_cost": "0.55" 6179 }, 6180 "windowing": { 6181 "windows": [ 6182 { 6183 "name": "<unnamed window>", 6184 "functions": [ 6185 "row_number" 6186 ] 6187 } 6188 ], 6189 "table": { 6190 "table_name": "t", 6191 "access_type": "ALL", 6192 "rows_examined_per_scan": 3, 6193 "rows_produced_per_join": 3, 6194 "filtered": "100.00", 6195 "cost_info": { 6196 "read_cost": "0.25", 6197 "eval_cost": "0.30", 6198 "prefix_cost": "0.55", 6199 "data_read_per_join": "24" 6200 } 6201 } 6202 } 6203 } 6204 } 6205 } 6206 } 6207 ] 6208 } 6209 } 6210} 6211Warnings: 6212Note 1003 /* select#1 */ select `test`.`t`.`a` AS `a` from `test`.`t` where <in_optimizer>(`test`.`t`.`a`,`test`.`t`.`a` in ( <materialize> (/* select#2 */ select row_number() OVER () from `test`.`t` having true ), <primary_index_lookup>(`test`.`t`.`a` in <temporary table> on <auto_distinct_key> where ((`test`.`t`.`a` = `<materialized_subquery>`.`ROW_NUMBER() OVER ()`))))) 6213SELECT * FROM t WHERE a = SOME(SELECT ROW_NUMBER() OVER () + 1 FROM t); 6214a 62152 62163 6217SELECT * FROM t WHERE a = ANY(SELECT ROW_NUMBER() OVER () + 1 FROM t); 6218a 62192 62203 6221=ALL => NOT EXISTS (select non-equal rows) 6222SELECT * FROM t WHERE a = ALL(SELECT ROW_NUMBER() OVER () + 1 FROM t); 6223ERROR 42000: This version of MySQL doesn't yet support 'the combination of this ALL/ANY/SOME/IN subquery with this comparison operator and with contained window functions' 6224<>SOME => EXISTS (select non-equal rows) 6225SELECT * FROM t WHERE a <> SOME(SELECT ROW_NUMBER() OVER () + 1 FROM t); 6226ERROR 42000: This version of MySQL doesn't yet support 'the combination of this ALL/ANY/SOME/IN subquery with this comparison operator and with contained window functions' 6227SELECT * FROM t WHERE a <> ANY(SELECT ROW_NUMBER() OVER () + 1 FROM t); 6228ERROR 42000: This version of MySQL doesn't yet support 'the combination of this ALL/ANY/SOME/IN subquery with this comparison operator and with contained window functions' 6229SELECT * FROM t WHERE a <> ALL(SELECT ROW_NUMBER() OVER () + 1 FROM t); 6230a 62311 6232SELECT * FROM t WHERE a >= ALL(SELECT ROW_NUMBER() OVER () + 1 FROM t); 6233a 6234SELECT * FROM t WHERE a >= SOME(SELECT ROW_NUMBER() OVER () + 1 FROM t); 6235a 62362 62373 6238SELECT * from t WHERE EXISTS(SELECT ROW_NUMBER() OVER () FROM t); 6239a 62401 62412 62423 6243EXPLAIN FORMAT=JSON SELECT * from t WHERE EXISTS(SELECT ROW_NUMBER() OVER () FROM t); 6244EXPLAIN 6245{ 6246 "query_block": { 6247 "select_id": 1, 6248 "cost_info": { 6249 "query_cost": "0.55" 6250 }, 6251 "table": { 6252 "table_name": "t", 6253 "access_type": "ALL", 6254 "rows_examined_per_scan": 3, 6255 "rows_produced_per_join": 3, 6256 "filtered": "100.00", 6257 "cost_info": { 6258 "read_cost": "0.25", 6259 "eval_cost": "0.30", 6260 "prefix_cost": "0.55", 6261 "data_read_per_join": "24" 6262 }, 6263 "used_columns": [ 6264 "a" 6265 ] 6266 }, 6267 "optimized_away_subqueries": [ 6268 { 6269 "dependent": false, 6270 "cacheable": true, 6271 "query_block": { 6272 "select_id": 2, 6273 "cost_info": { 6274 "query_cost": "0.55" 6275 }, 6276 "windowing": { 6277 "windows": [ 6278 { 6279 "name": "<unnamed window>", 6280 "functions": [ 6281 "row_number" 6282 ] 6283 } 6284 ], 6285 "table": { 6286 "table_name": "t", 6287 "access_type": "ALL", 6288 "rows_examined_per_scan": 3, 6289 "rows_produced_per_join": 3, 6290 "filtered": "100.00", 6291 "cost_info": { 6292 "read_cost": "0.25", 6293 "eval_cost": "0.30", 6294 "prefix_cost": "0.55", 6295 "data_read_per_join": "24" 6296 } 6297 } 6298 } 6299 } 6300 } 6301 ] 6302 } 6303} 6304Warnings: 6305Note 1003 /* select#1 */ select `test`.`t`.`a` AS `a` from `test`.`t` where true 6306FLUSH STATUS; 6307SELECT * FROM t upper 6308WHERE EXISTS(SELECT rn FROM (SELECT ROW_NUMBER() OVER () AS rn FROM t) ta 6309WHERE rn > upper.a); 6310a 63111 63122 6313SHOW STATUS LIKE 'Created_tmp_tables'; 6314Variable_name Value 6315Created_tmp_tables 1 6316EXPLAIN FORMAT=JSON SELECT * FROM t upper 6317WHERE EXISTS(SELECT rn FROM (SELECT ROW_NUMBER() OVER () AS rn FROM t) ta 6318WHERE rn > upper.a); 6319EXPLAIN 6320{ 6321 "query_block": { 6322 "select_id": 1, 6323 "cost_info": { 6324 "query_cost": "3.99" 6325 }, 6326 "nested_loop": [ 6327 { 6328 "table": { 6329 "table_name": "upper", 6330 "access_type": "ALL", 6331 "rows_examined_per_scan": 3, 6332 "rows_produced_per_join": 3, 6333 "filtered": "100.00", 6334 "cost_info": { 6335 "read_cost": "0.25", 6336 "eval_cost": "0.30", 6337 "prefix_cost": "0.55", 6338 "data_read_per_join": "24" 6339 }, 6340 "used_columns": [ 6341 "a" 6342 ] 6343 } 6344 }, 6345 { 6346 "table": { 6347 "table_name": "ta", 6348 "access_type": "ALL", 6349 "rows_examined_per_scan": 3, 6350 "rows_produced_per_join": 3, 6351 "filtered": "33.33", 6352 "first_match": "upper", 6353 "using_join_buffer": "hash join", 6354 "cost_info": { 6355 "read_cost": "2.54", 6356 "eval_cost": "0.30", 6357 "prefix_cost": "3.99", 6358 "data_read_per_join": "48" 6359 }, 6360 "used_columns": [ 6361 "rn" 6362 ], 6363 "attached_condition": "(`ta`.`rn` > `test`.`upper`.`a`)", 6364 "materialized_from_subquery": { 6365 "using_temporary_table": true, 6366 "dependent": false, 6367 "cacheable": true, 6368 "query_block": { 6369 "select_id": 3, 6370 "cost_info": { 6371 "query_cost": "0.55" 6372 }, 6373 "windowing": { 6374 "windows": [ 6375 { 6376 "name": "<unnamed window>", 6377 "functions": [ 6378 "row_number" 6379 ] 6380 } 6381 ], 6382 "table": { 6383 "table_name": "t", 6384 "access_type": "ALL", 6385 "rows_examined_per_scan": 3, 6386 "rows_produced_per_join": 3, 6387 "filtered": "100.00", 6388 "cost_info": { 6389 "read_cost": "0.25", 6390 "eval_cost": "0.30", 6391 "prefix_cost": "0.55", 6392 "data_read_per_join": "24" 6393 } 6394 } 6395 } 6396 } 6397 } 6398 } 6399 } 6400 ] 6401 } 6402} 6403Warnings: 6404Note 1276 Field or reference 'test.upper.a' of SELECT #2 was resolved in SELECT #1 6405Note 1003 /* select#1 */ select `test`.`upper`.`a` AS `a` from `test`.`t` `upper` semi join ((/* select#3 */ select row_number() OVER () AS `rn` from `test`.`t`) `ta`) where (`ta`.`rn` > `test`.`upper`.`a`) 6406# Correlated => no subquery materialization. 6407# IN => EXISTS, error 6408SELECT * FROM t upper 6409WHERE upper.a IN (SELECT ROW_NUMBER() OVER () FROM t 6410WHERE t.a > upper.a); 6411ERROR 42000: This version of MySQL doesn't yet support 'the combination of this ALL/ANY/SOME/IN subquery with this comparison operator and with contained window functions' 6412# Correlated => no subquery materialization. 6413# >ANY => EXISTS(select smaller rows), error 6414SELECT * FROM t upper 6415WHERE upper.a > ANY (SELECT ROW_NUMBER() OVER () FROM t 6416WHERE t.a > upper.a); 6417ERROR 42000: This version of MySQL doesn't yet support 'the combination of this ALL/ANY/SOME/IN subquery with this comparison operator and with contained window functions' 6418SELECT * FROM t upper 6419WHERE upper.a > ALL (SELECT ROW_NUMBER() OVER () FROM t 6420WHERE t.a > upper.a); 6421ERROR 42000: This version of MySQL doesn't yet support 'the combination of this ALL/ANY/SOME/IN subquery with this comparison operator and with contained window functions' 6422DROP TABLE t; 6423# 6424# Bug#25601138 WINDOW FUNCTION IN CORRELATED SUBQUERY GIVES WRONG RESULT 6425# 6426CREATE TABLE t1(i INT, j INT, k INT); 6427INSERT INTO t1 VALUES (1,1,1),(2,2,2); 6428# When we support this, expected result is 1,1,1 and 2,2,2 6429select * from t1 AS upper where i+1 6430IN (select row_number() over () + upper.i from t1 ); 6431ERROR 42000: This version of MySQL doesn't yet support 'the combination of this ALL/ANY/SOME/IN subquery with this comparison operator and with contained window functions' 6432DROP TABLE t1; 6433# 6434# Check window aggregate referencing columns from an outside query 6435# All results checked against Postgresql which also allows this. 6436CREATE TABLE t1(i INT, j INT, k INT); 6437INSERT INTO t1 VALUES (1,1,1),(2,2,2); 6438SELECT (SELECT ROW_NUMBER() OVER (ORDER BY upper.j) FROM t1 LIMIT 1) 6439FROM t1 AS upper; 6440(SELECT ROW_NUMBER() OVER (ORDER BY upper.j) FROM t1 LIMIT 1) 64411 64421 6443SELECT (SELECT ROW_NUMBER() OVER (PARTITION BY upper.j) FROM t1 LIMIT 1) 6444FROM t1 AS upper; 6445(SELECT ROW_NUMBER() OVER (PARTITION BY upper.j) FROM t1 LIMIT 1) 64461 64471 6448SELECT (SELECT ROW_NUMBER() OVER (PARTITION BY upper.j) 6449FROM t1 LIMIT 1 OFFSET 1) 6450FROM t1 AS upper; 6451(SELECT ROW_NUMBER() OVER (PARTITION BY upper.j) 6452FROM t1 LIMIT 1 OFFSET 1) 64532 64542 6455SELECT (SELECT FIRST_VALUE(j) OVER (ORDER BY upper.j) FROM tAccepted LIMIT Accepted) 6456FROM tAccepted AS upper; 6457(SELECT FIRST_VALUE(j) OVER (ORDER BY upper.j) FROM tAccepted LIMIT Accepted) 6458Accepted 6459Accepted 6460SELECT (SELECT FIRST_VALUE(j) OVER (ORDER BY upper.j DESC) FROM tAccepted LIMIT Accepted) 6461FROM tAccepted AS upper; 6462(SELECT FIRST_VALUE(j) OVER (ORDER BY upper.j DESC) FROM tAccepted LIMIT Accepted) 6463Accepted 6464Accepted 6465SELECT (SELECT FIRST_VALUE(upper.j) OVER (ORDER BY upper.j DESC) 6466FROM t1 LIMIT 1) 6467FROM t1 AS upper; 6468(SELECT FIRST_VALUE(upper.j) OVER (ORDER BY upper.j DESC) 6469FROM t1 LIMIT 1) 64701 64712 6472SELECT (SELECT LAST_VALUE(j) OVER (ORDER BY upper.j) FROM tAccepted LIMIT Accepted) 6473FROM tAccepted AS upper; 6474(SELECT LAST_VALUE(j) OVER (ORDER BY upper.j) FROM tAccepted LIMIT Accepted) 6475Accepted 6476Accepted 6477SELECT (SELECT LAST_VALUE(j) OVER (ORDER BY upper.j DESC) FROM tAccepted LIMIT Accepted) 6478FROM tAccepted AS upper; 6479(SELECT LAST_VALUE(j) OVER (ORDER BY upper.j DESC) FROM tAccepted LIMIT Accepted) 6480Accepted 6481Accepted 6482SELECT * FROM t1 AS upper 6483WHERE (SELECT FIRST_VALUE(upper.j) OVER (ORDER BY upper.j) 6484FROM t1 LIMIT 1) = 1 6485; 6486i j k 64871 1 1 6488SELECT * FROM t1 AS upper 6489WHERE (SELECT FIRST_VALUE(upper.j) OVER (ORDER BY upper.j) 6490FROM t1 LIMIT 1) = 2; 6491i j k 64922 2 2 6493SELECT FIRST_VALUE(j) OVER (ORDER BY 0 + 6494(SELECT FIRST_VALUE(upper.j) OVER (ORDER BY upper.j) 6495FROM t1 LIMIT 1)) 6496FROM t1 AS upper; 6497FIRST_VALUE(j) OVER (ORDER BY 0 + 6498(SELECT FIRST_VALUE(upper.j) OVER (ORDER BY upper.j) 6499FROM t1 LIMIT 1)) 65001 65011 6502SELECT LAST_VALUE(j) OVER (ORDER BY 0 + 6503(SELECT FIRST_VALUE(upper.j) OVER (ORDER BY upper.j) 6504FROM t1 LIMIT 1)) 6505FROM t1 AS upper; 6506LAST_VALUE(j) OVER (ORDER BY 0 + 6507(SELECT FIRST_VALUE(upper.j) OVER (ORDER BY upper.j) 6508FROM t1 LIMIT 1)) 65091 65102 6511SELECT * FROM t1 AS upper ORDER BY (0 + (SELECT FIRST_VALUE(upper.j) OVER (ORDER BY upper.j) 6512FROM t1 LIMIT 1)); 6513i j k 65141 1 1 65152 2 2 6516SELECT LAST_VALUE((SELECT upper.j FROM t1 LIMIT 1)) OVER (ORDER BY i) 6517FROM t1 AS upper; 6518LAST_VALUE((SELECT upper.j FROM t1 LIMIT 1)) OVER (ORDER BY i) 65191 65202 6521SELECT FIRST_VALUE((SELECT upper.j FROM t1 LIMIT 1)) OVER (ORDER BY i) 6522FROM t1 AS upper; 6523FIRST_VALUE((SELECT upper.j FROM t1 LIMIT 1)) OVER (ORDER BY i) 65241 65251 6526SELECT SUM(j + (SELECT upper.j FROM t1 LIMIT 1)) OVER (ORDER BY i) 6527FROM t1 AS upper; 6528SUM(j + (SELECT upper.j FROM t1 LIMIT 1)) OVER (ORDER BY i) 65292 65306 6531SELECT LAST_VALUE(1 IN (SELECT upper.j FROM t1)) OVER (ORDER BY i) 6532FROM t1 AS upper; 6533LAST_VALUE(1 IN (SELECT upper.j FROM t1)) OVER (ORDER BY i) 65341 65350 6536SELECT LAST_VALUE(upper.j IN (SELECT 2 FROM t1)) OVER (ORDER BY i) 6537FROM t1 AS upper; 6538LAST_VALUE(upper.j IN (SELECT 2 FROM t1)) OVER (ORDER BY i) 65390 65401 6541DROP TABLE t1; 6542 6543Example of usefulness of expression in partition clause 6544 6545CREATE TABLE t(i INT, c VARCHAR(20)); 6546INSERT INTO t VALUES (1, 'abra'),(2, 'akaba'),(3, 'bravo'),(4, 'beg'); 6547SELECT i, SUBSTR(c,1,2), SUM(i) OVER (PARTITION BY SUBSTR(c,1,2)) `sum` FROM t; 6548i SUBSTR(c,1,2) sum 65491 ab 1 65502 ak 2 65514 be 4 65523 br 3 6553select I, substr(c,1,2), SUM(i) OVER (PARTITION BY SUBSTR(c,1,1)) `sum` FROM t; 6554I substr(c,1,2) sum 65551 ab 3 65562 ak 3 65573 br 7 65584 be 7 6559DROP TABLE t; 6560 6561Example of usefulness of grouped aggregate in partition clause 6562 6563CREATE TABLE t1( i INT, j INT); 6564INSERT INTO t1 VALUES (1,1), 6565(2,1), 6566(3,2), 6567(4,2), 6568(1,3), 6569(2,3), 6570(3,4), 6571(4,4); 6572SELECT j AS Having_same_sum_of_i, SUM(i), SUM(SUM(i)) OVER (PARTITION BY SUM(i)) AS sum_sum 6573FROM t1 GROUP BY j; 6574Having_same_sum_of_i SUM(i) sum_sum 65751 3 6 65763 3 6 65772 7 14 65784 7 14 6579DROP TABLE t1; 6580CREATE TABLE t1(a INT, b INT); 6581CREATE TABLE t2(c INT, d INT); 6582INSERT INTO t1 VALUES(1,1),(2,2); 6583SELECT ROW_NUMBER() OVER (), c 6584FROM t1 LEFT JOIN t2 ON a = c GROUP BY c; 6585ROW_NUMBER() OVER () c 65861 NULL 6587SELECT ROW_NUMBER() OVER (), 'c' 6588FROM t1 LEFT JOIN t2 ON a = c GROUP BY 'c'; 6589ROW_NUMBER() OVER () c 65901 c 6591DROP TABLE t1,t2; 6592 6593Bug#25461670 CRASHES OR WRONG RESULTS WITH WINDOW FUNCTIONS IN SUBQUERY 6594 6595CREATE TABLE t(a INT, b INT); 6596INSERT INTO t VALUES (5,6), (1,7); 6597SELECT (SELECT SUM(a) OVER ()) FROM t; 6598(SELECT SUM(a) OVER ()) 65995 66001 6601SELECT (SELECT SUM(a) OVER () FROM t LIMIT 1) FROM t; 6602(SELECT SUM(a) OVER () FROM t LIMIT 1) 66036 66046 6605CREATE TABLE t2(i INT); 6606INSERT INTO t2 VALUES (10),(100); 6607SELECT (SELECT SUM(a) OVER () FROM t2 LIMIT 1) FROM t; 6608(SELECT SUM(a) OVER () FROM t2 LIMIT 1) 660910 66102 6611DROP TABLE t, t2; 6612CREATE TABLE t(a INT, b INT); 6613INSERT INTO t VALUES (1,2), (4,5); 6614Check that a grouped aggregate argument doesn't directly contain a window 6615function. 6616SELECT AVG(SUM(a) OVER ()) FROM t; 6617ERROR HY000: You cannot use the window function 'sum' in this context.' 6618Check that a grouped aggregate argument doesn't directly contain a window 6619function by alias. 6620SELECT SUM(a) OVER () AS c, (SELECT SUM(c)) FROM t; 6621ERROR HY000: You cannot use the alias 'c' of an expression containing a window function in this context.' 6622At same query level, alias isn't even visible; the visibility 6623of alias in subquery is a MySQL extension 6624SELECT SUM(a) OVER () AS c, SUM(c) FROM t; 6625ERROR 42S22: Unknown column 'c' in 'field list' 6626but ok in final ORDER BY 6627SELECT SUM(a) OVER () AS c FROM t ORDER BY c; 6628c 66295 66305 6631One more nesting level makes the wf legal inside a grouped aggregate 6632argument. 6633SELECT AVG(a+(SELECT SUM(a) OVER () FROM t LIMIT 1)) FROM t; 6634AVG(a+(SELECT SUM(a) OVER () FROM t LIMIT 1)) 66357.5000 6636But not if it's a window function alias from an outer level 6637SELECT SUM(a) OVER () AS c, (SELECT SUM(1 + (SELECT c FROM DUAL))) FROM t; 6638ERROR HY000: You cannot use the alias 'c' of an expression containing a window function in this context.' 6639SELECT 1+SUM(a) OVER () AS c, (SELECT SUM(1 + (SELECT c FROM DUAL))) FROM t; 6640ERROR HY000: You cannot use the alias 'c' of an expression containing a window function in this context.' 6641This is illegal even if not referenced from a grouped aggregate since 6642logically all windowing happens later. 6643SELECT SUM(a) OVER () AS c, (SELECT 1 + (SELECT c FROM DUAL)) FROM t; 6644ERROR HY000: You cannot use the alias 'c' of an expression containing a window function in this context.' 6645SELECT 1+SUM(a) OVER () AS c, (SELECT 1 + (SELECT c FROM DUAL)) FROM t; 6646ERROR HY000: You cannot use the alias 'c' of an expression containing a window function in this context.' 6647DROP TABLE t; 6648CREATE TABLE t(a INT, b INT, c INT); 6649INSERT INTO t VALUES (1,1,1),(1,2,3),(2,1,2),(2,2,3); 6650SELECT RANK() OVER (PARTITION BY c ORDER BY c) FROM t GROUP BY a,b; 6651ERROR 42000: Expression #1 of PARTITION BY or ORDER BY clause of window '<unnamed window>' is not in GROUP BY clause and contains nonaggregated column 'test.t.c' which is not functionally dependent on columns in GROUP BY clause; this is incompatible with sql_mode=only_full_group_by 6652SELECT RANK() OVER (PARTITION BY c ORDER BY c) FROM t; 6653RANK() OVER (PARTITION BY c ORDER BY c) 66541 66551 66561 66571 6658SELECT RANK() OVER (PARTITION BY a ORDER BY b) FROM t GROUP BY a,b; 6659RANK() OVER (PARTITION BY a ORDER BY b) 66601 66612 66621 66632 6664SELECT RANK() OVER (PARTITION BY a ORDER BY b) FROM t; 6665RANK() OVER (PARTITION BY a ORDER BY b) 66661 66672 66681 66692 6670SELECT RANK() OVER (PARTITION BY (a+b) ORDER BY (b+a)) FROM t GROUP BY a,b; 6671RANK() OVER (PARTITION BY (a+b) ORDER BY (b+a)) 66721 66731 66741 66751 6676SELECT AVG(a), RANK() OVER (ORDER BY a) FROM t; 6677ERROR 42000: In aggregated query without GROUP BY, expression #1 of PARTITION BY or ORDER BY clause of window '<unnamed window>' contains nonaggregated column 'test.t.a'; this is incompatible with sql_mode=only_full_group_by 6678SELECT AVG(a), SUM(AVG(a)) OVER (PARTITION BY a) FROM t; 6679ERROR 42000: In aggregated query without GROUP BY, expression #1 of PARTITION BY or ORDER BY clause of window '<unnamed window>' contains nonaggregated column 'test.t.a'; this is incompatible with sql_mode=only_full_group_by 6680SELECT AVG(a), SUM(a) OVER () FROM t; 6681ERROR 42000: In aggregated query without GROUP BY, expression #2 of SELECT list contains nonaggregated column 'test.t.a'; this is incompatible with sql_mode=only_full_group_by 6682DROP TABLE t; 6683 6684Bug#25643783 EXPRESSION INVOLVING WF WITH GROUPED AGGREGATE ARG GIVES WRONG VALUE 6685 6686CREATE TABLE t1(i INT, j INT, k INT); 6687INSERT INTO t1 VALUES (1,1,1),(2,2,2),(3,3,3),(4,4,4); 6688SELECT i, SUM(i), 1+SUM(i), SUM(SUM(i)) OVER w, 1+SUM(SUM(i)) OVER w FROM t1 6689GROUP BY i WINDOW w AS (PARTITION BY i); 6690i SUM(i) 1+SUM(i) SUM(SUM(i)) OVER w 1+SUM(SUM(i)) OVER w 66911 1 2 1 2 66922 2 3 2 3 66933 3 4 3 4 66944 4 5 4 5 6695DROP TABLE t1; 6696 6697Bug with missing update of cached example after split_sum_func 6698for FIRST_VALUE, LAST_VALUE 6699 6700CREATE TABLE t(a INT, b INT, c INT, d INT); 6701INSERT INTO t VALUES (1,1,1,1), (2,2,4,2), (3,3,9,3); 6702SELECT SUM(c/d), LAST_VALUE(SUM(c/d)) OVER (ORDER BY a) FROM t GROUP BY a,b; 6703SUM(c/d) LAST_VALUE(SUM(c/d)) OVER (ORDER BY a) 67041.0000 1.0000 67052.0000 2.0000 67063.0000 3.0000 6707SELECT LAST_VALUE(SUM(c/d)) OVER (ORDER BY a) FROM t GROUP BY a,b; 6708LAST_VALUE(SUM(c/d)) OVER (ORDER BY a) 67091.0000 67102.0000 67113.0000 6712SELECT 1+FIRST_VALUE(SUM(c/d)) OVER (ORDER BY a) FROM t GROUP BY a,b; 67131+FIRST_VALUE(SUM(c/d)) OVER (ORDER BY a) 67142.0000 67152.0000 67162.0000 6717SELECT ROW_NUMBER() OVER () rn, 67181+FIRST_VALUE(SUM(c/d)) OVER (ORDER BY a) plus_fv, 67191+LAST_VALUE(SUM(c/d)) OVER (ORDER BY a) plus_lv FROM t GROUP BY a,b; 6720rn plus_fv plus_lv 67211 2.0000 2.0000 67222 2.0000 3.0000 67233 2.0000 4.0000 6724DROP TABLE t; 6725 6726Bug 25724779 6727 6728CREATE TABLE t1(a INT, b INT); 6729INSERT INTO t1 VALUES (1,2),(3,4); 6730SELECT a, SUM(b) FROM t1 GROUP BY a; 6731a SUM(b) 67321 2 67333 4 6734SELECT a, SUM(b) FROM t1 GROUP BY a HAVING a=1; 6735a SUM(b) 67361 2 6737SELECT a, SUM(SUM(b)) OVER () FROM t1 GROUP BY a HAVING a=1; 6738a SUM(SUM(b)) OVER () 67391 2 6740DROP TABLE t1; 6741 6742Bug#25365929 PROBLEM TRYING TO ADD GCOL USING A 6743WINDOW FUNCTION 6744CREATE TABLE t (a INT , b INT as (ROW_NUMBER() OVER (ORDER BY a))); 6745ERROR HY000: You cannot use the window function 'row_number' in this context.' 6746CREATE TABLE t (a INT DEFAULT NULL); 6747INSERT INTO t VALUES (1),(2),(3),(4),(5); 6748ALTER TABLE t ADD b INT AS (ROW_NUMBER() OVER (ORDER BY a)); 6749ERROR HY000: You cannot use the window function 'row_number' in this context.' 6750DROP TABLE t; 6751CREATE TABLE t (a INT DEFAULT NULL); 6752ALTER TABLE t ADD COLUMN b INT as (ROW_NUMBER() OVER (ORDER BY a)); 6753ERROR HY000: You cannot use the window function 'row_number' in this context.' 6754DROP TABLE t; 6755 6756Bug exposed by PERCENT_RANK() and CUME_DIST() in non-optimized mode 6757(default when we add a DOUBLE SUM to the mix) 6758 6759CREATE TABLE t1 (id INTEGER, sex CHAR(1), d DOUBLE); 6760INSERT INTO t1 VALUES (1, 'M', 1.0); 6761INSERT INTO t1 VALUES (2, 'F', 2.0); 6762INSERT INTO t1 VALUES (3, 'F', 3.0); 6763INSERT INTO t1 VALUES (4, 'F', 4.0); 6764INSERT INTO t1 VALUES (5, 'M', 5.0); 6765INSERT INTO t1 VALUES (10, NULL, 10.0); 6766INSERT INTO t1 VALUES (11, NULL, 11.0); 6767SELECT sex, id, PERCENT_RANK() OVER w, CUME_DIST() OVER w, SUM(d) OVER w 6768FROM t1 WINDOW w AS (PARTITION BY sex ORDER BY id DESC ROWS 6769BETWEEN 1 PRECEDING AND CURRENT ROW); 6770sex id PERCENT_RANK() OVER w CUME_DIST() OVER w SUM(d) OVER w 6771NULL 11 0 0.5 11 6772NULL 10 1 1 21 6773F 4 0 0.3333333333333333 4 6774F 3 0.5 0.6666666666666666 7 6775F 2 1 1 5 6776M 5 0 0.5 5 6777M 1 1 1 6 6778DROP TABLE t1; 6779 6780Bug#25478832 6781 6782SET @savmode=@@SESSION.SQL_MODE; 6783SET SESSION SQL_MODE='TRADITIONAL'; 6784CREATE TABLE t(a int); 6785INSERT INTO t VALUES (1),(2); 6786SELECT COUNT(*), ROW_NUMBER() OVER (ORDER BY a) AS rownum 6787FROM t ORDER BY rownum; 6788COUNT(*) rownum 67892 1 6790SET SESSION SQL_MODE=@savmode; 6791DROP TABLE t; 6792follow-up bug repro 6793CREATE TABLE t1 (id INTEGER, sex CHAR(1)); 6794PREPARE p from 'SELECT sex, PERCENT_RANK() OVER (ORDER BY AVG(id) DESC) 6795 FROM t1 GROUP BY sex ORDER BY sex'; 6796EXECUTE p; 6797sex PERCENT_RANK() OVER (ORDER BY AVG(id) DESC) 6798DROP TABLE t1; 6799 6800Bug#25819164 6801 6802CREATE TABLE t1 (a INT PRIMARY KEY); 6803CREATE TABLE t2 LIKE t1; 6804INSERT INTO t1 VALUES(1); 6805ANALYZE TABLE t1,t2; 6806Table Op Msg_type Msg_text 6807test.t1 analyze status OK 6808test.t2 analyze status OK 6809SELECT ROW_NUMBER() OVER () 6810FROM t1 AS alias1, t2 AS alias2 6811WHERE alias1.a = 1 6812ORDER BY alias2.a; 6813ROW_NUMBER() OVER () 6814query's ORDER BY with index order without filesort 6815EXPLAIN SELECT ROW_NUMBER() OVER () 6816FROM t1 AS alias1, t2 AS alias2 6817WHERE alias1.a = 1 6818ORDER BY alias2.a; 6819id select_type table partitions type possible_keys key key_len ref rows filtered Extra 68201 SIMPLE alias1 NULL const PRIMARY PRIMARY 4 const 1 100.00 Using index 68211 SIMPLE alias2 NULL index NULL PRIMARY 4 NULL 1 100.00 Using index 6822Warnings: 6823Note 3598 To get information about window functions use EXPLAIN FORMAT=JSON 6824Note 1003 /* select#1 */ select row_number() OVER () AS `ROW_NUMBER() OVER ()` from `test`.`t1` `alias1` join `test`.`t2` `alias2` where true order by `test`.`alias2`.`a` 6825Add more rows 6826INSERT INTO t1 VALUES (2); 6827INSERT INTO t2 VALUES (1),(2); 6828ANALYZE TABLE t1,t2; 6829Table Op Msg_type Msg_text 6830test.t1 analyze status OK 6831test.t2 analyze status OK 6832SELECT alias1.a, alias2.a as ord_key_asc, 6833ROW_NUMBER() OVER () 6834FROM t1 AS alias1, t2 AS alias2 6835WHERE alias1.a = 1 6836ORDER BY alias2.a; 6837a ord_key_asc ROW_NUMBER() OVER () 68381 1 1 68391 2 2 6840query's ORDER BY with index order without filesort 6841EXPLAIN SELECT alias1.a, alias2.a as ord_key_asc, 6842ROW_NUMBER() OVER () 6843FROM t1 AS alias1, t2 AS alias2 6844WHERE alias1.a = 1 6845ORDER BY alias2.a; 6846id select_type table partitions type possible_keys key key_len ref rows filtered Extra 68471 SIMPLE alias1 NULL const PRIMARY PRIMARY 4 const 1 100.00 Using index 68481 SIMPLE alias2 NULL index NULL PRIMARY 4 NULL 2 100.00 Using index 6849Warnings: 6850Note 3598 To get information about window functions use EXPLAIN FORMAT=JSON 6851Note 1003 /* select#1 */ select '1' AS `a`,`test`.`alias2`.`a` AS `ord_key_asc`,row_number() OVER () AS `ROW_NUMBER() OVER ()` from `test`.`t1` `alias1` join `test`.`t2` `alias2` where true order by `test`.`alias2`.`a` 6852SELECT alias1.a, alias2.a as ord_key_asc, 6853ROW_NUMBER() OVER (ORDER BY -alias2.a) 6854FROM t1 AS alias1, t2 AS alias2 6855WHERE alias1.a = 1 6856ORDER BY alias2.a; 6857a ord_key_asc ROW_NUMBER() OVER (ORDER BY -alias2.a) 68581 1 2 68591 2 1 6860query's ORDER BY with filesort as WF has ORDER BY which shuffles rows 6861EXPLAIN SELECT alias1.a, alias2.a as ord_key_asc, 6862ROW_NUMBER() OVER (ORDER BY -alias2.a) 6863FROM t1 AS alias1, t2 AS alias2 6864WHERE alias1.a = 1 6865ORDER BY alias2.a; 6866id select_type table partitions type possible_keys key key_len ref rows filtered Extra 68671 SIMPLE alias1 NULL const PRIMARY PRIMARY 4 const 1 100.00 Using index; Using filesort 68681 SIMPLE alias2 NULL index NULL PRIMARY 4 NULL 2 100.00 Using index 6869Warnings: 6870Note 3598 To get information about window functions use EXPLAIN FORMAT=JSON 6871Note 1003 /* select#1 */ select '1' AS `a`,`test`.`alias2`.`a` AS `ord_key_asc`,row_number() OVER (ORDER BY -(`test`.`alias2`.`a`) ) AS `ROW_NUMBER() OVER (ORDER BY -alias2.a)` from `test`.`t1` `alias1` join `test`.`t2` `alias2` where true order by `test`.`alias2`.`a` 6872DROP TABLE t1, t2; 6873 6874Bug#25472253 6875 6876CREATE TABLE t ( 6877col_date date 6878); 6879INSERT INTO t VALUES ('2017-01-10'), ('2017-01-18'); 6880SELECT 6881MIN(alias1.col_date) OVER () FV, 6882DENSE_RANK() OVER (ORDER BY alias2.col_date 6883RANGE UNBOUNDED PRECEDING) DR, 6884RANK() OVER (ORDER BY alias1.col_date 6885RANGE BETWEEN INTERVAL 1 WEEK PRECEDING AND CURRENT ROW) R 6886FROM (t AS alias1, t AS alias2); 6887FV DR R 68882017-01-10 1 1 68892017-01-10 1 3 68902017-01-10 2 1 68912017-01-10 2 3 6892DROP TABLE t; 6893 6894Bug#25819199 6895 6896CREATE TABLE t (a INT PRIMARY KEY, b INT); 6897INSERT INTO t VALUES(1, 1); 6898SELECT NTILE (3) OVER (ORDER BY alias1.a), AVG(alias1.a) OVER () 6899FROM t AS alias1 RIGHT JOIN t AS alias2 ON (alias1.a = alias2.b); 6900NTILE (3) OVER (ORDER BY alias1.a) AVG(alias1.a) OVER () 69011 1.0000 6902CREATE TABLE t1(a INT); 6903INSERT INTO t1 VALUES(1),(1),(2),(2); 6904CREATE TABLE t2(a INT NOT NULL); 6905INSERT INTO t2 VALUES(3); 6906SELECT t1.a, t2.a FROM t1 LEFT JOIN t2 ON 0; 6907a a 69081 NULL 69091 NULL 69102 NULL 69112 NULL 6912SELECT t1.a, FIRST_VALUE(t2.a) OVER () FROM t1 LEFT JOIN t2 ON 0; 6913a FIRST_VALUE(t2.a) OVER () 69141 NULL 69151 NULL 69162 NULL 69172 NULL 6918DROP TABLE t, t1, t2; 6919 6920Bug#25551456 6921 6922CREATE TABLE t1 (id INT); 6923INSERT INTO t1 VALUES (1), (2), (3), (2); 6924SELECT SUM(MAX(id)) OVER (ORDER BY MAX(id)) FROM t1; 6925SUM(MAX(id)) OVER (ORDER BY MAX(id)) 69263 6927CREATE INDEX idx ON t1(id); 6928Used to be NULL rather than 3: 6929SELECT SUM(MAX(id)) OVER (ORDER BY MAX(id)) AS ss FROM t1; 6930ss 69313 6932DROP TABLE t1; 6933 6934Bug#25835846 6935 6936CREATE TABLE t(a INT PRIMARY KEY); 6937INSERT INTO t VALUES (1); 6938SELECT NTILE(2) OVER (ORDER BY a) FROM t WHERE a = 1; 6939NTILE(2) OVER (ORDER BY a) 69401 6941SELECT CUME_DIST() OVER (ORDER BY a) FROM t WHERE a = 1; 6942CUME_DIST() OVER (ORDER BY a) 69431 6944DROP TABLE t; 6945 6946Bug#25835149 6947 6948SET @savmode=@@SESSION.SQL_MODE; 6949SET SESSION SQL_MODE=''; 6950CREATE TABLE `test`( 6951`pk` INT(11) NOT NULL AUTO_INCREMENT PRIMARY KEY, 6952`dt` DATETIME DEFAULT NULL, 6953`ge` GEOMETRY DEFAULT NULL 6954); 6955Warnings: 6956Warning 1681 Integer display width is deprecated and will be removed in a future release. 6957INSERT INTO `test` VALUES 6958(1, '2007-05-08 12:10:55', ST_GeomFromText('POINT(1 1)')), 6959(2, NULL, NULL), (3, NULL, NULL), 6960(4, '2001-01-18 00:00:00', ST_GeomFromText('POINT(4 4)')), 6961(5, '2009-11-24 00:00:00', ST_GeomFromText('POINT(5 5)')), 6962(6, '2001-11-22 21:41:15', ST_GeomFromText('POINT(6 6)')), 6963(7, NULL, NULL), 6964(8, '0000-00-00 00:00:00', ST_GeomFromText('POINT(8 8)')), 6965(9, '2003-05-13 18:03:04', ST_GeomFromText('POINT(9 9)')), 6966(10, '2008-04-15 09:44:20', ST_GeomFromText('POINT(10 10)')), 6967(11, '2009-07-15 00:00:00', ST_GeomFromText('POINT(11 11)')), 6968(12, '2007-04-27 13:53:37', ST_GeomFromText('POINT(12 12)')), 6969(13, '0000-00-00 00:00:00', ST_GeomFromText('POINT(13 13)')), 6970(14, '2000-02-02 02:15:28', ST_GeomFromText('POINT(14 14)')), 6971(15, '2004-06-06 00:00:00', ST_GeomFromText('POINT(15 15)')), 6972(16, NULL, NULL), 6973(17, '2002-06-21 00:00:00', ST_GeomFromText('POINT(17 17)')), 6974(18, '2007-03-23 00:00:00', ST_GeomFromText('POINT(18 18)')), 6975(19, '2006-10-06 00:00:00', ST_GeomFromText('POINT(19 19)')), 6976(20, '2008-07-07 00:00:00', ST_GeomFromText('POINT(20 20)')); 6977SELECT dt, FIRST_VALUE(dt) OVER w1 fv, 6978CAST(FIRST_VALUE(ge) OVER w1 AS JSON) ge FROM test WHERE `pk` = 2 WINDOW w1 AS (); 6979dt fv ge 6980NULL NULL NULL 6981SELECT dt, FIRST_VALUE(dt) OVER w1, 6982CAST(FIRST_VALUE(ge) OVER w1 AS JSON) ge FROM test WHERE `pk` > 3 WINDOW w1 AS (); 6983dt FIRST_VALUE(dt) OVER w1 ge 69842001-01-18 00:00:00 2001-01-18 00:00:00 {"type": "Point", "coordinates": [4.0, 4.0]} 69852009-11-24 00:00:00 2001-01-18 00:00:00 {"type": "Point", "coordinates": [4.0, 4.0]} 69862001-11-22 21:41:15 2001-01-18 00:00:00 {"type": "Point", "coordinates": [4.0, 4.0]} 6987NULL 2001-01-18 00:00:00 {"type": "Point", "coordinates": [4.0, 4.0]} 69880000-00-00 00:00:00 2001-01-18 00:00:00 {"type": "Point", "coordinates": [4.0, 4.0]} 69892003-05-13 18:03:04 2001-01-18 00:00:00 {"type": "Point", "coordinates": [4.0, 4.0]} 69902008-04-15 09:44:20 2001-01-18 00:00:00 {"type": "Point", "coordinates": [4.0, 4.0]} 69912009-07-15 00:00:00 2001-01-18 00:00:00 {"type": "Point", "coordinates": [4.0, 4.0]} 69922007-04-27 13:53:37 2001-01-18 00:00:00 {"type": "Point", "coordinates": [4.0, 4.0]} 69930000-00-00 00:00:00 2001-01-18 00:00:00 {"type": "Point", "coordinates": [4.0, 4.0]} 69942000-02-02 02:15:28 2001-01-18 00:00:00 {"type": "Point", "coordinates": [4.0, 4.0]} 69952004-06-06 00:00:00 2001-01-18 00:00:00 {"type": "Point", "coordinates": [4.0, 4.0]} 6996NULL 2001-01-18 00:00:00 {"type": "Point", "coordinates": [4.0, 4.0]} 69972002-06-21 00:00:00 2001-01-18 00:00:00 {"type": "Point", "coordinates": [4.0, 4.0]} 69982007-03-23 00:00:00 2001-01-18 00:00:00 {"type": "Point", "coordinates": [4.0, 4.0]} 69992006-10-06 00:00:00 2001-01-18 00:00:00 {"type": "Point", "coordinates": [4.0, 4.0]} 70002008-07-07 00:00:00 2001-01-18 00:00:00 {"type": "Point", "coordinates": [4.0, 4.0]} 7001SELECT dt, FIRST_VALUE(CAST(dt AS TIME)) OVER w1 FROM test WHERE `pk` > 3 WINDOW w1 AS (); 7002dt FIRST_VALUE(CAST(dt AS TIME)) OVER w1 70032001-01-18 00:00:00 00:00:00 70042009-11-24 00:00:00 00:00:00 70052001-11-22 21:41:15 00:00:00 7006NULL 00:00:00 70070000-00-00 00:00:00 00:00:00 70082003-05-13 18:03:04 00:00:00 70092008-04-15 09:44:20 00:00:00 70102009-07-15 00:00:00 00:00:00 70112007-04-27 13:53:37 00:00:00 70120000-00-00 00:00:00 00:00:00 70132000-02-02 02:15:28 00:00:00 70142004-06-06 00:00:00 00:00:00 7015NULL 00:00:00 70162002-06-21 00:00:00 00:00:00 70172007-03-23 00:00:00 00:00:00 70182006-10-06 00:00:00 00:00:00 70192008-07-07 00:00:00 00:00:00 7020SET SESSION SQL_MODE=@savmode; 7021DROP TABLE `test`; 7022 7023Bug#25835329 7024 7025CREATE TABLE t(a int PRIMARY KEY, b date); 7026INSERT INTO t VALUES (1, '1979-01-01'); 7027SELECT STRAIGHT_JOIN CUME_DIST() OVER (ORDER BY alias1.a) 7028FROM t AS alias1 RIGHT JOIN t AS alias2 ON alias1.a = alias2.a 7029WHERE alias1.a = 1 ; 7030CUME_DIST() OVER (ORDER BY alias1.a) 70311 7032INSERT INTO t VALUES (2, '1979-01-02'), (3, '1979-01-03'); 7033SELECT STRAIGHT_JOIN CUME_DIST() OVER (ORDER BY alias1.a) 7034FROM t AS alias1 RIGHT JOIN t AS alias2 ON alias1.a = alias2.a 7035WHERE alias1.a > 1 ; 7036CUME_DIST() OVER (ORDER BY alias1.a) 70370.5 70381 7039DROP TABLE t; 7040 7041Bug#25840140 7042 7043CREATE TABLE t(a int); 7044INSERT INTO t VALUES (1),(2); 7045ANALYZE TABLE t; 7046Table Op Msg_type Msg_text 7047test.t analyze status OK 7048SELECT ROW_NUMBER() OVER (ORDER BY AVG(a)) AS rn FROM t ORDER BY rn ; 7049rn 70501 7051EXPLAIN SELECT ROW_NUMBER() OVER (ORDER BY AVG(a)) AS rn FROM t ORDER BY rn ; 7052id select_type table partitions type possible_keys key key_len ref rows filtered Extra 70531 SIMPLE t NULL ALL NULL NULL NULL NULL 2 100.00 NULL 7054Warnings: 7055Note 1003 /* select#1 */ select row_number() OVER (ORDER BY avg(`test`.`t`.`a`) ) AS `rn` from `test`.`t` 7056EXPLAIN FORMAT=JSON SELECT ROW_NUMBER() OVER (ORDER BY AVG(a)) AS rn FROM t ORDER BY rn ; 7057EXPLAIN 7058{ 7059 "query_block": { 7060 "select_id": 1, 7061 "cost_info": { 7062 "query_cost": "0.45" 7063 }, 7064 "table": { 7065 "table_name": "t", 7066 "access_type": "ALL", 7067 "rows_examined_per_scan": 2, 7068 "rows_produced_per_join": 2, 7069 "filtered": "100.00", 7070 "cost_info": { 7071 "read_cost": "0.25", 7072 "eval_cost": "0.20", 7073 "prefix_cost": "0.45", 7074 "data_read_per_join": "16" 7075 }, 7076 "used_columns": [ 7077 "a" 7078 ] 7079 } 7080 } 7081} 7082Warnings: 7083Note 1003 /* select#1 */ select row_number() OVER (ORDER BY avg(`test`.`t`.`a`) ) AS `rn` from `test`.`t` 7084Even multiple windows lead to no windowing steps when we have implicit grouping 7085SELECT ROW_NUMBER() OVER (ORDER BY AVG(a)) AS rn, SUM(AVG(a)) OVER (), 7086CUME_DIST() OVER (ORDER BY AVG(a)) FROM t ORDER BY rn; 7087rn SUM(AVG(a)) OVER () CUME_DIST() OVER (ORDER BY AVG(a)) 70881 1.5000 1 7089EXPLAIN FORMAT=JSON SELECT ROW_NUMBER() OVER (ORDER BY AVG(a)) AS rn, SUM(AVG(a)) OVER (), 7090CUME_DIST() OVER (ORDER BY AVG(a)) FROM t ORDER BY rn; 7091EXPLAIN 7092{ 7093 "query_block": { 7094 "select_id": 1, 7095 "cost_info": { 7096 "query_cost": "0.45" 7097 }, 7098 "table": { 7099 "table_name": "t", 7100 "access_type": "ALL", 7101 "rows_examined_per_scan": 2, 7102 "rows_produced_per_join": 2, 7103 "filtered": "100.00", 7104 "cost_info": { 7105 "read_cost": "0.25", 7106 "eval_cost": "0.20", 7107 "prefix_cost": "0.45", 7108 "data_read_per_join": "16" 7109 }, 7110 "used_columns": [ 7111 "a" 7112 ] 7113 } 7114 } 7115} 7116Warnings: 7117Note 1003 /* select#1 */ select row_number() OVER (ORDER BY avg(`test`.`t`.`a`) ) AS `rn`,sum(avg(`test`.`t`.`a`)) OVER () AS `SUM(AVG(a)) OVER ()`,cume_dist() OVER (ORDER BY avg(`test`.`t`.`a`) ) AS `CUME_DIST() OVER (ORDER BY AVG(a))` from `test`.`t` 7118DROP TABLE t; 7119Test for circular dependency in graph of windows 7120CREATE TABLE t1 (a INT PRIMARY KEY); 7121SELECT ROW_NUMBER() OVER w 7122FROM t1 7123WINDOW w AS (w2), w1 AS (), w2 as (w1); 7124ROW_NUMBER() OVER w 7125SELECT ROW_NUMBER() OVER w 7126FROM t1 7127WINDOW w AS (w2), w1 AS (w), w2 as (w1); 7128ERROR HY000: There is a circularity in the window dependency graph. 7129SELECT ROW_NUMBER() OVER w 7130FROM t1 7131WINDOW w AS (w1), w1 AS (w2), w2 as (w1); 7132ERROR HY000: There is a circularity in the window dependency graph. 7133SELECT ROW_NUMBER() OVER w FROM t1 WINDOW w AS (w); 7134ERROR HY000: There is a circularity in the window dependency graph. 7135SELECT ROW_NUMBER() OVER w 7136FROM t1 7137WINDOW w AS (w4), w5 AS (w4), w4 AS (), w3 AS (w2), w1 AS (w3), w2 as (w1); 7138ERROR HY000: There is a circularity in the window dependency graph. 7139Test for bad name 7140SELECT ROW_NUMBER() OVER w FROM t1 WINDOW w1 AS (); 7141ERROR HY000: Window name 'w' is not defined. 7142Test for good name with other unnamed window present 7143SELECT SUM(a) OVER w, ROW_NUMBER() OVER () FROM t1 WINDOW w AS (); 7144SUM(a) OVER w ROW_NUMBER() OVER () 7145Test for bad name with other unnamed window present 7146SELECT SUM(a) OVER w2, ROW_NUMBER() OVER () FROM t1 WINDOW w AS (); 7147ERROR HY000: Window name 'w2' is not defined. 7148Test for bad window name in window building on other window 7149OK 7150SELECT SUM(a) OVER w1, ROW_NUMBER() OVER w2 FROM t1 7151WINDOW w2 AS (), 7152w1 AS (w2 ORDER BY a); 7153SUM(a) OVER w1 ROW_NUMBER() OVER w2 7154Bad 7155SELECT SUM(a) OVER w1, ROW_NUMBER() OVER w2 FROM t1 7156WINDOW w2 AS (), 7157w1 AS (w22 ORDER BY a); 7158ERROR HY000: Window name 'w22' is not defined. 7159Tests without ORDER BY (corner cases) 7160CREATE TABLE tno (a INT); 7161SELECT RANK() OVER () FROM tno; 7162RANK() OVER () 7163SELECT DENSE_RANK() OVER () FROM tno; 7164DENSE_RANK() OVER () 7165SELECT PERCENT_RANK() OVER () FROM tno; 7166PERCENT_RANK() OVER () 7167SELECT CUME_DIST() OVER () FROM tno; 7168CUME_DIST() OVER () 7169INSERT INTO tno VALUES(20); 7170SELECT RANK() OVER () FROM tno; 7171RANK() OVER () 71721 7173SELECT DENSE_RANK() OVER () FROM tno; 7174DENSE_RANK() OVER () 71751 7176SELECT PERCENT_RANK() OVER () FROM tno; 7177PERCENT_RANK() OVER () 71780 7179SELECT CUME_DIST() OVER () FROM tno; 7180CUME_DIST() OVER () 71811 7182INSERT INTO tno VALUES(20),(21),(21),(null); 7183SELECT RANK() OVER (PARTITION BY a) FROM tno; 7184RANK() OVER (PARTITION BY a) 71851 71861 71871 71881 71891 7190SELECT DENSE_RANK() OVER (PARTITION BY a) FROM tno; 7191DENSE_RANK() OVER (PARTITION BY a) 71921 71931 71941 71951 71961 7197SELECT PERCENT_RANK() OVER (PARTITION BY a) FROM tno; 7198PERCENT_RANK() OVER (PARTITION BY a) 71990 72000 72010 72020 72030 7204SELECT CUME_DIST() OVER (PARTITION BY a) FROM tno; 7205CUME_DIST() OVER (PARTITION BY a) 72061 72071 72081 72091 72101 7211DROP TABLE tno; 7212# constant table is a special case (window without tmp table) 7213SELECT RANK() OVER () FROM (SELECT 1) t; 7214RANK() OVER () 72151 7216SELECT DENSE_RANK() OVER () FROM (SELECT 1) t; 7217DENSE_RANK() OVER () 72181 7219SELECT PERCENT_RANK() OVER () FROM (SELECT 1) t; 7220PERCENT_RANK() OVER () 72210 7222SELECT CUME_DIST() OVER () FROM (SELECT 1) t; 7223CUME_DIST() OVER () 72241 7225Test for bad frame bounds 7226SELECT ROW_NUMBER() OVER (ROWS BETWEEN UNBOUNDED FOLLOWING AND CURRENT ROW) 7227FROM t1; 7228ERROR HY000: Window '<unnamed window>': frame start cannot be UNBOUNDED FOLLOWING. 7229SELECT ROW_NUMBER() OVER (ROWS BETWEEN CURRENT ROW AND UNBOUNDED PRECEDING) 7230FROM t1; 7231ERROR HY000: Window '<unnamed window>': frame end cannot be UNBOUNDED PRECEDING. 7232SELECT ROW_NUMBER() OVER (ORDER BY a RANGE BETWEEN CURRENT ROW AND UNBOUNDED PRECEDING) 7233FROM t1; 7234ERROR HY000: Window '<unnamed window>': frame end cannot be UNBOUNDED PRECEDING. 7235SELECT ROW_NUMBER() OVER (ROWS BETWEEN INTERVAL 2 DAY PRECEDING AND UNBOUNDED FOLLOWING) 7236FROM t1; 7237ERROR HY000: Window '<unnamed window>': INTERVAL can only be used with RANGE frames. 7238SELECT ROW_NUMBER() OVER (ROWS BETWEEN UNBOUNDED PRECEDING AND INTERVAL 2 DAY FOLLOWING) 7239FROM t1; 7240ERROR HY000: Window '<unnamed window>': INTERVAL can only be used with RANGE frames. 7241DROP TABLE t1; 7242 7243Bug#25879669 SIG11 AT ADD_FROM_ITEM IN SQL/WINDOW.CC 7244 7245CREATE TABLE t(a int, b int); 7246INSERT INTO t VALUES (1,2),(3,4); 7247SELECT COUNT(*) AS count, 7248ROW_NUMBER() OVER (ORDER BY b) AS rn 7249FROM t ORDER BY b; 7250ERROR 42000: In aggregated query without GROUP BY, expression #1 of PARTITION BY or ORDER BY clause of window '<unnamed window>' contains nonaggregated column 'test.t.b'; this is incompatible with sql_mode=only_full_group_by 7251SELECT 1 UNION 7252(SELECT ROW_NUMBER() OVER ( ORDER BY b ) AS rn FROM t ORDER BY b); 72531 72541 72552 7256DROP TABLE t; 7257# 7258# Bug#25874481 WL9603:ASSERTION `JOIN()->ORDERED_INDEX_USAGE != (FILESORT->ORDER == JOIN()->ORD 7259# 7260CREATE TABLE AA ( 7261pk int(11) NOT NULL AUTO_INCREMENT, 7262col_dec_key decimal(20,4) NOT NULL, 7263col_varchar_key varchar(1) NOT NULL, 7264PRIMARY KEY (pk), 7265KEY col_dec_key (col_dec_key), 7266KEY col_varchar_key (col_varchar_key,pk) 7267) ENGINE=InnoDB AUTO_INCREMENT=11; 7268Warnings: 7269Warning 1681 Integer display width is deprecated and will be removed in a future release. 7270INSERT INTO AA VALUES(10,6473.2230,'a'); 7271CREATE TABLE D ( 7272pk int(11) NOT NULL AUTO_INCREMENT, 7273col_int int(11) NOT NULL, 7274col_int_key int(11) NOT NULL, 7275col_dec decimal(20,4) NOT NULL, 7276col_dec_key decimal(20,4) NOT NULL, 7277col_varchar_key varchar(1) NOT NULL, 7278col_varchar varchar(1) NOT NULL, 7279PRIMARY KEY (pk), 7280KEY col_int_key (col_int_key), 7281KEY col_dec_key (col_dec_key), 7282KEY col_varchar_key (col_varchar_key,col_int_key) 7283) ENGINE=InnoDB AUTO_INCREMENT=31 DEFAULT CHARSET=latin1; 7284Warnings: 7285Warning 1681 Integer display width is deprecated and will be removed in a future release. 7286Warning 1681 Integer display width is deprecated and will be removed in a future release. 7287Warning 1681 Integer display width is deprecated and will be removed in a future release. 7288INSERT INTO D VALUES 7289(1,5,4,4090.3920,5170.7060,'q','q'),(2,8,3,2634.3480,695.3360,'e','e'); 7290SET SQL_MODE=''; 7291UPDATE 7292D AS OUTR1 LEFT JOIN D AS OUTR2 7293ON ( OUTR1 . pk = OUTR2 . col_int_key ) 7294SET OUTR1.col_varchar_key = 0 7295WHERE OUTR1 . col_int_key < ( 7296SELECT DISTINCT FIRST_VALUE(7) OVER ( ) AS y 7297FROM AA AS INNR1 7298WHERE OUTR2 . col_dec_key <= 6 7299ORDER BY INNR1 . col_varchar_key LIMIT 1); 7300SET SQL_MODE=DEFAULT; 7301DROP TABLE AA, D; 7302# 7303# Bug#25880362 WL9603:ASSERTION `TAB->TYPE() == JT_REF || TAB->TYPE() == JT_EQ_REF' FAILED. 7304# 7305CREATE TABLE G ( 7306pk int(11) NOT NULL AUTO_INCREMENT, 7307col_int int(11) DEFAULT NULL, 7308PRIMARY KEY (pk) 7309) ENGINE=innodb AUTO_INCREMENT=6 DEFAULT CHARSET=latin1; 7310Warnings: 7311Warning 1681 Integer display width is deprecated and will be removed in a future release. 7312Warning 1681 Integer display width is deprecated and will be removed in a future release. 7313INSERT INTO G VALUES (1,7); 7314CREATE TABLE H ( 7315col_varchar_10_utf8 varchar(10) CHARACTER SET utf8 DEFAULT NULL, 7316col_varchar_10_latin1 varchar(10) DEFAULT NULL, 7317pk int(11) NOT NULL AUTO_INCREMENT, 7318PRIMARY KEY (pk) 7319) ENGINE=innodb AUTO_INCREMENT=6 DEFAULT CHARSET=latin1; 7320Warnings: 7321Warning 3719 'utf8' is currently an alias for the character set UTF8MB3, but will be an alias for UTF8MB4 in a future release. Please consider using UTF8MB4 in order to be unambiguous. 7322Warning 1681 Integer display width is deprecated and will be removed in a future release. 7323INSERT INTO H VALUES('RDDGR','so',1); 7324SELECT 7325alias1 . col_varchar_10_latin1 AS field1, 7326NTH_VALUE(alias2.col_int, 4) OVER ( ORDER BY alias1.col_varchar_10_utf8 ASC ) 7327AS field2 7328FROM ( SELECT * FROM H ) AS alias1 LEFT JOIN ( SELECT * FROM G ) AS 7329alias2 7330ON alias1 . pk = alias2 . pk 7331WHERE alias1 . pk = 1; 7332field1 field2 7333so NULL 7334DROP TABLE G,H; 7335# 7336# Bug#25840052: PROBLEM WITH FIELD::REAL_MAYBE_NULL IN SQL/FIELD.H 7337# FOR MIN/MAX(<WF>) 7338# 7339CREATE TABLE t(a int); 7340INSERT INTO t VALUES (1),(2); 7341SELECT MAX(row_number() OVER ()) FROM t; 7342ERROR HY000: You cannot use the window function 'row_number' in this context.' 7343SELECT MIN(rank() OVER (ORDER BY a)) FROM t; 7344ERROR HY000: You cannot use the window function 'rank' in this context.' 7345SELECT BIT_AND(rank() OVER (ORDER BY a)) FROM t; 7346ERROR HY000: You cannot use the window function 'rank' in this context.' 7347SELECT MIN(a),SUM(rank() OVER (ORDER BY a)) FROM t GROUP BY a; 7348ERROR HY000: You cannot use the window function 'rank' in this context.' 7349DROP TABLE t; 7350# 7351# Bug#25868905: PROBLEM AT FILESORT::MAKE_SORTORDER IN SQL/FILESORT.CC 7352# FOR PREPARED STMTS 7353# 7354CREATE TABLE t(pk int PRIMARY KEY, col_int int, col_varchar varchar(10)); 7355PREPARE ps FROM "SELECT 7356RANK() OVER ( PARTITION BY pk ORDER BY col_int, col_varchar, pk) AS rnk, 7357CUME_DIST() OVER ( PARTITION BY col_int ORDER BY pk ) AS c_dist 7358FROM t"; 7359EXECUTE ps; 7360rnk c_dist 7361DROP PREPARE ps; 7362DROP TABLE t; 7363# 7364# Bug#25877151: PROBLEM IN ITEM_CACHE* MAKE_RESULT_ITEM(ITEM*) 7365# 7366CREATE TABLE t1 (a INT, d CHAR(1)); 7367PREPARE ps FROM " 7368SELECT ROW_NUMBER() OVER ( ORDER BY d ) AS rn 7369FROM t1 7370WINDOW w1 AS ( ORDER BY d ), 7371 w2 AS ( ORDER BY a RANGE 5 PRECEDING )"; 7372EXECUTE ps; 7373rn 7374DROP PREPARE ps; 7375DROP TABLE t1; 7376# 7377# Bug#25889341 WL#9603: RANK FUNCTIONS RETURN INCORRECT RESULT IN PREPARED SATEMENT 7378# 7379CREATE TABLE t (a int, b int, c int); 7380INSERT INTO t VALUES (5,6,1),(NULL,6,1),(4,6,1),(5,6,1),(NULL,6,1); 7381SELECT ROW_NUMBER() OVER ( ORDER BY a ) AS f1, 7382RANK() OVER ( ORDER BY c ) AS f2, 7383LEAD(c) OVER ( ORDER BY a ROWS UNBOUNDED PRECEDING ) AS f7 7384FROM t; 7385f1 f2 f7 73861 1 1 73872 1 1 73883 1 1 73894 1 1 73905 1 NULL 7391DROP TABLE t; 7392# 7393# Bug#25886572 WL#9603: SIG11 AT ITEM_REF::VAL_INT IN SQL/ITEM.CC 7394# 7395SET sql_mode=''; 7396CREATE TABLE t (a int, b int); 7397INSERT INTO t values (1,2),(3,4); 7398SELECT RANK() OVER w1 AS rnk FROM t 7399WINDOW w1 AS (ORDER BY AVG(a)) 7400ORDER BY b; 7401rnk 74021 7403SET sql_mode=DEFAULT; 7404DROP TABLE t; 7405# 7406# Bug#25880999 : WL#9603: ASSERT `SELECT_LEX->ACTIVE_OPTIONS() 7407# & (1ULL << 17)' IN SQL_SELECT.CC 7408CREATE TABLE t1(a int, b int); 7409CREATE TABLE t2(a int, b int); 7410SELECT ROW_NUMBER() OVER ( ORDER BY t1.a ) AS rn 7411FROM t1, t2 WHERE t1.a = 1 GROUP BY t1.a; 7412rn 7413DROP TABLE t1,t2; 7414# 7415# Bug#25907777 WL#9603: ASSERT `SELECT_LEX->LEAF_TABLE_COUNT == 0...' IN SQL/SQL_OPTIMIZER.CC 7416# 7417CREATE TABLE t1 (a date); 7418CREATE TABLE t2 (a int); 7419INSERT INTO t1 SELECT CURRENT_DATE(); 7420INSERT INTO t2 VALUES (5); 7421SELECT RANK() OVER (ORDER BY a RANGE INTERVAL (SELECT a FROM t2) MINUTE PRECEDING) FROM t1; 7422ERROR HY000: Window '<unnamed window>' has a non-constant frame bound. 7423SELECT RANK() OVER (ORDER BY a RANGE BETWEEN INTERVAL 1 MINUTE PRECEDING AND INTERVAL (SELECT a FROM t2) MINUTE FOLLOWING) FROM t1; 7424ERROR HY000: Window '<unnamed window>' has a non-constant frame bound. 7425SELECT RANK() OVER (ORDER BY a RANGE INTERVAL 1+(SELECT a FROM t2) MINUTE PRECEDING) FROM t1; 7426ERROR HY000: Window '<unnamed window>' has a non-constant frame bound. 7427SELECT RANK() OVER (ORDER BY a ROWS (SELECT a FROM t2) PRECEDING) FROM t1; 7428ERROR 42000: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '(SELECT a FROM t2) PRECEDING) FROM t1' at line 1 7429SELECT RANK() OVER (ORDER BY a ROWS BETWEEN 1 PRECEDING AND (SELECT a FROM t2) FOLLOWING) FROM t1; 7430ERROR 42000: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '(SELECT a FROM t2) FOLLOWING) FROM t1' at line 1 7431DROP TABLE t1, t2; 7432# 7433# Bug#25921353 WL#9603: EMPTY RESULT WITH WF IN SELECT, UNGROUPED HAVING AND ORDER BY 7434# 7435CREATE TABLE t (a int, b varchar(10)); 7436INSERT INTO t VALUES (1, 'are'), (2, 'not'), (3, 'have'); 7437SELECT a AS field1, ROW_NUMBER() OVER () AS field2 FROM t 7438HAVING field1 >= 2; 7439field1 field2 74402 1 74413 2 7442Used to give empty set 7443SELECT a AS field1, ROW_NUMBER() OVER () AS field2 FROM t 7444HAVING field1 >= 2 ORDER BY field1; 7445field1 field2 74462 1 74473 2 7448DROP TABLE t; 7449# 7450# Bug#25902905 WL9603:ASSERTION `!(TAB->TABLE()->REGINFO.NOT_EXISTS_OPTIMIZE && !TAB->CONDITION 7451# 7452CREATE TABLE a (pk int PRIMARY KEY, c varchar(10)); 7453INSERT INTO a VALUES (6, 's'); 7454CREATE TABLE b (pk int PRIMARY KEY, c varchar(255)); 7455INSERT INTO b VALUES (9,'s'); 7456EXPLAIN FORMAT=tree SELECT ROW_NUMBER() OVER (ORDER BY a.c) 7457FROM a LEFT JOIN b 7458ON a.c = b.c 7459WHERE (b.pk IS NULL AND a.pk IN (6)); 7460EXPLAIN 7461-> Window aggregate: row_number() OVER (ORDER BY 's' ) 7462 -> Filter: (b.pk is null) 7463 -> Nested loop left join 7464 -> Rows fetched before execution 7465 -> Sort: 's' (cost=0.35 rows=1) 7466 -> Filter: ('s' = b.c) 7467 -> Table scan on b 7468 7469SELECT ROW_NUMBER() OVER (ORDER BY a.c) 7470FROM a LEFT JOIN b 7471ON a.c = b.c 7472WHERE (b.pk IS NULL AND a.pk IN (6)); 7473ROW_NUMBER() OVER (ORDER BY a.c) 7474DROP TABLE a, b; 7475# 7476# Bug#25907063 WL#9603:ASSERT `INITED == INDEX' AT HANDLER::HA_INDEX_READ_MAP IN SQL/HANDLER.CC 7477# 7478SET SQL_MODE=''; 7479Warnings: 7480Warning 3719 'utf8' is currently an alias for the character set UTF8MB3, but will be an alias for UTF8MB4 in a future release. Please consider using UTF8MB4 in order to be unambiguous. 7481Warning 3719 'utf8' is currently an alias for the character set UTF8MB3, but will be an alias for UTF8MB4 in a future release. Please consider using UTF8MB4 in order to be unambiguous. 7482Warning 3719 'utf8' is currently an alias for the character set UTF8MB3, but will be an alias for UTF8MB4 in a future release. Please consider using UTF8MB4 in order to be unambiguous. 7483Warning 1681 Integer display width is deprecated and will be removed in a future release. 7484Warning 1681 Integer display width is deprecated and will be removed in a future release. 7485Warning 3719 'utf8' is currently an alias for the character set UTF8MB3, but will be an alias for UTF8MB4 in a future release. Please consider using UTF8MB4 in order to be unambiguous. 7486Warning 1681 Integer display width is deprecated and will be removed in a future release. 7487Warnings: 7488Warning 3719 'utf8' is currently an alias for the character set UTF8MB3, but will be an alias for UTF8MB4 in a future release. Please consider using UTF8MB4 in order to be unambiguous. 7489Warning 3719 'utf8' is currently an alias for the character set UTF8MB3, but will be an alias for UTF8MB4 in a future release. Please consider using UTF8MB4 in order to be unambiguous. 7490Warning 3719 'utf8' is currently an alias for the character set UTF8MB3, but will be an alias for UTF8MB4 in a future release. Please consider using UTF8MB4 in order to be unambiguous. 7491Warning 1681 Integer display width is deprecated and will be removed in a future release. 7492Warning 1681 Integer display width is deprecated and will be removed in a future release. 7493Warning 1681 Integer display width is deprecated and will be removed in a future release. 7494Warning 3719 'utf8' is currently an alias for the character set UTF8MB3, but will be an alias for UTF8MB4 in a future release. Please consider using UTF8MB4 in order to be unambiguous. 7495SET tmp_table_size= 16384; 7496SELECT DISTINCT MAX( table2.`col_varchar_255_utf8` ) AS max1 , 7497MIN( table1.`col_date` ) AS min1 , 7498AVG( table2.`col_int` ) AS avg1 , 7499MAX( table1.`col_varchar_255_utf8_2` ) AS max2 , 7500table2.`col_varchar_255_utf8` , 7501FIRST_VALUE( table1. `col_varchar_255_utf8` ) OVER (ORDER BY MAX( table2.`col_varchar_255_utf8` ), 7502MIN( table1.`col_date` ), 7503AVG( table2.`col_int` ), 7504MAX( table1.`col_varchar_255_utf8_2` ), 7505table2.`col_varchar_255_utf8` ) AS 1st_val 7506FROM C AS table1 LEFT JOIN 7507B AS table2 ON table1.`col_int_2` < table2.`col_int_2` 7508 GROUP BY table2.`col_varchar_255_utf8`, table1.`col_varchar_255_utf8`; 7509max1 min1 avg1 max2 col_varchar_255_utf8 1st_val 7510NULL NULL NULL JGMQN NULL that 7511NULL NULL NULL why NULL that 7512NULL 2002-11-15 NULL n NULL that 7513NULL 2002-12-16 NULL e NULL that 7514NULL 2003-05-16 NULL JXEJC NULL that 7515NDQUO 0000-00-00 1827930112.0000 crutdamugrogyzzsaemysmyzomflzilnslxzvupfxrajygqpclheykudxgaloqoaaeirdfcljrcqdrciywuiaapvivsmnlvcsfognnabeubyprqcfadvzrjzhaanyybmbhzwohfockjujddqwqj NDQUO that 7516NDQUO 0000-00-00 1827930112.0000 f NDQUO that 7517NDQUO 0000-00-00 1827930112.0000 gvasnskcrutdamugrogyzzsaemysmyzomflzilnslxzvupfxrajygqpclheykudxgaloqoaaeirdfcljrcqdrciywuiaapvivsmnlvcsfognnabeubyprqcfadvzrjzhaanyybmbhzwohfockjujddqwqjyzxeehuuwqvnyfejvjqzdvjjatukdbpiiabrdqveoesihupydtk NDQUO that 7518NDQUO 0000-00-00 1827930112.0000 so NDQUO that 7519pxkwzyojewtgvasnskcrutdamugrogyzzsaemysmyzomflzilnslxzvupfxrajygqpclheykudxgaloqoaaeirdf NULL 1662451712.0000 MBLMH pxkwzyojewtgvasnskcrutdamugrogyzzsaemysmyzomflzilnslxzvupfxrajygqpclheykudxgaloqoaaeirdf that 7520pxkwzyojewtgvasnskcrutdamugrogyzzsaemysmyzomflzilnslxzvupfxrajygqpclheykudxgaloqoaaeirdf 0000-00-00 1662451712.0000 crutdamugrogyzzsaemysmyzomflzilnslxzvupfxrajygqpclheykudxgaloqoaaeirdfcljrcqdrciywuiaapvivsmnlvcsfognnabeubyprqcfadvzrjzhaanyybmbhzwohfockjujddqwqj pxkwzyojewtgvasnskcrutdamugrogyzzsaemysmyzomflzilnslxzvupfxrajygqpclheykudxgaloqoaaeirdf that 7521pxkwzyojewtgvasnskcrutdamugrogyzzsaemysmyzomflzilnslxzvupfxrajygqpclheykudxgaloqoaaeirdf 0000-00-00 1662451712.0000 f pxkwzyojewtgvasnskcrutdamugrogyzzsaemysmyzomflzilnslxzvupfxrajygqpclheykudxgaloqoaaeirdf that 7522pxkwzyojewtgvasnskcrutdamugrogyzzsaemysmyzomflzilnslxzvupfxrajygqpclheykudxgaloqoaaeirdf 0000-00-00 1662451712.0000 gvasnskcrutdamugrogyzzsaemysmyzomflzilnslxzvupfxrajygqpclheykudxgaloqoaaeirdfcljrcqdrciywuiaapvivsmnlvcsfognnabeubyprqcfadvzrjzhaanyybmbhzwohfockjujddqwqjyzxeehuuwqvnyfejvjqzdvjjatukdbpiiabrdqveoesihupydtk pxkwzyojewtgvasnskcrutdamugrogyzzsaemysmyzomflzilnslxzvupfxrajygqpclheykudxgaloqoaaeirdf that 7523pxkwzyojewtgvasnskcrutdamugrogyzzsaemysmyzomflzilnslxzvupfxrajygqpclheykudxgaloqoaaeirdf 0000-00-00 1662451712.0000 so pxkwzyojewtgvasnskcrutdamugrogyzzsaemysmyzomflzilnslxzvupfxrajygqpclheykudxgaloqoaaeirdf that 7524wzyojewtgvasnskcrutdamugrogyzzsaemysmyzomflzilnslxzvupfxrajygqpclheykudxgaloqoaaeirdfcljrcqdrciywuiaapvivsmnlvcsfognnabeubyprqcfadvzrjzhaanyybmbhzwohfockjujddqwqjyzxeehuuwqvnyfejvjqzdvjjatukdbpiiabrdqveoesihupydtktqmkglyemowxmzkymvicqwnriwvvsarcpejhamxqxr 0000-00-00 8.0000 crutdamugrogyzzsaemysmyzomflzilnslxzvupfxrajygqpclheykudxgaloqoaaeirdfcljrcqdrciywuiaapvivsmnlvcsfognnabeubyprqcfadvzrjzhaanyybmbhzwohfockjujddqwqj wzyojewtgvasnskcrutdamugrogyzzsaemysmyzomflzilnslxzvupfxrajygqpclheykudxgaloqoaaeirdfcljrcqdrciywuiaapvivsmnlvcsfognnabeubyprqcfadvzrjzhaanyybmbhzwohfockjujddqwqjyzxeehuuwqvnyfejvjqzdvjjatukdbpiiabrdqveoesihupydtktqmkglyemowxmzkymvicqwnriwvvsarcpejhamxqxr that 7525wzyojewtgvasnskcrutdamugrogyzzsaemysmyzomflzilnslxzvupfxrajygqpclheykudxgaloqoaaeirdfcljrcqdrciywuiaapvivsmnlvcsfognnabeubyprqcfadvzrjzhaanyybmbhzwohfockjujddqwqjyzxeehuuwqvnyfejvjqzdvjjatukdbpiiabrdqveoesihupydtktqmkglyemowxmzkymvicqwnriwvvsarcpejhamxqxr 0000-00-00 8.0000 f wzyojewtgvasnskcrutdamugrogyzzsaemysmyzomflzilnslxzvupfxrajygqpclheykudxgaloqoaaeirdfcljrcqdrciywuiaapvivsmnlvcsfognnabeubyprqcfadvzrjzhaanyybmbhzwohfockjujddqwqjyzxeehuuwqvnyfejvjqzdvjjatukdbpiiabrdqveoesihupydtktqmkglyemowxmzkymvicqwnriwvvsarcpejhamxqxr that 7526wzyojewtgvasnskcrutdamugrogyzzsaemysmyzomflzilnslxzvupfxrajygqpclheykudxgaloqoaaeirdfcljrcqdrciywuiaapvivsmnlvcsfognnabeubyprqcfadvzrjzhaanyybmbhzwohfockjujddqwqjyzxeehuuwqvnyfejvjqzdvjjatukdbpiiabrdqveoesihupydtktqmkglyemowxmzkymvicqwnriwvvsarcpejhamxqxr 0000-00-00 8.0000 gvasnskcrutdamugrogyzzsaemysmyzomflzilnslxzvupfxrajygqpclheykudxgaloqoaaeirdfcljrcqdrciywuiaapvivsmnlvcsfognnabeubyprqcfadvzrjzhaanyybmbhzwohfockjujddqwqjyzxeehuuwqvnyfejvjqzdvjjatukdbpiiabrdqveoesihupydtk wzyojewtgvasnskcrutdamugrogyzzsaemysmyzomflzilnslxzvupfxrajygqpclheykudxgaloqoaaeirdfcljrcqdrciywuiaapvivsmnlvcsfognnabeubyprqcfadvzrjzhaanyybmbhzwohfockjujddqwqjyzxeehuuwqvnyfejvjqzdvjjatukdbpiiabrdqveoesihupydtktqmkglyemowxmzkymvicqwnriwvvsarcpejhamxqxr that 7527wzyojewtgvasnskcrutdamugrogyzzsaemysmyzomflzilnslxzvupfxrajygqpclheykudxgaloqoaaeirdfcljrcqdrciywuiaapvivsmnlvcsfognnabeubyprqcfadvzrjzhaanyybmbhzwohfockjujddqwqjyzxeehuuwqvnyfejvjqzdvjjatukdbpiiabrdqveoesihupydtktqmkglyemowxmzkymvicqwnriwvvsarcpejhamxqxr 0000-00-00 8.0000 so wzyojewtgvasnskcrutdamugrogyzzsaemysmyzomflzilnslxzvupfxrajygqpclheykudxgaloqoaaeirdfcljrcqdrciywuiaapvivsmnlvcsfognnabeubyprqcfadvzrjzhaanyybmbhzwohfockjujddqwqjyzxeehuuwqvnyfejvjqzdvjjatukdbpiiabrdqveoesihupydtktqmkglyemowxmzkymvicqwnriwvvsarcpejhamxqxr that 7528SET tmp_table_size=DEFAULT, SQL_MODE=DEFAULT; 7529DROP TABLE C,B; 7530# 7531# Bug#25894860: PROBLEM IN PROTOCOL_SEND 7532CREATE TABLE t (a int PRIMARY KEY, b varchar(10), KEY idx1 (b)); 7533INSERT INTO t VALUES (2,'b'),(1,'back'),(5,'think'),(4,'v'),(3,'y'); 7534SELECT LAG(b) OVER () AS wf_lag, 7535ROW_NUMBER() OVER () AS wf_rn 7536FROM t 7537WHERE b LIKE ('_') AND a=2; 7538wf_lag wf_rn 7539NULL 1 7540DROP TABLE t; 7541# 7542# Bug#25895300: PROBLEM IN PROTOCOL::SEND 7543CREATE TABLE t1 ( 7544pk int(11) NOT NULL AUTO_INCREMENT, 7545col_int int(11) DEFAULT NULL, 7546col_datetime datetime DEFAULT NULL, 7547PRIMARY KEY (pk) 7548); 7549Warnings: 7550Warning 1681 Integer display width is deprecated and will be removed in a future release. 7551Warning 1681 Integer display width is deprecated and will be removed in a future release. 7552INSERT INTO t1 VALUES (1,8,'2004-03-26 11:59:45'),(2,-692387840,'2000-11-03 755314:56:50'),(3,8,'2008-04-11 14:04:45'),(4,-660865024,'2004-08-11 755407:07:20'),(5,9,'2001-04-11 00:00:00'); 7555CREATE TEMPORARY TABLE t2 ( 7556pk int(11) NOT NULL AUTO_INCREMENT, 7557col1 varchar(10) CHARACTER SET utf8 DEFAULT NULL, 7558PRIMARY KEY (pk) 7559) ENGINE=Memory; 7560Warnings: 7561Warning 1681 Integer display width is deprecated and will be removed in a future release. 7562Warning 3719 'utf8' is currently an alias for the character set UTF8MB3, but will be an alias for UTF8MB4 in a future release. Please consider using UTF8MB4 in order to be unambiguous. 7563INSERT INTO t2 VALUES (1,'ntdajwwdda'); 7564SELECT LEAD(t2.col1, 5) 7565OVER ( ORDER BY t1.col_int DESC ) as lead1 , 7566t1.pk, LEAD(t1.pk, 4) OVER ( PARTITION BY t1.col_int ORDER BY t1.col_int 7567) as lead2 FROM t1 RIGHT JOIN t2 ON t1.pk=t2.pk 7568WHERE t2.pk IS NOT NULL; 7569lead1 pk lead2 7570NULL 1 NULL 7571DROP TABLE t1,t2; 7572CREATE TABLE t1 ( 7573pk INTEGER NOT NULL AUTO_INCREMENT, 7574col1 varchar(255) DEFAULT NULL, 7575PRIMARY KEY (pk) 7576)DEFAULT CHARSET=latin1; 7577INSERT INTO t1 VALUES (1,'I\'ll'),(2,'BORKU'),(3,'HZISF'),(4,'q'),(5,'o'), 7578 (6,'now'),(7,'WIBYB'),(8,'could'),(9,'were'),(10,'on'),(11,'HYADL'), 7579 (12,'l'),(13,'asdas'),(14,'n'),(16,'my'),(17,'NNTTK'),(18,'as'), 7580 (19,'TOAAB'),(20,'asdas'),(21,'well'),(22,'i'),(23,'u'),(24,'e'), 7581 (25,'o'),(26,'c'),(28,'YLZRI'),(29,'well'),(30,'want'),(31,'with'), 7582 (32,'VMVLI'),(33,'right'),(34,'llotd'),(35,'DNLWV'),(36,'SIPKW'), 7583 (37,'o'),(38,'mean'),(39,'asdas'),(40,'asdas'); 7584CREATE VIEW view_t1 AS SELECT * FROM t1; 7585SELECT NTH_VALUE(view_t1.col1,2) OVER (), view_t1.col1, view_t1.pk FROM view_t1 LEFT JOIN t1 7586ON view_t1.pk = t1.pk WHERE t1.pk BETWEEN 4 AND 10 AND t1.pk IN (4); 7587NTH_VALUE(view_t1.col1,2) OVER () col1 pk 7588NULL q 4 7589DROP TABLE t1; 7590DROP VIEW view_t1; 7591# 7592# Bug#25914495: WL#9603: SIG11 AT ITEM_FIELD::ITEM_FIELD IN SQL/ITEM.CC 7593# 7594CREATE TABLE t1(a int); 7595CREATE TABLE t2(b int); 7596INSERT INTO t1 VALUES (1); 7597INSERT INTO t2 VALUES (3); 7598SELECT RANK() OVER ( ORDER BY a ) + 1 AS rank_expr FROM t1, t2; 7599rank_expr 76002 7601DROP TABLE t1,t2; 7602# 7603# Bug#25960114 WL#9603: RESULT DIFF SEEN WITHOUT PARTITION CLAUSE AND PARTITION BY NULL 7604# 7605CREATE TABLE t1 (a int, b float); 7606INSERT INTO t1 VALUES 7607(4,12),(5,2),(9,14),(12,10),(6,8),(5,7),(2,15),(2,15),(15,11),(14,5),(14,240), 7608(1,10),(14,5),(5,9),(1,11),(2,5),(11,9),(13,13),(6,187),(8,12),(10,12),(15,14), 7609(50,4),(14,3),(166,2),(15,13),(10,12),(48,4),(13,3),(7,10); 7610SELECT a, b, LEAD (b, 1) OVER (ORDER BY a,b ASC) AS c FROM t1 a ORDER BY a, b, c; 7611a b c 76121 10 11 76131 11 5 76142 5 15 76152 15 12 76162 15 15 76174 12 2 76185 2 7 76195 7 9 76205 9 8 76216 8 187 76226 187 10 76237 10 12 76248 12 14 76259 14 12 762610 12 9 762710 12 12 762811 9 10 762912 10 3 763013 3 13 763113 13 3 763214 3 5 763314 5 5 763414 5 240 763514 240 11 763615 11 13 763715 13 14 763815 14 4 763948 4 4 764050 4 2 7641166 2 NULL 7642SELECT a, b, LEAD (b, 1) OVER (PARTITION BY NULL ORDER BY a,b ASC) AS c FROM t1 a ORDER BY a, b, c; 7643a b c 76441 10 11 76451 11 5 76462 5 15 76472 15 12 76482 15 15 76494 12 2 76505 2 7 76515 7 9 76525 9 8 76536 8 187 76546 187 10 76557 10 12 76568 12 14 76579 14 12 765810 12 9 765910 12 12 766011 9 10 766112 10 3 766213 3 13 766313 13 3 766414 3 5 766514 5 5 766614 5 240 766714 240 11 766815 11 13 766915 13 14 767015 14 4 767148 4 4 767250 4 2 7673166 2 NULL 7674DROP TABLE t1; 7675# 7676# Bug#26781725 INCORRECT RESULTS FOR QUERY(MAX FUNC+HAVING CLAUSE) WHEN USED INSIDE VIEW 7677# 7678CREATE TABLE CC ( 7679pk int(11) NOT NULL AUTO_INCREMENT, 7680col_varchar_key varchar(1) NOT NULL, 7681col_varchar varchar(1) NOT NULL, 7682PRIMARY KEY (pk), 7683KEY col_varchar_key (col_varchar_key) 7684) ENGINE=InnoDB AUTO_INCREMENT=30 DEFAULT CHARSET=utf8mb4; 7685Warnings: 7686Warning 1681 Integer display width is deprecated and will be removed in a future release. 7687INSERT INTO CC VALUES 7688(10,'v','v'),(18,'a','a'),(19,'v','v'),(20,'u','u'),(21,'s','s'),(22,'y','y'), 7689(23,'z','z'),(28,'y','y'); 7690SELECT MAX( col_varchar_key ) AS field1 FROM CC AS alias1 7691HAVING field1 <> 5; 7692field1 7693z 7694CREATE OR REPLACE VIEW v1 AS 7695SELECT MAX( col_varchar_key ) AS field1 FROM CC AS alias1 7696HAVING field1 <> 5; 7697SELECT * FROM v1; 7698field1 7699z 7700DROP VIEW v1; 7701DROP TABLE CC; 7702# 7703# Tests added to improve GCOV Coverage data 7704# 7705Item_sum_sum::val_str "if (null_value): test 7706CREATE TABLE t(v INT); 7707INSERT INTO t VALUES (NULL), (3); 7708SELECT SUM((SELECT v FROM t LIMIT 1)) OVER (); 7709SUM((SELECT v FROM t LIMIT 1)) OVER () 7710NULL 7711DROP TABLE t; 7712# 7713# Bug#25976248 WL#9603 ASSERT ERROR IN ITEM_SUM::FIX_AFTER_PULLOUT 7714# 7715CREATE TABLE t1(a TEXT); 7716INSERT INTO t1 VALUES('1'); 7717SELECT SUM( distinct a ) FROM t1 GROUP BY a HAVING a IN ( (select row_number() over ())); 7718SUM( distinct a ) 77191 7720SELECT SUM( distinct a ) FROM t1 GROUP BY a HAVING a + 1 IN ( (select row_number() over ())); 7721SUM( distinct a ) 7722SELECT SUM( distinct a ) FROM t1 GROUP BY a HAVING a IN ( (select ntile(3) over (order by null))); 7723SUM( distinct a ) 77241 7725SELECT SUM( distinct a ) FROM t1 GROUP BY a HAVING a + 1 IN ( (select ntile(3) over (order by null))); 7726SUM( distinct a ) 7727SELECT SUM( distinct a ) FROM t1 GROUP BY a HAVING a IN ( (select avg(1) over ())); 7728SUM( distinct a ) 77291 7730SELECT SUM( distinct a ) FROM t1 GROUP BY a HAVING a + 1 IN ( (select avg(1) over ())); 7731SUM( distinct a ) 7732SELECT SUM( distinct a ) FROM t1 GROUP BY a HAVING a IN ( (select sum(1) over ())); 7733SUM( distinct a ) 77341 7735SELECT SUM( distinct a ) FROM t1 GROUP BY a HAVING a + 1 IN ( (select sum(1) over ())); 7736SUM( distinct a ) 7737SELECT SUM( distinct a ) FROM t1 GROUP BY a HAVING a IN ( (select count(1) over ())); 7738SUM( distinct a ) 77391 7740SELECT SUM( distinct a ) FROM t1 GROUP BY a HAVING a + 1 IN ( (select count(1) over ())); 7741SUM( distinct a ) 7742SELECT SUM( distinct a ) FROM t1 GROUP BY a HAVING a IN ( (select rank() over (ORDER BY null))); 7743SUM( distinct a ) 77441 7745SELECT SUM( distinct a ) FROM t1 GROUP BY a HAVING a + 1 IN ( (select rank() over (ORDER BY null))); 7746SUM( distinct a ) 7747SELECT SUM( distinct a ) FROM t1 GROUP BY a HAVING a IN ( (select dense_rank() over (ORDER BY null))); 7748SUM( distinct a ) 77491 7750SELECT SUM( distinct a ) FROM t1 GROUP BY a HAVING a + 1 IN ( (select dense_rank() over (ORDER BY null))); 7751SUM( distinct a ) 7752SELECT SUM( distinct a ) FROM t1 GROUP BY a HAVING a - 1 IN ( (select percent_rank() over (ORDER BY null))); 7753SUM( distinct a ) 77541 7755SELECT SUM( distinct a ) FROM t1 GROUP BY a HAVING a IN ( (select percent_rank() over (ORDER BY null))); 7756SUM( distinct a ) 7757SELECT SUM( distinct a ) FROM t1 GROUP BY a HAVING a IN ( (select cume_dist() over (ORDER BY null))); 7758SUM( distinct a ) 77591 7760SELECT SUM( distinct a ) FROM t1 GROUP BY a HAVING a + 1 IN ( (select cume_dist() over (ORDER BY null))); 7761SUM( distinct a ) 7762SELECT SUM( distinct a ) FROM t1 GROUP BY a HAVING a IN ( (select lead(1, 0) over (ORDER BY null))); 7763SUM( distinct a ) 77641 7765SELECT SUM( distinct a ) FROM t1 GROUP BY a HAVING a + 1 IN ( (select lead(1, 0) over (ORDER BY null))); 7766SUM( distinct a ) 7767SELECT SUM( distinct a ) FROM t1 GROUP BY a HAVING a IN ( (select nth_value(1, 1) over (ORDER BY null))); 7768SUM( distinct a ) 77691 7770SELECT SUM( distinct a ) FROM t1 GROUP BY a HAVING a + 1 IN ( (select nth_value(1, 1) over (ORDER BY null))); 7771SUM( distinct a ) 7772SELECT SUM( distinct a ) FROM t1 GROUP BY a HAVING a IN ( (select first_value(1) over (ORDER BY null))); 7773SUM( distinct a ) 77741 7775SELECT SUM( distinct a ) FROM t1 GROUP BY a HAVING a +1 IN ( (select first_value(1) over (ORDER BY null))); 7776SUM( distinct a ) 7777SELECT SUM( distinct a ) FROM t1 GROUP BY a HAVING a IN ( (select last_value(1) over (ORDER BY null))); 7778SUM( distinct a ) 77791 7780SELECT SUM( distinct a ) FROM t1 GROUP BY a HAVING a +1 IN ( (select last_value(1) over (ORDER BY null))); 7781SUM( distinct a ) 7782DROP TABLE t1; 7783CREATE TABLE t1(a DECIMAL(4,2)); 7784INSERT INTO t1 VALUES(1); 7785SELECT SUM( distinct a ) FROM t1 GROUP BY a HAVING a IN ( (select row_number() over ())); 7786SUM( distinct a ) 77871.00 7788SELECT SUM( distinct a ) FROM t1 GROUP BY a HAVING a + 1 IN ( (select row_number() over ())); 7789SUM( distinct a ) 7790SELECT SUM( distinct a ) FROM t1 GROUP BY a HAVING a IN ( (select ntile(3) over (order by null))); 7791SUM( distinct a ) 77921.00 7793SELECT SUM( distinct a ) FROM t1 GROUP BY a HAVING a + 1 IN ( (select ntile(3) over (order by null))); 7794SUM( distinct a ) 7795SELECT SUM( distinct a ) FROM t1 GROUP BY a HAVING a IN ( (select avg(1) over ())); 7796SUM( distinct a ) 77971.00 7798SELECT SUM( distinct a ) FROM t1 GROUP BY a HAVING a + 1 IN ( (select avg(1) over ())); 7799SUM( distinct a ) 7800SELECT SUM( distinct a ) FROM t1 GROUP BY a HAVING a IN ( (select sum(1) over ())); 7801SUM( distinct a ) 78021.00 7803SELECT SUM( distinct a ) FROM t1 GROUP BY a HAVING a + 1 IN ( (select sum(1) over ())); 7804SUM( distinct a ) 7805SELECT SUM( distinct a ) FROM t1 GROUP BY a HAVING a IN ( (select count(1) over ())); 7806SUM( distinct a ) 78071.00 7808SELECT SUM( distinct a ) FROM t1 GROUP BY a HAVING a + 1 IN ( (select count(1) over ())); 7809SUM( distinct a ) 7810SELECT SUM( distinct a ) FROM t1 GROUP BY a HAVING a IN ( (select rank() over (ORDER BY null))); 7811SUM( distinct a ) 78121.00 7813SELECT SUM( distinct a ) FROM t1 GROUP BY a HAVING a + 1 IN ( (select rank() over (ORDER BY null))); 7814SUM( distinct a ) 7815SELECT SUM( distinct a ) FROM t1 GROUP BY a HAVING a IN ( (select dense_rank() over (ORDER BY null))); 7816SUM( distinct a ) 78171.00 7818SELECT SUM( distinct a ) FROM t1 GROUP BY a HAVING a + 1 IN ( (select dense_rank() over (ORDER BY null))); 7819SUM( distinct a ) 7820SELECT SUM( distinct a ) FROM t1 GROUP BY a HAVING a - 1 IN ( (select percent_rank() over (ORDER BY null))); 7821SUM( distinct a ) 78221.00 7823SELECT SUM( distinct a ) FROM t1 GROUP BY a HAVING a IN ( (select percent_rank() over (ORDER BY null))); 7824SUM( distinct a ) 7825SELECT SUM( distinct a ) FROM t1 GROUP BY a HAVING a IN ( (select cume_dist() over (ORDER BY null))); 7826SUM( distinct a ) 78271.00 7828SELECT SUM( distinct a ) FROM t1 GROUP BY a HAVING a + 1 IN ( (select cume_dist() over (ORDER BY null))); 7829SUM( distinct a ) 7830SELECT SUM( distinct a ) FROM t1 GROUP BY a HAVING a IN ( (select lead(1, 0) over (ORDER BY null))); 7831SUM( distinct a ) 78321.00 7833SELECT SUM( distinct a ) FROM t1 GROUP BY a HAVING a + 1 IN ( (select lead(1, 0) over (ORDER BY null))); 7834SUM( distinct a ) 7835SELECT SUM( distinct a ) FROM t1 GROUP BY a HAVING a IN ( (select nth_value(1, 1) over (ORDER BY null))); 7836SUM( distinct a ) 78371.00 7838SELECT SUM( distinct a ) FROM t1 GROUP BY a HAVING a + 1 IN ( (select nth_value(1, 1) over (ORDER BY null))); 7839SUM( distinct a ) 7840SELECT SUM( distinct a ) FROM t1 GROUP BY a HAVING a IN ( (select first_value(1) over (ORDER BY null))); 7841SUM( distinct a ) 78421.00 7843SELECT SUM( distinct a ) FROM t1 GROUP BY a HAVING a +1 IN ( (select first_value(1) over (ORDER BY null))); 7844SUM( distinct a ) 7845SELECT SUM( distinct a ) FROM t1 GROUP BY a HAVING a IN ( (select last_value(1) over (ORDER BY null))); 7846SUM( distinct a ) 78471.00 7848SELECT SUM( distinct a ) FROM t1 GROUP BY a HAVING a +1 IN ( (select last_value(1) over (ORDER BY null))); 7849SUM( distinct a ) 7850DROP TABLE t1; 7851CREATE TABLE t1(a REAL); 7852INSERT INTO t1 VALUES(1.0); 7853SELECT SUM( distinct a ) FROM t1 GROUP BY a HAVING a IN ( (select row_number() over ())); 7854SUM( distinct a ) 78551 7856SELECT SUM( distinct a ) FROM t1 GROUP BY a HAVING a + 1 IN ( (select row_number() over ())); 7857SUM( distinct a ) 7858SELECT SUM( distinct a ) FROM t1 GROUP BY a HAVING a IN ( (select ntile(3) over (order by null))); 7859SUM( distinct a ) 78601 7861SELECT SUM( distinct a ) FROM t1 GROUP BY a HAVING a + 1 IN ( (select ntile(3) over (order by null))); 7862SUM( distinct a ) 7863SELECT SUM( distinct a ) FROM t1 GROUP BY a HAVING a IN ( (select avg(1) over ())); 7864SUM( distinct a ) 78651 7866SELECT SUM( distinct a ) FROM t1 GROUP BY a HAVING a + 1 IN ( (select avg(1) over ())); 7867SUM( distinct a ) 7868SELECT SUM( distinct a ) FROM t1 GROUP BY a HAVING a IN ( (select sum(1) over ())); 7869SUM( distinct a ) 78701 7871SELECT SUM( distinct a ) FROM t1 GROUP BY a HAVING a + 1 IN ( (select sum(1) over ())); 7872SUM( distinct a ) 7873SELECT SUM( distinct a ) FROM t1 GROUP BY a HAVING a IN ( (select count(1) over ())); 7874SUM( distinct a ) 78751 7876SELECT SUM( distinct a ) FROM t1 GROUP BY a HAVING a + 1 IN ( (select count(1) over ())); 7877SUM( distinct a ) 7878SELECT SUM( distinct a ) FROM t1 GROUP BY a HAVING a IN ( (select rank() over (ORDER BY null))); 7879SUM( distinct a ) 78801 7881SELECT SUM( distinct a ) FROM t1 GROUP BY a HAVING a + 1 IN ( (select rank() over (ORDER BY null))); 7882SUM( distinct a ) 7883SELECT SUM( distinct a ) FROM t1 GROUP BY a HAVING a IN ( (select dense_rank() over (ORDER BY null))); 7884SUM( distinct a ) 78851 7886SELECT SUM( distinct a ) FROM t1 GROUP BY a HAVING a + 1 IN ( (select dense_rank() over (ORDER BY null))); 7887SUM( distinct a ) 7888SELECT SUM( distinct a ) FROM t1 GROUP BY a HAVING a - 1 IN ( (select percent_rank() over (ORDER BY null))); 7889SUM( distinct a ) 78901 7891SELECT SUM( distinct a ) FROM t1 GROUP BY a HAVING a IN ( (select percent_rank() over (ORDER BY null))); 7892SUM( distinct a ) 7893SELECT SUM( distinct a ) FROM t1 GROUP BY a HAVING a IN ( (select cume_dist() over (ORDER BY null))); 7894SUM( distinct a ) 78951 7896SELECT SUM( distinct a ) FROM t1 GROUP BY a HAVING a + 1 IN ( (select cume_dist() over (ORDER BY null))); 7897SUM( distinct a ) 7898SELECT SUM( distinct a ) FROM t1 GROUP BY a HAVING a IN ( (select lead(1, 0) over (ORDER BY null))); 7899SUM( distinct a ) 79001 7901SELECT SUM( distinct a ) FROM t1 GROUP BY a HAVING a + 1 IN ( (select lead(1, 0) over (ORDER BY null))); 7902SUM( distinct a ) 7903SELECT SUM( distinct a ) FROM t1 GROUP BY a HAVING a IN ( (select nth_value(1, 1) over (ORDER BY null))); 7904SUM( distinct a ) 79051 7906SELECT SUM( distinct a ) FROM t1 GROUP BY a HAVING a + 1 IN ( (select nth_value(1, 1) over (ORDER BY null))); 7907SUM( distinct a ) 7908SELECT SUM( distinct a ) FROM t1 GROUP BY a HAVING a IN ( (select first_value(1) over (ORDER BY null))); 7909SUM( distinct a ) 79101 7911SELECT SUM( distinct a ) FROM t1 GROUP BY a HAVING a +1 IN ( (select first_value(1) over (ORDER BY null))); 7912SUM( distinct a ) 7913SELECT SUM( distinct a ) FROM t1 GROUP BY a HAVING a IN ( (select last_value(1) over (ORDER BY null))); 7914SUM( distinct a ) 79151 7916SELECT SUM( distinct a ) FROM t1 GROUP BY a HAVING a +1 IN ( (select last_value(1) over (ORDER BY null))); 7917SUM( distinct a ) 7918DROP TABLE t1; 7919CREATE TABLE t1(a TIME); 7920INSERT INTO t1 VALUES('00:00:01'); 7921SELECT SUM( distinct a ) FROM t1 GROUP BY a HAVING a IN ( (select row_number() over ())); 7922SUM( distinct a ) 79231 7924SELECT SUM( distinct a ) FROM t1 GROUP BY a HAVING a + 1 IN ( (select row_number() over ())); 7925SUM( distinct a ) 7926SELECT SUM( distinct a ) FROM t1 GROUP BY a HAVING a IN ( (select ntile(3) over (order by null))); 7927SUM( distinct a ) 79281 7929SELECT SUM( distinct a ) FROM t1 GROUP BY a HAVING a + 1 IN ( (select ntile(3) over (order by null))); 7930SUM( distinct a ) 7931SELECT SUM( distinct a ) FROM t1 GROUP BY a HAVING a IN ( (select avg(1) over ())); 7932SUM( distinct a ) 79331 7934SELECT SUM( distinct a ) FROM t1 GROUP BY a HAVING a + 1 IN ( (select avg(1) over ())); 7935SUM( distinct a ) 7936SELECT SUM( distinct a ) FROM t1 GROUP BY a HAVING a IN ( (select sum(1) over ())); 7937SUM( distinct a ) 79381 7939SELECT SUM( distinct a ) FROM t1 GROUP BY a HAVING a + 1 IN ( (select sum(1) over ())); 7940SUM( distinct a ) 7941SELECT SUM( distinct a ) FROM t1 GROUP BY a HAVING a IN ( (select count(1) over ())); 7942SUM( distinct a ) 79431 7944SELECT SUM( distinct a ) FROM t1 GROUP BY a HAVING a + 1 IN ( (select count(1) over ())); 7945SUM( distinct a ) 7946SELECT SUM( distinct a ) FROM t1 GROUP BY a HAVING a IN ( (select rank() over (ORDER BY null))); 7947SUM( distinct a ) 79481 7949SELECT SUM( distinct a ) FROM t1 GROUP BY a HAVING a + 1 IN ( (select rank() over (ORDER BY null))); 7950SUM( distinct a ) 7951SELECT SUM( distinct a ) FROM t1 GROUP BY a HAVING a IN ( (select dense_rank() over (ORDER BY null))); 7952SUM( distinct a ) 79531 7954SELECT SUM( distinct a ) FROM t1 GROUP BY a HAVING a + 1 IN ( (select dense_rank() over (ORDER BY null))); 7955SUM( distinct a ) 7956SELECT SUM( distinct a ) FROM t1 GROUP BY a HAVING a - 1 IN ( (select percent_rank() over (ORDER BY null))); 7957SUM( distinct a ) 79581 7959SELECT SUM( distinct a ) FROM t1 GROUP BY a HAVING a IN ( (select percent_rank() over (ORDER BY null))); 7960SUM( distinct a ) 7961SELECT SUM( distinct a ) FROM t1 GROUP BY a HAVING a IN ( (select cume_dist() over (ORDER BY null))); 7962SUM( distinct a ) 79631 7964SELECT SUM( distinct a ) FROM t1 GROUP BY a HAVING a + 1 IN ( (select cume_dist() over (ORDER BY null))); 7965SUM( distinct a ) 7966SELECT SUM( distinct a ) FROM t1 GROUP BY a HAVING a IN ( (select lead(1, 0) over (ORDER BY null))); 7967SUM( distinct a ) 79681 7969SELECT SUM( distinct a ) FROM t1 GROUP BY a HAVING a + 1 IN ( (select lead(1, 0) over (ORDER BY null))); 7970SUM( distinct a ) 7971SELECT SUM( distinct a ) FROM t1 GROUP BY a HAVING a IN ( (select nth_value(1, 1) over (ORDER BY null))); 7972SUM( distinct a ) 79731 7974SELECT SUM( distinct a ) FROM t1 GROUP BY a HAVING a + 1 IN ( (select nth_value(1, 1) over (ORDER BY null))); 7975SUM( distinct a ) 7976SELECT SUM( distinct a ) FROM t1 GROUP BY a HAVING a IN ( (select first_value(1) over (ORDER BY null))); 7977SUM( distinct a ) 79781 7979SELECT SUM( distinct a ) FROM t1 GROUP BY a HAVING a +1 IN ( (select first_value(1) over (ORDER BY null))); 7980SUM( distinct a ) 7981SELECT SUM( distinct a ) FROM t1 GROUP BY a HAVING a IN ( (select last_value(1) over (ORDER BY null))); 7982SUM( distinct a ) 79831 7984SELECT SUM( distinct a ) FROM t1 GROUP BY a HAVING a +1 IN ( (select last_value(1) over (ORDER BY null))); 7985SUM( distinct a ) 7986DROP TABLE t1; 7987CREATE TABLE t1(a INT); 7988INSERT INTO t1 VALUES(1); 7989SELECT SUM( distinct a ) FROM t1 GROUP BY a HAVING a IN ( (select row_number() over ())); 7990SUM( distinct a ) 79911 7992SELECT SUM( distinct a ) FROM t1 GROUP BY a HAVING a + 1 IN ( (select row_number() over ())); 7993SUM( distinct a ) 7994SELECT SUM( distinct a ) FROM t1 GROUP BY a HAVING a IN ( (select ntile(3) over (order by null))); 7995SUM( distinct a ) 79961 7997SELECT SUM( distinct a ) FROM t1 GROUP BY a HAVING a + 1 IN ( (select ntile(3) over (order by null))); 7998SUM( distinct a ) 7999SELECT SUM( distinct a ) FROM t1 GROUP BY a HAVING a IN ( (select avg(1) over ())); 8000SUM( distinct a ) 80011 8002SELECT SUM( distinct a ) FROM t1 GROUP BY a HAVING a + 1 IN ( (select avg(1) over ())); 8003SUM( distinct a ) 8004SELECT SUM( distinct a ) FROM t1 GROUP BY a HAVING a IN ( (select sum(1) over ())); 8005SUM( distinct a ) 80061 8007SELECT SUM( distinct a ) FROM t1 GROUP BY a HAVING a + 1 IN ( (select sum(1) over ())); 8008SUM( distinct a ) 8009SELECT SUM( distinct a ) FROM t1 GROUP BY a HAVING a IN ( (select count(1) over ())); 8010SUM( distinct a ) 80111 8012SELECT SUM( distinct a ) FROM t1 GROUP BY a HAVING a + 1 IN ( (select count(1) over ())); 8013SUM( distinct a ) 8014SELECT SUM( distinct a ) FROM t1 GROUP BY a HAVING a IN ( (select rank() over (ORDER BY null))); 8015SUM( distinct a ) 80161 8017SELECT SUM( distinct a ) FROM t1 GROUP BY a HAVING a + 1 IN ( (select rank() over (ORDER BY null))); 8018SUM( distinct a ) 8019SELECT SUM( distinct a ) FROM t1 GROUP BY a HAVING a IN ( (select dense_rank() over (ORDER BY null))); 8020SUM( distinct a ) 80211 8022SELECT SUM( distinct a ) FROM t1 GROUP BY a HAVING a + 1 IN ( (select dense_rank() over (ORDER BY null))); 8023SUM( distinct a ) 8024SELECT SUM( distinct a ) FROM t1 GROUP BY a HAVING a - 1 IN ( (select percent_rank() over (ORDER BY null))); 8025SUM( distinct a ) 80261 8027SELECT SUM( distinct a ) FROM t1 GROUP BY a HAVING a IN ( (select percent_rank() over (ORDER BY null))); 8028SUM( distinct a ) 8029SELECT SUM( distinct a ) FROM t1 GROUP BY a HAVING a IN ( (select cume_dist() over (ORDER BY null))); 8030SUM( distinct a ) 80311 8032SELECT SUM( distinct a ) FROM t1 GROUP BY a HAVING a + 1 IN ( (select cume_dist() over (ORDER BY null))); 8033SUM( distinct a ) 8034SELECT SUM( distinct a ) FROM t1 GROUP BY a HAVING a IN ( (select lead(1, 0) over (ORDER BY null))); 8035SUM( distinct a ) 80361 8037SELECT SUM( distinct a ) FROM t1 GROUP BY a HAVING a + 1 IN ( (select lead(1, 0) over (ORDER BY null))); 8038SUM( distinct a ) 8039SELECT SUM( distinct a ) FROM t1 GROUP BY a HAVING a IN ( (select nth_value(1, 1) over (ORDER BY null))); 8040SUM( distinct a ) 80411 8042SELECT SUM( distinct a ) FROM t1 GROUP BY a HAVING a + 1 IN ( (select nth_value(1, 1) over (ORDER BY null))); 8043SUM( distinct a ) 8044SELECT SUM( distinct a ) FROM t1 GROUP BY a HAVING a IN ( (select first_value(1) over (ORDER BY null))); 8045SUM( distinct a ) 80461 8047SELECT SUM( distinct a ) FROM t1 GROUP BY a HAVING a +1 IN ( (select first_value(1) over (ORDER BY null))); 8048SUM( distinct a ) 8049SELECT SUM( distinct a ) FROM t1 GROUP BY a HAVING a IN ( (select last_value(1) over (ORDER BY null))); 8050SUM( distinct a ) 80511 8052SELECT SUM( distinct a ) FROM t1 GROUP BY a HAVING a +1 IN ( (select last_value(1) over (ORDER BY null))); 8053SUM( distinct a ) 8054DROP TABLE t1; 8055CREATE TABLE t1(a JSON); 8056INSERT INTO t1 VALUES('1'); 8057SELECT SUM( distinct a ) FROM t1 GROUP BY a HAVING a IN ( (select row_number() over ())); 8058SUM( distinct a ) 80591 8060SELECT SUM( distinct a ) FROM t1 GROUP BY a HAVING a + 1 IN ( (select row_number() over ())); 8061SUM( distinct a ) 8062SELECT SUM( distinct a ) FROM t1 GROUP BY a HAVING a IN ( (select ntile(3) over (order by null))); 8063SUM( distinct a ) 80641 8065SELECT SUM( distinct a ) FROM t1 GROUP BY a HAVING a + 1 IN ( (select ntile(3) over (order by null))); 8066SUM( distinct a ) 8067SELECT SUM( distinct a ) FROM t1 GROUP BY a HAVING a IN ( (select avg(1) over ())); 8068SUM( distinct a ) 80691 8070SELECT SUM( distinct a ) FROM t1 GROUP BY a HAVING a + 1 IN ( (select avg(1) over ())); 8071SUM( distinct a ) 8072SELECT SUM( distinct a ) FROM t1 GROUP BY a HAVING a IN ( (select sum(1) over ())); 8073SUM( distinct a ) 80741 8075SELECT SUM( distinct a ) FROM t1 GROUP BY a HAVING a + 1 IN ( (select sum(1) over ())); 8076SUM( distinct a ) 8077SELECT SUM( distinct a ) FROM t1 GROUP BY a HAVING a IN ( (select count(1) over ())); 8078SUM( distinct a ) 80791 8080SELECT SUM( distinct a ) FROM t1 GROUP BY a HAVING a + 1 IN ( (select count(1) over ())); 8081SUM( distinct a ) 8082SELECT SUM( distinct a ) FROM t1 GROUP BY a HAVING a IN ( (select rank() over (ORDER BY null))); 8083SUM( distinct a ) 80841 8085SELECT SUM( distinct a ) FROM t1 GROUP BY a HAVING a + 1 IN ( (select rank() over (ORDER BY null))); 8086SUM( distinct a ) 8087SELECT SUM( distinct a ) FROM t1 GROUP BY a HAVING a IN ( (select dense_rank() over (ORDER BY null))); 8088SUM( distinct a ) 80891 8090SELECT SUM( distinct a ) FROM t1 GROUP BY a HAVING a + 1 IN ( (select dense_rank() over (ORDER BY null))); 8091SUM( distinct a ) 8092SELECT SUM( distinct a ) FROM t1 GROUP BY a HAVING a - 1 IN ( (select percent_rank() over (ORDER BY null))); 8093SUM( distinct a ) 80941 8095SELECT SUM( distinct a ) FROM t1 GROUP BY a HAVING a IN ( (select percent_rank() over (ORDER BY null))); 8096SUM( distinct a ) 8097SELECT SUM( distinct a ) FROM t1 GROUP BY a HAVING a IN ( (select cume_dist() over (ORDER BY null))); 8098SUM( distinct a ) 80991 8100SELECT SUM( distinct a ) FROM t1 GROUP BY a HAVING a + 1 IN ( (select cume_dist() over (ORDER BY null))); 8101SUM( distinct a ) 8102SELECT SUM( distinct a ) FROM t1 GROUP BY a HAVING a IN ( (select lead(1, 0) over (ORDER BY null))); 8103SUM( distinct a ) 81041 8105SELECT SUM( distinct a ) FROM t1 GROUP BY a HAVING a + 1 IN ( (select lead(1, 0) over (ORDER BY null))); 8106SUM( distinct a ) 8107SELECT SUM( distinct a ) FROM t1 GROUP BY a HAVING a IN ( (select nth_value(1, 1) over (ORDER BY null))); 8108SUM( distinct a ) 81091 8110SELECT SUM( distinct a ) FROM t1 GROUP BY a HAVING a + 1 IN ( (select nth_value(1, 1) over (ORDER BY null))); 8111SUM( distinct a ) 8112SELECT SUM( distinct a ) FROM t1 GROUP BY a HAVING a IN ( (select first_value(1) over (ORDER BY null))); 8113SUM( distinct a ) 81141 8115SELECT SUM( distinct a ) FROM t1 GROUP BY a HAVING a +1 IN ( (select first_value(1) over (ORDER BY null))); 8116SUM( distinct a ) 8117SELECT SUM( distinct a ) FROM t1 GROUP BY a HAVING a IN ( (select last_value(1) over (ORDER BY null))); 8118SUM( distinct a ) 81191 8120SELECT SUM( distinct a ) FROM t1 GROUP BY a HAVING a +1 IN ( (select last_value(1) over (ORDER BY null))); 8121SUM( distinct a ) 8122DROP TABLE t1; 8123# 8124# Problem with ROLLUP and WINDOW functions 8125# 8126CREATE TABLE t1 (i INTEGER); 8127INSERT INTO t1 values (1); 8128INSERT INTO t1 values (1); 8129INSERT INTO t1 values (2); 8130INSERT INTO t1 values (2); 8131INSERT INTO t1 values (3); 8132INSERT INTO t1 values (3); 8133INSERT INTO t1 values (4); 8134INSERT INTO t1 values (4); 8135SELECT i, i+1, SUM(i) OVER () FROM t1 GROUP BY i WITH ROLLUP; 8136i i+1 SUM(i) OVER () 81371 2 10 81382 3 10 81393 4 10 81404 5 10 8141NULL NULL 10 8142DROP TABLE t1; 8143# 8144# Bug#26033662 WL#9603: ZERO RESULT FOR WINDOW FUNCTIONS WITH DERIVED TABLE HAVING GROUP BY 8145# 8146CREATE TABLE t(a INT, b INT); 8147INSERT INTO t VALUES (1,1), (1,2), (2,3), (2,4), (2,5), (2,6), (3,7), (3,11), (4,8); 8148SELECT * FROM (SELECT b, RANK() OVER (ORDER BY b) AS c FROM t GROUP BY a,b) s1 UNION 8149(SELECT b, MAX(a) AS c FROM t GROUP BY a,b ) ORDER BY b; 8150b c 81511 1 815211 3 815311 9 81542 1 81552 2 81563 2 81573 3 81584 2 81594 4 81605 2 81615 5 81626 2 81636 6 81647 3 81657 7 81668 4 81678 8 8168DROP TABLE t; 8169# 8170# Bug#26035785 WL#9603: NUMBER OF ROWS PER NTILE BUCKET DIFFER UNEVENLY 8171# 8172CREATE TABLE t (col_int int) ; 8173INSERT INTO t VALUES 8174(7),(184),(12),(8),(14),(14),(2),(11),(6),(113),(4),(9),(2),(6),(10),(178),(1) 8175,(2),(8),(204),(4),(15),(7),(253),(14),(3),(10),(11),(15),(15); 8176SELECT nt, COUNT(*) FROM 8177(SELECT col_int, NTILE( 9 ) OVER ( ORDER BY col_int) nt FROM t ) AS s1 8178GROUP BY nt; 8179nt COUNT(*) 81801 4 81812 4 81823 4 81834 3 81845 3 81856 3 81867 3 81878 3 81889 3 8189DROP TABLE t; 8190# 8191# Bug#26048785 WL9603: RANGE UNBOUNDED PRECEDING IS TOO STRICT ON ORDER BY 8192# Tests for RANGE w/o ORDER BY, and with ORDER BY <char/blob> 8193# 8194CREATE TABLE t1(a INT, b CHAR(1), c DATETIME, d BLOB); 8195INSERT INTO t1 VALUES (1,'x','2010-01-01','blob'), (2, 'y', '2011-01-01', ''), 8196(3, 'y', '2012-01-01', ''), (4, 't', '2012-01-01', 'blob'), 8197(5, null, '2013-01-01', null); 8198SELECT a, b, c, SUM(a) OVER 8199(RANGE BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW) 8200FROM t1; 8201a b c SUM(a) OVER 8202(RANGE BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW) 82031 x 2010-01-01 00:00:00 15 82042 y 2011-01-01 00:00:00 15 82053 y 2012-01-01 00:00:00 15 82064 t 2012-01-01 00:00:00 15 82075 NULL 2013-01-01 00:00:00 15 8208SELECT a, b, c, SUM(a) OVER 8209(ORDER BY b RANGE BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW) 8210FROM t1; 8211a b c SUM(a) OVER 8212(ORDER BY b RANGE BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW) 82135 NULL 2013-01-01 00:00:00 5 82144 t 2012-01-01 00:00:00 9 82151 x 2010-01-01 00:00:00 10 82162 y 2011-01-01 00:00:00 15 82173 y 2012-01-01 00:00:00 15 8218SELECT a, b, c, SUM(a) OVER 8219(w RANGE BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW) 8220FROM t1 8221WINDOW w AS (ORDER BY b); 8222a b c SUM(a) OVER 8223(w RANGE BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW) 82245 NULL 2013-01-01 00:00:00 5 82254 t 2012-01-01 00:00:00 9 82261 x 2010-01-01 00:00:00 10 82272 y 2011-01-01 00:00:00 15 82283 y 2012-01-01 00:00:00 15 8229SELECT a, b, c, d, SUM(a) OVER 8230(ORDER BY d RANGE BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW) 8231FROM t1; 8232a b c d SUM(a) OVER 8233(ORDER BY d RANGE BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW) 82345 NULL 2013-01-01 00:00:00 NULL 5 82352 y 2011-01-01 00:00:00 10 82363 y 2012-01-01 00:00:00 10 82371 x 2010-01-01 00:00:00 blob 15 82384 t 2012-01-01 00:00:00 blob 15 8239SELECT a, b, c, SUM(a) OVER 8240(RANGE UNBOUNDED PRECEDING) 8241FROM t1; 8242a b c SUM(a) OVER 8243(RANGE UNBOUNDED PRECEDING) 82441 x 2010-01-01 00:00:00 15 82452 y 2011-01-01 00:00:00 15 82463 y 2012-01-01 00:00:00 15 82474 t 2012-01-01 00:00:00 15 82485 NULL 2013-01-01 00:00:00 15 8249SELECT a, b, c, SUM(a) OVER 8250(w RANGE UNBOUNDED PRECEDING) 8251FROM t1 8252WINDOW w AS (); 8253a b c SUM(a) OVER 8254(w RANGE UNBOUNDED PRECEDING) 82551 x 2010-01-01 00:00:00 15 82562 y 2011-01-01 00:00:00 15 82573 y 2012-01-01 00:00:00 15 82584 t 2012-01-01 00:00:00 15 82595 NULL 2013-01-01 00:00:00 15 8260SELECT a, b, c, SUM(a) OVER 8261(RANGE BETWEEN 2 PRECEDING AND CURRENT ROW) 8262FROM t1; 8263ERROR HY000: Window '<unnamed window>' with RANGE N PRECEDING/FOLLOWING frame requires exactly one ORDER BY expression, of numeric or temporal type 8264SELECT a, b, c, SUM(a) OVER 8265(ORDER BY b RANGE BETWEEN 2 PRECEDING AND CURRENT ROW) 8266FROM t1; 8267ERROR HY000: Window '<unnamed window>' with RANGE N PRECEDING/FOLLOWING frame requires exactly one ORDER BY expression, of numeric or temporal type 8268SELECT a, b, c, SUM(a) OVER 8269(ORDER BY a RANGE BETWEEN 2 PRECEDING AND CURRENT ROW) 8270FROM t1; 8271a b c SUM(a) OVER 8272(ORDER BY a RANGE BETWEEN 2 PRECEDING AND CURRENT ROW) 82731 x 2010-01-01 00:00:00 1 82742 y 2011-01-01 00:00:00 3 82753 y 2012-01-01 00:00:00 6 82764 t 2012-01-01 00:00:00 9 82775 NULL 2013-01-01 00:00:00 12 8278SELECT a, b, c, SUM(a) OVER 8279(ORDER BY c RANGE BETWEEN 2 PRECEDING AND CURRENT ROW) 8280FROM t1; 8281ERROR HY000: Window '<unnamed window>' with RANGE frame has ORDER BY expression of datetime type. Only INTERVAL bound value allowed. 8282SELECT a, b, c, SUM(a) OVER 8283(ORDER BY c RANGE BETWEEN INTERVAL 1 HOUR PRECEDING AND CURRENT ROW) 8284FROM t1; 8285a b c SUM(a) OVER 8286(ORDER BY c RANGE BETWEEN INTERVAL 1 HOUR PRECEDING AND CURRENT ROW) 82871 x 2010-01-01 00:00:00 1 82882 y 2011-01-01 00:00:00 2 82893 y 2012-01-01 00:00:00 7 82904 t 2012-01-01 00:00:00 7 82915 NULL 2013-01-01 00:00:00 5 8292DROP TABLE t1; 8293# 8294# Bug#26022124 WL#9603: SIG11 AT AGGREGATOR_DISTINCT::ENDUP IN SQL/ITEM_SUM.CC 8295# 8296CREATE TABLE t1 (pk int NOT NULL, col_int int DEFAULT NULL); 8297CREATE TABLE t2 (pk int NOT NULL AUTO_INCREMENT PRIMARY KEY, 8298col_int int DEFAULT NULL, 8299col_int2 int DEFAULT NULL); 8300INSERT INTO t1 VALUES (1,88), (2,99); 8301INSERT INTO t2 VALUES (1,2,3) , (4,5,6); 8302SELECT ROW_NUMBER() OVER (ORDER BY t2.col_int) AS rn, 8303COUNT(DISTINCT t1.col_int) AS cnt, 8304SUM(DISTINCT t1.col_int) AS `sum` 8305 FROM t1 LEFT JOIN t2 ON t1.pk = t2.pk WHERE t1.pk IN (1) 8306GROUP BY t1.pk; 8307rn cnt sum 83081 1 88 8309SELECT ROW_NUMBER() OVER () AS rn, 8310COUNT(DISTINCT t1.col_int) AS cnt, 8311SUM(DISTINCT t1.col_int) AS `sum` 8312 FROM t1 LEFT JOIN t2 ON t1.pk = t2.pk WHERE t1.pk IN (1) 8313GROUP BY t1.pk; 8314rn cnt sum 83151 1 88 8316SELECT ROW_NUMBER() OVER (), 8317FIRST_VALUE(SUM(DISTINCT t1.col_int)) OVER (ORDER BY t1.pk), 8318FIRST_VALUE(SUM(DISTINCT t1.col_int) + 1) OVER (ORDER BY t1.pk), 8319SUM(DISTINCT t1.col_int), 8320RANK() OVER (ORDER BY t1.pk) 8321FROM t1 LEFT JOIN t2 ON t1.pk = t2.pk WHERE t1.pk IN (1) 8322GROUP BY t1.pk; 8323ROW_NUMBER() OVER () FIRST_VALUE(SUM(DISTINCT t1.col_int)) OVER (ORDER BY t1.pk) FIRST_VALUE(SUM(DISTINCT t1.col_int) + 1) OVER (ORDER BY t1.pk) SUM(DISTINCT t1.col_int) RANK() OVER (ORDER BY t1.pk) 83241 88 89 88 1 8325DROP TABLE t1, t2; 8326# 8327# Bug #26115726 WL#9727: RESULT DIFF FOR STD_SAMP IN A VIEW 8328# 8329CREATE TABLE t(a int); 8330INSERT INTO t VALUES (NULL), (1), (3), (6), (10); 8331CREATE VIEW v1 AS 8332SELECT STDDEV_SAMP(a) OVER ( ORDER BY a ROWS CURRENT ROW) AS std_dev_samp FROM t; 8333SELECT STDDEV_SAMP(a) OVER ( ORDER BY a ROWS CURRENT ROW ) AS std_dev_samp FROM t; 8334std_dev_samp 8335NULL 8336NULL 8337NULL 8338NULL 8339NULL 8340SELECT * FROM v1; 8341std_dev_samp 8342NULL 8343NULL 8344NULL 8345NULL 8346NULL 8347CREATE OR REPLACE VIEW v1 AS SELECT STDDEV_SAMP(a) FROM t; 8348SELECT STDDEV_SAMP(a) FROM t; 8349STDDEV_SAMP(a) 83503.9157800414902435 8351SELECT * FROM v1; 8352STDDEV_SAMP(a) 83533.9157800414902435 8354DROP VIEW v1; 8355DROP TABLE t; 8356# 8357# Bug #26132963 WL#9603: SIG11 AT SORTLENGTH() SQL/FILESORT.CC 8358# 8359CREATE TABLE t1(c1 int, c2 int); 8360CREATE TABLE t2(c1 int, c2 int); 8361INSERT INTO t1 VALUES 8362(1,-1208352768),(2,NULL),(3,212140032),(4,5),(5,3),(6,NULL),(7,1),(8,9), 8363(9,-1563688960),(10,-288358400),(11,NULL),(12,9),(13,9),(14,NULL),(15,NULL), 8364(16,NULL),(17,5),(18,1),(19,4),(20,0),(21,-2036334592),(22,1), 8365(23,1961558016),(24,0),(25,NULL); 8366INSERT INTO t2 VALUES 8367(1,553648128),(2,0),(3,3),(4,8),(5,1662844928),(6,0),(7,NULL), 8368(8,7),(9,NULL),(10,NULL); 8369SELECT RANK() OVER ( ORDER BY MIN( t2 . c1 ) + MAX( t1 . c2 ) ) AS rnk 8370FROM t1 RIGHT OUTER JOIN t2 ON t1.c1 = t2.c2 8371ORDER BY rnk; 8372rnk 83731 8374SELECT 2 FROM t1 8375ORDER BY SUM(c1) OVER 8376(PARTITION BY COUNT(c2)+(SELECT SUM(c1) FROM t2)) DESC; 83772 83782 8379DROP TABLE t1, t2; 8380# 8381# Bug #26115664 WL#9603: WRONG RESULT WITH STORED PROGRAM+AGG WFS ON SINGLE ROW MEMORY TABLE 8382# 8383CREATE TABLE te2 (c1 int, c2 int) ENGINE=Memory; 8384INSERT INTO te2 VALUES(1,1351614464 ); 8385PREPARE ps FROM "SELECT AVG(c2) OVER ( ) AS res FROM te2"; 8386EXECUTE ps; 8387res 83881351614464.0000 8389EXECUTE ps; 8390res 83911351614464.0000 8392EXECUTE ps; 8393res 83941351614464.0000 8395CREATE PROCEDURE p1() SELECT AVG(c2) OVER ( ) AS res FROM te2; 8396CALL p1(); 8397res 83981351614464.0000 8399CALL p1(); 8400res 84011351614464.0000 8402CALL p1(); 8403res 84041351614464.0000 8405CREATE FUNCTION f1() 8406RETURNS char(255) 8407BEGIN 8408DECLARE ret char(255); 8409SELECT AVG(c2) OVER ( ) AS res FROM te2 INTO ret; 8410RETURN ret; 8411END! 8412SELECT f1(); 8413f1() 84141351614464 8415SELECT f1(); 8416f1() 84171351614464 8418SELECT f1(); 8419f1() 84201351614464 8421DROP FUNCTION f1; 8422DROP PROCEDURE p1; 8423DROP TABLE te2; 8424# 8425# Bug#26129809 WL#9603: VALGRIND REPORTS INVALID READ AT MY_STRNNCOLLSP_SIMPLE|CTYPE-SIMPLE.CC 8426# 8427set sql_mode=''; 8428CREATE TABLE `A` ( 8429`col_varchar_10_utf8` varchar(10) CHARACTER SET utf8 DEFAULT NULL, 8430`col_varchar_10_latin1_key` varchar(10) DEFAULT NULL, 8431`col_int` int(11) DEFAULT NULL, 8432`col_date` date DEFAULT NULL, 8433`col_date_key` date DEFAULT NULL, 8434`col_varchar_255_utf8` varchar(255) CHARACTER SET utf8 DEFAULT NULL, 8435`pk` int(11) NOT NULL AUTO_INCREMENT, 8436`col_varchar_255_latin1` varchar(255) DEFAULT NULL, 8437`col_datetime_key` datetime DEFAULT NULL, 8438`col_varchar_10_utf8_key` varchar(10) CHARACTER SET utf8 DEFAULT NULL, 8439`col_varchar_255_utf8_key` varchar(255) CHARACTER SET utf8 DEFAULT NULL, 8440`col_varchar_10_latin1` varchar(10) DEFAULT NULL, 8441`col_datetime` datetime DEFAULT NULL, 8442`col_varchar_255_latin1_key` varchar(255) DEFAULT NULL, 8443`col_int_key` int(11) DEFAULT NULL, 8444PRIMARY KEY (`pk`), 8445KEY `col_varchar_10_latin1_key` (`col_varchar_10_latin1_key`), 8446KEY `col_date_key` (`col_date_key`), 8447KEY `col_datetime_key` (`col_datetime_key`), 8448KEY `col_varchar_10_utf8_key` (`col_varchar_10_utf8_key`), 8449KEY `col_varchar_255_utf8_key` (`col_varchar_255_utf8_key`), 8450KEY `col_varchar_255_latin1_key` (`col_varchar_255_latin1_key`), 8451KEY `col_int_key` (`col_int_key`) 8452) ENGINE=InnoDB AUTO_INCREMENT=6 DEFAULT CHARSET=latin1; 8453Warnings: 8454Warning 3719 'utf8' is currently an alias for the character set UTF8MB3, but will be an alias for UTF8MB4 in a future release. Please consider using UTF8MB4 in order to be unambiguous. 8455Warning 1681 Integer display width is deprecated and will be removed in a future release. 8456Warning 3719 'utf8' is currently an alias for the character set UTF8MB3, but will be an alias for UTF8MB4 in a future release. Please consider using UTF8MB4 in order to be unambiguous. 8457Warning 1681 Integer display width is deprecated and will be removed in a future release. 8458Warning 3719 'utf8' is currently an alias for the character set UTF8MB3, but will be an alias for UTF8MB4 in a future release. Please consider using UTF8MB4 in order to be unambiguous. 8459Warning 3719 'utf8' is currently an alias for the character set UTF8MB3, but will be an alias for UTF8MB4 in a future release. Please consider using UTF8MB4 in order to be unambiguous. 8460Warning 1681 Integer display width is deprecated and will be removed in a future release. 8461INSERT INTO `A` VALUES ('nznrrizpfk','znrrizpfkx',66,'2006-07-12','2004-06-08','nrrizpfkxceksatefqsdksjijcszxwbjjvvkymalukqukkoeiwsgpmfyvvuqvtjncdsvqhlhtrovamzqrdcenchyhuoowityzgktbkjrkmqmknbtoervqlzsvasurqdhucjxdoygxqxnvgqmwcidtumxwcftedxqyciakhxptzfpjwrgeozxnbycjzxiecurgmhbubpijrllquntppirzdphpduhwqhnsmujjjbldnkiwsrsxndolpujdnozrhh',1,'TWLFR',NULL,'CROGF','the','back','0000-00-00 00:00:00','LHIBY',217),('c','GQPFO',28,NULL,'0000-00-00','well',2,'or',NULL,'rrizpfkxce','rizpfkxceksatef','izpfkxceks','0000-00-00 00:00:00','zpfkxceksatefqsdksjijcszxwbjjvvkymalukqukkoeiwsgpmfyvvuqvtjncdsvqhlhtrovamzqrdcenchyhuoowityzgktbkjrkmqmknbtoervqlzsvasurqdhucjxdoygxqxnvgqmwcidtumxwcftedxqyciakhxptzfpjwrgeozxnbycjzxiecurgmhbubpijrllquntppirzdphpduhwqhnsmujjjbldnkiwsrsxndolpujdnozrhhcxsx',179),('but','XXDMG',44,'2002-09-18',NULL,'some',3,'come','2004-10-05 00:00:00','pfkxceksat','it','p','2001-02-20 00:00:00','YOTTT',238),('fkxceksate','WVXCD',168,'2009-12-23','2005-04-16','kxceksatefqsdksjijcszxwbjjvvkymalukqukkoeiwsgpmfyvvuqvtjncdsvqhlhtrovamzqrdcenchyhuoowityzgktbkjrkmqmknbtoervqlzsvasurqdhucjxdoygxqxnvgqmwcidtumxwcftedxqyciakhxptzfpjwrgeozxnbycjzxiecurgmhbubpijrllquntppirzdphpduhwqhnsmujjjbldnkiwsrsxndolpujdnozrhhcxsxwuj',4,'SJYOI',NULL,'y','xceksatefqsdksjijcszxwbjjvvkymalukqukkoeiwsgpmfyvvuqvtjncdsvqhlhtrovamzqrdcenchyhuoowityzgktbkjrkmqmknbtoervqlzsvasurqdhucjxdoygxqxnvgqmwcidtumxwcftedxqyciakhxptzfpjwrgeozxnbycjzxiecurgmhbubpijrllquntppirzdphpduhwqhnsmujjjbldnkiwsrsxndolpujdnozrhhcxsxwuju','have','0000-00-00 00:00:00','ceksatefqsdksjijcszxwbjjvvkymalukqukkoeiwsgpmfyvvuqvtjncdsvqhlhtrovamzqrdcenchyhuoowityzgktbkjrkmqmknbtoervqlzsvasurqdhucjxdoygxqxnvgqmwcidtumxwcftedxqyciakhxptzfpjwrgeozxnbycjzxiecurgmhbubpijrllquntppirzdphpduhwqhnsmujjjbldnkiwsrsxndolpujdnozrhhcxsxwujul',99),('just','i',0,'2003-11-07','0000-00-00','come',5,'can\'t','2006-03-17 18:41:01','not','eksatefqsdksjijcszxwbjjvvkymalukqukkoeiwsgpmfyvvuqvtjncdsvqhlhtrovamzqrdcenchyhuoowityzgktbkjrkmqmknbtoervqlzsvasurqdhucjxdoygxqxnvgqmwcidtumxwcftedxqyciakhxptzfpjwrgeozxnbycjzxiecurgmhbubpijrllquntppirzdphpduhwqhnsmujjjbldnkiwsrsxndolpujdnozrhhcxsxwujulp','ksatefqsdk','2000-11-05 22:46:38','k',75); 8462CREATE TABLE `AA` ( 8463`col_datetime` datetime DEFAULT NULL, 8464`col_date` date DEFAULT NULL, 8465`col_varchar_255_latin1` varchar(255) DEFAULT NULL, 8466`col_varchar_255_utf8_key` varchar(255) CHARACTER SET utf8 DEFAULT NULL, 8467`col_datetime_key` datetime DEFAULT NULL, 8468`col_int` int(11) DEFAULT NULL, 8469`col_varchar_255_utf8` varchar(255) CHARACTER SET utf8 DEFAULT NULL, 8470`col_date_key` date DEFAULT NULL, 8471`col_varchar_255_latin1_key` varchar(255) DEFAULT NULL, 8472`col_varchar_10_utf8_key` varchar(10) CHARACTER SET utf8 DEFAULT NULL, 8473`col_varchar_10_latin1_key` varchar(10) DEFAULT NULL, 8474`pk` int(11) NOT NULL AUTO_INCREMENT, 8475`col_varchar_10_latin1` varchar(10) DEFAULT NULL, 8476`col_varchar_10_utf8` varchar(10) CHARACTER SET utf8 DEFAULT NULL, 8477`col_int_key` int(11) DEFAULT NULL, 8478PRIMARY KEY (`pk`), 8479KEY `col_varchar_255_utf8_key` (`col_varchar_255_utf8_key`), 8480KEY `col_datetime_key` (`col_datetime_key`), 8481KEY `col_date_key` (`col_date_key`), 8482KEY `col_varchar_255_latin1_key` (`col_varchar_255_latin1_key`), 8483KEY `col_varchar_10_utf8_key` (`col_varchar_10_utf8_key`), 8484KEY `col_varchar_10_latin1_key` (`col_varchar_10_latin1_key`), 8485KEY `col_int_key` (`col_int_key`) 8486) ENGINE=InnoDB AUTO_INCREMENT=6 DEFAULT CHARSET=latin1; 8487Warnings: 8488Warning 3719 'utf8' is currently an alias for the character set UTF8MB3, but will be an alias for UTF8MB4 in a future release. Please consider using UTF8MB4 in order to be unambiguous. 8489Warning 1681 Integer display width is deprecated and will be removed in a future release. 8490Warning 3719 'utf8' is currently an alias for the character set UTF8MB3, but will be an alias for UTF8MB4 in a future release. Please consider using UTF8MB4 in order to be unambiguous. 8491Warning 3719 'utf8' is currently an alias for the character set UTF8MB3, but will be an alias for UTF8MB4 in a future release. Please consider using UTF8MB4 in order to be unambiguous. 8492Warning 1681 Integer display width is deprecated and will be removed in a future release. 8493Warning 3719 'utf8' is currently an alias for the character set UTF8MB3, but will be an alias for UTF8MB4 in a future release. Please consider using UTF8MB4 in order to be unambiguous. 8494Warning 1681 Integer display width is deprecated and will be removed in a future release. 8495INSERT INTO `AA` VALUES (NULL,'0000-00-00','b','r','2003-08-19 23:01:25',154,'zgktbkjrkmqmknbtoervqlzsvasurqdhucjxdoygxqxnvgqmwcidtumxwcftedxqyciakhxptzfpjwrgeozxnbycjzxiecurgmhbubpijrllquntppirzdphpduhwqhnsmujjjbldnkiwsrsxndolpujdnozrhhcxsxwujulpjdzvkpaijursprnwgrpquarwkazzjeiwvdmdivjqsxmhjwagewclcfykywlcnemiuaabrrifnhuufzasunkrcp','0000-00-00','is','gktbkjrkmq','o',1,'ktbkjrkmqm','tbkjrkmqmk',215),(NULL,'2007-05-17','e','VNIVI','2000-11-03 02:44:07',205,'u','0000-00-00','IXNLZ','bkjrkmqmkn','k',2,'hey','BDYIT',25),('2003-11-09 08:17:05','2000-12-18','DTSDH','ZPWFP','2005-05-15 11:04:35',1,'t','2005-12-07','kjrkmqmknbtoervqlzsvasurqdhucjxdoygxqxnvgqmwcidtumxwcftedxqyciakhxptzfpjwrge','CHSSV','AILLJ',3,'w','IRGCS',56),('2002-12-28 21:31:59','2006-12-11','jrkmqmknbtoervqlzsvasurqdhucjxdoygxqxnvgqmwcidtumxwcftedxqyciakhxptzfpjwrgeozxnbycjzxiecurgmhbubpijrllquntppirzdphpduhwqhnsmujjjbldnkiwsrsxndolpujdnozrhhcxsxwujulpjdzvkpaijursprnwgrpquarwkazzjeiwvdmdivjqsxmhjwagewclcfykywlcnemiuaabrrifnhuufzasunkrcpvasdqk','c',NULL,55,'c','2009-07-13','w','b','o',4,'were','her',200),('0000-00-00 00:00:00','0000-00-00','e','FBJDG','0000-00-00 00:00:00',52,'WPOMI','2003-09-05','HZSNI','all','know',5,'KQHJW','rkmqmknbto',252); 8496CREATE TABLE `B` ( 8497`pk` int(11) NOT NULL AUTO_INCREMENT, 8498`col_int` int(11) DEFAULT NULL, 8499`col_varchar_255_utf8_key` varchar(255) CHARACTER SET utf8 DEFAULT NULL, 8500`col_datetime` datetime DEFAULT NULL, 8501`col_varchar_255_latin1_key` varchar(255) DEFAULT NULL, 8502`col_datetime_key` datetime DEFAULT NULL, 8503`col_varchar_255_utf8` varchar(255) CHARACTER SET utf8 DEFAULT NULL, 8504`col_varchar_10_utf8_key` varchar(10) CHARACTER SET utf8 DEFAULT NULL, 8505`col_varchar_10_latin1_key` varchar(10) DEFAULT NULL, 8506`col_date` date DEFAULT NULL, 8507`col_varchar_10_utf8` varchar(10) CHARACTER SET utf8 DEFAULT NULL, 8508`col_varchar_255_latin1` varchar(255) DEFAULT NULL, 8509`col_varchar_10_latin1` varchar(10) DEFAULT NULL, 8510`col_int_key` int(11) DEFAULT NULL, 8511`col_date_key` date DEFAULT NULL, 8512PRIMARY KEY (`pk`), 8513KEY `col_varchar_255_utf8_key` (`col_varchar_255_utf8_key`), 8514KEY `col_varchar_255_latin1_key` (`col_varchar_255_latin1_key`), 8515KEY `col_datetime_key` (`col_datetime_key`), 8516KEY `col_varchar_10_utf8_key` (`col_varchar_10_utf8_key`), 8517KEY `col_varchar_10_latin1_key` (`col_varchar_10_latin1_key`), 8518KEY `col_int_key` (`col_int_key`), 8519KEY `col_date_key` (`col_date_key`) 8520) ENGINE=InnoDB AUTO_INCREMENT=6 DEFAULT CHARSET=latin1; 8521Warnings: 8522Warning 1681 Integer display width is deprecated and will be removed in a future release. 8523Warning 1681 Integer display width is deprecated and will be removed in a future release. 8524Warning 3719 'utf8' is currently an alias for the character set UTF8MB3, but will be an alias for UTF8MB4 in a future release. Please consider using UTF8MB4 in order to be unambiguous. 8525Warning 3719 'utf8' is currently an alias for the character set UTF8MB3, but will be an alias for UTF8MB4 in a future release. Please consider using UTF8MB4 in order to be unambiguous. 8526Warning 3719 'utf8' is currently an alias for the character set UTF8MB3, but will be an alias for UTF8MB4 in a future release. Please consider using UTF8MB4 in order to be unambiguous. 8527Warning 3719 'utf8' is currently an alias for the character set UTF8MB3, but will be an alias for UTF8MB4 in a future release. Please consider using UTF8MB4 in order to be unambiguous. 8528Warning 1681 Integer display width is deprecated and will be removed in a future release. 8529INSERT INTO `B` VALUES (1,229,'a','2003-05-12 00:00:00','x','2002-07-16 00:00:00','p','satefqsdks','can\'t','0000-00-00','atefqsdksj','tefqsdksjijcszxwbjjvvkymalukqukkoeiwsgpmfyvvuqvt','my',92,NULL),(2,159,'efqsdksjijcszxwbjjvvkymalukqukkoeiwsgpmfyvvuqvtjncdsvqhlhtrovamzqrdcenchyhuoowityzgktbkjrkmqmknbtoervqlzsvasurqdhucjxdoygxqxnvgqmwcidtumxwcftedxqyciakhxptzfpjwrgeozxnbycjzxiecurgmhbubpijrllquntppirzdphpduhwqhnsmujjjbldnkiwsrsxndolpujdnoz','0000-00-00 00:00:00','fqsdksjijcszxwbjjvvkymalukqukkoeiwsgpmfyvvuqvtjncdsvqhlhtrovamzqrdcenchyhuoowityzgktbkjrkmqmknbtoervqlzsvasurqdhucjxdoygxqxnvgqmwcidtumxwcftedxqyciakhxptzfpjwrgeozxnbycjzxiecurgmhbubpijrllquntppirzdphpduhwqhnsmujjjbldnkiwsrsxnd','2007-04-08 09:43:38','her','qsdksjijcs','sdksjijcsz','2006-03-26','VXECY','why','JNAFV',70,'0000-00-00'),(3,42,'dksjijcszxwbjjvvkymalukqukkoeiwsgpmfyvvuqvtjncdsvqhlhtrovamzqrdcenchyhuoowityzgktbkjrkmqmknbtoervqlzsvasurqdhucjxdoygxqxnvgqmwcidtumxwcftedxqyciakhxptzfpjwrgeozxnbycjzxiecurgmhbubpijrllquntppirzdphpduhwqhnsmujjjbldnkiwsrsxndolpujdnozrhhcxsxwujulpjdzvkpaij','2009-02-12 04:14:15','back','2000-07-26 07:47:30','r','go','ksjijcszxw','0000-00-00','s','o','sjijcszxwb',200,'0000-00-00'),(4,146,'jijcszxwbjjvvkymalukqukkoeiwsgpmfyvvuqvtjncdsvqhlhtrovamzqrdcenchyhuoowityzgktbkjrkmqmknbtoervqlzsvasurqdhucjxdoygxqxnvgqmwcidtumxwcftedxqyciakhxptzfpjwrgeozxnbycjzxiecurgmhbubpijrllquntppirzdphpduhwqhnsmujjjbldnkiwsrsxndolpujdnozrhhcxsxwujulpjdzvkpaijurs','0000-00-00 00:00:00','EXVCL','0000-00-00 00:00:00','DFVNQ','ijcszxwbjj','NMTOX','2004-12-17','p','jcszxwbjjvvkymalukqukkoeiwsgpmfyvvuqvtjncdsvqhlhtrovamzqrdcenchyhuoowityzgktbkjrkmqmknbtoervqlzsvasurqdhucjxdoygxqxnvgqmwcidtumxwcftedxqyciakhxptzfpjwrgeozxnbycjzxiecurgmhbubpijrllquntppirzdphpduhwqhnsmujjjbldnkiwsrsxndolpujdnozrhhcxsxwujulpjdzvkpaijurspr','i',104,'2002-03-06'),(5,235,'i','0000-00-00 00:00:00','he\'s','2000-01-06 20:32:48','cszxwbjjvvkymalukqukkoeiwsgpmfyvvuqvtjncdsvqhlhtrovamzqrdcenchyhuoowityzgktbkjrkmqmknbtoervqlzsvasurqdhucjxdoygxqxnvgqmwcidtumxwcftedxqyci','YGVHA','EFEOY','2000-01-18','szxwbjjvvk','oh','not',239,'2001-07-22'); 8530ALTER TABLE A ADD COLUMN col_varchar_10_latin1_gckey VARCHAR(20) GENERATED ALWAYS AS (CONCAT(col_varchar_10_latin1_key,'x')) VIRTUAL; 8531ALTER TABLE B ADD COLUMN col_int_gckey INT GENERATED ALWAYS AS (col_int_key + col_int_key) VIRTUAL; 8532ALTER TABLE B ADD COLUMN col_varchar_255_utf8_gckey TEXT GENERATED ALWAYS AS (CONCAT(col_varchar_255_utf8_key,'x')) VIRTUAL; 8533set sql_mode=default; 8534SELECT 8535PERCENT_RANK() OVER ( PARTITION BY alias1 . `col_int`, 8536alias2 . `col_int`, 8537alias1 . `col_int_key`, 8538alias1 . `col_varchar_10_utf8_key` 8539 ORDER BY alias1 . `col_varchar_255_utf8_key`, 8540alias1 . `col_varchar_10_latin1_gckey`, 8541alias2 . `col_varchar_255_utf8_gckey`, 8542alias1 . `pk` 8543 ROWS 430 PRECEDING ) AS pr 8544FROM A AS alias1 RIGHT OUTER JOIN 8545B AS alias2 8546LEFT JOIN 8547AA AS alias3 8548ON alias2 . `col_int_key` = alias3 . `pk` 8549 ON alias1 . `pk` = alias2 . `col_int_gckey` 8550 WHERE alias2 . `pk` > 5 AND alias2 . `pk` <= ( 5 + 5 ) 8551AND alias2 . `pk` > 5 AND alias2 . `pk` <= ( 5 + 2 ) 8552AND alias3 . `pk` IN (5, 0) 8553AND alias3 . `pk` >= 5 8554AND alias3 . `pk` < ( 8 + 4 ) 8555AND alias1 . `col_int` >= 5 8556AND alias1 . `col_int` <= ( 4 + 0 ) 8557OR alias2 . `col_int` != 5 8558OR alias3 . `col_int` <> 7 8559OR alias3 . `col_int` >= 5 8560AND alias3 . `col_int` < ( 5 + 2 ) 8561ORDER BY pr LIMIT 1000 OFFSET 4; 8562pr 85630 8564DROP TABLE `A`, `AA`, `B`; 8565# 8566# Bug#26114804: WL#9603: ER_WINDOW_NESTED_WINDOW_FUNC_USE_IN_WINDOW_SPEC 8567# FOR NESTED AGGREGATE 8568CREATE TABLE t (a INT, b INT); 8569INSERT INTO t VALUES(1,2),(3,4),(4,6),(4,7); 8570SELECT SUM(a) OVER (ORDER BY SUM(a)) FROM t GROUP BY a; 8571SUM(a) OVER (ORDER BY SUM(a)) 85721 85734 85748 8575SELECT SUM(a) OVER (ORDER BY 1+SUM(a)) FROM t GROUP BY a; 8576SUM(a) OVER (ORDER BY 1+SUM(a)) 85771 85784 85798 8580SELECT SUM(a) OVER (PARTITION BY 1+SUM(a)) FROM t GROUP BY a; 8581SUM(a) OVER (PARTITION BY 1+SUM(a)) 85821 85833 85844 8585SELECT SUM(a) OVER (ORDER BY AVG(a)) FROM t GROUP BY a,b; 8586SUM(a) OVER (ORDER BY AVG(a)) 85871 85884 858912 859012 8591SELECT SUM(a) OVER (ORDER BY SUM(b)) FROM t GROUP BY a,b; 8592SUM(a) OVER (ORDER BY SUM(b)) 85931 85944 85958 859612 8597DROP TABLE t; 8598# 8599# Bug#26162009: WL#9603: WRONG RESULT WITH MULTIPLE EXECUTIONS OF A 8600# STORED PROGRAM 8601CREATE TABLE t ( 8602col_int INT, 8603pk INT NOT NULL AUTO_INCREMENT, 8604col_int_key INT GENERATED ALWAYS AS (col_int+col_int) 8605VIRTUAL, PRIMARY KEY (pk)); 8606INSERT INTO t (col_int, pk) VALUES 8607(7,6),(6,5),(4,4),(3,3),(2,2),(1,1); 8608PREPARE ps FROM "SELECT AVG(col_int_key) OVER () FROM t 8609WHERE pk=3 GROUP BY col_int_key"; 8610EXECUTE ps; 8611AVG(col_int_key) OVER () 86126.0000 8613EXECUTE ps; 8614AVG(col_int_key) OVER () 86156.0000 8616PREPARE ps FROM "SELECT AVG(12) OVER () FROM dual"; 8617EXECUTE ps; 8618AVG(12) OVER () 861912.0000 8620EXECUTE ps; 8621AVG(12) OVER () 862212.0000 8623DROP PREPARE ps; 8624DROP TABLE t; 8625# 8626# Bug#26174648: WL9603: ASSERTION FAILURE IN ITEM_SUM_SUM::VAL_INT 8627# 8628SELECT 0 & (SUM(1) OVER w) FROM (select 1) as dt WINDOW w as (); 86290 & (SUM(1) OVER w) 86300 8631SELECT 1 & (SUM(1) OVER w) FROM (select 1) as dt WINDOW w as (); 86321 & (SUM(1) OVER w) 86331 8634# 8635# Bug#26178061: WL#9603: RANK AND UNCORRELATED SUBQUERY: WRONG RESULT 8636# 8637CREATE TABLE t(a int); 8638INSERT INTO t VALUES 8639(NULL),(NULL),(NULL),(NULL),(NULL), 8640(NULL),(NULL),(NULL),(NULL),(NULL), 8641(-1802764288),(-1438121984),(-1237843968), 8642(6),(9),(10),(11),(12),(13),(15), 8643(476839936),(780206080),(1887961088); 8644CREATE TABLE twf AS SELECT RANK() OVER ( ORDER BY a ) AS rnk FROM t GROUP BY a; 8645SELECT RANK() OVER ( ORDER BY a ) AS rnk FROM t GROUP BY a; 8646rnk 86471 86482 86493 86504 86515 86526 86537 86548 86559 865610 865711 865812 865913 866014 8661SELECT * FROM twf WHERE rnk IN (SELECT RANK() OVER ( ORDER BY a ) AS rnk FROM 8662t GROUP BY a); 8663rnk 86641 86652 86663 86674 86685 86696 86707 86718 86729 867310 867411 867512 867613 867714 8678SELECT * FROM twf WHERE rnk NOT IN (SELECT RANK() OVER ( ORDER BY a ) AS rnk 8679FROM t GROUP BY a); 8680rnk 8681DROP TABLE twf,t; 8682# 8683# Bug#26802696: DENSE_RANK WRONG WITH BUFFERED PROCESSING 8684# 8685CREATE TABLE employee(department_id INT, salary INT); 8686INSERT INTO employee VALUES (10, NULL), 8687(10, 100000), 8688(10, 60000), 8689(10, 60000), 8690(10, 70000), 8691(20, 80000), 8692(20, 65000), 8693(20, 65000), 8694(30, 300000), 8695(30, 70000), 8696(NULL, 75000); 8697with partitions 8698SELECT department_id, salary, 8699RANK() OVER w AS rnk, 8700DENSE_RANK() OVER w AS dense, 8701NTILE(4) over w AS ntil, 8702CUME_DIST() OVER w AS cume, 8703PERCENT_RANK() over w AS `%rnk` 8704 FROM employee 8705WINDOW w AS (PARTITION BY department_id 8706ORDER BY salary DESC) 8707ORDER BY department_id, salary DESC, ntil; 8708department_id salary rnk dense ntil cume %rnk 8709NULL 75000 1 1 1 1 0 871010 100000 1 1 1 0.2 0 871110 70000 2 2 1 0.4 0.25 871210 60000 3 3 2 0.8 0.5 871310 60000 3 3 3 0.8 0.5 871410 NULL 5 4 4 1 1 871520 80000 1 1 1 0.3333333333333333 0 871620 65000 2 2 2 1 0.5 871720 65000 2 2 3 1 0.5 871830 300000 1 1 1 0.5 0 871930 70000 2 2 2 1 1 8720just default partition 8721SELECT department_id, salary, 8722RANK() OVER w AS rnk, 8723DENSE_RANK() OVER w AS dense, 8724NTILE(4) over w AS ntil, 8725CUME_DIST() OVER w AS cume, 8726PERCENT_RANK() over w AS `%rnk` 8727 FROM employee 8728WINDOW w AS (ORDER BY salary DESC) 8729ORDER BY salary DESC, department_id, ntil; 8730department_id salary rnk dense ntil cume %rnk 873130 300000 1 1 1 0.09090909090909091 0 873210 100000 2 2 1 0.18181818181818182 0.1 873320 80000 3 3 1 0.2727272727272727 0.2 8734NULL 75000 4 4 2 0.36363636363636365 0.3 873510 70000 5 5 2 0.5454545454545454 0.4 873630 70000 5 5 2 0.5454545454545454 0.4 873720 65000 7 6 3 0.7272727272727273 0.6 873820 65000 7 6 3 0.7272727272727273 0.6 873910 60000 9 7 3 0.9090909090909091 0.8 874010 60000 9 7 4 0.9090909090909091 0.8 874110 NULL 11 8 4 1 1 8742DROP TABLE employee; 8743# 8744# Bug#26500442: WINDOW FUNCTIONS: CRASH IN WINDOW::HAS_WINDOWING_STEPS 8745# 8746CREATE TABLE t(a INT); 8747INSERT INTO t VALUES (1), (2), (3), (4); 8748SELECT ISNULL(COS(RANK() OVER())) FROM t; 8749ISNULL(COS(RANK() OVER())) 87500 87510 87520 87530 8754DROP TABLE t; 8755# 8756# Bug#26164633 WL#9603: WRONG RESULT WHEN PARTITION EXPR USING AGGREGATES EVALUATES TO NULL 8757# 8758CREATE TABLE t1 ( 8759pk int NOT NULL DEFAULT '0', 8760col_int int DEFAULT NULL, 8761col_int_key int DEFAULT NULL 8762); 8763INSERT INTO t1 VALUES 8764(1,4,858718208), (2,-28508160,723386368), 8765(3,6,3), (4,828112896,-409141248), 8766(5,1454702592,856424448), (6,7,7), 8767(7,2,4), (8,7,4), 8768(9,1990590464,8), (10,-538705920,2), 8769(11,7,2002124800), (12,776273920,-1472200704), 8770(13,7,8), (14,0,952041472), 8771(15,9,-427819008), (16,8,-686096384), 8772(17,397934592,7), (18,-768671744,6), 8773(19,3,5), (20,1533739008,5), 8774(21,1,-1301872640), (22,798425088,4), 8775(23,5,-561971200), (24,9,-1901854720), 8776(25,9,811401216), (26,1856700416,0), 8777(27,7,4), (28,9,-1491992576), 8778(29,9,1), (30,1724252160,7); 8779SELECT MIN(table2.pk) + table2.col_int AS part_expr, 8780DENSE_RANK() OVER (PARTITION BY MIN(table2.pk) + table2.col_int 8781ORDER BY table1.col_int_key) AS field1 8782FROM t1 AS table1 LEFT JOIN t1 AS table2 8783ON table1.pk = table2.col_int 8784GROUP BY table2.col_int, table1.col_int_key; 8785part_expr field1 8786NULL 1 8787NULL 2 8788NULL 3 8789NULL 4 8790NULL 5 8791NULL 6 8792NULL 7 8793NULL 8 8794NULL 9 8795NULL 10 8796NULL 11 8797NULL 12 8798NULL 13 8799NULL 14 8800NULL 15 8801NULL 16 8802NULL 17 8803NULL 18 88045 1 88059 1 88069 2 880713 1 880822 1 880922 2 881024 1 881124 2 881228 1 8813DROP TABLE t1; 8814# 8815# Bug#26188578 - WL#9603: HAVING CONDITION IS OPTIMIZED OUT FOR ALIAS ON AGGREGATE W/O GROUP BY 8816# 8817CREATE TABLE E ( 8818col_int int(11) DEFAULT NULL, 8819col_varchar_10_utf8 varchar(10) CHARACTER SET utf8 DEFAULT NULL, 8820pk int(11) NOT NULL AUTO_INCREMENT, 8821col_varchar_10_latin1 varchar(10) DEFAULT NULL, 8822col_varchar_255_utf8 varchar(255) CHARACTER SET utf8 DEFAULT NULL, 8823col_varchar_255_latin1 varchar(255) DEFAULT NULL, 8824col_int_key int(11) GENERATED ALWAYS AS ((col_int + col_int)) VIRTUAL, 8825col_varchar_255_utf8_key varchar(255) CHARACTER SET utf8 8826GENERATED ALWAYS AS 8827(concat(repeat(col_varchar_10_utf8,3),repeat(col_varchar_10_utf8,3))) 8828VIRTUAL, 8829col_varchar_255_latin1_key varchar(255) 8830GENERATED ALWAYS AS 8831(concat(repeat(col_varchar_10_latin1,3),repeat(col_varchar_10_latin1,3))) 8832VIRTUAL, 8833col_varchar_10_utf8_key varchar(10) CHARACTER SET utf8 8834GENERATED ALWAYS AS 8835(repeat(substr(col_varchar_10_utf8,-(1)),5)) VIRTUAL, 8836col_varchar_10_latin1_key varchar(10) GENERATED ALWAYS AS 8837(repeat(substr(col_varchar_10_latin1,-(1)),7)) VIRTUAL, 8838PRIMARY KEY (pk DESC), 8839UNIQUE KEY ucover_key1 8840(pk DESC, col_int_key DESC, col_varchar_255_utf8_key DESC, 8841col_varchar_255_latin1_key DESC, col_varchar_10_utf8_key DESC, 8842col_varchar_10_latin1_key DESC), 8843UNIQUE KEY ucover_key2 8844(pk, col_int_key, col_varchar_255_utf8_key, col_varchar_255_latin1_key, 8845col_varchar_10_utf8_key, col_varchar_10_latin1_key), 8846KEY col_int_key (col_int_key DESC), 8847KEY col_varchar_255_utf8_key (col_varchar_255_utf8_key DESC), 8848KEY col_varchar_255_latin1_key (col_varchar_255_latin1_key DESC), 8849KEY col_varchar_10_utf8_key (col_varchar_10_utf8_key DESC), 8850KEY col_varchar_10_latin1_key (col_varchar_10_latin1_key), 8851KEY multi_key1 (col_int_key DESC, col_varchar_10_latin1_key), 8852KEY multi_key1a (col_int_key DESC, col_varchar_10_latin1_key DESC), 8853KEY multi_key2 (col_int_key,col_int DESC), 8854KEY multi_key3 (col_varchar_255_utf8_key DESC, col_varchar_255_utf8), 8855KEY multi_key4 (col_varchar_255_latin1_key, col_varchar_255_latin1), 8856KEY multi_key5 (pk DESC,col_int_key DESC, col_varchar_10_latin1_key), 8857KEY cover_key1 8858(pk DESC,col_int_key DESC, col_varchar_255_utf8_key, 8859col_varchar_255_latin1_key DESC, col_varchar_10_utf8_key DESC, 8860col_varchar_10_latin1_key) 8861); 8862Warnings: 8863Warning 1681 Integer display width is deprecated and will be removed in a future release. 8864Warning 3719 'utf8' is currently an alias for the character set UTF8MB3, but will be an alias for UTF8MB4 in a future release. Please consider using UTF8MB4 in order to be unambiguous. 8865Warning 1681 Integer display width is deprecated and will be removed in a future release. 8866Warning 3719 'utf8' is currently an alias for the character set UTF8MB3, but will be an alias for UTF8MB4 in a future release. Please consider using UTF8MB4 in order to be unambiguous. 8867Warning 1681 Integer display width is deprecated and will be removed in a future release. 8868Warning 3719 'utf8' is currently an alias for the character set UTF8MB3, but will be an alias for UTF8MB4 in a future release. Please consider using UTF8MB4 in order to be unambiguous. 8869Warning 3719 'utf8' is currently an alias for the character set UTF8MB3, but will be an alias for UTF8MB4 in a future release. Please consider using UTF8MB4 in order to be unambiguous. 8870INSERT INTO E (col_int, col_varchar_10_utf8, pk, col_varchar_10_latin1, 8871col_varchar_255_utf8, col_varchar_255_latin1) 8872VALUES 8873(NULL,NULL,10,'this','really','mxwcftedxq'), 8874(22216704,'159973376',9,'l','mean','cidtumxwcf'), 8875(-693376,'1',8,'of','gqmwcidtum','qmwcidtumx'), 8876(-183840,'5',7,'xnvgqmwcid','b','nvgqmwcidt'), 8877(2,NULL,6,'is','f','gxqxnvgqmw'), 8878(4,NULL,5,'xdoygxqxnv','h','good'), 8879(3,'1074462720',4,'z','cjxdoygxqx','m'), 8880(-584581120,'-1176634',3,'urqdhj','rhjxdo','but'), 8881(-19295040,'1235025920',2,'svasurqdhu','can','a'), 8882(9,'951910400',1,'qlzsvasurq','lzsvasurqd','in'); 8883CREATE TABLE C ( 8884col_int int(11) DEFAULT NULL, 8885col_varchar_10_utf8 varchar(10) CHARACTER SET utf8 DEFAULT NULL, 8886pk int(11) NOT NULL AUTO_INCREMENT, 8887col_varchar_10_latin1 varchar(10) DEFAULT NULL, 8888col_varchar_255_utf8 varchar(255) CHARACTER SET utf8 DEFAULT NULL, 8889col_varchar_255_latin1 varchar(255) DEFAULT NULL, 8890col_int_key int(11) GENERATED ALWAYS AS ((col_int + col_int)) VIRTUAL, 8891col_varchar_255_utf8_key varchar(255) CHARACTER SET utf8 8892GENERATED ALWAYS AS 8893(concat(repeat(col_varchar_10_utf8,3),repeat(col_varchar_10_utf8,3))) 8894VIRTUAL, 8895col_varchar_255_latin1_key varchar(255) GENERATED ALWAYS AS 8896(concat(repeat(col_varchar_10_latin1,3),repeat(col_varchar_10_latin1,3))) 8897VIRTUAL, 8898col_varchar_10_utf8_key varchar(10) CHARACTER SET utf8 GENERATED ALWAYS AS 8899(repeat(substr(col_varchar_10_utf8,-(1)),5)) VIRTUAL, 8900col_varchar_10_latin1_key varchar(10) GENERATED ALWAYS AS 8901(repeat(substr(col_varchar_10_latin1,-(1)),7)) VIRTUAL, 8902PRIMARY KEY (pk DESC), 8903UNIQUE KEY ucover_key1 8904(pk DESC, col_int_key DESC, col_varchar_255_utf8_key DESC, 8905col_varchar_255_latin1_key DESC, col_varchar_10_utf8_key DESC, 8906col_varchar_10_latin1_key DESC), 8907UNIQUE KEY ucover_key2 8908(pk, col_int_key, col_varchar_255_utf8_key, col_varchar_255_latin1_key, 8909col_varchar_10_utf8_key, col_varchar_10_latin1_key), 8910KEY col_int_key (col_int_key DESC), 8911KEY col_varchar_255_utf8_key (col_varchar_255_utf8_key DESC), 8912KEY col_varchar_255_latin1_key (col_varchar_255_latin1_key DESC), 8913KEY col_varchar_10_utf8_key (col_varchar_10_utf8_key DESC), 8914KEY col_varchar_10_latin1_key (col_varchar_10_latin1_key), 8915KEY multi_key1 (col_int_key DESC, col_varchar_10_latin1_key), 8916KEY multi_key1a (col_int_key DESC, col_varchar_10_latin1_key DESC), 8917KEY multi_key2 (col_int_key, col_int DESC), 8918KEY multi_key3 (col_varchar_255_utf8_key DESC, col_varchar_255_utf8), 8919KEY multi_key4 (col_varchar_255_latin1_key, col_varchar_255_latin1), 8920KEY multi_key5 8921(pk DESC, col_int_key DESC, col_varchar_10_latin1_key), 8922KEY cover_key1 8923(pk DESC, col_int_key DESC, col_varchar_255_utf8_key, 8924col_varchar_255_latin1_key DESC, col_varchar_10_utf8_key DESC, 8925col_varchar_10_latin1_key) 8926); 8927Warnings: 8928Warning 1681 Integer display width is deprecated and will be removed in a future release. 8929Warning 3719 'utf8' is currently an alias for the character set UTF8MB3, but will be an alias for UTF8MB4 in a future release. Please consider using UTF8MB4 in order to be unambiguous. 8930Warning 1681 Integer display width is deprecated and will be removed in a future release. 8931Warning 3719 'utf8' is currently an alias for the character set UTF8MB3, but will be an alias for UTF8MB4 in a future release. Please consider using UTF8MB4 in order to be unambiguous. 8932Warning 1681 Integer display width is deprecated and will be removed in a future release. 8933Warning 3719 'utf8' is currently an alias for the character set UTF8MB3, but will be an alias for UTF8MB4 in a future release. Please consider using UTF8MB4 in order to be unambiguous. 8934Warning 3719 'utf8' is currently an alias for the character set UTF8MB3, but will be an alias for UTF8MB4 in a future release. Please consider using UTF8MB4 in order to be unambiguous. 8935INSERT INTO C 8936(col_int, col_varchar_10_utf8, pk, col_varchar_10_latin1, 8937col_varchar_255_utf8, col_varchar_255_latin1) 8938VALUES 8939(5,'pmfyvvuqvt',6,'so','he','mfyvvuq'), 8940(5,'q',5,'iwsgpmfyvv','wsgpmf','s'), 8941(9,'oeiwsgpmfy',4,'at','can','come'), 8942(-108947046,'kkoeiwsgpm',3,'koeiwsgpmf','o','t'), 8943(NULL,'p',2,'ukkoeiwsgp','now','is'), 8944(NULL,'now',1,'up','u','u'); 8945CREATE VIEW view_E AS SELECT * FROM E; 8946SELECT MAX( alias1 .col_int ) AS field1 FROM view_E AS alias1 LEFT 8947JOIN C AS alias2 ON alias1 .col_int = alias2 .pk WHERE alias1 .pk 8948IN ( 5 ) HAVING field1 <= 6; 8949field1 89504 8951SELECT * FROM (SELECT MAX( alias1.col_int) AS field1 FROM view_E AS alias1 8952LEFT JOIN C AS alias2 8953ON alias1 .col_int = alias2 .pk 8954WHERE alias1 .pk IN (5) HAVING field1 <= 6) s1; 8955field1 89564 8957SELECT * FROM (SELECT MAX( alias1.col_int) AS field1 FROM view_E AS alias1 8958LEFT JOIN C AS alias2 8959ON alias1 .col_int = alias2 .pk 8960WHERE alias1 .pk IN (5) HAVING MAX(alias1 .col_int) <= 6) s1; 8961field1 89624 8963DROP TABLE E,C; 8964DROP VIEW view_E; 8965# 8966# Bug #26496733: WINDOW FUNCTIONS: ASSERT FALSE WITH JSON COLUMN 8967# 8968SET @savmode=@@SESSION.SQL_MODE; 8969SET SQL_MODE=''; 8970CREATE TABLE t(a JSON NOT NULL); 8971INSERT INTO t VALUES(); 8972Warnings: 8973Warning 1364 Field 'a' doesn't have a default value 8974SELECT PERCENT_RANK() OVER (ORDER BY a RANGE CURRENT ROW) FROM t; 8975PERCENT_RANK() OVER (ORDER BY a RANGE CURRENT ROW) 89760 8977SET SESSION SQL_MODE=@savmode; 8978DROP TABLE t; 8979# End of test for Bug#26496733 8980# 8981# Bug#26497353: ASSERTION FAILED: M_OPENED_TABLE != NULLPTR 8982# 8983CREATE TABLE t(a INTEGER); 8984INSERT INTO t VALUES (1), (2), (3); 8985SELECT DENSE_RANK() OVER w2 FROM t GROUP BY 'a' WITH ROLLUP 8986WINDOW w2 AS (ROWS UNBOUNDED PRECEDING); 8987DENSE_RANK() OVER w2 89881 89891 8990DROP TABLE t; 8991# End of test for Bug#26497353 8992# 8993# Bug #26496645: WINDOW FUNCTIONS: CRASH IN 8994# WINDOW::RESTORE_SPECIAL_RECORD 8995# 8996CREATE TABLE t ( 8997b VARCHAR(20) DEFAULT NULL, 8998d INT DEFAULT NULL); 8999INSERT INTO t VALUES(1,-1); 9000INSERT INTO t VALUES(1,-1); 9001SELECT b,d,LAST_VALUE(b) OVER(ORDER BY d RANGE BETWEEN 1 FOLLOWING AND 9002UNBOUNDED FOLLOWING) FROM t; 9003b d LAST_VALUE(b) OVER(ORDER BY d RANGE BETWEEN 1 FOLLOWING AND 9004UNBOUNDED FOLLOWING) 90051 -1 NULL 90061 -1 NULL 9007DROP TABLE t; 9008# End of test for Bug#26496645 9009# 9010# Bug#26411055: WINDOW FRAME ACCEPTS NON-INTEGER ARG FOR ROWS 9011# 9012CREATE TABLE t(w INT); 9013INSERT INTO t VALUES (1),(2),(3),(4),(5),(6),(7),(8),(9); 9014SELECT w, SUM(w) OVER (ROWS 3.14 PRECEDING) FROM t; 9015ERROR HY000: Window '<unnamed window>': frame start or end is negative, NULL or of non-integral type 9016SELECT w, SUM(w) OVER (ROWS BETWEEN 3 PRECEDING AND 3.4 FOLLOWING) FROM t; 9017ERROR HY000: Window '<unnamed window>': frame start or end is negative, NULL or of non-integral type 9018SELECT w, SUM(w) OVER (ROWS BETWEEN CURRENT ROW AND 3.4 FOLLOWING) FROM t; 9019ERROR HY000: Window '<unnamed window>': frame start or end is negative, NULL or of non-integral type 9020PREPARE stmt FROM "SELECT w, SUM(w) OVER (ROWS 3.14 PRECEDING) FROM t"; 9021ERROR HY000: Window '<unnamed window>': frame start or end is negative, NULL or of non-integral type 9022PREPARE stmt FROM "SELECT w, SUM(w) OVER (ROWS ? PRECEDING) FROM t"; 9023SET @p1= 2; 9024EXECUTE stmt USING @p1; 9025w SUM(w) OVER (ROWS ? PRECEDING) 90261 1 90272 3 90283 6 90294 9 90305 12 90316 15 90327 18 90338 21 90349 24 9035SET @p1= 2.3; 9036EXECUTE stmt USING @p1; 9037ERROR HY000: Window '<unnamed window>': frame start or end is negative, NULL or of non-integral type 9038DROP PREPARE stmt; 9039DROP TABLE t; 9040# End of test for Bug#26411055 9041# 9042# Bug#26497247:WINDOW FUNCTIONS: CRASH IN DO_COPY_MAYBE_NULL 9043# 9044CREATE TABLE t(a INTEGER NOT NULL); 9045INSERT INTO t VALUES ('1'), ('2'), ('3'); 9046SELECT LEAD(a,1) OVER() FROM t GROUP BY a WITH ROLLUP; 9047LEAD(a,1) OVER() 90482 90493 9050NULL 9051NULL 9052DROP TABLE t; 9053#End of test for Bug#26497247 9054# 9055# Bug #26496880: CRASH IN FIELD_BLOB::GET_KEY_IMAGE 9056# 9057CREATE TABLE t( 9058a INTEGER, 9059b BLOB, 9060PRIMARY KEY (b(1)) 9061); 9062INSERT INTO t(b) VALUES('a'),('b'),('c'),('d'); 9063FLUSH TABLES; 9064SELECT LEAD(1,1,1) OVER(PARTITION BY a) FROM t; 9065LEAD(1,1,1) OVER(PARTITION BY a) 90661 90671 90681 90691 9070DROP TABLE t; 9071#End of test for Bug#26496880 9072# 9073# Bug#26730020 REEXECUTE PREPARED STATEMENT CRASH WITH WINDOW FUNCTIONS 9074# 9075CREATE TABLE t(a INT); 9076PREPARE s FROM 'DO (SELECT a FROM t WINDOW w2 AS (w1), w1 AS (ORDER BY a, a));'; 9077EXECUTE s; 9078EXECUTE s; 9079DROP TABLE t; 9080# 9081# Bug#26907753 COMBINING CTE AND WINDOW FUNCTION GIVES WRONG RESULT 9082# 9083CREATE TABLE t1 (i INTEGER); 9084INSERT INTO t1 VALUES (1),(1),(2); 9085SELECT * FROM 9086(SELECT LEAD(i) OVER w AS a, i AS b FROM t1 9087WINDOW w AS 9088(ORDER BY i ROWS CURRENT ROW)) 9089AS t WHERE a = b; 9090a b 90911 1 9092WITH t2(i) AS (SELECT i+1 FROM t1) 9093SELECT * FROM 9094(SELECT LEAD(i) OVER w AS a, i AS b FROM t2 9095WINDOW w AS 9096(ORDER BY i ROWS CURRENT ROW)) 9097AS t3 WHERE a = b; 9098a b 90992 2 9100DROP TABLE t1; 9101# 9102# Bug#26813454 UNWARRANTED ERROR INDICATING WINDOW NAME NOT DEFINED 9103# 9104CREATE TABLE sales(id INT AUTO_INCREMENT PRIMARY KEY, `date` DATE, sale INT); 9105INSERT INTO sales(`date`, sale) VALUES 9106('2017-03-01', 200), 9107('2017-04-01', 300), 9108('2017-05-01', 400), 9109('2017-06-01', 200), 9110('2017-07-01', 600), 9111('2017-08-01', 100), 9112('2017-03-01', 400), 9113('2017-04-01', 300), 9114('2017-05-01', 500), 9115('2017-06-01', 400), 9116('2017-07-01', 600), 9117('2017-08-01', 150); 9118SELECT MONTH(date), SUM(sale), 9119AVG(SUM(sale)) OVER w AS sliding_avg FROM sales GROUP BY MONTH(date) 9120WINDOW w AS (ORDER BY MONTH(date) 9121RANGE BETWEEN 1 PRECEDING AND 1 FOLLOWING) 9122ORDER BY AVG(SUM(sale)) OVER (ORDER BY MONTH(date) 9123RANGE BETWEEN 1 PRECEDING AND 1 FOLLOWING), 9124MONTH(date); 9125MONTH(date) SUM(sale) sliding_avg 91263 600 600.0000 91277 1200 683.3333 91284 600 700.0000 91295 900 700.0000 91308 250 725.0000 91316 600 900.0000 9132DROP TABLE sales; 9133# 9134# Bug#26739028: WINDOW FUNC + FROM_UNIXTIME CRASH 9135# 9136SELECT FROM_UNIXTIME(LAG('',99)OVER(ROWS UNBOUNDED PRECEDING),'%I %l %I'); 9137FROM_UNIXTIME(LAG('',99)OVER(ROWS UNBOUNDED PRECEDING),'%I %l %I') 9138NULL 9139SELECT FROM_UNIXTIME(FIRST_VALUE(@b)RESPECT NULLS OVER(), 9140REPEAT('1',32))IS NOT FALSE; 9141FROM_UNIXTIME(FIRST_VALUE(@b)RESPECT NULLS OVER(), 9142REPEAT('1',32))IS NOT FALSE 91431 9144SELECT ((NTILE(70)OVER())<<(FROM_UNIXTIME(LEAD('',67) RESPECT NULLS 9145OVER(),SHA('')))); 9146((NTILE(70)OVER())<<(FROM_UNIXTIME(LEAD('',67) RESPECT NULLS 9147OVER(),SHA('')))) 9148NULL 9149SELECT FROM_UNIXTIME(LAG('-778:36:16.905133',246) RESPECT NULLS OVER(), 9150REPLACE('%M%V ','','')); 9151FROM_UNIXTIME(LAG('-778:36:16.905133',246) RESPECT NULLS OVER(), 9152REPLACE('%M%V ','','')) 9153NULL 9154# End of test for Bug#26739028 9155# 9156# Bug#26389508: INT JOIN_READ_KEY(QEP_TAB*): ASSERTION 9157# `!TABLE->HAS_NULL_ROW()' FAILED 9158# 9159CREATE TABLE t1 ( 9160pk int(11) NOT NULL AUTO_INCREMENT, 9161col_int int(11) DEFAULT NULL, 9162PRIMARY KEY (pk) 9163); 9164Warnings: 9165Warning 1681 Integer display width is deprecated and will be removed in a future release. 9166Warning 1681 Integer display width is deprecated and will be removed in a future release. 9167INSERT INTO t1 VALUES (1,NULL),(2,4),(3,-501481472),(4,NULL),(5,3); 9168CREATE TABLE t2 ( 9169col_int_key int(11) DEFAULT NULL, 9170KEY col_int_key (col_int_key) 9171); 9172Warnings: 9173Warning 1681 Integer display width is deprecated and will be removed in a future release. 9174INSERT INTO t2 VALUES (NULL),(NULL),(NULL),(NULL),(5),(5); 9175This JOIN needs the fix 9176SELECT FIRST_VALUE( alias1.pk ) OVER 9177(ROWS BETWEEN UNBOUNDED PRECEDING AND 1 FOLLOWING) AS field1 9178FROM t1 AS alias1 RIGHT JOIN t2 AS alias2 9179ON alias1.pk = alias2.col_int_key; 9180field1 9181NULL 9182NULL 9183NULL 9184NULL 9185NULL 9186NULL 9187This doesn't and shouldn't do save/restore it (not JT_EQ_REF). Verify 9188with debugger. 9189SELECT FIRST_VALUE( alias1.pk ) OVER 9190(ROWS BETWEEN UNBOUNDED PRECEDING AND 1 FOLLOWING) AS field1 9191FROM t1 AS alias1 RIGHT JOIN t2 AS alias2 9192ON alias1.pk > alias2.col_int_key; 9193field1 9194NULL 9195NULL 9196NULL 9197NULL 9198NULL 9199NULL 9200Nor this one (not first window). Verify with debugger. 9201SELECT ROW_NUMBER() OVER () AS `row#`, 9202FIRST_VALUE( alias1.pk ) OVER 9203(ROWS BETWEEN UNBOUNDED PRECEDING AND 1 FOLLOWING) AS field1 9204FROM t1 AS alias1 RIGHT JOIN t2 AS alias2 9205ON alias1.pk = alias2.col_int_key; 9206row# field1 92071 NULL 92082 NULL 92093 NULL 92104 NULL 92115 NULL 92126 NULL 9213DROP TABLE t1, t2; 9214# 9215# Bug#26975882: CRASH IN STRING::COPY, USUALLY WITH WKB FUNCTIONS 9216# / WINDOW FUNC 9217# 9218SELECT ST_BUFFER(ST_ASBINARY(1), ST_ASBINARY(NTH_VALUE('4714-05-04',1)OVER(),'axis-order=long-lat')); 9219ERROR 22023: Invalid GIS data provided to function st_aswkb. 9220# End of test for Bug#26975882 9221# 9222# Bug#26740557: WINDOW FUNC + JSON: ASSERTION FAILED: 9223# FALSE IN ITEM::VAL_JSON 9224# 9225SELECT ((MAKETIME(((QUARTER('| !*c>*{/'))<=> 9226(FIRST_VALUE(JSON_OBJECTAGG('key4',0x067c13d0d0d7d8c8d768aef7) 9227)OVER())),'9236-05-27',0xe2a7d4))^(0x1109)); 9228((MAKETIME(((QUARTER('| !*c>*{/'))<=> 9229(FIRST_VALUE(JSON_OBJECTAGG('key4',0x067c13d0d0d7d8c8d768aef7) 9230)OVER())),'9236-05-27',0xe2a7d4))^(0x1109)) 9231NULL 9232Warnings: 9233Warning 1292 Incorrect datetime value: '| !*c>*{/' 9234Warning 1292 Incorrect datetime value: '| !*c>*{/' 9235Warning 1292 Truncated incorrect INTEGER value: '9236-05-27' 9236SELECT ((FIRST_VALUE(JSON_MERGE_PATCH(1.755913e+308,'{ }'))OVER())<=(1)); 9237ERROR 22032: Invalid data type for JSON data in argument 1 to function json_merge_patch; a JSON string or JSON type is required. 9238SELECT ((QUOTE(JSON_KEYS(FIRST_VALUE(JSON_KEYS(EXP(-15676),ABS('d0')) 9239)OVER())))>=(CONNECTION_ID())); 9240ERROR 22032: Invalid data type for JSON data in argument 1 to function json_keys; a JSON string or JSON type is required. 9241SELECT JSON_LENGTH(FIRST_VALUE(JSON_OBJECTAGG('key2','*B'))OVER()); 9242JSON_LENGTH(FIRST_VALUE(JSON_OBJECTAGG('key2','*B'))OVER()) 92431 9244# End of test for Bug#26740557 9245# 9246# Check for errors 9247# 9248CREATE TABLE t1 (i INTEGER); 9249SELECT AVG(i) FROM t1 WINDOW w AS (ORDER BY i); 9250ERROR 42000: In aggregated query without GROUP BY, expression #1 of PARTITION BY or ORDER BY clause of window 'w' contains nonaggregated column 'test.t1.i'; this is incompatible with sql_mode=only_full_group_by 9251SELECT AVG(i), RANK() OVER (ORDER BY i) FROM t1; 9252ERROR 42000: In aggregated query without GROUP BY, expression #1 of PARTITION BY or ORDER BY clause of window '<unnamed window>' contains nonaggregated column 'test.t1.i'; this is incompatible with sql_mode=only_full_group_by 9253SELECT AVG(i) FROM t1 ORDER BY RANK() OVER (PARTITION BY AVG(i) ORDER BY i); 9254ERROR 42000: In aggregated query without GROUP BY, expression #1 of PARTITION BY or ORDER BY clause of window '<unnamed window>' contains nonaggregated column 'test.t1.i'; this is incompatible with sql_mode=only_full_group_by 9255SELECT AVG(i), RANK() OVER w FROM t1 WINDOW w AS (ORDER BY i); 9256ERROR 42000: In aggregated query without GROUP BY, expression #1 of PARTITION BY or ORDER BY clause of window 'w' contains nonaggregated column 'test.t1.i'; this is incompatible with sql_mode=only_full_group_by 9257SELECT (select AVG(i)+RANK() OVER (ORDER BY i)) FROM t1; 9258ERROR 42000: In aggregated query without GROUP BY, expression #1 of SELECT list contains nonaggregated column 'test.t1.i'; this is incompatible with sql_mode=only_full_group_by 9259DROP TABLE t1; 9260# End of checking for errors 9261# 9262# Bug#27010574 WINDOW FUNCTIONS: BUG WITH LAST_VALUE AND FRAME 9263# 9264CREATE TABLE t(a INT, b INT); 9265INSERT INTO t VALUES (1,1), 9266(2,1), 9267(3,2), 9268(4,2), 9269(5,3), 9270(6,3); 9271SELECT a, b, 9272LAST_VALUE(a) OVER (ORDER BY b,a) AS `last` FROM t; 9273a b last 92741 1 1 92752 1 2 92763 2 3 92774 2 4 92785 3 5 92796 3 6 9280SELECT a, b, 9281LAST_VALUE(a) OVER (ORDER BY b,a 9282RANGE BETWEEN UNBOUNDED PRECEDING AND 9283CURRENT ROW) AS `last` 9284 FROM t; 9285a b last 92861 1 1 92872 1 2 92883 2 3 92894 2 4 92905 3 5 92916 3 6 9292INSERT INTO t VALUES (1,1), 9293(4,2), 9294(NULL, 2), 9295(NULL, NULL), 9296(2, NULL); 9297SELECT a, b, COUNT(a) OVER w AS cnt, 9298COUNT(*) OVER w AS `cnt(*)`, 9299FIRST_VALUE(a) OVER w AS first, 9300LAST_VALUE (a) OVER w AS last 9301FROM t WINDOW w AS (ORDER BY b,a DESC); 9302a b cnt cnt(*) first last 93032 NULL 1 1 2 2 9304NULL NULL 1 2 2 NULL 93052 1 2 3 2 2 93061 1 4 5 2 1 93071 1 4 5 2 1 93084 2 6 7 2 4 93094 2 6 7 2 4 93103 2 7 8 2 3 9311NULL 2 7 9 2 NULL 93126 3 8 10 2 6 93135 3 9 11 2 5 9314SELECT a, b, COUNT(a) OVER w AS cnt, 9315COUNT(*) OVER w AS `cnt(*)`, 9316FIRST_VALUE(a) OVER w AS first, 9317LAST_VALUE (a) OVER w AS last 9318FROM t WINDOW w AS (ORDER BY b,a DESC 9319RANGE BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW); 9320a b cnt cnt(*) first last 93212 NULL 1 1 2 2 9322NULL NULL 1 2 2 NULL 93232 1 2 3 2 2 93241 1 4 5 2 1 93251 1 4 5 2 1 93264 2 6 7 2 4 93274 2 6 7 2 4 93283 2 7 8 2 3 9329NULL 2 7 9 2 NULL 93306 3 8 10 2 6 93315 3 9 11 2 5 9332SELECT a, b, COUNT(a) OVER w AS cnt, 9333COUNT(*) OVER w AS `cnt(*)`, 9334FIRST_VALUE(a) OVER w AS first, 9335LAST_VALUE (a) OVER w AS last 9336FROM t WINDOW w AS (ORDER BY b,a DESC 9337RANGE BETWEEN CURRENT ROW AND UNBOUNDED FOLLOWING); 9338a b cnt cnt(*) first last 93392 NULL 9 11 2 5 9340NULL NULL 8 10 NULL 5 93412 1 8 9 2 5 93421 1 7 8 1 5 93431 1 7 8 1 5 93444 2 5 6 4 5 93454 2 5 6 4 5 93463 2 3 4 3 5 9347NULL 2 2 3 NULL 5 93486 3 2 2 6 5 93495 3 1 1 5 5 9350DROP TABLE t; 9351# 9352# Bug#26975848: IFNULL: CRASHES WITH FIRST_VALUE 9353# Bug#27062796: WINDOW FUNC: CRASH IN STRING::COPY WITH WKB FUNCTIONS 9354# Bug#27062694: WINDOW FUNC: MUCH CRASHING WITH FIRST_VALUE/LAST_VALUE!! 9355# 9356SELECT IFNULL(JSON_TYPE(CASE WHEN(1) THEN(1) ELSE(1) END), 9357LAST_VALUE('')OVER()); 9358ERROR 22032: Invalid data type for JSON data in argument 1 to function json_type; a JSON string or JSON type is required. 9359SELECT IFNULL((UUID_TO_BIN(0xAB8631)),(FIRST_VALUE(2.469566E+307)OVER())); 9360ERROR HY000: Incorrect string value: '\xAB\x861' for function uuid_to_bin 9361SELECT IFNULL((JSON_TYPE(@c)),(FIRST_VALUE(@b)OVER())); 9362IFNULL((JSON_TYPE(@c)),(FIRST_VALUE(@b)OVER())) 9363NULL 9364DO ST_WITHIN(ST_ASWKB(ST_ASBINARY(1,'axis-order=srid-defined')), ST_ASWKT( 9365ST_ASBINARY(LAST_VALUE(4732)OVER()),'axis-order=srid-defined')); 9366ERROR 22023: Invalid GIS data provided to function st_aswkb. 9367DO ST_OVERLAPS(ST_ASWKB(ST_ASBINARY(1,'axis-order=lat-long'), 9368'axis-order=srid-defined'), ST_ASWKT(LAST_VALUE('*7') OVER())); 9369ERROR 22023: Invalid GIS data provided to function st_aswkb. 9370DO ST_POINTN(ST_ASWKT(ST_ASWKT(ST_ASWKB(6565))), 9371ST_ASBINARY(LAST_VALUE(0xfd8b9af2bedb16c0d7f1cca63b5c9e) OVER())); 9372ERROR 22023: Invalid GIS data provided to function st_aswkb. 9373DO POW(-8714,REPEAT('1',32)) OR 9374VALIDATE_PASSWORD_STRENGTH(LAST_VALUE(1) OVER()); 9375ERROR 22003: DOUBLE value is out of range in 'pow(-(8714),repeat('1',32))' 9376DO SUBSTRING_INDEX(((((RADIANS(0xBD)) && (ST_SRID(1, 4326)))) OR 9377(COT(UUID_SHORT()))), FIRST_VALUE(ROW_COUNT())RESPECT NULLS OVER(), 171); 9378ERROR 22023: Invalid GIS data provided to function st_srid. 9379DO ((JSON_TYPE('4:8')) AND (((ROLES_GRAPHML()) LIKE (FIRST_VALUE(31543) 9380RESPECT NULLS OVER())))); 9381ERROR 22032: Invalid JSON text in argument 1 to function json_type: "The document root must not be followed by other values." at position 1. 9382DO CONCAT_WS( JSON_OBJECTAGG( ROW_COUNT(), BIT_COUNT(-23163)), 9383DEGREES((1.595545e+308)), REPEAT(FIRST_VALUE('%0') OVER(), 30)); 9384ERROR 22003: DOUBLE value is out of range in 'degrees(1.595545e+308)' 9385DO REPLACE((( RELEASE_LOCK( ACOS(0x41))) OR (1)), LAST_VALUE(9.750062e+306) 9386RESPECT NULLS OVER(), UUID_SHORT()); 9387ERROR 42000: Incorrect user-level lock name 'NULL'. 9388# End of test for Bug#26975848, Bug#27062796, Bug#27062694 9389