mirror of
https://github.com/MariaDB/server.git
synced 2025-01-18 04:53:01 +01:00
c30119adc7
- Make queries that use multiple window functions not to leak memory - Code cleanup in sql_window.cc
94 lines
2.5 KiB
Text
94 lines
2.5 KiB
Text
drop table if exists t1,t2;
|
|
# ########################################################################
|
|
# # Parser tests
|
|
# ########################################################################
|
|
#
|
|
# Check what happens when one attempts to use window function without OVER clause
|
|
create table t1 (a int, b int);
|
|
insert into t1 values (1,1),(2,2);
|
|
select row_number() from t1;
|
|
ERROR 42000: You have an error in your SQL syntax; check the manual that corresponds to your MariaDB server version for the right syntax to use near 'from t1' at line 1
|
|
select rank() from t1;
|
|
ERROR 42000: You have an error in your SQL syntax; check the manual that corresponds to your MariaDB server version for the right syntax to use near 'from t1' at line 1
|
|
# Attempt to use window function in the WHERE clause
|
|
select * from t1 where 1=rank() over (order by a);
|
|
ERROR HY000: Invalid use of group function
|
|
select * from t1 where 1>row_number() over (partition by b order by a);
|
|
ERROR HY000: Invalid use of group function
|
|
drop table t1;
|
|
# ########################################################################
|
|
# # Functionality tests
|
|
# ########################################################################
|
|
#
|
|
# Check if ROW_NUMBER() works in basic cases
|
|
create table t1(a int, b int, x char(32));
|
|
insert into t1 values (2, 10, 'xx');
|
|
insert into t1 values (2, 10, 'zz');
|
|
insert into t1 values (2, 20, 'yy');
|
|
insert into t1 values (3, 10, 'xxx');
|
|
insert into t1 values (3, 20, 'vvv');
|
|
select a, row_number() over (partition by a order by b) from t1;
|
|
a row_number() over (partition by a order by b)
|
|
2 1
|
|
2 2
|
|
2 3
|
|
3 1
|
|
3 2
|
|
select a, b, x, row_number() over (partition by a order by x) from t1;
|
|
a b x row_number() over (partition by a order by x)
|
|
2 10 xx 1
|
|
2 10 zz 3
|
|
2 20 yy 2
|
|
3 10 xxx 2
|
|
3 20 vvv 1
|
|
drop table t1;
|
|
create table t1 (pk int primary key, a int, b int);
|
|
insert into t1 values
|
|
(1, 10, 22),
|
|
(2, 11, 21),
|
|
(3, 12, 20),
|
|
(4, 13, 19),
|
|
(5, 14, 18);
|
|
select
|
|
pk, a, b,
|
|
row_number() over (order by a),
|
|
row_number() over (order by b)
|
|
from t1;
|
|
pk a b row_number() over (order by a) row_number() over (order by b)
|
|
1 10 22 1 5
|
|
2 11 21 2 4
|
|
3 12 20 3 3
|
|
4 13 19 4 2
|
|
5 14 18 5 1
|
|
drop table t1;
|
|
#
|
|
# Try RANK() function
|
|
#
|
|
create table t2 (
|
|
pk int primary key,
|
|
a int
|
|
);
|
|
insert into t2 values
|
|
( 1 , 0),
|
|
( 2 , 0),
|
|
( 3 , 1),
|
|
( 4 , 1),
|
|
( 8 , 2),
|
|
( 5 , 2),
|
|
( 6 , 2),
|
|
( 7 , 2),
|
|
( 9 , 4),
|
|
(10 , 4);
|
|
select pk, a, rank() over (order by a) from t2;
|
|
pk a rank() over (order by a)
|
|
1 0 1
|
|
2 0 1
|
|
3 1 3
|
|
4 1 3
|
|
8 2 5
|
|
5 2 5
|
|
6 2 5
|
|
7 2 5
|
|
9 4 9
|
|
10 4 9
|
|
drop table t2;
|