summaryrefslogtreecommitdiff
path: root/mysql-test/main/rownum.test
blob: 291bd9bd99330b071cfbf01566eed51f816e7459 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
--source include/have_sequence.inc
#
# Test of basic rownum() functionallity
# Test are done with Aria to ensure that row order is stable
#

CREATE OR REPLACE TABLE t1(a int, b int) engine=aria;
CREATE OR REPLACE TABLE t2(a int, b int) engine=aria;
insert into t1 values (1,10),(2,20),(3,30);
insert into t2 values (2,21),(3,31),(4,41);

--echo #
--echo # Simple selects
--echo #

select a,b,rownum() from t1;
select a,b,rownum() from t1 where rownum() < 2;
select a,b from t1 where rownum() <= 2;
select a,b from t1 where rownum() > 2;

--echo #
--echo # Subqueries
--echo #

select t1.a,rownum(),t3.a,t3.t2_rownum from t1, (select t2.a,rownum() as t2_rownum from t2 where rownum() <=2) t3;
select t1.a, (select t2.b from t2 where t1.a=t2.a and rownum() <= 1) 'b' from t1;
select t1.a, t3.a from t1, (select * from t2 where rownum() <= 2) t3;
select * from (select tt.*,rownum() as id from (select * from t1) tt) t3 where id>2;

--echo #
--echo # Joins
--echo #

select t1.a,t1.b,t2.a,t2.b,rownum() from t1,t2 where rownum() <= 4;
select *,rownum() from t1,t2 where t1.a=t2.a and rownum()<=2;
select * from t1 left join t2 on (t2.a=t1.a and rownum()=0);
select * from t1 left join t2 on (t2.a=t1.a and rownum()>1);
select * from t1 left join t2 on (t2.a=t1.a and rownum()<1);
select * from t1 left join t2 on (t2.a=t1.a and rownum()=1);
select * from t1 left join t2 on (t2.a=t1.a and rownum()>=1);

--echo #
--echo # Union
--echo #

select * from t1 where rownum() <=2 union select * from t2 where rownum()<= 1;

--echo #
--echo # Order by
--echo #

select * from t1 where rownum() <= 2 order by a desc;
explain select * from t1 where rownum() <= 2 order by a desc;
select t1.a,t1.b,rownum() from t1 where rownum() <= 2 order by a desc;
explain select t1.a,t1.b,rownum() from t1 where rownum() <= 2 order by a desc;
select *,rownum() from t1,t2;
select *,rownum() from t1,t2 order by t2.a desc, t1.a desc;
select * from (select * from t1 order by a desc) as t where rownum() <= 2;
select * from t1,t2 where t1.a=t2.a and rownum()<=2 order by t1.a,t2.a;

create view v1 as
select * from (select * from t1 order by a desc) as t where rownum() <= 2;
select * from v1;
drop view v1;

--echo #
--echo # Having
--echo #

select t1.a, sum(t1.b), rownum() as 'r' from t1 group by t1.a having r <= 2;
select * from t1 having rownum() <= 2;
select t1.a, sum(t1.b), rownum() from t1 group by t1.a;
select t1.a, sum(t1.b), rownum() as 'r' from t1 group by t1.a having r <= 2;
select t1.a, sum(t1.b), rownum() as 'r' from t1 group by t1.a having rownum() <= 2;
select t1.a, sum(t1.b), rownum() as 'r' from t1 group by t1.a having r <= 2 order by a desc;
select t1.a, sum(t1.b), rownum() as 'r' from t1 group by t1.a having rownum() <= 2 order by a desc;

--echo #
--echo # Sum functions
--echo #

select max(rownum()),min(rownum()) from t1;
select sum(rownum()),avg(rownum()) from t1;

--echo #
--echo # Group by
--echo #

select t1.a,sum(t1.b) from t1 where rownum() < 2 group by t1.a;
select t1.a,sum(t2.b) from t1 JOIN t2 ON (t1.a=t2.a) where rownum() <= 2 group by t1.a;
select * from (select t1.a,sum(t2.b) from t1 JOIN t2 ON (t1.a=t2.a) group by t1.a) as t where rownum() <= 1;
select t1.a,sum(rownum()),count(*) from t1 where rownum() <= 2 group by t1.a;
select * from (select t1.a,sum(t1.b) from t1 group by t1.a) as t3 where rownum() < 2;

create table t3 (a int) engine=myisam;
insert into t3 values (3),(5),(5),(3);
select a, max(rownum()) from t3 group by a;
drop table t3;

CREATE TABLE t3 (
  a int(11) DEFAULT NULL,
  b varchar(1024) DEFAULT NULL
);
insert into t3 select mod(seq*3,20)+1, repeat(char(33+mod(seq,90)),mod(seq,10)*100) from seq_1_to_23;
#chack after fix MDEV-28571
--disable_view_protocol
SELECT sq.a,length(sq.f) FROM (SELECT a, GROUP_CONCAT(b,b) AS f FROM t3 GROUP BY a ORDER BY a desc) as sq WHERE ROWNUM() <= 10;
--enable_view_protocol
drop table t3;

--echo #
--echo # Prepared statements
--echo #

PREPARE stmt1 from "select a,b,rownum() from t1 where rownum() <= 2";
execute stmt1;
execute stmt1;
deallocate prepare stmt1;

--echo #
--echo # Views
--echo #

create view v1 as select t1.a,rownum() from t1;
select * from v1;
select t1.a,v1.* from t1,v1 where t1.a=v1.a;
drop view v1;

CREATE TABLE t3 (a INT);
INSERT INTO t3 VALUES (1),(2),(3);
CREATE VIEW v1 AS SELECT a FROM t3 WHERE ROWNUM() <= 2;
SELECT * FROM v1;
drop view v1;
drop table t3;

--echo #
--echo # Reserved words
--echo #

create table t4 (a int, rownum int);
insert into t4 (a,rownum) values (1,2);
select t4.a,t4.rownum from t4;
drop table t4;

--echo #
--echo # Test Oracle mode
--echo #

set SQL_MODE=ORACLE;
select t1.a,rownum from t1 where rownum<=2;
select t1.a,rownum() from t1 where rownum()<=2;
--error ER_PARSE_ERROR
create table t4 (a int, rownum int);

DELIMITER |;
DECLARE
   CURSOR c_cursor
   IS select a,b,rownum from t1 where rownum <= 2;
   v_a  t1.a%TYPE;
   v_b  t1.b%TYPE;
   v_rn t1.a%TYPE;
BEGIN
  OPEN c_cursor;
  FETCH c_cursor INTO v_a, v_b, v_rn;
  WHILE c_cursor%FOUND LOOP
     SELECT concat(v_a,'--',v_b,'--',v_rn);
     FETCH c_cursor INTO v_a, v_b, v_rn;
  END LOOP;
  CLOSE c_cursor;
END;|
DELIMITER ;|

select a, rownum from t1 group by a, rownum having rownum < 3;
select a, rownum as r from t1 group by a, rownum having r < 3;
select a, rownum from t1 group by a, rownum having "rownum" < 3;
select a, rownum from t1 group by a, rownum having rownum < 3 order by a desc;
select a, rownum as r from t1 group by a, rownum having r < 3 order by a desc;
select a, rownum from t1 group by a, rownum having "rownum" < 3 order by a desc;

set SQL_MODE=DEFAULT;

--echo # Cleanup

drop table t1,t2;

--echo #
--echo # INSERT
--echo #

create table t1 (a int not null primary key, b int);
insert into t1 values (1,rownum()),(2,rownum()),(3,rownum());
select * from t1;
drop table t1;

--echo #
--echo # INSERT DELAYED
--echo #

create table t1 (a int not null primary key, b int);
insert delayed into t1 values (1,rownum()),(2,rownum()),(3,rownum());
let $wait_condition= SELECT COUNT(*)=3 FROM t1;
source include/wait_condition.inc;
select * from t1;
drop table t1;

--echo #
--echo # INSERT IGNORED
--echo #

create table t1 (a int not null primary key, b int);
# with VALUES
insert ignore into t1 values (1,rownum()),(2,rownum()),(2,rownum()),(3,rownum());
select * from t1;
delete from t1;
# with SELECT
insert ignore into t1 select * from (values (1,rownum()),(2,rownum()),(2,rownum()),(3,rownum())) t;
select * from t1;
drop table t1;

--echo #
--echo # INSERT ... RETURNING
--echo #

create or replace table t1 (a int);
insert into t1 values (1),(2) returning a, rownum();
drop table t1;

--echo #
--echo # UPDATE
--echo #

create table t1 (a int not null primary key, b int);
insert into t1 values (1,1),(2,2),(3,3);
update t1 set b=0;
update t1 set b=rownum()+1;
select * from t1;

update t1 set b=0;
update t1 set b=rownum() where a < 10 and rownum() < 2;
select * from t1;
drop table t1;

create table t1 (a int);
insert into t1 values (10),(20),(30);
update t1 set a = rownum();
select * from t1;
update t1 set a = rownum();
select * from t1;
drop table t1;

--echo #
--echo # DELETE
--echo #

create table t1 (a int not null primary key, b int);
insert into t1 values (1,1),(2,0),(3,0);
delete from t1 where a < 10 and rownum() < 2;
select * from t1;
drop table t1;

--echo #
--echo # MULTI-TABLE-DELETE
--echo #

create table t1 (a int not null primary key);
insert into t1 values (1),(2),(3);
create table t2 (a int not null primary key);
insert into t2 values (1),(2),(3);

delete t1,t2 from t1,t2 where t1.a=t2.a and rownum() <= 2;
select * from t1;
select * from t2;
drop table t1,t2;

--echo #
--echo # MULTI-TABLE-UPDATE

CREATE TABLE t1 (ID INT);
CREATE TABLE t2 (ID INT,
  s1 TEXT, s2 TEXT, s3 VARCHAR(10), s4 TEXT, s5 VARCHAR(10));

INSERT INTO t1 VALUES (1),(2);
INSERT INTO t2 VALUES (1,'test', 'test', 'test', 'test', 'test'),
                      (2,'test', 'test', 'test', 'test', 'test');

SELECT * FROM t1 LEFT JOIN t2 USING(ID);
UPDATE t1 LEFT JOIN t2 USING(ID) SET s1 = 'changed';
select * from t2;
update t2 set s1="";
UPDATE t1 LEFT JOIN t2 USING(ID) SET s1 = 'changed' where rownum() <=1;
select * from t2;
drop table t1,t2;

--echo #
--echo # LOAD DATA
--echo #

create table t1 (a int, b int, c int);
load data infile '../../std_data/loaddata7.dat' into table t1 fields terminated by ',' lines terminated by "\r\n" (a,b) set c=rownum();
select * from t1;
drop table t1;

--echo #
--echo # LIMIT OPTIMIZATION
--echo #

create table t1 (a int);
insert into t1 select seq from seq_1_to_100;

flush status;
select * from t1 where rownum() <= 3;
show status like "Rows_read";
flush status;
select * from t1 where rownum() <= 4 and rownum() <= 3;
show status like "Rows_read";
flush status;
select * from t1 where rownum() < 4 and a > 10;
show status like "Rows_read";
flush status;
select * from t1 where 3 >= rownum();
show status like "Rows_read";
flush status;
select * from t1 where 4 > rownum() and a > 20;
show status like "Rows_read";
flush status;
select * from t1 where rownum() = 1 and a > 10;
show status like "Rows_read";
flush status;
select * from t1 where a > 30 && 1 = rownum();
show status like "Rows_read";
flush status;

--echo # No limit optimization

select * from t1 where rownum() > 10;
show status like "Rows_read";
flush status;
select * from t1 where 10 < rownum();
show status like "Rows_read";
flush status;
select * from t1 where rownum() >= 10;
show status like "Rows_read";
flush status;
select * from t1 where 10 < rownum();
show status like "Rows_read";
flush status;
select * from t1 where 10 <= rownum();
show status like "Rows_read";
flush status;
select * from t1 where 2 = rownum();
show status like "Rows_read";
flush status;
select * from t1 where rownum() = 2;
show status like "Rows_read";
flush status;
select * from t1 where rownum() <= 0;
show status like "Rows_read";
flush status;
select *,rownum() from t1 where rownum() < 10 limit 4, 4;
show status like "Rows_read";
flush status;
select * from t1 where rownum() < 10 order by a;
show status like "Rows_read";
flush status;


--echo # rownum and limit

select * from t1 where rownum() < 4 limit 10;
show status like "Rows_read";
flush status;
select * from t1 where rownum() < 10 limit 4;
show status like "Rows_read";

drop table t1;

--echo #
--echo # Rownum examples from Woqutech
--echo #

set SQL_MODE=ORACLE;
create table t1 (c1 int ,c2 varchar(20)) engine=myisam;
insert into t1 values (1, 'aaa'),(2, 'bbb'),(3, 'ccc'),(4, 'ddd'),(5, 'eee');
update t1 set c2 = 'xxx' where rownum = 2;
select * from t1 where c2='xxx';
update t1 set c2 = 'xxx' where  rownum < 3;
select * from t1 where c2='xxx';
delete from t1 where rownum = 2;
select count(*) from t1;
delete from t1 where rownum < 3;
select count(*) from t1;
delete from t1 where c1=rownum ;
select count(*) from t1;
delete from t1 where c1=rownum+2 ;
select count(*) from t1;
set SQL_MODE=DEFAULT;
drop table t1;

--echo #
--echo # Rownum() used in not supported places (returns 0 or gives an error)
--echo #

set @a=rownum();
select @a;

--error ER_GENERATED_COLUMN_FUNCTION_IS_NOT_ALLOWED
create or replace table t (a int, b int as (rownum()) virtual);

create table t1 (a int);
insert into t1 values (3),(1),(5),(8),(4);
handler t1 open;
--error ER_GENERATED_COLUMN_FUNCTION_IS_NOT_ALLOWED
handler t1 read next where rownum() < 1;
handler t1 close;
drop table t1;

# rownum() executed in a function will be run in the function context.

create table t1 (a int not null primary key, b int);
insert into t1 values (1,1),(2,2),(3,3);
create function f() returns int return rownum();
select a, rownum(), f() from t1;
drop function f;
drop table t1;

# rownum() executed in a trigger will be run in the function context.

create or replace table t1 (a int, r int);
create trigger tr before update on t1 for each row set NEW.r = rownum();
insert into t1 (a) values (1),(2);
select * from t1;
update t1 set a=a+10;
select * from t1;
drop trigger tr;
drop table t1;

--echo #
--echo # LIMIT optimisation
--echo #

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

--disable_view_protocol
let $query=
select * from (select a from t1 where a < 1000) as tt where rownum() <= 2;
flush status;
eval $query;
show status like "Rows_read";
eval explain extended $query;
eval prepare stmt from "$query";
flush status;
execute stmt;
show status like "Rows_read";
flush status;
execute stmt;
show status like "Rows_read";
deallocate prepare stmt;


let $query=
select * from (select a from t1 where a < 1000 group by a) as tt where rownum() <= 2;
flush status;
eval $query;
show status like "Rows_read";
eval explain extended $query;
eval prepare stmt from "$query";
execute stmt;
execute stmt;
deallocate prepare stmt;

let $query=
select * from (select a from t1 where a < 1000 group by a order by 1) as tt where rownum() <= 2;
flush status;
eval $query;
show status like "Rows_read";
eval explain extended $query;
eval prepare stmt from "$query";
execute stmt;
execute stmt;
deallocate prepare stmt;

let $query=
select * from (select a from t1 where a < 1000 union select 10) as tt where rownum() <= 2;
flush status;
eval $query;
show status like "Rows_read";
eval explain extended $query;
eval prepare stmt from "$query";
execute stmt;
execute stmt;
deallocate prepare stmt;
--enable_view_protocol

--echo # Other limit

let $query=
select * from (select a from t1 where a < 1000 group by a order by 1 limit 1) as tt where rownum() <= 2;
eval $query;
eval explain extended $query;
eval prepare stmt from "$query";
execute stmt;
execute stmt;
deallocate prepare stmt;

--echo # Other limit less

let $query=
select * from (select a from t1 where a < 1000 group by a order by 1 limit 10) as tt where rownum() <= 2;
eval $query;
eval explain extended $query;
eval prepare stmt from "$query";
execute stmt;
execute stmt;
deallocate prepare stmt;

let $query=
select * from (select a from t1 where a < 1000 union select 10 limit 1) as tt where rownum() <= 2;
eval $query;
eval explain extended $query;
eval prepare stmt from "$query";
execute stmt;
execute stmt;
deallocate prepare stmt;

--echo # < rownum

let $query=
select * from (select a from t1 where a < 1000) as tt where rownum() < 2;
eval $query;
eval explain extended $query;
eval prepare stmt from "$query";
execute stmt;
execute stmt;
deallocate prepare stmt;

--echo # Simple expression

let $query=
select * from (select a from t1 where a < 1000) as tt where rownum() <= 1+1;
eval $query;
eval explain extended $query;
eval prepare stmt from "$query";
execute stmt;
execute stmt;
deallocate prepare stmt;

--echo # Simple expression reversed

let $query=
select * from (select a from t1 where a < 1000) as tt where  1+1 >= rownum();
eval $query;
eval explain extended $query;
eval prepare stmt from "$query";
execute stmt;
execute stmt;
deallocate prepare stmt;

--echo # expensive (no opt)

let $query=
select * from (select a from t1 where a < 1000) as tt where  (select max(a) from t1) >= rownum();
eval $query;
eval explain extended $query;
eval prepare stmt from "$query";
execute stmt;
execute stmt;
deallocate prepare stmt;

drop table t1;

--echo #
--echo # Table value constructors
--echo #
values ("first row"),("next row is 3"),(rownum()),("next row is 5"),(rownum());

--echo #
--echo # MDEV-31073: Server crash, assertion `table != 0 &&
--echo # view->field_translation != 0' failure with ROWNUM and view
--echo #

CREATE TABLE t (f INT);
INSERT INTO t VALUES (1),(2);
CREATE VIEW v AS SELECT * FROM t;
UPDATE v SET f = 10 WHERE ROWNUM() > 42 LIMIT 1;

# Cleanup
DROP VIEW v;
DROP TABLE t;

CREATE TABLE t (f INT);
INSERT INTO t VALUES (1),(2);
CREATE VIEW v AS SELECT f, 3 as e FROM t;
UPDATE v SET f = 10 WHERE e > 42 LIMIT 1;

# Cleanup
DROP VIEW v;
DROP TABLE t;

CREATE TABLE t (f INT);
INSERT INTO t VALUES (1),(2);
CREATE VIEW v AS SELECT f, ROWNUM() as e FROM t;
--error ER_NON_UPDATABLE_TABLE
UPDATE v SET f = 10 WHERE e > 42 LIMIT 1;

# Cleanup
DROP VIEW v;
DROP TABLE t;

--echo #
--echo # End of 10.6 tests
--echo #