-
Notifications
You must be signed in to change notification settings - Fork 152
Expand file tree
/
Copy pathq18_large_volume_customer.py
More file actions
104 lines (92 loc) · 3.16 KB
/
q18_large_volume_customer.py
File metadata and controls
104 lines (92 loc) · 3.16 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
# Licensed to the Apache Software Foundation (ASF) under one
# or more contributor license agreements. See the NOTICE file
# distributed with this work for additional information
# regarding copyright ownership. The ASF licenses this file
# to you under the Apache License, Version 2.0 (the
# "License"); you may not use this file except in compliance
# with the License. You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing,
# software distributed under the License is distributed on an
# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
# KIND, either express or implied. See the License for the
# specific language governing permissions and limitations
# under the License.
"""
TPC-H Problem Statement Query 18:
The Large Volume Customer Query finds a list of the top 100 customers who have ever placed large
quantity orders. The query lists the customer name, customer key, the order key, date and total
price and the quantity for the order.
The above problem statement text is copyrighted by the Transaction Processing Performance Council
as part of their TPC Benchmark H Specification revision 2.18.0.
Reference SQL (from TPC-H specification, used by the benchmark suite)::
select
c_name,
c_custkey,
o_orderkey,
o_orderdate,
o_totalprice,
sum(l_quantity)
from
customer,
orders,
lineitem
where
o_orderkey in (
select
l_orderkey
from
lineitem
group by
l_orderkey having
sum(l_quantity) > 300
)
and c_custkey = o_custkey
and o_orderkey = l_orderkey
group by
c_name,
c_custkey,
o_orderkey,
o_orderdate,
o_totalprice
order by
o_totalprice desc,
o_orderdate limit 100;
"""
from datafusion import SessionContext, col
from datafusion import functions as F
from util import get_data_path
QUANTITY = 300
# Load the dataframes we need
ctx = SessionContext()
df_customer = ctx.read_parquet(get_data_path("customer.parquet")).select(
"c_custkey", "c_name"
)
df_orders = ctx.read_parquet(get_data_path("orders.parquet")).select(
"o_orderkey", "o_custkey", "o_orderdate", "o_totalprice"
)
df_lineitem = ctx.read_parquet(get_data_path("lineitem.parquet")).select(
"l_orderkey", "l_quantity", "l_extendedprice"
)
# Find orders whose total quantity exceeds the threshold, then join in the
# order + customer details the problem statement requires and sort.
df = (
df_lineitem.aggregate(
["l_orderkey"], [F.sum(col("l_quantity")).alias("total_quantity")]
)
.filter(col("total_quantity") > QUANTITY)
.join(df_orders, left_on="l_orderkey", right_on="o_orderkey")
.join(df_customer, left_on="o_custkey", right_on="c_custkey")
.select(
"c_name",
"c_custkey",
"o_orderkey",
"o_orderdate",
"o_totalprice",
"total_quantity",
)
.sort(col("o_totalprice").sort(ascending=False), "o_orderdate")
)
df.show()