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.
227 lines
11 KiB
227 lines
11 KiB
--echo # Test of SQL window functions NTH_VALUE
|
|
--echo # ----------------------------------------------------------------------
|
|
|
|
SET NAMES utf8;
|
|
|
|
SELECT NTH_VALUE(6,1) OVER ();
|
|
SELECT NTH_VALUE(NULL,1) OVER ();
|
|
SELECT NTH_VALUE(6,2) OVER ();
|
|
SELECT NTH_VALUE(6,1) OVER (ROWS BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW);
|
|
SELECT NTH_VALUE(NULL,1) OVER (ROWS BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW);
|
|
SELECT NTH_VALUE(6,2) OVER (ROWS BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW);
|
|
|
|
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);
|
|
--echo not supported
|
|
--error ER_NOT_SUPPORTED_YET
|
|
SELECT id, sex, NTH_VALUE(id, 1) FROM LAST OVER () FROM t1;
|
|
--echo ok
|
|
SELECT id, sex, NTH_VALUE(id, 1) FROM FIRST OVER () FROM t1;
|
|
--error ER_NOT_SUPPORTED_YET
|
|
SELECT id, sex, NTH_VALUE(id, 1) IGNORE NULLS OVER () FROM t1;
|
|
--error ER_WRONG_ARGUMENTS
|
|
SELECT id, sex, NTH_VALUE(id, 1.2) OVER () FROM t1;
|
|
--error ER_WRONG_ARGUMENTS
|
|
SELECT id, sex, NTH_VALUE(id, 'a') OVER () FROM t1;
|
|
--error ER_WRONG_ARGUMENTS
|
|
SELECT id, sex, NTH_VALUE(id, -3) OVER () le1 FROM t1;
|
|
--echo NULL argument is OK
|
|
SELECT id, sex, NTH_VALUE(id, NULL) OVER () FROM t1;
|
|
|
|
PREPARE p FROM "SELECT id, sex, NTH_VALUE(id, ?) OVER () FROM t1";
|
|
|
|
SET @p1= 3;
|
|
EXECUTE p USING @p1;
|
|
|
|
SET @p1= NULL;
|
|
EXECUTE p USING @p1;
|
|
|
|
SET @p1= '1';
|
|
--error ER_WRONG_ARGUMENTS
|
|
EXECUTE p USING @p1;
|
|
|
|
SET @p1= 1.2;
|
|
--error ER_WRONG_ARGUMENTS
|
|
EXECUTE p USING @p1;
|
|
|
|
DROP PREPARE p;
|
|
|
|
--echo Ok, default semantics:
|
|
SELECT id, sex, NTH_VALUE(id, 1) RESPECT NULLS OVER () FROM t1;
|
|
SELECT id, sex, NTH_VALUE(NULL, 1) RESPECT NULLS OVER () FROM t1;
|
|
SELECT id, sex, NTH_VALUE(id+100, 1) RESPECT NULLS OVER () FROM t1;
|
|
SELECT id, sex, NTH_VALUE(100, 1) RESPECT NULLS OVER () FROM t1;
|
|
SELECT id, sex, NTH_VALUE(100, 100) RESPECT NULLS OVER () FROM t1;
|
|
|
|
--echo static wf
|
|
SELECT id, sex, NTH_VALUE(id, 1) OVER () FROM t1;
|
|
SELECT id, sex, NTH_VALUE(id, 3) OVER () FROM t1;
|
|
|
|
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);
|
|
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);
|
|
|
|
--echo unbuffered
|
|
SELECT id, sex, NTH_VALUE(id, 2) OVER w FROM t1 WINDOW w as (PARTITION BY SEX ORDER BY ID ROWS UNBOUNDED PRECEDING);
|
|
SELECT id, sex, NTH_VALUE(id, 2) OVER w FROM t1 WINDOW w as (PARTITION BY SEX ORDER BY ID RANGE UNBOUNDED PRECEDING);
|
|
|
|
--echo test unoptimized path: trick: add DOUBLE type w/SUM which is unoptimized by default
|
|
--echo 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);
|
|
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);
|
|
|
|
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);
|
|
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);
|
|
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);
|
|
|
|
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);
|
|
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);
|
|
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);
|
|
|
|
--echo 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);
|
|
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);
|
|
|
|
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);
|
|
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);
|
|
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);
|
|
|
|
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);
|
|
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);
|
|
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);
|
|
|
|
--echo Dynamic upper
|
|
#SELECT id, sex, NTH_VALUE(d, 2) OVER w FROM t1 WINDOW w as (ORDER BY id);
|
|
SELECT id, sex, NTH_VALUE(id, 2) OVER w FROM t1 WINDOW w as (ORDER BY id);
|
|
|
|
|
|
--echo optimized path
|
|
--echo ascending
|
|
SELECT id, sex, NTH_VALUE(id, 2) OVER w FROM t1 WINDOW w as (PARTITION BY SEX ORDER BY ID ROWS 2 PRECEDING);
|
|
SELECT id, sex, NTH_VALUE(id, 2) OVER w FROM t1 WINDOW w as (PARTITION BY SEX ORDER BY ID RANGE 2 PRECEDING);
|
|
|
|
SELECT id, COUNT(*) OVER w, NTH_VALUE(id, 2) OVER w FROM t1 WINDOW w as (ORDER BY ID ROWS 2 PRECEDING);
|
|
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);
|
|
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);
|
|
|
|
SELECT id, COUNT(*) OVER w, NTH_VALUE(id, 2) OVER w FROM t1 WINDOW w as (ORDER BY ID RANGE 2 PRECEDING);
|
|
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);
|
|
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);
|
|
|
|
--echo 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);
|
|
SELECT id, sex, NTH_VALUE(id, 2) OVER w FROM t1 WINDOW w as (PARTITION BY SEX ORDER BY ID DESC RANGE 2 PRECEDING);
|
|
|
|
SELECT id, COUNT(*) OVER w, NTH_VALUE(id, 2) OVER w FROM t1 WINDOW w as (ORDER BY ID DESC ROWS 2 PRECEDING);
|
|
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);
|
|
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);
|
|
|
|
SELECT id, COUNT(*) OVER w, NTH_VALUE(id, 2) OVER w FROM t1 WINDOW w as (ORDER BY ID DESC RANGE 2 PRECEDING);
|
|
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);
|
|
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);
|
|
|
|
--echo 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);
|
|
|
|
--echo 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);
|
|
|
|
DROP TABLE t1;
|
|
|
|
--echo
|
|
--echo Bug#25835149
|
|
--echo
|
|
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
|
|
);
|
|
|
|
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 ();
|
|
|
|
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 ();
|
|
|
|
SELECT dt, NTH_VALUE(CAST(dt AS TIME), 3) OVER w1
|
|
FROM test WHERE `pk` > 3 WINDOW w1 AS ();
|
|
|
|
SET SESSION SQL_MODE=@savmode;
|
|
DROP TABLE `test`;
|
|
|
|
--echo
|
|
--echo Bug with missing update of cached example after split_sum_func
|
|
--echo for NTH_VALUE
|
|
--echo
|
|
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;
|
|
SELECT NTH_VALUE(SUM(c/d), 3) OVER (ORDER BY a) FROM t GROUP BY a,b;
|
|
SELECT 1+NTH_VALUE(SUM(c/d), 1) OVER (ORDER BY a) FROM t GROUP BY a,b;
|
|
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;
|
|
DROP TABLE t;
|
|
|
|
--echo #
|
|
--echo # Bug#26740557: WINDOW FUNC + JSON: ASSERTION FAILED:
|
|
--echo # FALSE IN ITEM::VAL_JSON
|
|
--echo #
|
|
|
|
SELECT ((MAKETIME(((QUARTER('| !*c>*{/'))<=>
|
|
(NTH_VALUE(JSON_OBJECTAGG('key4',0x067c13d0d0d7d8c8d768aef7)
|
|
,1)OVER())),'9236-05-27',0xe2a7d4))^(0x1109));
|
|
SELECT ((NTH_VALUE(JSON_MERGE_PATCH(1.755913e+308,'{ }'),246)OVER())<=(1));
|
|
--error ER_INVALID_TYPE_FOR_JSON
|
|
SELECT ((NTH_VALUE(JSON_MERGE_PATCH(1.755913e+308,'{ }'),1)OVER())<=(1));
|
|
SELECT ((QUOTE(JSON_KEYS(NTH_VALUE(JSON_KEYS(EXP(-15676),ABS('d0')),
|
|
162)OVER())))>=(CONNECTION_ID()));
|
|
--error ER_INVALID_TYPE_FOR_JSON
|
|
SELECT ((QUOTE(JSON_KEYS(NTH_VALUE(JSON_KEYS(EXP(-15676),ABS('d0')),
|
|
1)OVER())))>=(CONNECTION_ID()));
|
|
SELECT JSON_LENGTH(NTH_VALUE(JSON_OBJECTAGG('key2','*B'),172)OVER());
|
|
SELECT JSON_LENGTH(NTH_VALUE(JSON_OBJECTAGG('key2','*B'),1)OVER());
|
|
|
|
--echo # End of test for Bug#26740557
|
|
|