-
Notifications
You must be signed in to change notification settings - Fork 152
Expand file tree
/
Copy pathq16_part_supplier_relationship.py
More file actions
119 lines (104 loc) · 3.74 KB
/
q16_part_supplier_relationship.py
File metadata and controls
119 lines (104 loc) · 3.74 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
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
# 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 16:
The Parts/Supplier Relationship Query counts the number of suppliers who can supply parts that
satisfy a particular customer's requirements. The customer is interested in parts of eight
different sizes as long as they are not of a given type, not of a given brand, and not from a
supplier who has had complaints registered at the Better Business Bureau. Results must be presented
in descending count and ascending brand, type, and size.
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
p_brand,
p_type,
p_size,
count(distinct ps_suppkey) as supplier_cnt
from
partsupp,
part
where
p_partkey = ps_partkey
and p_brand <> 'Brand#45'
and p_type not like 'MEDIUM POLISHED%'
and p_size in (49, 14, 23, 45, 19, 3, 36, 9)
and ps_suppkey not in (
select
s_suppkey
from
supplier
where
s_comment like '%Customer%Complaints%'
)
group by
p_brand,
p_type,
p_size
order by
supplier_cnt desc,
p_brand,
p_type,
p_size;
"""
from datafusion import SessionContext, col, lit
from datafusion import functions as F
from util import get_data_path
BRAND = "Brand#45"
TYPE_TO_IGNORE = "MEDIUM POLISHED"
SIZES_OF_INTEREST = [49, 14, 23, 45, 19, 3, 36, 9]
# Load the dataframes we need
ctx = SessionContext()
df_part = ctx.read_parquet(get_data_path("part.parquet")).select(
"p_partkey", "p_brand", "p_type", "p_size"
)
df_partsupp = ctx.read_parquet(get_data_path("partsupp.parquet")).select(
"ps_suppkey", "ps_partkey"
)
df_supplier = ctx.read_parquet(get_data_path("supplier.parquet")).select(
"s_suppkey", "s_comment"
)
df_unwanted_suppliers = df_supplier.filter(
F.regexp_like(col("s_comment"), lit("Customer.*Complaints"))
)
# Remove unwanted suppliers via an anti join (DataFrame form of NOT IN).
df_partsupp = df_partsupp.join(
df_unwanted_suppliers, left_on="ps_suppkey", right_on="s_suppkey", how="anti"
)
# Select the parts we are interested in.
df_part = df_part.filter(
col("p_brand") != BRAND,
~F.starts_with(col("p_type"), lit(TYPE_TO_IGNORE)),
F.in_list(col("p_size"), [lit(s) for s in SIZES_OF_INTEREST]),
)
# For each (brand, type, size), count the distinct suppliers remaining.
df = (
df_part.join(df_partsupp, left_on="p_partkey", right_on="ps_partkey")
.select("p_brand", "p_type", "p_size", "ps_suppkey")
.distinct()
.aggregate(
["p_brand", "p_type", "p_size"],
[F.count(col("ps_suppkey")).alias("supplier_cnt")],
)
.sort(
col("supplier_cnt").sort(ascending=False),
"p_brand",
"p_type",
"p_size",
)
)
df.show()