36
36
37
37
38
38
class QueryCompilerCasterCalculator :
39
-
39
+ """
40
+ Calculate which QueryCompiler should be used for an operation.
41
+
42
+ Given a set of QueryCompilers; containing various data, determine
43
+ which query compiler everything should be cast to which minimizes
44
+ the cost of casting, or coercion. Use the aggregate sum of coercion
45
+ to determine overall cost.
46
+ """
47
+
40
48
def __init__ (self ):
41
49
self ._caster_costing_map = {}
42
50
self ._data_cls_map = {}
@@ -45,6 +53,13 @@ def __init__(self):
45
53
self ._result_type = None
46
54
47
55
def add_query_compiler (self , query_compiler ):
56
+ """
57
+ Add a query compiler to be considered for casting.
58
+
59
+ Parameters
60
+ ----------
61
+ query_compiler : QueryCompiler
62
+ """
48
63
if isinstance (query_compiler , type ):
49
64
# class
50
65
qc_type = query_compiler
@@ -56,6 +71,14 @@ def add_query_compiler(self, query_compiler):
56
71
self ._qc_cls_list .append (qc_type )
57
72
58
73
def calculate (self ):
74
+ """
75
+ Calculate which query compiler we should cast to.
76
+
77
+ Returns
78
+ -------
79
+ type
80
+ QueryCompiler class which should be used for the operation.
81
+ """
59
82
if self ._result_type is not None :
60
83
return self ._result_type
61
84
if len (self ._qc_cls_list ) == 1 :
@@ -79,6 +102,14 @@ def calculate(self):
79
102
return self ._result_type
80
103
81
104
def _add_cost_data (self , costs : dict ):
105
+ """
106
+ Add the cost data to the calculator.
107
+
108
+ Parameters
109
+ ----------
110
+ costs : dict
111
+ Dictionary of query compiler classes to costs.
112
+ """
82
113
for k , v in costs .items ():
83
114
# filter out any extranious query compilers not in this operation
84
115
if k in self ._qc_cls_list :
@@ -91,6 +122,14 @@ def _add_cost_data(self, costs: dict):
91
122
)
92
123
93
124
def result_data_frame (self ):
125
+ """
126
+ Return the data frame associated with the calculated query compiler.
127
+
128
+ Returns
129
+ -------
130
+ DataFrame object
131
+ DataFrame object associated with the preferred query compiler.
132
+ """
94
133
qc_type = self .calculate ()
95
134
return self ._data_cls_map [qc_type ]
96
135
@@ -119,38 +158,37 @@ def __init_subclass__(
119
158
apply_argument_cast (cls )
120
159
121
160
122
- def visit_nested_args (arguments , current_qc : BaseQueryCompiler , fn : callable ):
161
+ def visit_nested_args (arguments , fn : callable ):
123
162
"""
124
- Cast all arguments in nested fashion to current query compiler .
125
-
163
+ Visit each argument recursively, calling fn on each one .
164
+
126
165
Parameters
127
166
----------
128
167
arguments : tuple or dict
129
- current_qc : BaseQueryCompiler
168
+ fn : Callable to apply to matching arguments
130
169
131
170
Returns
132
171
-------
133
172
tuple or dict
134
173
Returns args and kwargs with all query compilers casted to current_qc.
135
174
"""
136
-
137
175
imutable_types = (FrozenList , tuple )
138
176
if isinstance (arguments , imutable_types ):
139
177
args_type = type (arguments )
140
178
arguments = list (arguments )
141
- arguments = visit_nested_args (arguments , current_qc , fn )
179
+ arguments = visit_nested_args (arguments , fn )
142
180
143
181
return args_type (arguments )
144
182
if isinstance (arguments , list ):
145
183
for i in range (len (arguments )):
146
184
if isinstance (arguments [i ], (list , dict )):
147
- visit_nested_args (arguments [i ], current_qc , fn )
185
+ visit_nested_args (arguments [i ], fn )
148
186
else :
149
187
arguments [i ] = fn (arguments [i ])
150
188
elif isinstance (arguments , dict ):
151
189
for key in arguments :
152
190
if isinstance (arguments [key ], (list , dict )):
153
- visit_nested_args (arguments [key ], current_qc , fn )
191
+ visit_nested_args (arguments [key ], fn )
154
192
else :
155
193
arguments [key ] = fn (arguments [key ])
156
194
return arguments
@@ -239,11 +277,11 @@ def cast_to_qc(arg):
239
277
return result
240
278
241
279
if isinstance (current_qc , BaseQueryCompiler ):
242
- visit_nested_args (kwargs , current_qc , register_query_compilers )
243
- visit_nested_args (args , current_qc , register_query_compilers )
280
+ visit_nested_args (kwargs , register_query_compilers )
281
+ visit_nested_args (args , register_query_compilers )
244
282
245
- args = visit_nested_args (args , current_qc , cast_to_qc )
246
- kwargs = visit_nested_args (kwargs , current_qc , cast_to_qc )
283
+ args = visit_nested_args (args , cast_to_qc )
284
+ kwargs = visit_nested_args (kwargs , cast_to_qc )
247
285
248
286
qc = calculator .calculate ()
249
287
0 commit comments