Merge pull request #21094 from den-crane/tests/window_functions

test for window functions
This commit is contained in:
Alexander Kuzmenkov 2021-03-01 19:00:45 +03:00 committed by GitHub
commit 977b98ad69
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 228 additions and 0 deletions

View File

@ -0,0 +1,4 @@
---- arrays ----
6360452672161319041
---- window f ----
6360452672161319041

View File

@ -0,0 +1,36 @@
drop table if exists stack;
set allow_experimental_window_functions = 1;
set max_insert_threads = 4;
create table stack(item_id Int64, brand_id Int64, rack_id Int64, dt DateTime, expiration_dt DateTime, quantity UInt64)
Engine = MergeTree
partition by toYYYYMM(dt)
order by (brand_id, toStartOfHour(dt));
insert into stack
select number%99991, number%11, number%1111, toDateTime('2020-01-01 00:00:00')+number/100,
toDateTime('2020-02-01 00:00:00')+number/10, intDiv(number,100)+1
from numbers_mt(10000000);
select '---- arrays ----';
select cityHash64( toString( groupArray (tuple(*) ) )) from (
select brand_id, rack_id, arrayJoin(arraySlice(arraySort(groupArray(quantity)),1,2)) quantity
from stack
group by brand_id, rack_id
order by brand_id, rack_id, quantity
) t;
select '---- window f ----';
select cityHash64( toString( groupArray (tuple(*) ) )) from (
select brand_id, rack_id, quantity from
( select brand_id, rack_id, quantity, row_number() over (partition by brand_id, rack_id order by quantity) rn
from stack ) as t0
where rn <= 2
order by brand_id, rack_id, quantity
) t;
drop table if exists stack;

View File

@ -0,0 +1,81 @@
---- Q1 ----
Dell Vostro 800.00 Laptop 850
HP Elite 1200.00 Laptop 850
Lenovo Thinkpad 700.00 Laptop 850
Sony VAIO 700.00 Laptop 850
HTC One 400.00 Smartphone 500
Microsoft Lumia 200.00 Smartphone 500
Nexus 500.00 Smartphone 500
iPhone 900.00 Smartphone 500
Kindle Fire 150.00 Tablet 350
Samsung Galaxy Tab 200.00 Tablet 350
iPad 700.00 Tablet 350
---- Q2 ----
Lenovo Thinkpad Laptop 700.00 1
Sony VAIO Laptop 700.00 1
Dell Vostro Laptop 800.00 3
HP Elite Laptop 1200.00 4
Microsoft Lumia Smartphone 200.00 1
HTC One Smartphone 400.00 2
Nexus Smartphone 500.00 3
iPhone Smartphone 900.00 4
Kindle Fire Tablet 150.00 1
Samsung Galaxy Tab Tablet 200.00 2
iPad Tablet 700.00 3
---- Q3 ----
HP Elite Laptop 1200.00 1
Dell Vostro Laptop 800.00 2
Lenovo Thinkpad Laptop 700.00 3
Sony VAIO Laptop 700.00 4
iPhone Smartphone 900.00 1
Nexus Smartphone 500.00 2
HTC One Smartphone 400.00 3
Microsoft Lumia Smartphone 200.00 4
iPad Tablet 700.00 1
Samsung Galaxy Tab Tablet 200.00 2
Kindle Fire Tablet 150.00 3
---- Q4 ----
Lenovo Thinkpad Laptop 700.00 700.00 1
Sony VAIO Laptop 700.00 700.00 1
Dell Vostro Laptop 800.00 700.00 2
HP Elite Laptop 1200.00 700.00 3
Microsoft Lumia Smartphone 200.00 200.00 1
HTC One Smartphone 400.00 200.00 2
Nexus Smartphone 500.00 200.00 3
iPhone Smartphone 900.00 200.00 4
---- Q5 ----
Sony VAIO Laptop 700.00 700.00
Lenovo Thinkpad Laptop 700.00 700.00
HP Elite Laptop 1200.00 700.00
Dell Vostro Laptop 800.00 700.00
iPhone Smartphone 900.00 900.00
Nexus Smartphone 500.00 900.00
Microsoft Lumia Smartphone 200.00 900.00
HTC One Smartphone 400.00 900.00
iPad Tablet 700.00 700.00
Samsung Galaxy Tab Tablet 200.00 700.00
Kindle Fire Tablet 150.00 700.00
---- Q6 ----
Dell Vostro Laptop 800.00 1200.00
HP Elite Laptop 1200.00 1200.00
Lenovo Thinkpad Laptop 700.00 1200.00
Sony VAIO Laptop 700.00 1200.00
HTC One Smartphone 400.00 900.00
Microsoft Lumia Smartphone 200.00 900.00
Nexus Smartphone 500.00 900.00
iPhone Smartphone 900.00 900.00
Kindle Fire Tablet 150.00 700.00
Samsung Galaxy Tab Tablet 200.00 700.00
iPad Tablet 700.00 700.00
---- Q7 ----
Dell Vostro 800.00 Laptop 733 850
HP Elite 1200.00 Laptop 850 850
Lenovo Thinkpad 700.00 Laptop 700 850
Sony VAIO 700.00 Laptop 700 850
HTC One 400.00 Smartphone 300 500
Microsoft Lumia 200.00 Smartphone 200 500
Nexus 500.00 Smartphone 367 500
iPhone 900.00 Smartphone 500 500
Kindle Fire 150.00 Tablet 150 350
Samsung Galaxy Tab 200.00 Tablet 175 350
iPad 700.00 Tablet 350 350

View File

@ -0,0 +1,107 @@
set allow_experimental_window_functions = 1;
drop table if exists product_groups;
drop table if exists products;
CREATE TABLE product_groups (
group_id Int64,
group_name String
) Engine = Memory;
CREATE TABLE products (
product_id Int64,
product_name String,
price DECIMAL(11, 2),
group_id Int64
) Engine = Memory;
INSERT INTO product_groups VALUES (1, 'Smartphone'),(2, 'Laptop'),(3, 'Tablet');
INSERT INTO products (product_id,product_name, group_id,price) VALUES (1, 'Microsoft Lumia', 1, 200), (2, 'HTC One', 1, 400), (3, 'Nexus', 1, 500), (4, 'iPhone', 1, 900),(5, 'HP Elite', 2, 1200),(6, 'Lenovo Thinkpad', 2, 700),(7, 'Sony VAIO', 2, 700),(8, 'Dell Vostro', 2, 800),(9, 'iPad', 3, 700),(10, 'Kindle Fire', 3, 150),(11, 'Samsung Galaxy Tab', 3, 200);
select '---- Q1 ----';
SELECT
product_name,
price,
group_name,
AVG(price) OVER (PARTITION BY group_name)
FROM products INNER JOIN product_groups USING (group_id)
order by group_name, product_name, price;
select '---- Q2 ----';
SELECT
product_name,
group_name,
price,
rank() OVER (PARTITION BY group_name ORDER BY price) rank
FROM products INNER JOIN product_groups USING (group_id)
order by group_name, rank, price;
select '---- Q3 ----';
SELECT
product_name,
group_name,
price,
row_number() OVER (PARTITION BY group_name ORDER BY price desc) rn
FROM products INNER JOIN product_groups USING (group_id)
ORDER BY group_name, rn;
select '---- Q4 ----';
SELECT *
FROM
(
SELECT
product_name,
group_name,
price,
min(price) OVER (PARTITION BY group_name) AS min_price,
dense_rank() OVER (PARTITION BY group_name ORDER BY price ASC) AS r
FROM products
INNER JOIN product_groups USING (group_id)
) AS t
WHERE min_price > 160
ORDER BY
group_name ASC,
r ASC,
product_name ASC;
select '---- Q5 ----';
SELECT
product_name,
group_name,
price,
FIRST_VALUE (price) OVER (PARTITION BY group_name ORDER BY product_name desc) AS price_per_group_per_alphab
FROM products INNER JOIN product_groups USING (group_id)
order by group_name, product_name desc;
select '---- Q6 ----';
SELECT
product_name,
group_name,
price,
LAST_VALUE (price) OVER (PARTITION BY group_name ORDER BY
price RANGE BETWEEN UNBOUNDED PRECEDING AND UNBOUNDED FOLLOWING
) AS highest_price_per_group
FROM
products
INNER JOIN product_groups USING (group_id)
order by group_name, product_name;
select '---- Q7 ----';
select product_name, price, group_name, round(avg0), round(avg1)
from (
SELECT
product_name,
price,
group_name,
avg(price) OVER (PARTITION BY group_name ORDER BY price) avg0,
avg(price) OVER (PARTITION BY group_name ORDER BY
price RANGE BETWEEN UNBOUNDED PRECEDING AND UNBOUNDED FOLLOWING) avg1
FROM products INNER JOIN product_groups USING (group_id)) t
order by group_name, product_name, price;
drop table product_groups;
drop table products;