-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathexpr.py
250 lines (238 loc) · 9.43 KB
/
expr.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
from util import *
from constants import *
from pred import *
expr_delta_cache = {}
def get_expr_delta_cache(v):
global expr_delta_cache
if v not in expr_delta_cache:
thread_ctx = symbctx.create_thread_ctx()
return expr_delta_cache[v]
def set_expr_delta_cache(key, value):
global expr_delta_cache
expr_delta_cache[key] = value
def type_larger(l, r):
if l == 'float' and (not r == 'float'):
return True
return False
class InitialValuePlaceholder(object):
def __init__(self,tipe=None):
self.tipe = tipe
def get_type(self):
return self.tipe
def __str__(self):
return 'orig_value'
class BinaryExpr(object):
def __init__(self, lh, op, rh):
self.lh = lh
self.op = op
self.rh = rh
# assert(is_query_field(self.lh) or \
# isinstance(self.lh, AtomValue) or \
# isinstance(self.lh, BinaryExpr) or\
# isinstance(self.lh, Parameter))
# assert(is_query_field(self.rh) or \
# isinstance(self.rh, AtomValue) or \
# isinstance(self.rh, BinaryExpr) or\
# isinstance(self.rh, Parameter))
assert(not isinstance(self.lh, UnaryExpr))
assert(not isinstance(self.rh, UnaryExpr))
def __eq__(self, other):
return type(self) == type(other) and self.lh == other.lh and self.op == other.op and self.rh == other.rh
def to_var_or_value(self, replace={}):
lh = replace[self.lh] if self.lh in replace else '{}'.format(self.lh.to_var_or_value(replace=replace))
rh = replace[self.rh] if self.rh in replace else '{}'.format(self.rh.to_var_or_value(replace=replace))
if type_larger(self.lh.get_type(), self.rh.get_type()):
rh = '({}){}'.format(get_cpp_types(self.lh.get_type()), rh)
if type_larger(self.rh.get_type(), self.lh.get_type()):
lh = '({}){}'.format(get_cpp_types(self.rh.get_type()), lh)
return '({} {} {})'.format(lh, expr_op_to_cpp_map[self.op], rh)
def __str__(self):
lh = str(self.lh)
op = expr_op_to_str_map[self.op]
rh = str(self.rh)
return '({} {} {})'.format(lh, op, rh)
def complete_field(self, table):
self.lh.complete_field(table)
self.rh.complete_field(table)
if isinstance(self.rh, AtomValue):
self.rh.tipe = self.lh.get_type()
def get_type(self):
lh_type = self.lh.get_type()
rh_type = self.rh.get_type()
if lh_type == 'float' or rh_type == 'float':
return 'float'
if lh_type:
return lh_type
if rh_type:
return rh_type
assert(False)
return None
def get_all_fields(self):
return self.lh.get_all_fields() + self.rh.get_all_fields()
def get_curlevel_fields(self, include_assoc=False):
return self.lh.get_curlevel_fields(include_assoc) + self.rh.get_curlevel_fields(include_assoc)
def get_all_params(self):
return self.lh.get_all_params() + self.rh.get_all_params()
def query_pred_eq(self, other):
return self.__eq__(other)
def get_all_atom_values(self):
return self.lh.get_all_atom_values() + self.rh.get_all_atom_values()
def to_json(self):
return '({}){}({})'.format(self.lh.to_json(), expr_op_to_str_map[self.op], self.rh.to_json())
class UnaryExpr(object):
def __init__(self, op, operand=""):
self.op = op
self.operand = operand
# assert(is_query_field(self.operand) or \
# isinstance(self.operand, BinaryExpr) or \
# isinstance(self.operand, AtomValue) or \
# isinstance(self.operand, IfThenElseExpr) or \
# self.operand == '')
if self.op == AVG:
self.sum_var = None
self.count_var = None
def __eq__(self, other):
return type(self) == type(other) and self.op == other.op and self.operand == other.operand
def to_var_or_value(self, init_var='', replace_var={}):
if self.op == COUNT:
opd = ''
elif self.operand in replace_var:
opd = replace_var[self.operand]
else:
opd = '{}'.format(self.operand.to_var_or_value(replace_var))
if self.op == MAX:
return 'if ({}<{}) {} = {};\n'.format(init_var, opd, init_var, opd)
elif self.op == MIN:
return 'if ({}>{}) {} = {};\n'.format(init_var, opd, init_var, opd)
elif self.op == COUNT:
return '{}++;\n'.format(init_var)
elif self.op == SUM:
return '{} = {} + {};\n'.format(init_var, init_var, opd)
elif self.op == AVG:
return ''
else:
assert(False)
def __str__(self):
op = expr_op_to_str_map[self.op]
operand = str(self.operand)
return '{}({})'.format(op, operand)
def get_type(self):
if self.op == COUNT:
return 'uint'
tipe = self.operand.get_type()
if self.op == SUM:
return get_sum_type(tipe)
return tipe
assert(False)
def complete_field(self, table):
if self.op != COUNT:
self.operand.complete_field(table)
def get_all_fields(self):
if self.op == COUNT:
return []
return self.operand.get_all_fields()
def get_curlevel_fields(self, include_assoc=False):
if self.op == COUNT:
return []
return self.operand.get_curlevel_fields(include_assoc)
def get_all_atom_values(self):
if self.op == COUNT:
return [AtomValue(1)]
return self.operand.get_all_atom_values()
def get_all_params(self):
if self.op == COUNT:
return []
return self.operand.get_all_params()
def query_pred_eq(self, other):
return self.__eq__(other)
def to_json(self):
if self.op == COUNT:
return 'count()'
else:
return '{}({})'.format(expr_op_to_str_map[self.op], self.operand.to_json())
class IfThenElseExpr(object):
def __init__(self, cond, expr1, expr2):
self.cond = cond
self.expr1 = expr1
self.expr2 = expr2
self.op = IFTHENELSE
# assert(isinstance(self.cond, BinaryExpr))
# assert(is_query_field(self.expr1) or isinstance(self.expr1, AtomValue) or isinstance(self.expr1, BinaryExpr))
# assert(is_query_field(self.expr2) or isinstance(self.expr2, AtomValue) or isinstance(self.expr2, BinaryExpr))
assert(not isinstance(self.expr1, UnaryExpr))
assert(not isinstance(self.expr2, UnaryExpr))
def __eq__(self, other):
return type(self) == type(other) and self.cond == other.cond and self.expr1 == other.expr1 and self.expr2 == other.expr2
def to_var_or_value(self, init_var='', replace_var={}):
cond = self.cond.to_var_or_value(replace_var)
if isinstance(self.expr1, UnaryExpr):
expr1 = '{}'.format(self.expr1.to_var_or_value(init_var, replace_var))
else:
expr1 = '{} = {};\n'.format(init_var, self.expr1.to_var_or_value(replace_var))
if isinstance(self.expr2, UnaryExpr):
expr2 = '{}'.format(self.expr2.to_var_or_value(init_var, replace_var))
else:
expr2 = '{} = {};\n'.format(init_var, self.expr2.to_var_or_value(replace_var))
return 'if ({}) {}else {}'.format(cond, expr1, expr2)
def __str__(self):
return 'if ({}) then ({}) else ({})'.format(self.cond, self.expr1, self.expr2)
def get_type(self):
tipe = self.expr1.get_type()
return tipe
def complete_field(self, table):
self.cond.complete_field(table)
self.expr1.complete_field(table)
self.expr2.complete_field(table)
def get_all_fields(self):
return self.cond.get_all_fields() + self.expr1.get_all_fields() + self.expr2.get_all_fields()
def get_curlevel_fields(self, include_assoc=False):
return self.cond.get_curlevel_fields(include_assoc) + \
self.expr1.get_curlevel_fields(include_assoc) + \
self.expr2.get_curlevel_fields(include_assoc)
def get_all_params(self):
return self.cond.get_all_params() + self.expr1.get_all_params() + self.expr2.get_all_params()
def get_all_atom_values(self):
return self.cond.get_all_atom_values() + self.expr1.get_all_atom_values() + self.expr2.get_all_atom_values()
def query_pred_eq(self, other):
return self.__eq__(other)
def to_json(self):
return 'ite({}, {}, {})'.format(self.cond.to_json(), self.expr1.to_json(), self.expr2.to_json())
def replace_subexpr_with_var(expr, placeholder):
if isinstance(expr, BinaryExpr):
return BinaryExpr(replace_subexpr_with_var(expr.lh, placeholder), expr.op, replace_subexpr_with_var(expr.rh, placeholder))
elif isinstance(expr, UnaryExpr):
return UnaryExpr(expr.op, replace_subexpr_with_var(expr.operand, placeholder))
elif isinstance(expr, IfThenElseExpr):
return IfThenElseExpr(replace_subexpr_with_var(expr.cond, placeholder), \
replace_subexpr_with_var(expr.expr1, placeholder), \
replace_subexpr_with_var(expr.expr2, placeholder))
elif isinstance(expr, AssocOp):
assert(expr in placeholder)
return placeholder[expr]
elif isinstance(expr, BinOp):
return BinOp(replace_subexpr_with_var(expr.lh, placeholder), expr.op, replace_subexpr_with_var(expr.rh, placeholder))
else:
return expr
def get_curlevel_fields(expr):
if isinstance(expr, QueryField):
if isinstance(expr, UpperQueryField):
return []
return [expr]
elif isinstance(expr, BinOp):
return get_curlevel_fields(expr.lh) + get_curlevel_fields(expr.rh)
elif isinstance(expr, SetOp):
return []
elif isinstance(expr, ConnectOp):
return get_curlevel_fields(expr.lh) + get_curlevel_fields(expr.rh)
elif isinstance(expr, AssocOp):
return []
elif isinstance(expr, UnaryOp):
return get_curlevel_fields(expr.operand)
elif isinstance(expr, IfThenElseExpr):
return get_curlevel_fields(expr.cond) + get_curlevel_fields(expr.expr1) + get_curlevel_fields(expr.expr2)
elif isinstance(expr, BinaryExpr):
return get_curlevel_fields(expr.lh) + get_curlevel_fields(expr.rh)
elif isinstance(expr, UnaryExpr):
return get_curlevel_fields(expr.operand)
else:
return []