ClickHouse/tests/queries/0_stateless/01591_window_functions.sql

363 lines
14 KiB
MySQL
Raw Normal View History

-- { echo }
2020-12-18 01:25:21 +00:00
set allow_experimental_window_functions = 1;
-- just something basic
select number, count() over (partition by intDiv(number, 3) order by number rows unbounded preceding) from numbers(10);
-- proper calculation across blocks
select number, max(number) over (partition by intDiv(number, 3) order by number desc rows unbounded preceding) from numbers(10) settings max_block_size = 2;
-- not a window function
select number, abs(number) over (partition by toString(intDiv(number, 3)) rows unbounded preceding) from numbers(10); -- { serverError 63 }
-- no partition by
select number, avg(number) over (order by number rows unbounded preceding) from numbers(10);
-- no order by
select number, quantileExact(number) over (partition by intDiv(number, 3) rows unbounded preceding) from numbers(10);
-- can add an alias after window spec
select number, quantileExact(number) over (partition by intDiv(number, 3) rows unbounded preceding) q from numbers(10);
-- can't reference it yet -- the window functions are calculated at the
-- last stage of select, after all other functions.
select q * 10, quantileExact(number) over (partition by intDiv(number, 3) rows unbounded preceding) q from numbers(10); -- { serverError 47 }
-- must work in WHERE if you wrap it in a subquery
select * from (select count(*) over (rows unbounded preceding) c from numbers(3)) where c > 0;
2020-12-25 03:13:30 +00:00
-- should work in ORDER BY
select number, max(number) over (partition by intDiv(number, 3) order by number desc rows unbounded preceding) m from numbers(10) order by m desc, number;
2020-12-24 04:03:33 +00:00
2020-12-25 03:13:30 +00:00
-- also works in ORDER BY if you wrap it in a subquery
select * from (select count(*) over (rows unbounded preceding) c from numbers(3)) order by c;
2020-12-24 04:03:33 +00:00
-- Example with window function only in ORDER BY. Here we make a rank of all
-- numbers sorted descending, and then sort by this rank descending, and must get
-- the ascending order.
select * from (select * from numbers(5) order by rand()) order by count() over (order by number desc rows unbounded preceding) desc;
-- Aggregate functions as window function arguments. This query is semantically
-- the same as the above one, only we replace `number` with
-- `any(number) group by number` and so on.
select * from (select * from numbers(5) order by rand()) group by number order by sum(any(number + 1)) over (order by min(number) desc rows unbounded preceding) desc;
2021-01-13 19:29:52 +00:00
-- some more simple cases w/aggregate functions
select sum(any(number)) over (rows unbounded preceding) from numbers(1);
select sum(any(number) + 1) over (rows unbounded preceding) from numbers(1);
select sum(any(number + 1)) over (rows unbounded preceding) from numbers(1);
2020-12-22 01:37:45 +00:00
-- different windows
-- an explain test would also be helpful, but it's too immature now and I don't
-- want to change reference all the time
select number, max(number) over (partition by intDiv(number, 3) order by number desc rows unbounded preceding), count(number) over (partition by intDiv(number, 5) order by number rows unbounded preceding) as m from numbers(31) order by number settings max_block_size = 2;
2020-12-22 01:37:45 +00:00
-- two functions over the same window
-- an explain test would also be helpful, but it's too immature now and I don't
-- want to change reference all the time
select number, max(number) over (partition by intDiv(number, 3) order by number desc rows unbounded preceding), count(number) over (partition by intDiv(number, 3) order by number desc rows unbounded preceding) as m from numbers(7) order by number settings max_block_size = 2;
2020-12-24 04:03:33 +00:00
-- check that we can work with constant columns
select median(x) over (partition by x) from (select 1 x);
-- an empty window definition is valid as well
select groupArray(number) over (rows unbounded preceding) from numbers(3);
2020-12-24 04:03:33 +00:00
select groupArray(number) over () from numbers(3);
-- This one tests we properly process the window function arguments.
-- Seen errors like 'column `1` not found' from count(1).
select count(1) over (rows unbounded preceding), max(number + 1) over () from numbers(3);
2020-12-25 03:13:30 +00:00
-- Should work in DISTINCT
select distinct sum(0) over (rows unbounded preceding) from numbers(2);
select distinct any(number) over (rows unbounded preceding) from numbers(2);
2020-12-28 09:56:38 +00:00
-- Various kinds of aliases are properly substituted into various parts of window
-- function definition.
with number + 1 as x select intDiv(number, 3) as y, sum(x + y) over (partition by y order by x rows unbounded preceding) from numbers(7);
2021-01-13 19:29:52 +00:00
-- WINDOW clause
select 1 window w1 as ();
select sum(number) over w1, sum(number) over w2
from numbers(10)
window
w1 as (rows unbounded preceding),
w2 as (partition by intDiv(number, 3) rows unbounded preceding)
2021-01-13 19:29:52 +00:00
;
-- FIXME both functions should use the same window, but they don't. Add an
-- EXPLAIN test for this.
2021-01-13 19:29:52 +00:00
select
sum(number) over w1,
sum(number) over (partition by intDiv(number, 3) rows unbounded preceding)
2021-01-13 19:29:52 +00:00
from numbers(10)
window
w1 as (partition by intDiv(number, 3) rows unbounded preceding)
2021-01-13 19:29:52 +00:00
;
2021-01-19 01:00:39 +00:00
2021-01-27 00:08:15 +00:00
-- RANGE frame
-- It's the default
select sum(number) over () from numbers(3);
2021-01-27 00:08:15 +00:00
-- Try some mutually prime sizes of partition, group and block, for the number
-- of rows that is their least common multiple + 1, so that we see all the
-- interesting corner cases.
2021-01-27 00:08:15 +00:00
select number, intDiv(number, 3) p, mod(number, 2) o, count(number) over w as c
from numbers(31)
2021-01-27 00:08:15 +00:00
window w as (partition by p order by o range unbounded preceding)
order by number
settings max_block_size = 5
;
select number, intDiv(number, 5) p, mod(number, 3) o, count(number) over w as c
from numbers(31)
2021-01-27 00:08:15 +00:00
window w as (partition by p order by o range unbounded preceding)
order by number
settings max_block_size = 2
;
select number, intDiv(number, 5) p, mod(number, 2) o, count(number) over w as c
from numbers(31)
2021-01-27 00:08:15 +00:00
window w as (partition by p order by o range unbounded preceding)
order by number
settings max_block_size = 3
;
select number, intDiv(number, 3) p, mod(number, 5) o, count(number) over w as c
from numbers(31)
2021-01-27 00:08:15 +00:00
window w as (partition by p order by o range unbounded preceding)
order by number
settings max_block_size = 2
;
select number, intDiv(number, 2) p, mod(number, 5) o, count(number) over w as c
from numbers(31)
2021-01-27 00:08:15 +00:00
window w as (partition by p order by o range unbounded preceding)
order by number
settings max_block_size = 3
;
select number, intDiv(number, 2) p, mod(number, 3) o, count(number) over w as c
from numbers(31)
2021-01-27 00:08:15 +00:00
window w as (partition by p order by o range unbounded preceding)
order by number
settings max_block_size = 5
;
2021-01-28 18:18:16 +00:00
-- A case where the partition end is in the current block, and the frame end
-- is triggered by the partition end.
select min(number) over (partition by p) from (select number, intDiv(number, 3) p from numbers(10));
2021-01-30 01:16:44 +00:00
-- UNBOUNDED FOLLOWING frame end
select
min(number) over wa, min(number) over wo,
max(number) over wa, max(number) over wo
from
(select number, intDiv(number, 3) p, mod(number, 5) o
from numbers(31))
window
wa as (partition by p order by o
range between unbounded preceding and unbounded following),
wo as (partition by p order by o
rows between unbounded preceding and unbounded following)
settings max_block_size = 2;
2021-02-01 23:26:14 +00:00
-- ROWS offset frame start
select number, p,
count(*) over (partition by p order by number
rows between 1 preceding and unbounded following),
2021-02-01 23:11:40 +00:00
count(*) over (partition by p order by number
rows between current row and unbounded following),
2021-02-01 23:26:14 +00:00
count(*) over (partition by p order by number
rows between 1 following and unbounded following)
from (select number, intDiv(number, 5) p from numbers(31))
order by p, number
settings max_block_size = 2;
2021-02-04 07:41:09 +00:00
-- ROWS offset frame start and end
select number, p,
count(*) over (partition by p order by number
rows between 2 preceding and 2 following)
from (select number, intDiv(number, 7) p from numbers(71))
order by p, number
settings max_block_size = 2;
2021-02-03 12:50:25 +00:00
SELECT count(*) OVER (ROWS BETWEEN 2 PRECEDING AND CURRENT ROW) FROM numbers(4);
2021-02-04 07:41:09 +00:00
-- frame boundaries that runs into the partition end
select
count() over (partition by intDiv(number, 3)
rows between 100 following and unbounded following),
count() over (partition by intDiv(number, 3)
rows between current row and 100 following)
from numbers(10);
-- seen a use-after-free under MSan in this query once
SELECT number, max(number) OVER (PARTITION BY intDiv(number, 7) ORDER BY number ASC NULLS LAST ROWS BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW) FROM numbers(1024) SETTINGS max_block_size = 2 FORMAT Null;
2021-02-03 13:41:59 +00:00
-- a corner case
select count() over ();
-- RANGE CURRENT ROW frame start
select number, p, o,
count(*) over (partition by p order by o
range between current row and unbounded following)
from (select number, intDiv(number, 5) p, mod(number, 3) o
from numbers(31))
order by p, o, number
settings max_block_size = 2;
2021-02-03 14:55:40 +00:00
select
count(*) over (rows between current row and current row),
count(*) over (range between current row and current row)
from numbers(3);
2021-02-05 15:34:03 +00:00
-- RANGE OFFSET
-- a basic RANGE OFFSET frame
select x, min(x) over w, max(x) over w, count(x) over w from (
select toUInt8(number) x from numbers(11))
window w as (order by x asc range between 1 preceding and 2 following)
order by x;
-- overflow conditions
select x, min(x) over w, max(x) over w, count(x) over w
from (
select toUInt8(if(mod(number, 2),
toInt64(255 - intDiv(number, 2)),
toInt64(intDiv(number, 2)))) x
from numbers(10)
)
window w as (order by x range between 1 preceding and 2 following)
order by x;
select x, min(x) over w, max(x) over w, count(x) over w
from (
select toInt8(multiIf(
mod(number, 3) == 0, toInt64(intDiv(number, 3)),
mod(number, 3) == 1, toInt64(127 - intDiv(number, 3)),
toInt64(-128 + intDiv(number, 3)))) x
from numbers(15)
)
window w as (order by x range between 1 preceding and 2 following)
order by x;
2021-02-09 11:56:11 +00:00
-- RANGE OFFSET ORDER BY DESC
select x, min(x) over w, max(x) over w, count(x) over w from (
select toUInt8(number) x from numbers(11)) t
window w as (order by x desc range between 1 preceding and 2 following)
order by x
settings max_block_size = 1;
select x, min(x) over w, max(x) over w, count(x) over w from (
select toUInt8(number) x from numbers(11)) t
window w as (order by x desc range between 1 preceding and unbounded following)
order by x
settings max_block_size = 2;
select x, min(x) over w, max(x) over w, count(x) over w from (
select toUInt8(number) x from numbers(11)) t
window w as (order by x desc range between unbounded preceding and 2 following)
order by x
settings max_block_size = 3;
2021-02-09 14:44:04 +00:00
select x, min(x) over w, max(x) over w, count(x) over w from (
select toUInt8(number) x from numbers(11)) t
window w as (order by x desc range between unbounded preceding and 2 preceding)
order by x
settings max_block_size = 4;
2021-02-10 13:27:22 +00:00
-- Check that we put windows in such an order that we can reuse the sort.
-- First, check that at least the result is correct when we have many windows
-- with different sort order.
select
number,
count(*) over (partition by p order by number),
count(*) over (partition by p order by number, o),
count(*) over (),
count(*) over (order by number),
count(*) over (order by o),
count(*) over (order by o, number),
count(*) over (order by number, o),
count(*) over (partition by p order by o, number),
count(*) over (partition by p),
count(*) over (partition by p order by o),
count(*) over (partition by p, o order by number)
from
(select number, intDiv(number, 3) p, mod(number, 5) o
from numbers(16)) t
order by number
;
-- The EXPLAIN for the above query would be difficult to understand, so check some
-- simple cases instead.
explain select
count(*) over (partition by p),
count(*) over (),
count(*) over (partition by p order by o)
from
(select number, intDiv(number, 3) p, mod(number, 5) o
from numbers(16)) t
;
explain select
count(*) over (order by o, number),
count(*) over (order by number)
from
(select number, intDiv(number, 3) p, mod(number, 5) o
from numbers(16)) t
;
2021-02-11 08:39:39 +00:00
-- A test case for the sort comparator found by fuzzer.
SELECT
max(number) OVER (ORDER BY number DESC NULLS FIRST),
max(number) OVER (ORDER BY number ASC NULLS FIRST)
FROM numbers(2)
;
2021-02-11 13:29:30 +00:00
-- optimize_read_in_order conflicts with sorting for window functions, must
-- be disabled.
create table window_mt engine MergeTree order by number
as select number, mod(number, 3) p from numbers(100);
select number, count(*) over (partition by p)
from window_mt order by number limit 10 settings optimize_read_in_order = 0;
select number, count(*) over (partition by p)
from window_mt order by number limit 10 settings optimize_read_in_order = 1;
2021-02-11 13:29:30 +00:00
-- some true window functions -- rank and friends
select number, p, o,
count(*) over w,
rank() over w,
dense_rank() over w,
row_number() over w
from (select number, intDiv(number, 5) p, mod(number, 3) o
from numbers(31) order by o, number) t
window w as (partition by p order by o)
order by p, o, number
settings max_block_size = 2;
2021-02-11 15:07:42 +00:00
2021-02-12 10:37:27 +00:00
-- our replacement for lag/lead
2021-02-11 15:07:42 +00:00
select
2021-02-12 10:37:27 +00:00
anyOrNull(number)
over (order by number rows between 1 preceding and 1 preceding),
anyOrNull(number)
over (order by number rows between 1 following and 1 following)
from numbers(5);
2021-02-11 15:41:54 +00:00
2021-02-12 10:37:27 +00:00
-- case-insensitive SQL-standard synonyms for any and anyLast
2021-02-11 15:41:54 +00:00
select
number,
fIrSt_VaLue(number) over w,
lAsT_vAlUe(number) over w
from numbers(10)
window w as (order by number range between 1 preceding and 1 following)
order by number
;
-- In this case, we had a problem with PartialSortingTransform returning zero-row
-- chunks for input chunks w/o columns.
select count() over () from numbers(4) where number < 2;