|
|
# Test of SQL window functions LEAD/LAG
|
|
|
# ----------------------------------------------------------------------
|
|
|
SET NAMES utf8mb4;
|
|
|
SELECT LEAD(6, 0) OVER ();
|
|
|
LEAD(6, 0) OVER ()
|
|
|
6
|
|
|
SELECT LEAD(NULL, 0) OVER ();
|
|
|
LEAD(NULL, 0) OVER ()
|
|
|
NULL
|
|
|
SELECT LEAD(6, 0) OVER (ROWS BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW);
|
|
|
LEAD(6, 0) OVER (ROWS BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW)
|
|
|
6
|
|
|
SELECT LEAD(NULL, 0) OVER (ROWS BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW);
|
|
|
LEAD(NULL, 0) OVER (ROWS BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW)
|
|
|
NULL
|
|
|
SELECT LEAD(6, 1) OVER ();
|
|
|
LEAD(6, 1) OVER ()
|
|
|
NULL
|
|
|
SELECT LEAD(NULL, 1) OVER ();
|
|
|
LEAD(NULL, 1) OVER ()
|
|
|
NULL
|
|
|
SELECT LEAD(6, 1) OVER (ROWS BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW);
|
|
|
LEAD(6, 1) OVER (ROWS BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW)
|
|
|
NULL
|
|
|
SELECT LEAD(NULL, 1) OVER (ROWS BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW);
|
|
|
LEAD(NULL, 1) OVER (ROWS BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW)
|
|
|
NULL
|
|
|
SELECT LEAD(6, 1, 7) OVER ();
|
|
|
LEAD(6, 1, 7) OVER ()
|
|
|
7
|
|
|
SELECT LEAD(NULL, 1, 7) OVER ();
|
|
|
LEAD(NULL, 1, 7) OVER ()
|
|
|
7
|
|
|
SELECT LEAD(6, 1, 7) OVER (ROWS BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW);
|
|
|
LEAD(6, 1, 7) OVER (ROWS BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW)
|
|
|
7
|
|
|
SELECT LEAD(NULL, 1, 7) OVER (ROWS BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW);
|
|
|
LEAD(NULL, 1, 7) OVER (ROWS BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW)
|
|
|
7
|
|
|
SELECT LAG(6, 0) OVER ();
|
|
|
LAG(6, 0) OVER ()
|
|
|
6
|
|
|
SELECT LAG(NULL, 0) OVER ();
|
|
|
LAG(NULL, 0) OVER ()
|
|
|
NULL
|
|
|
SELECT LAG(6, 0) OVER (ROWS BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW);
|
|
|
LAG(6, 0) OVER (ROWS BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW)
|
|
|
6
|
|
|
SELECT LAG(NULL, 0) OVER (ROWS BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW);
|
|
|
LAG(NULL, 0) OVER (ROWS BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW)
|
|
|
NULL
|
|
|
SELECT LAG(6, 1) OVER ();
|
|
|
LAG(6, 1) OVER ()
|
|
|
NULL
|
|
|
SELECT LAG(NULL, 1) OVER ();
|
|
|
LAG(NULL, 1) OVER ()
|
|
|
NULL
|
|
|
SELECT LAG(6, 1) OVER (ROWS BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW);
|
|
|
LAG(6, 1) OVER (ROWS BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW)
|
|
|
NULL
|
|
|
SELECT LAG(NULL, 1) OVER (ROWS BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW);
|
|
|
LAG(NULL, 1) OVER (ROWS BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW)
|
|
|
NULL
|
|
|
SELECT LAG(6, 1, 7) OVER ();
|
|
|
LAG(6, 1, 7) OVER ()
|
|
|
7
|
|
|
SELECT LAG(NULL, 1, 7) OVER ();
|
|
|
LAG(NULL, 1, 7) OVER ()
|
|
|
7
|
|
|
SELECT LAG(6, 1, 7) OVER (ROWS BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW);
|
|
|
LAG(6, 1, 7) OVER (ROWS BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW)
|
|
|
7
|
|
|
SELECT LAG(NULL, 1, 7) OVER (ROWS BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW);
|
|
|
LAG(NULL, 1, 7) OVER (ROWS BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW)
|
|
|
7
|
|
|
CREATE TABLE t1 (d DOUBLE, id INT, sex CHAR(1), n INT NOT NULL AUTO_INCREMENT, PRIMARY KEY(n));
|
|
|
INSERT INTO t1(d, id, sex) VALUES (1.0, 1, 'M'),
|
|
|
(2.0, 2, 'F'),
|
|
|
(3.0, 3, 'F'),
|
|
|
(4.0, 4, 'F'),
|
|
|
(5.0, 5, 'M'),
|
|
|
(NULL, NULL, 'M'),
|
|
|
(10.0, 10, NULL),
|
|
|
(10.0, 10, NULL),
|
|
|
(11.0, 11, NULL);
|
|
|
SELECT id, sex, LEAD(id, -1) RESPECT NULLS OVER () FROM t1;
|
|
|
ERROR 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) RESPECT NULLS OVER () FROM t1' at line 1
|
|
|
SELECT id, sex, LEAD(id, 1.2) RESPECT NULLS OVER () FROM t1;
|
|
|
ERROR HY000: Incorrect arguments to lead
|
|
|
SELECT id, sex, LEAD(id, 'a') RESPECT NULLS OVER () FROM t1;
|
|
|
ERROR 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') RESPECT NULLS OVER () FROM t1' at line 1
|
|
|
SELECT id, sex, LEAD(id, NULL) RESPECT NULLS OVER () FROM t1;
|
|
|
ERROR 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 'NULL) RESPECT NULLS OVER () FROM t1' at line 1
|
|
|
PREPARE p FROM "SELECT id, sex, LEAD(id, ?) OVER () FROM t1";
|
|
|
SET @p1= 3;
|
|
|
EXECUTE p USING @p1;
|
|
|
id sex LEAD(id, ?) OVER ()
|
|
|
1 M 4
|
|
|
2 F 5
|
|
|
3 F NULL
|
|
|
4 F 10
|
|
|
5 M 10
|
|
|
NULL M 11
|
|
|
10 NULL NULL
|
|
|
10 NULL NULL
|
|
|
11 NULL NULL
|
|
|
SET @p1= -3;
|
|
|
EXECUTE p USING @p1;
|
|
|
id sex LEAD(id, ?) OVER ()
|
|
|
1 M NULL
|
|
|
2 F NULL
|
|
|
3 F NULL
|
|
|
4 F 1
|
|
|
5 M 2
|
|
|
NULL M 3
|
|
|
10 NULL 4
|
|
|
10 NULL 5
|
|
|
11 NULL NULL
|
|
|
SET @p1= 'a';
|
|
|
EXECUTE p USING @p1;
|
|
|
ERROR HY000: Incorrect arguments to lead
|
|
|
PREPARE p FROM "SELECT id, sex, LEAD(id+?, ?, ?) RESPECT NULLS OVER () FROM t1";
|
|
|
SET @p1= 8;
|
|
|
SET @p2= 3;
|
|
|
SET @p3= 7;
|
|
|
EXECUTE p USING @p1, @p2, @p3;
|
|
|
id sex LEAD(id+?, ?, ?) RESPECT NULLS OVER ()
|
|
|
1 M 12
|
|
|
2 F 13
|
|
|
3 F NULL
|
|
|
4 F 18
|
|
|
5 M 18
|
|
|
NULL M 19
|
|
|
10 NULL 7
|
|
|
10 NULL 7
|
|
|
11 NULL 7
|
|
|
EXECUTE p USING @p1, @p2, @p3;
|
|
|
id sex LEAD(id+?, ?, ?) RESPECT NULLS OVER ()
|
|
|
1 M 12
|
|
|
2 F 13
|
|
|
3 F NULL
|
|
|
4 F 18
|
|
|
5 M 18
|
|
|
NULL M 19
|
|
|
10 NULL 7
|
|
|
10 NULL 7
|
|
|
11 NULL 7
|
|
|
DROP PREPARE p;
|
|
|
SELECT id, sex, LEAD(id, 1, 10) IGNORE NULLS OVER () FROM t1;
|
|
|
ERROR 42000: This version of MySQL doesn't yet support 'IGNORE NULLS'
|
|
|
Ok, default semantics:
|
|
|
First without default:
|
|
|
SELECT id, sex, LEAD(id, 1) RESPECT NULLS OVER () FROM t1;
|
|
|
id sex LEAD(id, 1) RESPECT NULLS OVER ()
|
|
|
1 M 2
|
|
|
2 F 3
|
|
|
3 F 4
|
|
|
4 F 5
|
|
|
5 M NULL
|
|
|
NULL M 10
|
|
|
10 NULL 10
|
|
|
10 NULL 11
|
|
|
11 NULL NULL
|
|
|
SELECT id, sex, LAG(id, 1) RESPECT NULLS OVER () FROM t1;
|
|
|
id sex LAG(id, 1) RESPECT NULLS OVER ()
|
|
|
1 M NULL
|
|
|
2 F 1
|
|
|
3 F 2
|
|
|
4 F 3
|
|
|
5 M 4
|
|
|
NULL M 5
|
|
|
10 NULL NULL
|
|
|
10 NULL 10
|
|
|
11 NULL 10
|
|
|
SELECT id, sex, LEAD(id, 0) RESPECT NULLS OVER () FROM t1;
|
|
|
id sex LEAD(id, 0) RESPECT NULLS OVER ()
|
|
|
1 M 1
|
|
|
2 F 2
|
|
|
3 F 3
|
|
|
4 F 4
|
|
|
5 M 5
|
|
|
NULL M NULL
|
|
|
10 NULL 10
|
|
|
10 NULL 10
|
|
|
11 NULL 11
|
|
|
Now with default:
|
|
|
SELECT id, sex, LEAD(id, 1, id) RESPECT NULLS OVER () FROM t1;
|
|
|
id sex LEAD(id, 1, id) RESPECT NULLS OVER ()
|
|
|
1 M 2
|
|
|
2 F 3
|
|
|
3 F 4
|
|
|
4 F 5
|
|
|
5 M NULL
|
|
|
NULL M 10
|
|
|
10 NULL 10
|
|
|
10 NULL 11
|
|
|
11 NULL 11
|
|
|
SELECT id, sex, LAG(id, 1, id) RESPECT NULLS OVER () FROM t1;
|
|
|
id sex LAG(id, 1, id) RESPECT NULLS OVER ()
|
|
|
1 M 1
|
|
|
2 F 1
|
|
|
3 F 2
|
|
|
4 F 3
|
|
|
5 M 4
|
|
|
NULL M 5
|
|
|
10 NULL NULL
|
|
|
10 NULL 10
|
|
|
11 NULL 10
|
|
|
SELECT id, sex, LEAD(id, 0, 7) RESPECT NULLS OVER () FROM t1;
|
|
|
id sex LEAD(id, 0, 7) RESPECT NULLS OVER ()
|
|
|
1 M 1
|
|
|
2 F 2
|
|
|
3 F 3
|
|
|
4 F 4
|
|
|
5 M 5
|
|
|
NULL M NULL
|
|
|
10 NULL 10
|
|
|
10 NULL 10
|
|
|
11 NULL 11
|
|
|
SELECT n, id, LEAD(id, 1, 3) OVER
|
|
|
(ORDER BY id DESC, n ASC ROWS BETWEEN CURRENT ROW AND 2 FOLLOWING) L FROM t1;
|
|
|
n id L
|
|
|
9 11 10
|
|
|
7 10 10
|
|
|
8 10 5
|
|
|
5 5 4
|
|
|
4 4 3
|
|
|
3 3 2
|
|
|
2 2 1
|
|
|
1 1 NULL
|
|
|
6 NULL 3
|
|
|
SELECT n, id, LAG(id, 0, n*n) OVER
|
|
|
(ORDER BY id DESC, n ASC ROWS BETWEEN CURRENT ROW AND 2 FOLLOWING) L FROM t1;
|
|
|
n id L
|
|
|
9 11 11
|
|
|
7 10 10
|
|
|
8 10 10
|
|
|
5 5 5
|
|
|
4 4 4
|
|
|
3 3 3
|
|
|
2 2 2
|
|
|
1 1 1
|
|
|
6 NULL NULL
|
|
|
SELECT n, id, LAG(id, 1, n*n) OVER
|
|
|
(ORDER BY id DESC, n ASC ROWS BETWEEN CURRENT ROW AND 2 FOLLOWING) L FROM t1;
|
|
|
n id L
|
|
|
9 11 81
|
|
|
7 10 11
|
|
|
8 10 10
|
|
|
5 5 10
|
|
|
4 4 5
|
|
|
3 3 4
|
|
|
2 2 3
|
|
|
1 1 2
|
|
|
6 NULL 1
|
|
|
SELECT n, id, LEAD(id, 1, n*n) OVER
|
|
|
(ORDER BY id DESC, n ASC ROWS BETWEEN CURRENT ROW AND 2 FOLLOWING) L FROM t1;
|
|
|
n id L
|
|
|
9 11 10
|
|
|
7 10 10
|
|
|
8 10 5
|
|
|
5 5 4
|
|
|
4 4 3
|
|
|
3 3 2
|
|
|
2 2 1
|
|
|
1 1 NULL
|
|
|
6 NULL 36
|
|
|
Check imcompatible character sets
|
|
|
CREATE TABLE t (c1 CHAR(10) CHARACTER SET big5,
|
|
|
i INT,
|
|
|
c2 VARCHAR(10) CHARACTER SET euckr);
|
|
|
SELECT c1, c2, LEAD(c1, 0, c2) OVER () l0 FROM t;
|
|
|
ERROR HY000: Illegal mix of collations (big5_chinese_ci,IMPLICIT) and (euckr_korean_ci,IMPLICIT) for operation 'lead'
|
|
|
DROP TABLE t;
|
|
|
Check default char set & collation promotion to result
|
|
|
CREATE TABLE t (c1 CHAR(10) CHARACTER SET utf8mb4,
|
|
|
i INT,
|
|
|
c2 VARCHAR(10) CHARACTER SET latin1);
|
|
|
INSERT INTO t VALUES('A', 1, '1');
|
|
|
INSERT INTO t VALUES('A', 3, '3');
|
|
|
INSERT INTO t VALUES(x'F09F90AC' /* dolphin */, 5, null);
|
|
|
INSERT INTO t VALUES('A', 5, null);
|
|
|
INSERT INTO t VALUES(null, 10, '0');
|
|
|
Result sets should contain dolphin in columns c1 and l1.
|
|
|
SELECT c1, c2, LEAD(c1, 0, c2) OVER () l0 FROM t;
|
|
|
c1 c2 l0
|
|
|
A 1 A
|
|
|
A 3 A
|
|
|
🐬 NULL 🐬
|
|
|
A NULL A
|
|
|
NULL 0 NULL
|
|
|
SELECT c1, c2, LEAD(c1, 1, c2) OVER () l1 FROM t;
|
|
|
c1 c2 l1
|
|
|
A 1 A
|
|
|
A 3 🐬
|
|
|
🐬 NULL A
|
|
|
A NULL NULL
|
|
|
NULL 0 0
|
|
|
SELECT c1, c2, LEAD(c2, 1, c1) OVER () l1 FROM t;
|
|
|
c1 c2 l1
|
|
|
A 1 3
|
|
|
A 3 NULL
|
|
|
🐬 NULL NULL
|
|
|
A NULL 0
|
|
|
NULL 0 NULL
|
|
|
Use CREATE TABLE AS to show type of the resulting LEAD function
|
|
|
CREATE TABLE tt AS SELECT LEAD(c1, 0, c2) OVER () c FROM t;
|
|
|
CREATE TABLE ts AS SELECT LEAD(c2, 1, c1) OVER () c FROM t;
|
|
|
Both tables should have c as VARCHAR(10) CHARACTER SET utf8mb4
|
|
|
even though only c1 has that type.
|
|
|
SHOW CREATE TABLE tt;
|
|
|
Table Create Table
|
|
|
tt CREATE TABLE `tt` (
|
|
|
`c` varchar(10) DEFAULT NULL
|
|
|
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_0900_ai_ci
|
|
|
SHOW CREATE TABLE ts;
|
|
|
Table Create Table
|
|
|
ts CREATE TABLE `ts` (
|
|
|
`c` varchar(10) DEFAULT NULL
|
|
|
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_0900_ai_ci
|
|
|
DROP TABLE t, tt, ts;
|
|
|
Defaults: check non obvious type pairs, comparing with IFNULL
|
|
|
whose type reconciliation behavior we emulate when combining
|
|
|
types in LEAD/LAG with default value.
|
|
|
CREATE TABLE t (c1 VARCHAR(10),
|
|
|
j1 JSON,
|
|
|
g1 POINT,
|
|
|
i1 INT,
|
|
|
b1 BLOB,
|
|
|
d1 DOUBLE,
|
|
|
e1 DECIMAL(5,4),
|
|
|
e2 DECIMAL(5,2));
|
|
|
INSERT INTO t VALUES (null, '[6]', ST_POINTFROMTEXT('POINT(6 6)'), 6, '6', 6.0, 10.0/3, 20.0/3),
|
|
|
('7', null , ST_POINTFROMTEXT('POINT(7 7)'), 7, '7', 7.0, 10.0/3, 20.0/3),
|
|
|
('8', '[8]' , null, 7, '8', 8.0, 10.0/3, 20.0/3),
|
|
|
('9', '[9]' , ST_POINTFROMTEXT('POINT(9 9)'), null, '9', 9.0, 10.0/3, 20.0/3),
|
|
|
('0', '[0]' , ST_POINTFROMTEXT('POINT(0 0)'), 0, null, 0.0, 10.0/3, 20.0/3),
|
|
|
('1', '[1]' , ST_POINTFROMTEXT('POINT(1 1)'), 1, '1', null, 10.0/3, 20.0/3),
|
|
|
('2', '[2]' , ST_POINTFROMTEXT('POINT(2 2)'), 2, '2', 2.0, null, 20.0/3),
|
|
|
('3', '[3]' , ST_POINTFROMTEXT('POINT(3 3)'), 3, '3', 3.0, 10.0/3, null);
|
|
|
Warnings:
|
|
|
Note 1265 Data truncated for column 'e1' at row 1
|
|
|
Note 1265 Data truncated for column 'e2' at row 1
|
|
|
Note 1265 Data truncated for column 'e1' at row 2
|
|
|
Note 1265 Data truncated for column 'e2' at row 2
|
|
|
Note 1265 Data truncated for column 'e1' at row 3
|
|
|
Note 1265 Data truncated for column 'e2' at row 3
|
|
|
Note 1265 Data truncated for column 'e1' at row 4
|
|
|
Note 1265 Data truncated for column 'e2' at row 4
|
|
|
Note 1265 Data truncated for column 'e1' at row 5
|
|
|
Note 1265 Data truncated for column 'e2' at row 5
|
|
|
Note 1265 Data truncated for column 'e1' at row 6
|
|
|
Note 1265 Data truncated for column 'e2' at row 6
|
|
|
Note 1265 Data truncated for column 'e2' at row 7
|
|
|
Note 1265 Data truncated for column 'e1' at row 8
|
|
|
SELECT LEAD(c1, 100, j1) OVER () lcj, IFNULL(c1, j1) ifn_cj FROM t;
|
|
|
lcj ifn_cj
|
|
|
[6] [6]
|
|
|
NULL 7
|
|
|
[8] 8
|
|
|
[9] 9
|
|
|
[0] 0
|
|
|
[1] 1
|
|
|
[2] 2
|
|
|
[3] 3
|
|
|
SELECT LEAD(j1, 100, j1) OVER () lcj, IFNULL(c1, j1) ifn_cj FROM t;
|
|
|
lcj ifn_cj
|
|
|
[6] [6]
|
|
|
NULL 7
|
|
|
[8] 8
|
|
|
[9] 9
|
|
|
[0] 0
|
|
|
[1] 1
|
|
|
[2] 2
|
|
|
[3] 3
|
|
|
SELECT LEAD(c1, 100, g1) OVER () lcg, IFNULL(c1, g1) ifn_cg FROM t;
|
|
|
lcg ifn_cg
|
|
|
|