-
Notifications
You must be signed in to change notification settings - Fork 0
/
parse.py
96 lines (73 loc) · 3.04 KB
/
parse.py
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
"""
Contains functions for transforming SQL statements into an IR
that can be made into a futhark call
"""
from moz_sql_parser import parse
import json
def getIndex(elements, value):
for i, v in enumerate(elements):
if v == value:
return i
return -1
def sql_parse(tables, sql_statement):
"""
Parses an SQL statement
"""
"""resolving the from table is problably the best palce to start this.
Then we have access to the infromation we need about corresponding columns
"""
js_obj = parse(sql_statement)
table = None
table_name = js_obj['from']
if table_name in tables:
table = tables[table_name]
else:
raise Exception(f"{table_name} is not in tables")
"""
Now we resolve the select columns??
"""
columns = table.get_schema()
if "groupby" not in js_obj.keys():
fut_cols_selects = []
select_pairs = js_obj['select']
for pair in select_pairs:
# how do that pairs come
if "value" in pair:
col_name = pair["value"]
idx = getIndex(columns, col_name)
if idx < 0:
raise Exception(f"{col_name} is not in the schema of table {table_name}")
else:
fut_cols_selects += [idx]
print("We are about to return")
return {"table": table.get_data(), "select": fut_cols_selects}
if "groupby" in js_obj.keys():
# Instantiate variables that need to be filled
fut_cols_selects = []
typ_cols_selects = []
g_col = None # This will probably be an array if i fix futhark implementation of groupby
g_col_name = js_obj["groupby"]["value"]
g_col = getIndex(columns, g_col_name)
if g_col < 0:
raise Exception(f"{g_col_name} is not in the schema of table {table_name}")
select_pairs = js_obj['select']
for dic in select_pairs:
if dic["value"] == g_col_name:
fut_cols_selects += [g_col]
typ_cols_selects += [0]
elif type(dic["value"]) == type(""):
bad_col_name = dic["value"]
raise Exception(f"{bad_col_name} is not an aggregation function or the columns thats grouped on")
else:
# Assume we are dealing with dic["value"] -> {"agg" : "col_name"}
funcToFut = {"prod" : 1, "sum" : 2, "max" : 3, "min" : 4}
for agg_func, agg_val in funcToFut.items():
if agg_func in dic["value"]:
agg_col_name = dic["value"][agg_func]
agg_col = getIndex(columns, agg_col_name)
if agg_col < 0:
raise Exception(f"{agg_col_name} is not in the schema of table {table_name}")
fut_cols_selects += [agg_col]
typ_cols_selects += [agg_val]
return {"select": fut_cols_selects, "groupbys": typ_cols_selects, "table": table.get_data(), "g_col": g_col}
return {}