-
Notifications
You must be signed in to change notification settings - Fork 152
Expand file tree
/
Copy pathq03_shipping_priority.py
More file actions
109 lines (89 loc) · 3.53 KB
/
q03_shipping_priority.py
File metadata and controls
109 lines (89 loc) · 3.53 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
# 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 3:
The Shipping Priority Query retrieves the shipping priority and potential revenue, defined as the
sum of l_extendedprice * (1-l_discount), of the orders having the largest revenue among those that
had not been shipped as of a given date. Orders are listed in decreasing order of revenue. If more
than 10 unshipped orders exist, only the 10 orders with the largest revenue are listed.
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
l_orderkey,
sum(l_extendedprice * (1 - l_discount)) as revenue,
o_orderdate,
o_shippriority
from
customer,
orders,
lineitem
where
c_mktsegment = 'BUILDING'
and c_custkey = o_custkey
and l_orderkey = o_orderkey
and o_orderdate < date '1995-03-15'
and l_shipdate > date '1995-03-15'
group by
l_orderkey,
o_orderdate,
o_shippriority
order by
revenue desc,
o_orderdate limit 10;
"""
from datafusion import SessionContext, col, lit
from datafusion import functions as F
from util import get_data_path
SEGMENT_OF_INTEREST = "BUILDING"
DATE_OF_INTEREST = "1995-03-15"
# Load the dataframes we need
ctx = SessionContext()
df_customer = ctx.read_parquet(get_data_path("customer.parquet")).select(
"c_mktsegment", "c_custkey"
)
df_orders = ctx.read_parquet(get_data_path("orders.parquet")).select(
"o_orderdate", "o_shippriority", "o_custkey", "o_orderkey"
)
df_lineitem = ctx.read_parquet(get_data_path("lineitem.parquet")).select(
"l_orderkey", "l_extendedprice", "l_discount", "l_shipdate"
)
# Limit dataframes to the rows of interest
df_customer = df_customer.filter(col("c_mktsegment") == SEGMENT_OF_INTEREST)
df_orders = df_orders.filter(col("o_orderdate") < lit(DATE_OF_INTEREST))
df_lineitem = df_lineitem.filter(col("l_shipdate") > lit(DATE_OF_INTEREST))
# Join all 3 dataframes
df = df_customer.join(df_orders, left_on="c_custkey", right_on="o_custkey").join(
df_lineitem, left_on="o_orderkey", right_on="l_orderkey"
)
# Compute the revenue
df = df.aggregate(
["l_orderkey"],
[
F.first_value(col("o_orderdate")).alias("o_orderdate"),
F.first_value(col("o_shippriority")).alias("o_shippriority"),
F.sum(col("l_extendedprice") * (lit(1.0) - col("l_discount"))).alias("revenue"),
],
)
# Sort by priority, take 10, and project in the order expected by the spec.
df = (
df.sort(col("revenue").sort(ascending=False), "o_orderdate")
.limit(10)
.select("l_orderkey", "revenue", "o_orderdate", "o_shippriority")
)
# Show result
df.show()