Skip to content

Commit

Permalink
Add TPC-DS Queries (Postgres variant)
Browse files Browse the repository at this point in the history
  • Loading branch information
resetius committed Dec 28, 2023
1 parent cc85e0d commit 91fd975
Show file tree
Hide file tree
Showing 99 changed files with 4,965 additions and 0 deletions.
26 changes: 26 additions & 0 deletions ydb/library/benchmarks/queries/tpcds/pg/q01.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
{% include 'header.sql.jinja' %}

with customer_total_return as
(select sr_customer_sk as ctr_customer_sk
,sr_store_sk as ctr_store_sk
,sum(sr_fee) as ctr_total_return
from {{store_returns}}
,{{date_dim}}
where sr_returned_date_sk = d_date_sk
and d_year =2000
group by sr_customer_sk
,sr_store_sk)
select c_customer_id
from customer_total_return ctr1
,{{store}}
,{{customer}}
where ctr1.ctr_total_return > (select avg(ctr_total_return)*1.2::numeric
from customer_total_return ctr2
where ctr1.ctr_store_sk = ctr2.ctr_store_sk)
and s_store_sk = ctr1.ctr_store_sk
and s_state = 'TN'
and ctr1.ctr_customer_sk = c_customer_sk
order by c_customer_id
limit 100;

-- end query 1 in stream 0 using template ../query_templates/query1.tpl
61 changes: 61 additions & 0 deletions ydb/library/benchmarks/queries/tpcds/pg/q02.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
{% include 'header.sql.jinja' %}

with wscs as
(select sold_date_sk
,sales_price
from (select ws_sold_date_sk sold_date_sk
,ws_ext_sales_price sales_price
from {{web_sales}}
union all
select cs_sold_date_sk sold_date_sk
,cs_ext_sales_price sales_price
from {{catalog_sales}}) a),
wswscs as
(select d_week_seq,
sum(case when (d_day_name='Sunday') then sales_price else null::numeric end) sun_sales,
sum(case when (d_day_name='Monday') then sales_price else null::numeric end) mon_sales,
sum(case when (d_day_name='Tuesday') then sales_price else null::numeric end) tue_sales,
sum(case when (d_day_name='Wednesday') then sales_price else null::numeric end) wed_sales,
sum(case when (d_day_name='Thursday') then sales_price else null::numeric end) thu_sales,
sum(case when (d_day_name='Friday') then sales_price else null::numeric end) fri_sales,
sum(case when (d_day_name='Saturday') then sales_price else null::numeric end) sat_sales
from wscs
,{{date_dim}}
where d_date_sk = sold_date_sk
group by d_week_seq)
select d_week_seq1
,round(sun_sales1/sun_sales2,2)
,round(mon_sales1/mon_sales2,2)
,round(tue_sales1/tue_sales2,2)
,round(wed_sales1/wed_sales2,2)
,round(thu_sales1/thu_sales2,2)
,round(fri_sales1/fri_sales2,2)
,round(sat_sales1/sat_sales2,2)
from
(select wswscs.d_week_seq d_week_seq1
,sun_sales sun_sales1
,mon_sales mon_sales1
,tue_sales tue_sales1
,wed_sales wed_sales1
,thu_sales thu_sales1
,fri_sales fri_sales1
,sat_sales sat_sales1
from wswscs,{{date_dim}}
where date_dim.d_week_seq = wswscs.d_week_seq and
d_year = 2001) y,
(select wswscs.d_week_seq d_week_seq2
,sun_sales sun_sales2
,mon_sales mon_sales2
,tue_sales tue_sales2
,wed_sales wed_sales2
,thu_sales thu_sales2
,fri_sales fri_sales2
,sat_sales sat_sales2
from wswscs
,{{date_dim}}
where date_dim.d_week_seq = wswscs.d_week_seq and
d_year = 2001+1) z
where d_week_seq1=d_week_seq2-53
order by d_week_seq1;

-- end query 1 in stream 0 using template ../query_templates/query2.tpl
22 changes: 22 additions & 0 deletions ydb/library/benchmarks/queries/tpcds/pg/q03.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
{% include 'header.sql.jinja' %}

select dt.d_year
,item.i_brand_id brand_id
,item.i_brand brand
,sum(ss_ext_sales_price) sum_agg
from {{date_dim}} dt
,{{store_sales}}
,{{item}}
where dt.d_date_sk = store_sales.ss_sold_date_sk
and store_sales.ss_item_sk = item.i_item_sk
and item.i_manufact_id = 436
and dt.d_moy=12
group by dt.d_year
,item.i_brand
,item.i_brand_id
order by dt.d_year
,sum_agg desc
,brand_id
limit 100;

-- end query 1 in stream 0 using template ../query_templates/query3.tpl
117 changes: 117 additions & 0 deletions ydb/library/benchmarks/queries/tpcds/pg/q04.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,117 @@
{% include 'header.sql.jinja' %}

with year_total as (
select c_customer_id customer_id
,c_first_name customer_first_name
,c_last_name customer_last_name
,c_preferred_cust_flag customer_preferred_cust_flag
,c_birth_country customer_birth_country
,c_login customer_login
,c_email_address customer_email_address
,d_year dyear
,sum(((ss_ext_list_price-ss_ext_wholesale_cost-ss_ext_discount_amt)+ss_ext_sales_price)/2::numeric) year_total
,'s' sale_type
from {{customer}}
,{{store_sales}}
,{{date_dim}}
where c_customer_sk = ss_customer_sk
and ss_sold_date_sk = d_date_sk
group by c_customer_id
,c_first_name
,c_last_name
,c_preferred_cust_flag
,c_birth_country
,c_login
,c_email_address
,d_year
union all
select c_customer_id customer_id
,c_first_name customer_first_name
,c_last_name customer_last_name
,c_preferred_cust_flag customer_preferred_cust_flag
,c_birth_country customer_birth_country
,c_login customer_login
,c_email_address customer_email_address
,d_year dyear
,sum((((cs_ext_list_price-cs_ext_wholesale_cost-cs_ext_discount_amt)+cs_ext_sales_price)/2::numeric) ) year_total
,'c' sale_type
from {{customer}}
,{{catalog_sales}}
,{{date_dim}}
where c_customer_sk = cs_bill_customer_sk
and cs_sold_date_sk = d_date_sk
group by c_customer_id
,c_first_name
,c_last_name
,c_preferred_cust_flag
,c_birth_country
,c_login
,c_email_address
,d_year
union all
select c_customer_id customer_id
,c_first_name customer_first_name
,c_last_name customer_last_name
,c_preferred_cust_flag customer_preferred_cust_flag
,c_birth_country customer_birth_country
,c_login customer_login
,c_email_address customer_email_address
,d_year dyear
,sum((((ws_ext_list_price-ws_ext_wholesale_cost-ws_ext_discount_amt)+ws_ext_sales_price)/2::numeric) ) year_total
,'w' sale_type
from {{customer}}
,{{web_sales}}
,{{date_dim}}
where c_customer_sk = ws_bill_customer_sk
and ws_sold_date_sk = d_date_sk
group by c_customer_id
,c_first_name
,c_last_name
,c_preferred_cust_flag
,c_birth_country
,c_login
,c_email_address
,d_year
)
select
t_s_secyear.customer_id
,t_s_secyear.customer_first_name
,t_s_secyear.customer_last_name
,t_s_secyear.customer_email_address
from year_total t_s_firstyear
,year_total t_s_secyear
,year_total t_c_firstyear
,year_total t_c_secyear
,year_total t_w_firstyear
,year_total t_w_secyear
where t_s_secyear.customer_id = t_s_firstyear.customer_id
and t_s_firstyear.customer_id = t_c_secyear.customer_id
and t_s_firstyear.customer_id = t_c_firstyear.customer_id
and t_s_firstyear.customer_id = t_w_firstyear.customer_id
and t_s_firstyear.customer_id = t_w_secyear.customer_id
and t_s_firstyear.sale_type = 's'
and t_c_firstyear.sale_type = 'c'
and t_w_firstyear.sale_type = 'w'
and t_s_secyear.sale_type = 's'
and t_c_secyear.sale_type = 'c'
and t_w_secyear.sale_type = 'w'
and t_s_firstyear.dyear = 2001
and t_s_secyear.dyear = 2001+1
and t_c_firstyear.dyear = 2001
and t_c_secyear.dyear = 2001+1
and t_w_firstyear.dyear = 2001
and t_w_secyear.dyear = 2001+1
and t_s_firstyear.year_total > 0::numeric
and t_c_firstyear.year_total > 0::numeric
and t_w_firstyear.year_total > 0::numeric
and case when t_c_firstyear.year_total > 0::numeric then t_c_secyear.year_total / t_c_firstyear.year_total else null::numeric end
> case when t_s_firstyear.year_total > 0::numeric then t_s_secyear.year_total / t_s_firstyear.year_total else null::numeric end
and case when t_c_firstyear.year_total > 0::numeric then t_c_secyear.year_total / t_c_firstyear.year_total else null::numeric end
> case when t_w_firstyear.year_total > 0::numeric then t_w_secyear.year_total / t_w_firstyear.year_total else null::numeric end
order by t_s_secyear.customer_id
,t_s_secyear.customer_first_name
,t_s_secyear.customer_last_name
,t_s_secyear.customer_email_address
limit 100;

-- end query 1 in stream 0 using template ../query_templates/query4.tpl
129 changes: 129 additions & 0 deletions ydb/library/benchmarks/queries/tpcds/pg/q05.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,129 @@
{% include 'header.sql.jinja' %}

with ssr as
(select s_store_id,
sum(sales_price) as sales,
sum(profit) as profit,
sum(return_amt) as returns,
sum(net_loss) as profit_loss
from
( select ss_store_sk as store_sk,
ss_sold_date_sk as date_sk,
ss_ext_sales_price as sales_price,
ss_net_profit as profit,
cast(0 as decimal(7,2)) as return_amt,
cast(0 as decimal(7,2)) as net_loss
from {{store_sales}}
union all
select sr_store_sk as store_sk,
sr_returned_date_sk as date_sk,
cast(0 as decimal(7,2)) as sales_price,
cast(0 as decimal(7,2)) as profit,
sr_return_amt as return_amt,
sr_net_loss as net_loss
from {{store_returns}}
) salesreturns,
{{date_dim}},
{{store}}
where date_sk = d_date_sk
and d_date between cast('1998-08-04' as date)
and (cast('1998-08-04' as date) + interval '14' day)::date
and store_sk = s_store_sk
group by s_store_id)
,
csr as
(select cp_catalog_page_id,
sum(sales_price) as sales,
sum(profit) as profit,
sum(return_amt) as returns,
sum(net_loss) as profit_loss
from
( select cs_catalog_page_sk as page_sk,
cs_sold_date_sk as date_sk,
cs_ext_sales_price as sales_price,
cs_net_profit as profit,
cast(0 as decimal(7,2)) as return_amt,
cast(0 as decimal(7,2)) as net_loss
from {{catalog_sales}}
union all
select cr_catalog_page_sk as page_sk,
cr_returned_date_sk as date_sk,
cast(0 as decimal(7,2)) as sales_price,
cast(0 as decimal(7,2)) as profit,
cr_return_amount as return_amt,
cr_net_loss as net_loss
from {{catalog_returns}}
) salesreturns,
{{date_dim}},
{{catalog_page}}
where date_sk = d_date_sk
and d_date between cast('1998-08-04' as date)
and (cast('1998-08-04' as date) + interval '14' day)::date
and page_sk = cp_catalog_page_sk
group by cp_catalog_page_id)
,
wsr as
(select web_site_id,
sum(sales_price) as sales,
sum(profit) as profit,
sum(return_amt) as returns,
sum(net_loss) as profit_loss
from
( select ws_web_site_sk as wsr_web_site_sk,
ws_sold_date_sk as date_sk,
ws_ext_sales_price as sales_price,
ws_net_profit as profit,
cast(0 as decimal(7,2)) as return_amt,
cast(0 as decimal(7,2)) as net_loss
from {{web_sales}}
union all
select ws_web_site_sk as wsr_web_site_sk,
wr_returned_date_sk as date_sk,
cast(0 as decimal(7,2)) as sales_price,
cast(0 as decimal(7,2)) as profit,
wr_return_amt as return_amt,
wr_net_loss as net_loss
from {{web_returns}} left outer join {{web_sales}} on
( wr_item_sk = ws_item_sk
and wr_order_number = ws_order_number)
) salesreturns,
{{date_dim}},
{{web_site}}
where date_sk = d_date_sk
and d_date between cast('1998-08-04' as date)
and (cast('1998-08-04' as date) + interval '14' day)::date
and wsr_web_site_sk = web_site_sk
group by web_site_id)
select channel
, id
, sum(sales) as sales
, sum(returns) as returns
, sum(profit) as profit
from
(select 'store channel' as channel
, 'store' || s_store_id as id
, sales
, returns
, (profit - profit_loss) as profit
from ssr
union all
select 'catalog channel' as channel
, 'catalog_page' || cp_catalog_page_id as id
, sales
, returns
, (profit - profit_loss) as profit
from csr
union all
select 'web channel' as channel
, 'web_site' || web_site_id as id
, sales
, returns
, (profit - profit_loss) as profit
from wsr
) x
group by rollup (channel, id)
order by channel
,id
limit 100;

-- end query 1 in stream 0 using template ../query_templates/query5.tpl
27 changes: 27 additions & 0 deletions ydb/library/benchmarks/queries/tpcds/pg/q06.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
{% include 'header.sql.jinja' %}

select a.ca_state state, count(*) cnt
from {{customer_address}} a
,{{customer}} c
,{{store_sales}} s
,{{date_dim}} d
,{{item}} i
where a.ca_address_sk = c.c_current_addr_sk
and c.c_customer_sk = s.ss_customer_sk
and s.ss_sold_date_sk = d.d_date_sk
and s.ss_item_sk = i.i_item_sk
and d.d_month_seq =
(select distinct (d_month_seq)
from {{date_dim}}
where d_year = 2000
and d_moy = 2 )
and i.i_current_price > 1.2::numeric *
(select avg(j.i_current_price)
from {{item}} j
where j.i_category = i.i_category)
group by a.ca_state
having count(*) >= 10
order by cnt, a.ca_state
limit 100;

-- end query 1 in stream 0 using template ../query_templates/query6.tpl
Loading

0 comments on commit 91fd975

Please sign in to comment.