You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
774 lines
20 KiB
774 lines
20 KiB
5 months ago
|
# Test of SQL window functions NTH_VALUE
|
||
|
# ----------------------------------------------------------------------
|
||
|
SET NAMES utf8;
|
||
|
Warnings:
|
||
|
Warning 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.
|
||
|
SELECT NTH_VALUE(6,1) OVER ();
|
||
|
NTH_VALUE(6,1) OVER ()
|
||
|
6
|
||
|
SELECT NTH_VALUE(NULL,1) OVER ();
|
||
|
NTH_VALUE(NULL,1) OVER ()
|
||
|
NULL
|
||
|
SELECT NTH_VALUE(6,2) OVER ();
|
||
|
NTH_VALUE(6,2) OVER ()
|
||
|
NULL
|
||
|
SELECT NTH_VALUE(6,1) OVER (ROWS BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW);
|
||
|
NTH_VALUE(6,1) OVER (ROWS BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW)
|
||
|
6
|
||
|
SELECT NTH_VALUE(NULL,1) OVER (ROWS BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW);
|
||
|
NTH_VALUE(NULL,1) OVER (ROWS BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW)
|
||
|
NULL
|
||
|
SELECT NTH_VALUE(6,2) OVER (ROWS BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW);
|
||
|
NTH_VALUE(6,2) OVER (ROWS BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW)
|
||
|
NULL
|
||
|
CREATE TABLE t1 (d DOUBLE, id INT, sex CHAR(1));
|
||
|
INSERT INTO t1 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);
|
||
|
not supported
|
||
|
SELECT id, sex, NTH_VALUE(id, 1) FROM LAST OVER () FROM t1;
|
||
|
ERROR 42000: This version of MySQL doesn't yet support 'FROM LAST'
|
||
|
ok
|
||
|
SELECT id, sex, NTH_VALUE(id, 1) FROM FIRST OVER () FROM t1;
|
||
|
id sex NTH_VALUE(id, 1) FROM FIRST OVER ()
|
||
|
1 M 1
|
||
|
2 F 1
|
||
|
3 F 1
|
||
|
4 F 1
|
||
|
5 M 1
|
||
|
NULL M 1
|
||
|
10 NULL 1
|
||
|
10 NULL 1
|
||
|
11 NULL 1
|
||
|
SELECT id, sex, NTH_VALUE(id, 1) IGNORE NULLS OVER () FROM t1;
|
||
|
ERROR 42000: This version of MySQL doesn't yet support 'IGNORE NULLS'
|
||
|
SELECT id, sex, NTH_VALUE(id, 1.2) OVER () FROM t1;
|
||
|
ERROR HY000: Incorrect arguments to nth_value
|
||
|
SELECT id, sex, NTH_VALUE(id, 'a') OVER () FROM t1;
|
||
|
ERROR HY000: Incorrect arguments to nth_value
|
||
|
SELECT id, sex, NTH_VALUE(id, -3) OVER () le1 FROM t1;
|
||
|
ERROR HY000: Incorrect arguments to nth_value
|
||
|
NULL argument is OK
|
||
|
SELECT id, sex, NTH_VALUE(id, NULL) OVER () FROM t1;
|
||
|
id sex NTH_VALUE(id, NULL) OVER ()
|
||
|
1 M NULL
|
||
|
2 F NULL
|
||
|
3 F NULL
|
||
|
4 F NULL
|
||
|
5 M NULL
|
||
|
NULL M NULL
|
||
|
10 NULL NULL
|
||
|
10 NULL NULL
|
||
|
11 NULL NULL
|
||
|
PREPARE p FROM "SELECT id, sex, NTH_VALUE(id, ?) OVER () FROM t1";
|
||
|
SET @p1= 3;
|
||
|
EXECUTE p USING @p1;
|
||
|
id sex NTH_VALUE(id, ?) OVER ()
|
||
|
1 M 3
|
||
|
2 F 3
|
||
|
3 F 3
|
||
|
4 F 3
|
||
|
5 M 3
|
||
|
NULL M 3
|
||
|
10 NULL 3
|
||
|
10 NULL 3
|
||
|
11 NULL 3
|
||
|
SET @p1= NULL;
|
||
|
EXECUTE p USING @p1;
|
||
|
id sex NTH_VALUE(id, ?) OVER ()
|
||
|
1 M NULL
|
||
|
2 F NULL
|
||
|
3 F NULL
|
||
|
4 F NULL
|
||
|
5 M NULL
|
||
|
NULL M NULL
|
||
|
10 NULL NULL
|
||
|
10 NULL NULL
|
||
|
11 NULL NULL
|
||
|
SET @p1= '1';
|
||
|
EXECUTE p USING @p1;
|
||
|
ERROR HY000: Incorrect arguments to nth_value
|
||
|
SET @p1= 1.2;
|
||
|
EXECUTE p USING @p1;
|
||
|
ERROR HY000: Incorrect arguments to nth_value
|
||
|
DROP PREPARE p;
|
||
|
Ok, default semantics:
|
||
|
SELECT id, sex, NTH_VALUE(id, 1) RESPECT NULLS OVER () FROM t1;
|
||
|
id sex NTH_VALUE(id, 1) RESPECT NULLS OVER ()
|
||
|
1 M 1
|
||
|
2 F 1
|
||
|
3 F 1
|
||
|
4 F 1
|
||
|
5 M 1
|
||
|
NULL M 1
|
||
|
10 NULL 1
|
||
|
10 NULL 1
|
||
|
11 NULL 1
|
||
|
SELECT id, sex, NTH_VALUE(NULL, 1) RESPECT NULLS OVER () FROM t1;
|
||
|
id sex NTH_VALUE(NULL, 1) RESPECT NULLS OVER ()
|
||
|
1 M NULL
|
||
|
2 F NULL
|
||
|
3 F NULL
|
||
|
4 F NULL
|
||
|
5 M NULL
|
||
|
NULL M NULL
|
||
|
10 NULL NULL
|
||
|
10 NULL NULL
|
||
|
11 NULL NULL
|
||
|
SELECT id, sex, NTH_VALUE(id+100, 1) RESPECT NULLS OVER () FROM t1;
|
||
|
id sex NTH_VALUE(id+100, 1) RESPECT NULLS OVER ()
|
||
|
1 M 101
|
||
|
2 F 101
|
||
|
3 F 101
|
||
|
4 F 101
|
||
|
5 M 101
|
||
|
NULL M 101
|
||
|
10 NULL 101
|
||
|
10 NULL 101
|
||
|
11 NULL 101
|
||
|
SELECT id, sex, NTH_VALUE(100, 1) RESPECT NULLS OVER () FROM t1;
|
||
|
id sex NTH_VALUE(100, 1) RESPECT NULLS OVER ()
|
||
|
1 M 100
|
||
|
2 F 100
|
||
|
3 F 100
|
||
|
4 F 100
|
||
|
5 M 100
|
||
|
NULL M 100
|
||
|
10 NULL 100
|
||
|
10 NULL 100
|
||
|
11 NULL 100
|
||
|
SELECT id, sex, NTH_VALUE(100, 100) RESPECT NULLS OVER () FROM t1;
|
||
|
id sex NTH_VALUE(100, 100) RESPECT NULLS OVER ()
|
||
|
1 M NULL
|
||
|
2 F NULL
|
||
|
3 F NULL
|
||
|
4 F NULL
|
||
|
5 M NULL
|
||
|
NULL M NULL
|
||
|
10 NULL NULL
|
||
|
10 NULL NULL
|
||
|
11 NULL NULL
|
||
|
static wf
|
||
|
SELECT id, sex, NTH_VALUE(id, 1) OVER () FROM t1;
|
||
|
id sex NTH_VALUE(id, 1) OVER ()
|
||
|
1 M 1
|
||
|
2 F 1
|
||
|
3 F 1
|
||
|
4 F 1
|
||
|
5 M 1
|
||
|
NULL M 1
|
||
|
10 NULL 1
|
||
|
10 NULL 1
|
||
|
11 NULL 1
|
||
|
SELECT id, sex, NTH_VALUE(id, 3) OVER () FROM t1;
|
||
|
id sex NTH_VALUE(id, 3) OVER ()
|
||
|
1 M 3
|
||
|
2 F 3
|
||
|
3 F 3
|
||
|
4 F 3
|
||
|
5 M 3
|
||
|
NULL M 3
|
||
|
10 NULL 3
|
||
|
10 NULL 3
|
||
|
11 NULL 3
|
||
|
SELECT id, sex, NTH_VALUE(id, 2) OVER w FROM t1 WINDOW w as (PARTITION BY sex ORDER BY id ROWS BETWEEN UNBOUNDED PRECEDING AND UNBOUNDED FOLLOWING);
|
||
|
id sex NTH_VALUE(id, 2) OVER w
|
||
|
10 NULL 10
|
||
|
10 NULL 10
|
||
|
11 NULL 10
|
||
|
2 F 3
|
||
|
3 F 3
|
||
|
4 F 3
|
||
|
NULL M 1
|
||
|
1 M 1
|
||
|
5 M 1
|
||
|
SELECT id, sex, NTH_VALUE(id, 2) OVER w FROM t1 WINDOW w as (PARTITION BY id ORDER BY sex ROWS BETWEEN UNBOUNDED PRECEDING AND UNBOUNDED FOLLOWING);
|
||
|
id sex NTH_VALUE(id, 2) OVER w
|
||
|
NULL M NULL
|
||
|
1 M NULL
|
||
|
2 F NULL
|
||
|
3 F NULL
|
||
|
4 F NULL
|
||
|
5 M NULL
|
||
|
10 NULL 10
|
||
|
10 NULL 10
|
||
|
11 NULL NULL
|
||
|
unbuffered
|
||
|
SELECT id, sex, NTH_VALUE(id, 2) OVER w FROM t1 WINDOW w as (PARTITION BY SEX ORDER BY ID ROWS UNBOUNDED PRECEDING);
|
||
|
id sex NTH_VALUE(id, 2) OVER w
|
||
|
10 NULL NULL
|
||
|
10 NULL 10
|
||
|
11 NULL 10
|
||
|
2 F NULL
|
||
|
3 F 3
|
||
|
4 F 3
|
||
|
NULL M NULL
|
||
|
1 M 1
|
||
|
5 M 1
|
||
|
SELECT id, sex, NTH_VALUE(id, 2) OVER w FROM t1 WINDOW w as (PARTITION BY SEX ORDER BY ID RANGE UNBOUNDED PRECEDING);
|
||
|
id sex NTH_VALUE(id, 2) OVER w
|
||
|
10 NULL 10
|
||
|
10 NULL 10
|
||
|
11 NULL 10
|
||
|
2 F NULL
|
||
|
3 F 3
|
||
|
4 F 3
|
||
|
NULL M NULL
|
||
|
1 M 1
|
||
|
5 M 1
|
||
|
test unoptimized path: trick: add DOUBLE type w/SUM which is unoptimized by default
|
||
|
ascending
|
||
|
SELECT d, SUM(d) OVER w, sex, NTH_VALUE(d, 2) OVER w FROM t1 WINDOW w as (PARTITION BY SEX ORDER BY D ROWS 2 PRECEDING);
|
||
|
d SUM(d) OVER w sex NTH_VALUE(d, 2) OVER w
|
||
|
10 10 NULL NULL
|
||
|
10 20 NULL 10
|
||
|
11 31 NULL 10
|
||
|
2 2 F NULL
|
||
|
3 5 F 3
|
||
|
4 9 F 3
|
||
|
NULL NULL M NULL
|
||
|
1 1 M 1
|
||
|
5 6 M 1
|
||
|
SELECT d, SUM(d) OVER w, sex, NTH_VALUE(d, 2) OVER w FROM t1 WINDOW w as (PARTITION BY SEX ORDER BY D RANGE 2 PRECEDING);
|
||
|
d SUM(d) OVER w sex NTH_VALUE(d, 2) OVER w
|
||
|
10 20 NULL 10
|
||
|
10 20 NULL 10
|
||
|
11 31 NULL 10
|
||
|
2 2 F NULL
|
||
|
3 5 F 3
|
||
|
4 9 F 3
|
||
|
NULL NULL M NULL
|
||
|
1 1 M NULL
|
||
|
5 5 M NULL
|
||
|
SELECT d, SUM(d) OVER w, COUNT(*) OVER w, NTH_VALUE(d, 2) OVER w FROM t1 WINDOW w as (ORDER BY D ROWS 2 PRECEDING);
|
||
|
d SUM(d) OVER w COUNT(*) OVER w NTH_VALUE(d, 2) OVER w
|
||
|
NULL NULL 1 NULL
|
||
|
1 1 2 1
|
||
|
2 3 3 1
|
||
|
3 6 3 2
|
||
|
4 9 3 3
|
||
|
5 12 3 4
|
||
|
10 19 3 5
|
||
|
10 25 3 10
|
||
|
11 31 3 10
|
||
|
SELECT d, SUM(d) OVER w, COUNT(*) OVER w, NTH_VALUE(d, 2) OVER w FROM t1 WINDOW w as (ORDER BY D ASC ROWS BETWEEN 2 PRECEDING AND 1 PRECEDING);
|
||
|
d SUM(d) OVER w COUNT(*) OVER w NTH_VALUE(d, 2) OVER w
|
||
|
NULL NULL 0 NULL
|
||
|
1 NULL 1 NULL
|
||
|
2 1 2 1
|
||
|
3 3 2 2
|
||
|
4 5 2 3
|
||
|
5 7 2 4
|
||
|
10 9 2 5
|
||
|
10 15 2 10
|
||
|
11 20 2 10
|
||
|
SELECT d, SUM(d) OVER w, COUNT(*) OVER w, NTH_VALUE(d, 2) OVER w FROM t1 WINDOW w as (ORDER BY D ASC ROWS BETWEEN 1 FOLLOWING AND 2 FOLLOWING);
|
||
|
d SUM(d) OVER w COUNT(*) OVER w NTH_VALUE(d, 2) OVER w
|
||
|
NULL 3 2 2
|
||
|
1 5 2 3
|
||
|
2 7 2 4
|
||
|
3 9 2 5
|
||
|
4 15 2 10
|
||
|
5 20 2 10
|
||
|
10 21 2 11
|
||
|
10 11 1 NULL
|
||
|
11 NULL 0 NULL
|
||
|
SELECT d, SUM(d) OVER w, COUNT(*) OVER w, NTH_VALUE(d, 2) OVER w FROM t1 WINDOW w as (ORDER BY D RANGE 2 PRECEDING);
|
||
|
d SUM(d) OVER w COUNT(*) OVER w NTH_VALUE(d, 2) OVER w
|
||
|
NULL NULL 1 NULL
|
||
|
1 1 1 NULL
|
||
|
2 3 2 2
|
||
|
3 6 3 2
|
||
|
4 9 3 3
|
||
|
5 12 3 4
|
||
|
10 20 2 10
|
||
|
10 20 2 10
|
||
|
11 31 3 10
|
||
|
SELECT d, SUM(d) OVER w, COUNT(*) OVER w, NTH_VALUE(d, 2) OVER w FROM t1 WINDOW w as (ORDER BY D RANGE BETWEEN 1 PRECEDING AND 1 FOLLOWING);
|
||
|
d SUM(d) OVER w COUNT(*) OVER w NTH_VALUE(d, 2) OVER w
|
||
|
NULL NULL 1 NULL
|
||
|
1 3 2 2
|
||
|
2 6 3 2
|
||
|
3 9 3 3
|
||
|
4 12 3 4
|
||
|
5 9 2 5
|
||
|
10 31 3 10
|
||
|
10 31 3 10
|
||
|
11 31 3 10
|
||
|
SELECT d, SUM(d) OVER w, COUNT(*) OVER w, NTH_VALUE(d, 2) OVER w FROM t1 WINDOW w as (ORDER BY D RANGE BETWEEN 1 FOLLOWING AND 2 FOLLOWING);
|
||
|
d SUM(d) OVER w COUNT(*) OVER w NTH_VALUE(d, 2) OVER w
|
||
|
NULL NULL 1 NULL
|
||
|
1 5 2 3
|
||
|
2 7 2 4
|
||
|
3 9 2 5
|
||
|
4 5 1 NULL
|
||
|
5 NULL 0 NULL
|
||
|
10 11 1 NULL
|
||
|
10 11 1 NULL
|
||
|
11 NULL 0 NULL
|
||
|
descending
|
||
|
SELECT d, SUM(d) OVER w, sex, NTH_VALUE(d, 2) OVER w FROM t1 WINDOW w as (PARTITION BY SEX ORDER BY D DESC ROWS 2 PRECEDING);
|
||
|
d SUM(d) OVER w sex NTH_VALUE(d, 2) OVER w
|
||
|
11 11 NULL NULL
|
||
|
10 21 NULL 10
|
||
|
10 31 NULL 10
|
||
|
4 4 F NULL
|
||
|
3 7 F 3
|
||
|
2 9 F 3
|
||
|
5 5 M NULL
|
||
|
1 6 M 1
|
||
|
NULL 6 M 1
|
||
|
SELECT d, SUM(d) OVER w, sex, NTH_VALUE(d, 2) OVER w FROM t1 WINDOW w as (PARTITION BY SEX ORDER BY D DESC RANGE 2 PRECEDING);
|
||
|
d SUM(d) OVER w sex NTH_VALUE(d, 2) OVER w
|
||
|
11 11 NULL NULL
|
||
|
10 31 NULL 10
|
||
|
10 31 NULL 10
|
||
|
4 4 F NULL
|
||
|
3 7 F 3
|
||
|
2 9 F 3
|
||
|
5 5 M NULL
|
||
|
1 1 M NULL
|
||
|
NULL NULL M NULL
|
||
|
SELECT d, SUM(d) OVER w, COUNT(*) OVER w, NTH_VALUE(d, 2) OVER w FROM t1 WINDOW w as (ORDER BY D DESC ROWS 2 PRECEDING);
|
||
|
d SUM(d) OVER w COUNT(*) OVER w NTH_VALUE(d, 2) OVER w
|
||
|
11 11 1 NULL
|
||
|
10 21 2 10
|
||
|
10 31 3 10
|
||
|
5 25 3 10
|
||
|
4 19 3 5
|
||
|
3 12 3 4
|
||
|
2 9 3 3
|
||
|
1 6 3 2
|
||
|
NULL 3 3 1
|
||
|
SELECT d, SUM(d) OVER w, COUNT(*) OVER w, NTH_VALUE(d, 2) OVER w FROM t1 WINDOW w as (ORDER BY D DESC ROWS BETWEEN 2 PRECEDING AND 1 PRECEDING);
|
||
|
d SUM(d) OVER w COUNT(*) OVER w NTH_VALUE(d, 2) OVER w
|
||
|
11 NULL 0 NULL
|
||
|
10 11 1 NULL
|
||
|
10 21 2 10
|
||
|
5 20 2 10
|
||
|
4 15 2 5
|
||
|
3 9 2 4
|
||
|
2 7 2 3
|
||
|
1 5 2 2
|
||
|
NULL 3 2 1
|
||
|
SELECT d, SUM(d) OVER w, COUNT(*) OVER w, NTH_VALUE(d, 2) OVER w FROM t1 WINDOW w as (ORDER BY D DESC ROWS BETWEEN 1 FOLLOWING AND 2 FOLLOWING);
|
||
|
d SUM(d) OVER w COUNT(*) OVER w NTH_VALUE(d, 2) OVER w
|
||
|
11 20 2 10
|
||
|
10 15 2 5
|
||
|
10 9 2 4
|
||
|
5 7 2 3
|
||
|
4 5 2 2
|
||
|
3 3 2 1
|
||
|
2 1 2 NULL
|
||
|
1 NULL 1 NULL
|
||
|
NULL NULL 0 NULL
|
||
|
SELECT d, SUM(d) OVER w, COUNT(*) OVER w, NTH_VALUE(d, 2) OVER w FROM t1 WINDOW w as (ORDER BY D DESC RANGE 2 PRECEDING);
|
||
|
d SUM(d) OVER w COUNT(*) OVER w NTH_VALUE(d, 2) OVER w
|
||
|
11 11 1 NULL
|
||
|
10 31 3 10
|
||
|
10 31 3 10
|
||
|
5 5 1 NULL
|
||
|
4 9 2 4
|
||
|
3 12 3 4
|
||
|
2 9 3 3
|
||
|
1 6 3 2
|
||
|
NULL NULL 1 NULL
|
||
|
SELECT d, SUM(d) OVER w, COUNT(*) OVER w, NTH_VALUE(d, 2) OVER w FROM t1 WINDOW w as (ORDER BY D DESC RANGE BETWEEN 1 PRECEDING AND 1 FOLLOWING);
|
||
|
d SUM(d) OVER w COUNT(*) OVER w NTH_VALUE(d, 2) OVER w
|
||
|
11 31 3 10
|
||
|
10 31 3 10
|
||
|
10 31 3 10
|
||
|
5 9 2 4
|
||
|
4 12 3 4
|
||
|
3 9 3 3
|
||
|
2 6 3 2
|
||
|
1 3 2 1
|
||
|
NULL NULL 1 NULL
|
||
|
SELECT d, SUM(d) OVER w, COUNT(*) OVER w, NTH_VALUE(d, 2) OVER w FROM t1 WINDOW w as (ORDER BY D DESC RANGE BETWEEN 1 FOLLOWING AND 2 FOLLOWING);
|
||
|
d SUM(d) OVER w COUNT(*) OVER w NTH_VALUE(d, 2) OVER w
|
||
|
11 20 2 10
|
||
|
10 NULL 0 NULL
|
||
|
10 NULL 0 NULL
|
||
|
5 7 2 3
|
||
|
4 5 2 2
|
||
|
3 3 2 1
|
||
|
2 1 1 NULL
|
||
|
1 NULL 0 NULL
|
||
|
NULL NULL 1 NULL
|
||
|
Dynamic upper
|
||
|
SELECT id, sex, NTH_VALUE(id, 2) OVER w FROM t1 WINDOW w as (ORDER BY id);
|
||
|
id sex NTH_VALUE(id, 2) OVER w
|
||
|
NULL M NULL
|
||
|
1 M 1
|
||
|
2 F 1
|
||
|
3 F 1
|
||
|
4 F 1
|
||
|
5 M 1
|
||
|
10 NULL 1
|
||
|
10 NULL 1
|
||
|
11 NULL 1
|
||
|
optimized path
|
||
|
ascending
|
||
|
SELECT id, sex, NTH_VALUE(id, 2) OVER w FROM t1 WINDOW w as (PARTITION BY SEX ORDER BY ID ROWS 2 PRECEDING);
|
||
|
id sex NTH_VALUE(id, 2) OVER w
|
||
|
10 NULL NULL
|
||
|
10 NULL 10
|
||
|
11 NULL 10
|
||
|
2 F NULL
|
||
|
3 F 3
|
||
|
4 F 3
|
||
|
NULL M NULL
|
||
|
1 M 1
|
||
|
5 M 1
|
||
|
SELECT id, sex, NTH_VALUE(id, 2) OVER w FROM t1 WINDOW w as (PARTITION BY SEX ORDER BY ID RANGE 2 PRECEDING);
|
||
|
id sex NTH_VALUE(id, 2) OVER w
|
||
|
10 NULL 10
|
||
|
10 NULL 10
|
||
|
11 NULL 10
|
||
|
2 F NULL
|
||
|
3 F 3
|
||
|
4 F 3
|
||
|
NULL M NULL
|
||
|
1 M NULL
|
||
|
5 M NULL
|
||
|
SELECT id, COUNT(*) OVER w, NTH_VALUE(id, 2) OVER w FROM t1 WINDOW w as (ORDER BY ID ROWS 2 PRECEDING);
|
||
|
id COUNT(*) OVER w NTH_VALUE(id, 2) OVER w
|
||
|
NULL 1 NULL
|
||
|
1 2 1
|
||
|
2 3 1
|
||
|
3 3 2
|
||
|
4 3 3
|
||
|
5 3 4
|
||
|
10 3 5
|
||
|
10 3 10
|
||
|
11 3 10
|
||
|
SELECT id, COUNT(*) OVER w, NTH_VALUE(id, 2) OVER w FROM t1 WINDOW w as (ORDER BY ID ASC ROWS BETWEEN 2 PRECEDING AND 1 PRECEDING);
|
||
|
id COUNT(*) OVER w NTH_VALUE(id, 2) OVER w
|
||
|
NULL 0 NULL
|
||
|
1 1 NULL
|
||
|
2 2 1
|
||
|
3 2 2
|
||
|
4 2 3
|
||
|
5 2 4
|
||
|
10 2 5
|
||
|
10 2 10
|
||
|
11 2 10
|
||
|
SELECT id, COUNT(*) OVER w, NTH_VALUE(id, 2) OVER w FROM t1 WINDOW w as (ORDER BY ID ASC ROWS BETWEEN 1 FOLLOWING AND 2 FOLLOWING);
|
||
|
id COUNT(*) OVER w NTH_VALUE(id, 2) OVER w
|
||
|
NULL 2 2
|
||
|
1 2 3
|
||
|
2 2 4
|
||
|
3 2 5
|
||
|
4 2 10
|
||
|
5 2 10
|
||
|
10 2 11
|
||
|
10 1 NULL
|
||
|
11 0 NULL
|
||
|
SELECT id, COUNT(*) OVER w, NTH_VALUE(id, 2) OVER w FROM t1 WINDOW w as (ORDER BY ID RANGE 2 PRECEDING);
|
||
|
id COUNT(*) OVER w NTH_VALUE(id, 2) OVER w
|
||
|
NULL 1 NULL
|
||
|
1 1 NULL
|
||
|
2 2 2
|
||
|
3 3 2
|
||
|
4 3 3
|
||
|
5 3 4
|
||
|
10 2 10
|
||
|
10 2 10
|
||
|
11 3 10
|
||
|
SELECT id, COUNT(*) OVER w, NTH_VALUE(id, 2) OVER w FROM t1 WINDOW w as (ORDER BY ID RANGE BETWEEN 1 PRECEDING AND 1 FOLLOWING);
|
||
|
id COUNT(*) OVER w NTH_VALUE(id, 2) OVER w
|
||
|
NULL 1 NULL
|
||
|
1 2 2
|
||
|
2 3 2
|
||
|
3 3 3
|
||
|
4 3 4
|
||
|
5 2 5
|
||
|
10 3 10
|
||
|
10 3 10
|
||
|
11 3 10
|
||
|
SELECT id, COUNT(*) OVER w, NTH_VALUE(id, 2) OVER w FROM t1 WINDOW w as (ORDER BY ID RANGE BETWEEN 1 FOLLOWING AND 2 FOLLOWING);
|
||
|
id COUNT(*) OVER w NTH_VALUE(id, 2) OVER w
|
||
|
NULL 1 NULL
|
||
|
1 2 3
|
||
|
2 2 4
|
||
|
3 2 5
|
||
|
4 1 NULL
|
||
|
5 0 NULL
|
||
|
10 1 NULL
|
||
|
10 1 NULL
|
||
|
11 0 NULL
|
||
|
descending
|
||
|
SELECT id, sex, NTH_VALUE(id, 2) OVER w FROM t1 WINDOW w as (PARTITION BY SEX ORDER BY ID DESC ROWS 2 PRECEDING);
|
||
|
id sex NTH_VALUE(id, 2) OVER w
|
||
|
11 NULL NULL
|
||
|
10 NULL 10
|
||
|
10 NULL 10
|
||
|
4 F NULL
|
||
|
3 F 3
|
||
|
2 F 3
|
||
|
5 M NULL
|
||
|
1 M 1
|
||
|
NULL M 1
|
||
|
SELECT id, sex, NTH_VALUE(id, 2) OVER w FROM t1 WINDOW w as (PARTITION BY SEX ORDER BY ID DESC RANGE 2 PRECEDING);
|
||
|
id sex NTH_VALUE(id, 2) OVER w
|
||
|
11 NULL NULL
|
||
|
10 NULL 10
|
||
|
10 NULL 10
|
||
|
4 F NULL
|
||
|
3 F 3
|
||
|
2 F 3
|
||
|
5 M NULL
|
||
|
1 M NULL
|
||
|
NULL M NULL
|
||
|
SELECT id, COUNT(*) OVER w, NTH_VALUE(id, 2) OVER w FROM t1 WINDOW w as (ORDER BY ID DESC ROWS 2 PRECEDING);
|
||
|
id COUNT(*) OVER w NTH_VALUE(id, 2) OVER w
|
||
|
11 1 NULL
|
||
|
10 2 10
|
||
|
10 3 10
|
||
|
5 3 10
|
||
|
4 3 5
|
||
|
3 3 4
|
||
|
2 3 3
|
||
|
1 3 2
|
||
|
NULL 3 1
|
||
|
SELECT id, COUNT(*) OVER w, NTH_VALUE(id, 2) OVER w FROM t1 WINDOW w as (ORDER BY ID DESC ROWS BETWEEN 2 PRECEDING AND 1 PRECEDING);
|
||
|
id COUNT(*) OVER w NTH_VALUE(id, 2) OVER w
|
||
|
11 0 NULL
|
||
|
10 1 NULL
|
||
|
10 2 10
|
||
|
5 2 10
|
||
|
4 2 5
|
||
|
3 2 4
|
||
|
2 2 3
|
||
|
1 2 2
|
||
|
NULL 2 1
|
||
|
SELECT id, COUNT(*) OVER w, NTH_VALUE(id, 2) OVER w FROM t1 WINDOW w as (ORDER BY ID DESC ROWS BETWEEN 1 FOLLOWING AND 2 FOLLOWING);
|
||
|
id COUNT(*) OVER w NTH_VALUE(id, 2) OVER w
|
||
|
11 2 10
|
||
|
10 2 5
|
||
|
10 2 4
|
||
|
5 2 3
|
||
|
4 2 2
|
||
|
3 2 1
|
||
|
2 2 NULL
|
||
|
1 1 NULL
|
||
|
NULL 0 NULL
|
||
|
SELECT id, COUNT(*) OVER w, NTH_VALUE(id, 2) OVER w FROM t1 WINDOW w as (ORDER BY ID DESC RANGE 2 PRECEDING);
|
||
|
id COUNT(*) OVER w NTH_VALUE(id, 2) OVER w
|
||
|
11 1 NULL
|
||
|
10 3 10
|
||
|
10 3 10
|
||
|
5 1 NULL
|
||
|
4 2 4
|
||
|
3 3 4
|
||
|
2 3 3
|
||
|
1 3 2
|
||
|
NULL 1 NULL
|
||
|
SELECT id, COUNT(*) OVER w, NTH_VALUE(id, 2) OVER w FROM t1 WINDOW w as (ORDER BY ID DESC RANGE BETWEEN 1 PRECEDING AND 1 FOLLOWING);
|
||
|
id COUNT(*) OVER w NTH_VALUE(id, 2) OVER w
|
||
|
11 3 10
|
||
|
10 3 10
|
||
|
10 3 10
|
||
|
5 2 4
|
||
|
4 3 4
|
||
|
3 3 3
|
||
|
2 3 2
|
||
|
1 2 1
|
||
|
NULL 1 NULL
|
||
|
SELECT id, COUNT(*) OVER w, NTH_VALUE(id, 2) OVER w FROM t1 WINDOW w as (ORDER BY ID DESC RANGE BETWEEN 1 FOLLOWING AND 2 FOLLOWING);
|
||
|
id COUNT(*) OVER w NTH_VALUE(id, 2) OVER w
|
||
|
11 2 10
|
||
|
10 0 NULL
|
||
|
10 0 NULL
|
||
|
5 2 3
|
||
|
4 2 2
|
||
|
3 2 1
|
||
|
2 1 NULL
|
||
|
1 0 NULL
|
||
|
NULL 1 NULL
|
||
|
many nth_value calls on one window, optimized path
|
||
|
SELECT id, COUNT(*) OVER w,
|
||
|
NTH_VALUE(id, 3) OVER w,
|
||
|
FIRST_VALUE(id) OVER w,
|
||
|
NTH_VALUE(id, 1) OVER w,
|
||
|
NTH_VALUE(id, 2) OVER w FROM t1
|
||
|
WINDOW w AS (ORDER BY id ASC ROWS BETWEEN 2 PRECEDING AND CURRENT ROW);
|
||
|
id COUNT(*) OVER w NTH_VALUE(id, 3) OVER w FIRST_VALUE(id) OVER w NTH_VALUE(id, 1) OVER w NTH_VALUE(id, 2) OVER w
|
||
|
NULL 1 NULL NULL NULL NULL
|
||
|
1 2 NULL NULL NULL 1
|
||
|
2 3 2 NULL NULL 1
|
||
|
3 3 3 1 1 2
|
||
|
4 3 4 2 2 3
|
||
|
5 3 5 3 3 4
|
||
|
10 3 10 4 4 5
|
||
|
10 3 10 5 5 10
|
||
|
11 3 11 10 10 10
|
||
|
many nth_value calls on one window, unoptimized path
|
||
|
SELECT id, SUM(d) OVER w, COUNT(*) OVER w,
|
||
|
NTH_VALUE(id, 3) OVER w,
|
||
|
FIRST_VALUE(id) OVER w,
|
||
|
NTH_VALUE(id, 1) OVER w,
|
||
|
NTH_VALUE(id, 2) OVER w FROM t1
|
||
|
WINDOW w AS (ORDER BY id ASC ROWS BETWEEN 2 PRECEDING AND CURRENT ROW);
|
||
|
id SUM(d) OVER w COUNT(*) OVER w NTH_VALUE(id, 3) OVER w FIRST_VALUE(id) OVER w NTH_VALUE(id, 1) OVER w NTH_VALUE(id, 2) OVER w
|
||
|
NULL NULL 1 NULL NULL NULL NULL
|
||
|
1 1 2 NULL NULL NULL 1
|
||
|
2 3 3 2 NULL NULL 1
|
||
|
3 6 3 3 1 1 2
|
||
|
4 9 3 4 2 2 3
|
||
|
5 12 3 5 3 3 4
|
||
|
10 19 3 10 4 4 5
|
||
|
10 25 3 10 5 5 10
|
||
|
11 31 3 11 10 10 10
|
||
|
DROP TABLE t1;
|
||
|
|
||
|
Bug#25835149
|
||
|
|
||
|
SET @savmode=@@SESSION.SQL_MODE;
|
||
|
SET SESSION SQL_MODE='';
|
||
|
CREATE TABLE `test`(
|
||
|
`pk` INT(11) NOT NULL AUTO_INCREMENT PRIMARY KEY,
|
||
|
`dt` DATETIME DEFAULT NULL,
|
||
|
`ge` GEOMETRY DEFAULT NULL
|
||
|
);
|
||
|
Warnings:
|
||
|
Warning 1681 Integer display width is deprecated and will be removed in a future release.
|
||
|
INSERT INTO `test` VALUES
|
||
|
(1, '2007-05-08 12:10:55', ST_GeomFromText('POINT(1 1)')),
|
||
|
(2, NULL, NULL), (3, NULL, NULL),
|
||
|
(4, '2001-01-18 00:00:00', ST_GeomFromText('POINT(4 4)')),
|
||
|
(5, '2009-11-24 00:00:00', ST_GeomFromText('POINT(5 5)')),
|
||
|
(6, '2001-11-22 21:41:15', ST_GeomFromText('POINT(6 6)')),
|
||
|
(7, NULL, NULL),
|
||
|
(8, '0000-00-00 00:00:00', ST_GeomFromText('POINT(8 8)')),
|
||
|
(9, '2003-05-13 18:03:04', ST_GeomFromText('POINT(9 9)')),
|
||
|
(10, '2008-04-15 09:44:20', ST_GeomFromText('POINT(10 10)')),
|
||
|
(11, '2009-07-15 00:00:00', ST_GeomFromText('POINT(11 11)')),
|
||
|
(12, '2007-04-27 13:53:37', ST_GeomFromText('POINT(12 12)')),
|
||
|
(13, '0000-00-00 00:00:00', ST_GeomFromText('POINT(13 13)')),
|
||
|
(14, '2000-02-02 02:15:28', ST_GeomFromText('POINT(14 14)')),
|
||
|
(15, '2004-06-06 00:00:00', ST_GeomFromText('POINT(15 15)')),
|
||
|
(16, NULL, NULL),
|
||
|
(17, '2002-06-21 00:00:00', ST_GeomFromText('POINT(17 17)')),
|
||
|
(18, '2007-03-23 00:00:00', ST_GeomFromText('POINT(18 18)')),
|
||
|
(19, '2006-10-06 00:00:00', ST_GeomFromText('POINT(19 19)')),
|
||
|
(20, '2008-07-07 00:00:00', ST_GeomFromText('POINT(20 20)'));
|
||
|
SELECT dt, NTH_VALUE(dt, 3) OVER w1 nth,
|
||
|
CAST(NTH_VALUE(ge, 3) OVER w1 AS JSON) ge
|
||
|
FROM test WHERE `pk` = 2 WINDOW w1 AS ();
|
||
|
dt nth ge
|
||
|
NULL NULL NULL
|
||
|
SELECT dt, NTH_VALUE(dt, 3) OVER w1 nth,
|
||
|
CAST(NTH_VALUE(ge, 3) OVER w1 AS JSON) ge
|
||
|
FROM test WHERE `pk` > 3 WINDOW w1 AS ();
|
||
|
dt nth ge
|
||
|
2001-01-18 00:00:00 2001-11-22 21:41:15 {"type": "Point", "coordinates": [6.0, 6.0]}
|
||
|
2009-11-24 00:00:00 2001-11-22 21:41:15 {"type": "Point", "coordinates": [6.0, 6.0]}
|
||
|
2001-11-22 21:41:15 2001-11-22 21:41:15 {"type": "Point", "coordinates": [6.0, 6.0]}
|
||
|
NULL 2001-11-22 21:41:15 {"type": "Point", "coordinates": [6.0, 6.0]}
|
||
|
0000-00-00 00:00:00 2001-11-22 21:41:15 {"type": "Point", "coordinates": [6.0, 6.0]}
|
||
|
2003-05-13 18:03:04 2001-11-22 21:41:15 {"type": "Point", "coordinates": [6.0, 6.0]}
|
||
|
2008-04-15 09:44:20 2001-11-22 21:41:15 {"type": "Point", "coordinates": [6.0, 6.0]}
|
||
|
2009-07-15 00:00:00 2001-11-22 21:41:15 {"type": "Point", "coordinates": [6.0, 6.0]}
|
||
|
2007-04-27 13:53:37 2001-11-22 21:41:15 {"type": "Point", "coordinates": [6.0, 6.0]}
|
||
|
0000-00-00 00:00:00 2001-11-22 21:41:15 {"type": "Point", "coordinates": [6.0, 6.0]}
|
||
|
2000-02-02 02:15:28 2001-11-22 21:41:15 {"type": "Point", "coordinates": [6.0, 6.0]}
|
||
|
2004-06-06 00:00:00 2001-11-22 21:41:15 {"type": "Point", "coordinates": [6.0, 6.0]}
|
||
|
NULL 2001-11-22 21:41:15 {"type": "Point", "coordinates": [6.0, 6.0]}
|
||
|
2002-06-21 00:00:00 2001-11-22 21:41:15 {"type": "Point", "coordinates": [6.0, 6.0]}
|
||
|
2007-03-23 00:00:00 2001-11-22 21:41:15 {"type": "Point", "coordinates": [6.0, 6.0]}
|
||
|
2006-10-06 00:00:00 2001-11-22 21:41:15 {"type": "Point", "coordinates": [6.0, 6.0]}
|
||
|
2008-07-07 00:00:00 2001-11-22 21:41:15 {"type": "Point", "coordinates": [6.0, 6.0]}
|
||
|
SELECT dt, NTH_VALUE(CAST(dt AS TIME), 3) OVER w1
|
||
|
FROM test WHERE `pk` > 3 WINDOW w1 AS ();
|
||
|
dt NTH_VALUE(CAST(dt AS TIME), 3) OVER w1
|
||
|
2001-01-18 00:00:00 21:41:15
|
||
|
2009-11-24 00:00:00 21:41:15
|
||
|
2001-11-22 21:41:15 21:41:15
|
||
|
NULL 21:41:15
|
||
|
0000-00-00 00:00:00 21:41:15
|
||
|
2003-05-13 18:03:04 21:41:15
|
||
|
2008-04-15 09:44:20 21:41:15
|
||
|
2009-07-15 00:00:00 21:41:15
|
||
|
2007-04-27 13:53:37 21:41:15
|
||
|
0000-00-00 00:00:00 21:41:15
|
||
|
2000-02-02 02:15:28 21:41:15
|
||
|
2004-06-06 00:00:00 21:41:15
|
||
|
NULL 21:41:15
|
||
|
2002-06-21 00:00:00 21:41:15
|
||
|
2007-03-23 00:00:00 21:41:15
|
||
|
2006-10-06 00:00:00 21:41:15
|
||
|
2008-07-07 00:00:00 21:41:15
|
||
|
SET SESSION SQL_MODE=@savmode;
|
||
|
DROP TABLE `test`;
|
||
|
|
||
|
Bug with missing update of cached example after split_sum_func
|
||
|
for NTH_VALUE
|
||
|
|
||
|
CREATE TABLE t(a INT, b INT, c INT, d INT);
|
||
|
INSERT INTO t VALUES (1,1,1,1), (2,2,4,2), (3,3,9,3);
|
||
|
SELECT SUM(c/d), NTH_VALUE(SUM(c/d),1) OVER (ORDER BY a) FROM t GROUP BY a,b;
|
||
|
SUM(c/d) NTH_VALUE(SUM(c/d),1) OVER (ORDER BY a)
|
||
|
1.0000 1.0000
|
||
|
2.0000 1.0000
|
||
|
3.0000 1.0000
|
||
|
SELECT NTH_VALUE(SUM(c/d), 3) OVER (ORDER BY a) FROM t GROUP BY a,b;
|
||
|
NTH_VALUE(SUM(c/d), 3) OVER (ORDER BY a)
|
||
|
NULL
|
||
|
NULL
|
||
|
3.0000
|
||
|
SELECT 1+NTH_VALUE(SUM(c/d), 1) OVER (ORDER BY a) FROM t GROUP BY a,b;
|
||
|
1+NTH_VALUE(SUM(c/d), 1) OVER (ORDER BY a)
|
||
|
2.0000
|
||
|
2.0000
|
||
|
2.0000
|
||
|
SELECT ROW_NUMBER() OVER () rn,
|
||
|
1+NTH_VALUE(SUM(c/d), 1) OVER (ORDER BY a) plus_fv,
|
||
|
1+NTH_VALUE(SUM(c/d), 2) OVER (ORDER BY a) plus_lv FROM t GROUP BY a,b;
|
||
|
rn plus_fv plus_lv
|
||
|
1 2.0000 NULL
|
||
|
2 2.0000 3.0000
|
||
|
3 2.0000 3.0000
|
||
|
DROP TABLE t;
|
||
|
#
|
||
|
# Bug#26740557: WINDOW FUNC + JSON: ASSERTION FAILED:
|
||
|
# FALSE IN ITEM::VAL_JSON
|
||
|
#
|
||
|
SELECT ((MAKETIME(((QUARTER('| !*c>*{/'))<=>
|
||
|
(NTH_VALUE(JSON_OBJECTAGG('key4',0x067c13d0d0d7d8c8d768aef7)
|
||
|
,1)OVER())),'9236-05-27',0xe2a7d4))^(0x1109));
|
||
|
((MAKETIME(((QUARTER('| !*c>*{/'))<=>
|
||
|
(NTH_VALUE(JSON_OBJECTAGG('key4',0x067c13d0d0d7d8c8d768aef7)
|
||
|
,1)OVER())),'9236-05-27',0xe2a7d4))^(0x1109))
|
||
|
NULL
|
||
|
Warnings:
|
||
|
Warning 1292 Incorrect datetime value: '| !*c>*{/'
|
||
|
Warning 1292 Incorrect datetime value: '| !*c>*{/'
|
||
|
Warning 1292 Truncated incorrect INTEGER value: '9236-05-27'
|
||
|
SELECT ((NTH_VALUE(JSON_MERGE_PATCH(1.755913e+308,'{ }'),246)OVER())<=(1));
|
||
|
((NTH_VALUE(JSON_MERGE_PATCH(1.755913e+308,'{ }'),246)OVER())<=(1))
|
||
|
NULL
|
||
|
SELECT ((NTH_VALUE(JSON_MERGE_PATCH(1.755913e+308,'{ }'),1)OVER())<=(1));
|
||
|
ERROR 22032: Invalid data type for JSON data in argument 1 to function json_merge_patch; a JSON string or JSON type is required.
|
||
|
SELECT ((QUOTE(JSON_KEYS(NTH_VALUE(JSON_KEYS(EXP(-15676),ABS('d0')),
|
||
|
162)OVER())))>=(CONNECTION_ID()));
|
||
|
((QUOTE(JSON_KEYS(NTH_VALUE(JSON_KEYS(EXP(-15676),ABS('d0')),
|
||
|
162)OVER())))>=(CONNECTION_ID()))
|
||
|
0
|
||
|
SELECT ((QUOTE(JSON_KEYS(NTH_VALUE(JSON_KEYS(EXP(-15676),ABS('d0')),
|
||
|
1)OVER())))>=(CONNECTION_ID()));
|
||
|
ERROR 22032: Invalid data type for JSON data in argument 1 to function json_keys; a JSON string or JSON type is required.
|
||
|
SELECT JSON_LENGTH(NTH_VALUE(JSON_OBJECTAGG('key2','*B'),172)OVER());
|
||
|
JSON_LENGTH(NTH_VALUE(JSON_OBJECTAGG('key2','*B'),172)OVER())
|
||
|
NULL
|
||
|
SELECT JSON_LENGTH(NTH_VALUE(JSON_OBJECTAGG('key2','*B'),1)OVER());
|
||
|
JSON_LENGTH(NTH_VALUE(JSON_OBJECTAGG('key2','*B'),1)OVER())
|
||
|
1
|
||
|
# End of test for Bug#26740557
|