-
Notifications
You must be signed in to change notification settings - Fork 59
Expand file tree
/
Copy pathast.py
More file actions
386 lines (299 loc) · 12.3 KB
/
ast.py
File metadata and controls
386 lines (299 loc) · 12.3 KB
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
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
# Copyright 2022 The FeatHub Authors
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# https://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
import json
from abc import ABC, abstractmethod
from typing import List, Dict, Any, Optional
from feathub.common.exceptions import FeathubException, FeathubExpressionException
from feathub.common.types import (
DType,
Int32,
Int64,
Float32,
Float64,
Bool,
get_type_by_name,
from_python_type,
MapType,
VectorType,
)
from feathub.dsl.built_in_func import get_builtin_func_def
TYPE_PRECISION_RANK: List[DType] = [Float64, Float32, Int64, Int32]
def _get_higher_precision_type(*dtype: DType) -> Optional[DType]:
res_type = dtype[0]
for t in dtype[1:]:
if res_type not in TYPE_PRECISION_RANK or t not in TYPE_PRECISION_RANK:
raise FeathubExpressionException(f"Illegal mixing of types: {dtype}")
if TYPE_PRECISION_RANK.index(res_type) > TYPE_PRECISION_RANK.index(t):
res_type = t
return res_type
class ExprAST(ABC):
def __init__(self, node_type: str) -> None:
self.node_type = node_type
@abstractmethod
def to_json(self) -> Dict:
"""
Returns a json-formatted object representing this node.
"""
pass
@abstractmethod
def eval_dtype(self, variable_types: Dict[str, DType]) -> DType:
pass
def __str__(self) -> str:
return json.dumps(self.to_json(), indent=2, sort_keys=True)
class AbstractUnaryOp(ExprAST, ABC):
def __init__(self, node_type: str, child: ExprAST):
super().__init__(node_type)
self.child = child
class AbstractBinaryOp(ExprAST, ABC):
def __init__(self, node_type: str, left_child: ExprAST, right_child: ExprAST):
super().__init__(node_type)
self.left_child = left_child
self.right_child = right_child
class BinaryOp(AbstractBinaryOp):
def __init__(self, op_type: str, left_child: ExprAST, right_child: ExprAST) -> None:
super().__init__(
node_type="BinaryOp", left_child=left_child, right_child=right_child
)
self.op_type = op_type
def eval_dtype(self, variable_types: Dict[str, DType]) -> DType:
left_type = self.left_child.eval_dtype(variable_types)
right_type = self.right_child.eval_dtype(variable_types)
return _get_higher_precision_type(left_type, right_type)
def to_json(self) -> Dict:
return {
"node_type": "BinaryOp",
"op_type": self.op_type,
"left_child": self.left_child.to_json(),
"right_child": self.right_child.to_json(),
}
class CompareOp(AbstractBinaryOp):
def __init__(self, op_type: str, left_child: ExprAST, right_child: ExprAST) -> None:
super().__init__(
node_type="CompareOp", left_child=left_child, right_child=right_child
)
self.op_type = op_type
def eval_dtype(self, variable_types: Dict[str, DType]) -> DType:
return Bool
def to_json(self) -> Dict:
return {
"node_type": "CompareOp",
"op_type": self.op_type,
"left_child": self.left_child.to_json(),
"right_child": self.right_child.to_json(),
}
class UminusOp(AbstractUnaryOp):
def __init__(self, child: ExprAST) -> None:
super().__init__(node_type="UminusOp", child=child)
def eval_dtype(self, variable_types: Dict[str, DType]) -> DType:
return self.child.eval_dtype(variable_types)
def to_json(self) -> Dict:
return {
"node_type": "UminusOp",
"child": self.child,
}
class LogicalOp(AbstractBinaryOp):
def __init__(self, op_type: str, left_child: ExprAST, right_child: ExprAST) -> None:
super().__init__(
node_type="LogicalOp", left_child=left_child, right_child=right_child
)
self.op_type = op_type.upper()
def eval_dtype(self, variable_types: Dict[str, DType]) -> DType:
return Bool
def to_json(self) -> Dict:
return {
"node_type": "LogicalOp",
"op_type": self.op_type,
"left_child": self.left_child.to_json(),
"right_child": self.right_child.to_json(),
}
class CastOp(AbstractUnaryOp):
def __init__(
self, child: ExprAST, type_name: str, exception_on_failure: bool = True
):
super().__init__(node_type="CastOp", child=child)
self.type_name = type_name
self.exception_on_failure = exception_on_failure
def eval_dtype(self, variable_types: Dict[str, DType]) -> DType:
return get_type_by_name(self.type_name)
def to_json(self) -> Dict:
return {
"node_type": "CastOp",
"child": self.child.to_json(),
"type_name": self.type_name,
"exception_on_failure": self.exception_on_failure,
}
class ValueNode(ExprAST):
def __init__(self, value: Any) -> None:
super().__init__(node_type="ValueNode")
self.value = value
def eval_dtype(self, variable_types: Dict[str, DType]) -> DType:
return from_python_type(type(self.value))
def to_json(self) -> Dict:
return {
"node_type": "ValueNode",
"value": self.value,
}
class VariableNode(ExprAST):
def __init__(self, var_name: str) -> None:
super().__init__(node_type="VariableNode")
self.var_name = var_name
def eval_dtype(self, variable_types: Dict[str, DType]) -> DType:
if self.var_name not in variable_types:
raise RuntimeError(f"Type of variable {self.var_name} is not given.")
return variable_types.get(self.var_name)
def to_json(self) -> Dict:
return {
"node_type": "VariableNode",
"var_name": self.var_name,
}
class ArgListNode(ExprAST):
def __init__(self, values: List[ExprAST]) -> None:
super().__init__(node_type="ArgListNode")
self.values = values
def eval_dtype(self, variable_types: Dict[str, DType]) -> DType:
raise NotImplementedError("This method should not be called.")
def to_json(self) -> Dict:
return {
"node_type": "ArgListNode",
"values": [value.to_json() for value in self.values],
}
class FuncCallOp(ExprAST):
def __init__(self, func_name: str, args: ArgListNode) -> None:
super().__init__(node_type="FuncCallOp")
self.func_name = func_name.upper()
self.args = args
def eval_dtype(self, variable_types: Dict[str, DType]) -> DType:
arg_types = [arg.eval_dtype(variable_types) for arg in self.args.values]
builtin_func_def = get_builtin_func_def(self.func_name)
return builtin_func_def.get_result_type(arg_types)
def to_json(self) -> Dict:
return {
"node_type": "FuncCallOp",
"func_name": self.func_name,
"args": self.args.to_json(),
}
class GroupNode(AbstractUnaryOp):
def __init__(self, child: ExprAST) -> None:
super().__init__("GroupNode", child=child)
def eval_dtype(self, variable_types: Dict[str, DType]) -> DType:
return self.child.eval_dtype(variable_types)
def to_json(self) -> Dict:
return {"node_type": "GroupNode", "child": self.child}
class NullNode(ExprAST):
def __init__(self) -> None:
super().__init__(node_type="NullNode")
def eval_dtype(self, variable_types: Dict[str, DType]) -> DType:
raise NotImplementedError("This method should not be called.")
def to_json(self) -> Dict:
return {"node_type": "NullNode"}
class IsOp(AbstractBinaryOp):
def __init__(
self, left_child: ExprAST, right_child: ExprAST, is_not: bool = False
) -> None:
super().__init__(
node_type="IsOp", left_child=left_child, right_child=right_child
)
if not isinstance(right_child, NullNode):
raise FeathubException("IS/IS NOT can only be concatenated with NULL.")
self.is_not = is_not
def eval_dtype(self, variable_types: Dict[str, DType]) -> DType:
return Bool
def to_json(self) -> Dict:
return {
"node_type": "IsOp",
"left_child": self.left_child.to_json(),
"is_not": self.is_not,
}
class BracketOp(AbstractBinaryOp):
def __init__(self, left_child: ExprAST, right_child: ExprAST) -> None:
super().__init__(
node_type="BracketOp", left_child=left_child, right_child=right_child
)
def eval_dtype(self, variable_types: Dict[str, DType]) -> DType:
left_child_type = self.left_child.eval_dtype(variable_types)
right_child_type = self.right_child.eval_dtype(variable_types)
if isinstance(left_child_type, MapType):
if right_child_type != left_child_type.key_dtype:
raise FeathubExpressionException(
f"Map key type {left_child_type.key_dtype} does not match "
f"with expected {right_child_type}."
)
return left_child_type.value_dtype
# TODO: Support parsing expression based on data types.
if isinstance(left_child_type, VectorType):
# Suppose parsing an expression "a[b]" into Flink SQL. If "a" is a map, it
# should be parsed into "a[b]". If "a" is a list, it should be parsed into
# "a[b + 1]". The parse result depends on the data type, but Feathub
# has not uniform AbstractAstEvaluator#eval and ExprAST#eval_dtype, so
# the parsing process cannot get type information yet.
raise FeathubException(
"Getting element from list by index is not supported yet."
)
raise FeathubExpressionException(f"{right_child_type} is not subscriptable.")
def to_json(self) -> Dict:
return {
"node_type": "BracketOp",
"left_child": self.left_child.to_json(),
"right_child": self.right_child.to_json(),
}
class CaseOp(ExprAST):
def __init__(
self,
conditions: List[ExprAST],
results: List[ExprAST],
default: ExprAST,
) -> None:
super().__init__(node_type="CaseOp")
if len(conditions) != len(results):
raise FeathubException(
"The number of conditions and results does not match."
)
if not conditions:
raise FeathubException("Cannot create CaseOp without cases.")
self.conditions = conditions
self.results = results
self.default = default
def eval_dtype(self, variable_types: Dict[str, DType]) -> DType:
result_types = set(
[result_expr.eval_dtype(variable_types) for result_expr in self.results]
)
if not isinstance(self.default, NullNode):
result_types.add(self.default.eval_dtype(variable_types))
if len(result_types) == 1:
return result_types.pop()
return _get_higher_precision_type(*result_types)
def to_json(self) -> Dict:
return {
"node_type": "CaseOp",
"conditions": [x.to_json() for x in self.conditions],
"results": [x.to_json() for x in self.results],
"default": self.default.to_json(),
}
@staticmethod
def new_builder() -> "CaseOp.Builder":
return CaseOp.Builder()
class Builder:
def __init__(self) -> None:
self.conditions: List[ExprAST] = []
self.results: List[ExprAST] = []
self.default_value: ExprAST = NullNode()
def case(self, condition: ExprAST, result: ExprAST) -> "CaseOp.Builder":
self.conditions.append(condition)
self.results.append(result)
return self
def default(self, result: ExprAST) -> "CaseOp.Builder":
self.default_value = result
return self
def build(self) -> "CaseOp":
return CaseOp(self.conditions, self.results, self.default_value)