-
Notifications
You must be signed in to change notification settings - Fork 7
/
JOBParser.py
executable file
·257 lines (223 loc) · 9.95 KB
/
JOBParser.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
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
from tkinter import E
import numpy as np
class Expr:
def __init__(self, expr,list_kind = 0):
self.expr = expr
self.list_kind = list_kind
self.isInt = False
self.val = 0
def isCol(self,):
return isinstance(self.expr, dict) and "ColumnRef" in self.expr
def getValue(self, value_expr):
if "A_Const" in value_expr:
value = value_expr["A_Const"]["val"]
if "String" in value:
return "'" + value["String"]["str"].replace("'","''")+"\'"
elif "Integer" in value:
self.isInt = True
self.val = value["Integer"]["ival"]
return str(value["Integer"]["ival"])
else:
raise "unknown Value in Expr"
elif "TypeCast" in value_expr:
if len(value_expr["TypeCast"]['typeName']['TypeName']['names'])==1:
return value_expr["TypeCast"]['typeName']['TypeName']['names'][0]['String']['str']+" '"+value_expr["TypeCast"]['arg']['A_Const']['val']['String']['str']+"'"
else:
if value_expr["TypeCast"]['typeName']['TypeName']['typmods'][0]['A_Const']['val']['Integer']['ival']==2:
return value_expr["TypeCast"]['typeName']['TypeName']['names'][1]['String']['str']+" '"+value_expr["TypeCast"]['arg']['A_Const']['val']['String']['str']+ "' month"
else:
return value_expr["TypeCast"]['typeName']['TypeName']['names'][1]['String']['str']+" '"+value_expr["TypeCast"]['arg']['A_Const']['val']['String']['str']+ "' year"
else:
print(value_expr.keys())
raise "unknown Value in Expr"
def getAliasName(self,):
return self.expr["ColumnRef"]["fields"][0]["String"]["str"]
def getColumnName(self,):
return self.expr["ColumnRef"]["fields"][1]["String"]["str"]
def __str__(self,):
if self.isCol():
return self.getAliasName()+"."+self.getColumnName()
elif isinstance(self.expr, dict) and "A_Const" in self.expr:
return self.getValue(self.expr)
elif isinstance(self.expr, dict) and "TypeCast" in self.expr:
return self.getValue(self.expr)
elif isinstance(self.expr, list):
if self.list_kind == 6:
return "("+",\n".join([self.getValue(x) for x in self.expr])+")"
elif self.list_kind == 10:
return " AND ".join([self.getValue(x) for x in self.expr])
else:
raise "list kind error"
else:
raise "No Known type of Expr"
class TargetTable:
def __init__(self, target):
"""
{'location': 7, 'name': 'alternative_name', 'val': {'FuncCall': {'funcname': [{'String': {'str': 'min'}}], 'args': [{'ColumnRef': {'fields': [{'String': {'str': 'an'}}, {'String': {'str': 'name'}}], 'location': 11}}], 'location': 7}}}
"""
self.target = target
# print(self.target)
def getValue(self,):
columnRef = self.target["val"]["FuncCall"]["args"][0]["ColumnRef"]["fields"]
return columnRef[0]["String"]["str"]+"."+columnRef[1]["String"]["str"]
def __str__(self,):
try:
return self.target["val"]["FuncCall"]["funcname"][0]["String"]["str"]+"(" + self.getValue() + ")" + " AS " + self.target['name']
except:
if "FuncCall" in self.target["val"]:
return "count(*)"
else:
return "*"
class FromTable:
def __init__(self, from_table):
"""
{'alias': {'Alias': {'aliasname': 'an'}}, 'location': 168, 'inhOpt': 2, 'relpersistence': 'p', 'relname': 'aka_name'}
"""
self.from_table = from_table
if not 'alias' in self.from_table:
self.from_table['alias']={'Alias': {'aliasname':from_table['relname'] }}
def getFullName(self,):
return self.from_table["relname"]
def getAliasName(self,):
return self.from_table["alias"]["Alias"]["aliasname"]
def __str__(self,):
try:
return self.getFullName()+" AS "+self.getAliasName()
except:
print(self.from_table)
raise
class Comparison:
def __init__(self, comparison):
self.comparison = comparison
self.column_list = []
if "A_Expr" in self.comparison:
self.lexpr = Expr(comparison["A_Expr"]["lexpr"])
self.column = str(self.lexpr)
self.kind = comparison["A_Expr"]["kind"]
if not "A_Expr" in comparison["A_Expr"]["rexpr"]:
self.rexpr = Expr(comparison["A_Expr"]["rexpr"],self.kind)
else:
self.rexpr = Comparison(comparison["A_Expr"]["rexpr"])
self.aliasname_list = []
if self.lexpr.isCol():
self.aliasname_list.append(self.lexpr.getAliasName())
self.column_list.append(self.lexpr.getColumnName())
if self.rexpr.isCol():
self.aliasname_list.append(self.rexpr.getAliasName())
self.column_list.append(self.rexpr.getColumnName())
self.comp_kind = 0
elif "NullTest" in self.comparison:
self.lexpr = Expr(comparison["NullTest"]["arg"])
self.column = str(self.lexpr)
self.kind = comparison["NullTest"]["nulltesttype"]
self.aliasname_list = []
if self.lexpr.isCol():
self.aliasname_list.append(self.lexpr.getAliasName())
self.column_list.append(self.lexpr.getColumnName())
self.comp_kind = 1
else:
# "boolop"
self.kind = comparison["BoolExpr"]["boolop"]
self.comp_list = [Comparison(x)
for x in comparison["BoolExpr"]["args"]]
self.aliasname_list = []
for comp in self.comp_list:
if comp.lexpr.isCol():
self.aliasname_list.append(comp.lexpr.getAliasName())
self.lexpr = comp.lexpr
self.column = str(self.lexpr)
self.column_list.append(comp.lexpr.getColumnName())
break
self.comp_kind = 2
def isCol(self,):
return False
def __str__(self,):
if self.comp_kind == 0:
Op = ""
if self.kind == 0:
Op = self.comparison["A_Expr"]["name"][0]["String"]["str"]
elif self.kind == 7:
if self.comparison["A_Expr"]["name"][0]["String"]["str"]=="!~~":
Op = "not like"
else:
Op = "like"
elif self.kind == 8:
if self.comparison["A_Expr"]["name"][0]["String"]["str"]=="~~*":
Op = "ilike"
else:
raise
elif self.kind == 6:
Op = "IN"
elif self.kind == 10:
Op = "BETWEEN"
else:
import json
print(json.dumps(self.comparison, sort_keys=True, indent=4))
raise "Operation ERROR"
return str(self.lexpr)+" "+Op+" "+str(self.rexpr)
elif self.comp_kind == 1:
if self.kind == 1:
return str(self.lexpr)+" IS NOT NULL"
else:
return str(self.lexpr)+" IS NULL"
else:
res = ""
for comp in self.comp_list:
if res == "":
res += "( "+str(comp)
else:
if self.kind == 1:
res += " OR "
else:
res += " AND "
res += str(comp)
res += ")"
return res
class Table:
def __init__(self, table_tree):
self.name = table_tree["relation"]["RangeVar"]["relname"]
self.column2idx = {}
self.idx2column = {}
self.column2type = {}
for idx, columndef in enumerate(table_tree["tableElts"]):
self.column2idx[columndef["ColumnDef"]["colname"]] = idx
self.column2type[columndef["ColumnDef"]["colname"]] = columndef["ColumnDef"]["typeName"]['TypeName']['names'][-1]['String']['str']
# print(columndef["ColumnDef"]["typeName"]['TypeName']['names'],self.column2type[columndef["ColumnDef"]["colname"]],self.column2type[columndef["ColumnDef"]["colname"]] in ['int4','text','varchar'])
assert self.column2type[columndef["ColumnDef"]["colname"]] in ['int4','text','varchar']
if self.column2type[columndef["ColumnDef"]["colname"]] =='int4':
self.column2type[columndef["ColumnDef"]["colname"]] = 'int'
else:
self.column2type[columndef["ColumnDef"]["colname"]] = 'str'
self.idx2column[idx] = columndef["ColumnDef"]["colname"]
def oneHotAll(self):
return np.zeros((1, len(self.column2idx)))
class DB:
def __init__(self, schema,TREE_NUM_IN_NET=40):
from psqlparse import parse_dict
parse_tree = parse_dict(schema)
self.tables = []
self.name2idx = {}
self.table_names = []
self.name2table = {}
self.size = 0
self.TREE_NUM_IN_NET = TREE_NUM_IN_NET
for idx, table_tree in enumerate(parse_tree):
self.tables.append(Table(table_tree["CreateStmt"]))
self.table_names.append(self.tables[-1].name)
self.name2idx[self.tables[-1].name] = idx
self.name2table[self.tables[-1].name] = self.tables[-1]
self.columns_total = 0
for table in self.tables:
self.columns_total += len(table.idx2column)
self.size = len(self.table_names)
def is_str(self,table,column):
table = self.name2table[table]
return table.column2type[column]=='str'
def __len__(self,):
if self.size == 0:
self.size = len(self.table_names)
return self.size
def oneHotAll(self,):
return np.zeros((1, self.size))
def network_size(self,):
return self.TREE_NUM_IN_NET*self.size