import numpy as np import vaex from vaex_queries import utils Q_NUM = 7 def q(): nation_ds = utils.get_nation_ds customer_ds = utils.get_customer_ds line_item_ds = utils.get_line_item_ds orders_ds = utils.get_orders_ds supplier_ds = utils.get_supplier_ds # first call one time to cache in case we don't include the IO times nation_ds() customer_ds() line_item_ds() orders_ds() supplier_ds() def query(): nonlocal nation_ds nonlocal customer_ds nonlocal line_item_ds nonlocal orders_ds nonlocal supplier_ds nation_ds = nation_ds() customer_ds = customer_ds() line_item_ds = line_item_ds() orders_ds = orders_ds() supplier_ds = supplier_ds() lineitem_filtered = line_item_ds[ (line_item_ds["l_shipdate"] >= np.datetime64("1995-01-01")) & (line_item_ds["l_shipdate"] < np.datetime64("1997-01-01")) ] lineitem_filtered["l_year"] = lineitem_filtered["l_shipdate"].apply( lambda x: x.year ) lineitem_filtered["revenue"] = lineitem_filtered["l_extendedprice"] * ( 1.0 - lineitem_filtered["l_discount"] ) lineitem_filtered = lineitem_filtered[ ["l_orderkey", "l_suppkey", "l_year", "revenue"] ] supplier_filtered = supplier_ds[["s_suppkey", "s_nationkey"]] orders_filtered = orders_ds[["o_orderkey", "o_custkey"]] customer_filtered = customer_ds[["c_custkey", "c_nationkey"]] n1 = nation_ds[(nation_ds["n_name"] == "FRANCE")][["n_nationkey", "n_name"]] n2 = nation_ds[(nation_ds["n_name"] == "GERMANY")][["n_nationkey", "n_name"]] # ----- do nation 1 ----- N1_C = customer_filtered.join( n1, left_on="c_nationkey", right_on="n_nationkey", how="inner", allow_duplication=True, ) N1_C = N1_C.drop(columns=["c_nationkey", "n_nationkey"]) N1_C.rename("n_name", "cust_nation") N1_C_O = N1_C.join( orders_filtered, left_on="c_custkey", right_on="o_custkey", how="inner", allow_duplication=True, ) N1_C_O = N1_C_O.drop(columns=["c_custkey", "o_custkey"]) N2_S = supplier_filtered.join( n2, left_on="s_nationkey", right_on="n_nationkey", how="inner", allow_duplication=True, ) N2_S = N2_S.drop(columns=["s_nationkey", "n_nationkey"]) N2_S.rename("n_name", "supp_nation") N2_S_L = N2_S.join( lineitem_filtered, left_on="s_suppkey", right_on="l_suppkey", how="inner", allow_duplication=True, ) N2_S_L = N2_S_L.drop(columns=["s_suppkey", "l_suppkey"]) total1 = N1_C_O.join( N2_S_L, left_on="o_orderkey", right_on="l_orderkey", how="inner", allow_duplication=True, ) total1 = total1.drop(columns=["o_orderkey", "l_orderkey"]) # ----- do nation 2 ----- (same as nation 1 section but with nation 2) N2_C = customer_filtered.join( n2, left_on="c_nationkey", right_on="n_nationkey", how="inner", allow_duplication=True, ) N2_C = N2_C.drop(columns=["c_nationkey", "n_nationkey"]) N2_C.rename("n_name", "cust_nation") N2_C_O = N2_C.join( orders_filtered, left_on="c_custkey", right_on="o_custkey", how="inner", allow_duplication=True, ) N2_C_O = N2_C_O.drop(columns=["c_custkey", "o_custkey"]) N1_S = supplier_filtered.join( n1, left_on="s_nationkey", right_on="n_nationkey", how="inner", allow_duplication=True, ) N1_S = N1_S.drop(columns=["s_nationkey", "n_nationkey"]) N1_S.rename("n_name", "supp_nation") N1_S_L = N1_S.join( lineitem_filtered, left_on="s_suppkey", right_on="l_suppkey", how="inner", allow_duplication=True, ) N1_S_L = N1_S_L.drop(columns=["s_suppkey", "l_suppkey"]) total2 = N2_C_O.merge( N1_S_L, left_on="o_orderkey", right_on="l_orderkey", how="inner", allow_duplication=True, ) total2 = total2.drop(columns=["o_orderkey", "l_orderkey"]) # concat results total = vaex.concat([total1, total2]) result_df = total.groupby(["supp_nation", "cust_nation", "l_year"]).agg( {"revenue": "sum"} ) result_df.columns = ["supp_nation", "cust_nation", "l_year", "revenue"] result_df = result_df.sort( by=["supp_nation", "cust_nation", "l_year"], ascending=[ True, True, True, ], ) return result_df utils.run_query(Q_NUM, query) if __name__ == "__main__": q()