Upload utils.py
Browse files- ibis_queries/utils.py +157 -0
ibis_queries/utils.py
ADDED
@@ -0,0 +1,157 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import timeit
|
2 |
+
from os.path import join
|
3 |
+
from typing import Callable
|
4 |
+
|
5 |
+
import ibis
|
6 |
+
import pandas as pd
|
7 |
+
from linetimer import CodeTimer, linetimer
|
8 |
+
from pandas.core.frame import DataFrame as PandasDF
|
9 |
+
|
10 |
+
from common_utils import (
|
11 |
+
ANSWERS_BASE_DIR,
|
12 |
+
DATASET_BASE_DIR,
|
13 |
+
FILE_TYPE,
|
14 |
+
LOG_TIMINGS,
|
15 |
+
SHOW_RESULTS,
|
16 |
+
append_row,
|
17 |
+
on_second_call,
|
18 |
+
)
|
19 |
+
|
20 |
+
|
21 |
+
BACKEND = str(ibis.get_backend()).split(".")[2]
|
22 |
+
|
23 |
+
|
24 |
+
def _read_ds(path: str) -> PandasDF:
|
25 |
+
path = f"{path}.{FILE_TYPE}"
|
26 |
+
if FILE_TYPE == "parquet":
|
27 |
+
return ibis.read_parquet(path)
|
28 |
+
# elif FILE_TYPE == "feather":
|
29 |
+
# return pd.read_feather(path)
|
30 |
+
else:
|
31 |
+
raise ValueError(f"file type: {FILE_TYPE} not expected")
|
32 |
+
|
33 |
+
|
34 |
+
def get_query_answer(query: int, base_dir: str = ANSWERS_BASE_DIR) -> PandasDF:
|
35 |
+
|
36 |
+
if BACKEND == "pandas":
|
37 |
+
answer_df = ibis.read_csv(
|
38 |
+
join(base_dir, f"q{query}.out"),
|
39 |
+
sep="|",
|
40 |
+
parse_dates=True,
|
41 |
+
infer_datetime_format=True,
|
42 |
+
)
|
43 |
+
|
44 |
+
elif BACKEND == "polars":
|
45 |
+
answer_df = ibis.read_csv(
|
46 |
+
join(base_dir, f"q{query}.out"),
|
47 |
+
separator="|",
|
48 |
+
has_header=True,
|
49 |
+
try_parse_dates=True,
|
50 |
+
)
|
51 |
+
|
52 |
+
renamed_columns = {col: col.strip() for col in answer_df.columns}
|
53 |
+
|
54 |
+
return answer_df.relabel(renamed_columns).execute()
|
55 |
+
|
56 |
+
|
57 |
+
def test_results(q_num: int, result_df: PandasDF):
|
58 |
+
with CodeTimer(name=f"Testing result of pandas Query {q_num}", unit="s"):
|
59 |
+
answer = get_query_answer(q_num)
|
60 |
+
|
61 |
+
for c, t in answer.dtypes.items():
|
62 |
+
s1 = result_df[c]
|
63 |
+
s2 = answer[c]
|
64 |
+
|
65 |
+
if t.name == "object":
|
66 |
+
s1 = s1.astype("string").apply(lambda x: x.strip())
|
67 |
+
s2 = s2.astype("string").apply(lambda x: x.strip())
|
68 |
+
|
69 |
+
pd.testing.assert_series_equal(left=s1, right=s2, check_index=False)
|
70 |
+
|
71 |
+
|
72 |
+
@on_second_call
|
73 |
+
def get_line_item_ds(base_dir: str = DATASET_BASE_DIR) -> PandasDF:
|
74 |
+
return _read_ds(join(base_dir, "lineitem"))
|
75 |
+
|
76 |
+
|
77 |
+
@on_second_call
|
78 |
+
def get_orders_ds(base_dir: str = DATASET_BASE_DIR) -> PandasDF:
|
79 |
+
return _read_ds(join(base_dir, "orders"))
|
80 |
+
|
81 |
+
|
82 |
+
@on_second_call
|
83 |
+
def get_customer_ds(base_dir: str = DATASET_BASE_DIR) -> PandasDF:
|
84 |
+
return _read_ds(join(base_dir, "customer"))
|
85 |
+
|
86 |
+
|
87 |
+
@on_second_call
|
88 |
+
def get_region_ds(base_dir: str = DATASET_BASE_DIR) -> PandasDF:
|
89 |
+
return _read_ds(join(base_dir, "region"))
|
90 |
+
|
91 |
+
|
92 |
+
@on_second_call
|
93 |
+
def get_nation_ds(base_dir: str = DATASET_BASE_DIR) -> PandasDF:
|
94 |
+
return _read_ds(join(base_dir, "nation"))
|
95 |
+
|
96 |
+
|
97 |
+
@on_second_call
|
98 |
+
def get_supplier_ds(base_dir: str = DATASET_BASE_DIR) -> PandasDF:
|
99 |
+
return _read_ds(join(base_dir, "supplier"))
|
100 |
+
|
101 |
+
|
102 |
+
@on_second_call
|
103 |
+
def get_part_ds(base_dir: str = DATASET_BASE_DIR) -> PandasDF:
|
104 |
+
return _read_ds(join(base_dir, "part"))
|
105 |
+
|
106 |
+
|
107 |
+
@on_second_call
|
108 |
+
def get_part_supp_ds(base_dir: str = DATASET_BASE_DIR) -> PandasDF:
|
109 |
+
return _read_ds(join(base_dir, "partsupp"))
|
110 |
+
|
111 |
+
|
112 |
+
# def run_query(q_num: int, query: Callable):
|
113 |
+
# @linetimer(name=f"Overall execution of pandas Query {q_num}", unit="s")
|
114 |
+
# def run():
|
115 |
+
# with CodeTimer(name=f"Get result of pandas Query {q_num}", unit="s"):
|
116 |
+
# t0 = timeit.default_timer()
|
117 |
+
# result = query().execute()
|
118 |
+
# secs = timeit.default_timer() - t0
|
119 |
+
|
120 |
+
# if LOG_TIMINGS:
|
121 |
+
# append_row(
|
122 |
+
# solution="pandas", version=pd.__version__, q=f"q{q_num}", secs=secs
|
123 |
+
# )
|
124 |
+
# else:
|
125 |
+
# test_results(q_num, result)
|
126 |
+
# # pass
|
127 |
+
|
128 |
+
# if SHOW_RESULTS:
|
129 |
+
# print(result)
|
130 |
+
|
131 |
+
# run()
|
132 |
+
|
133 |
+
|
134 |
+
def run_query(q_num: int, lp):
|
135 |
+
@linetimer(name=f"Overall execution of ibis Query {q_num}", unit="s")
|
136 |
+
def query():
|
137 |
+
# if SHOW_PLAN:
|
138 |
+
# print(lp.explain())
|
139 |
+
|
140 |
+
with CodeTimer(name=f"Get result of ibis Query {q_num}", unit="s"):
|
141 |
+
t0 = timeit.default_timer()
|
142 |
+
result = lp.execute()
|
143 |
+
|
144 |
+
secs = timeit.default_timer() - t0
|
145 |
+
|
146 |
+
if LOG_TIMINGS:
|
147 |
+
pass
|
148 |
+
# append_row(
|
149 |
+
# solution="polars", version=pl.__version__, q=f"q{q_num}", secs=secs
|
150 |
+
# )
|
151 |
+
else:
|
152 |
+
test_results(q_num, result)
|
153 |
+
|
154 |
+
if SHOW_RESULTS:
|
155 |
+
print(result)
|
156 |
+
|
157 |
+
query()
|