| # 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 13: | |
| This query determines the distribution of customers by the number of orders they have made, | |
| including customers who have no record of orders, past or present. It counts and reports how many | |
| customers have no orders, how many have 1, 2, 3, etc. A check is made to ensure that the orders | |
| counted do not fall into one of several special categories of orders. Special categories are | |
| identified in the order comment column by looking for a particular pattern. | |
| 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_count, | |
| count(*) as custdist | |
| from | |
| ( | |
| select | |
| c_custkey, | |
| count(o_orderkey) | |
| from | |
| customer left outer join orders on | |
| c_custkey = o_custkey | |
| and o_comment not like '%special%requests%' | |
| group by | |
| c_custkey | |
| ) as c_orders (c_custkey, c_count) | |
| group by | |
| c_count | |
| order by | |
| custdist desc, | |
| c_count desc; | |
| """ | |
| from datafusion import SessionContext, col, lit | |
| from datafusion import functions as F | |
| from util import get_data_path | |
| WORD_1 = "special" | |
| WORD_2 = "requests" | |
| # Load the dataframes we need | |
| ctx = SessionContext() | |
| df_orders = ctx.read_parquet(get_data_path("orders.parquet")).select( | |
| "o_custkey", "o_comment" | |
| ) | |
| df_customer = ctx.read_parquet(get_data_path("customer.parquet")).select("c_custkey") | |
| # Use a regex to remove special cases | |
| df_orders = df_orders.filter( | |
| F.regexp_match(col("o_comment"), lit(f"{WORD_1}.?*{WORD_2}")).is_null() | |
| ) | |
| # Customers with no orders still participate, so this is a left join. Count the | |
| # orders per customer, then count customers per order-count value. | |
| df = ( | |
| df_customer.join(df_orders, left_on="c_custkey", right_on="o_custkey", how="left") | |
| .aggregate(["c_custkey"], [F.count(col("o_custkey")).alias("c_count")]) | |
| .aggregate(["c_count"], [F.count_star().alias("custdist")]) | |
| .sort( | |
| col("custdist").sort(ascending=False), | |
| col("c_count").sort(ascending=False), | |
| ) | |
| ) | |
| df.show() |