1
1
class Operation :
2
+ """
3
+ Operation, single operation of execution plan.
4
+ """
2
5
def __init__ (self , name , args = None ):
6
+ """
7
+ Create a new operation.
8
+
9
+ Args:
10
+ name: string that represents the name of the operation
11
+ args: coperation arguments
12
+ """
3
13
self .name = name
4
14
self .args = args
5
15
self .children = []
@@ -26,23 +36,41 @@ def __eq__(self, o: object) -> bool:
26
36
27
37
def __str__ (self ) -> str :
28
38
args_str = "" if self .args is None else f" | { self .args } "
29
- if len (self .children ) == 0 :
30
- return f"{ self .name } { args_str } "
31
- else :
32
- children = "\n " .join ([" " + line for op in self .children for line in str (op ).splitlines ()])
33
- return f"{ self .name } { args_str } \n { children } "
39
+ return f"{ self .name } { args_str } "
34
40
35
41
36
42
class ExecutionPlan :
43
+ """
44
+ ExecutionPlan, collection of operations.
45
+ """
37
46
def __init__ (self , plan ):
47
+ """
48
+ Create a new execution plan.
49
+
50
+ Args:
51
+ plan: array of strings that represents the collection operations
52
+ """
38
53
if not isinstance (plan , list ):
39
54
raise Exception ("plan must be array" )
40
55
41
56
self .plan = plan
42
57
self .structured_plan = self ._operation_tree ()
43
58
59
+ def _operation_traverse (self , op , op_f , aggregate_f , combine_f ):
60
+ child_res = op_f (op )
61
+ if len (op .children ) == 0 :
62
+ return child_res
63
+ else :
64
+ children = [self ._operation_traverse (op , op_f , aggregate_f , combine_f ) for op in op .children ]
65
+ return combine_f (child_res , aggregate_f (children ))
66
+
44
67
def __str__ (self ) -> str :
45
- return "\n " .join (self .plan )
68
+ def aggraget_str (str_ops ):
69
+ return "\n " .join ([" " + line for str_op in str_ops for line in str_op .splitlines ()])
70
+
71
+ def combine_str (x , y ):
72
+ return f"{ x } \n { y } "
73
+ return self ._operation_traverse (self .structured_plan , str , aggraget_str , combine_str )
46
74
47
75
def _operation_tree (self ):
48
76
i = 0
0 commit comments