forked from substrait-io/substrait-python
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfunction_registry.py
340 lines (291 loc) · 11.3 KB
/
function_registry.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
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
from substrait.gen.proto.parameterized_types_pb2 import ParameterizedType
from substrait.gen.proto.type_pb2 import Type
from importlib.resources import files as importlib_files
import itertools
from collections import defaultdict
from collections.abc import Mapping
from pathlib import Path
from typing import Any, Optional, Union
from .derivation_expression import evaluate
import yaml
import re
DEFAULT_URI_PREFIX = "https://github.com/substrait-io/substrait/blob/main/extensions"
# mapping from argument types to shortened signature names: https://substrait.io/extensions/#function-signature-compound-names
_normalized_key_names = {
"i8": "i8",
"i16": "i16",
"i32": "i32",
"i64": "i64",
"fp32": "fp32",
"fp64": "fp64",
"string": "str",
"binary": "vbin",
"boolean": "bool",
"timestamp": "ts",
"timestamp_tz": "tstz",
"date": "date",
"time": "time",
"interval_year": "iyear",
"interval_day": "iday",
"interval_compound": "icompound",
"uuid": "uuid",
"fixedchar": "fchar",
"varchar": "vchar",
"fixedbinary": "fbin",
"decimal": "dec",
"precision_time": "pt",
"precision_timestamp": "pts",
"precision_timestamp_tz": "ptstz",
"struct": "struct",
"list": "list",
"map": "map",
}
def normalize_substrait_type_names(typ: str) -> str:
# First strip nullability marker
typ = typ.strip("?").lower()
# Strip type specifiers
typ = typ.split("<")[0]
if typ.startswith("any"):
return "any"
elif typ.startswith("u!"):
return typ
else:
return _normalized_key_names[typ]
def to_integer_option(txt: str):
if txt.isnumeric():
return ParameterizedType.IntegerOption(literal=int(txt))
else:
return ParameterizedType.IntegerOption(
parameter=ParameterizedType.IntegerParameter(name=txt)
)
def to_parameterized_type(dtype: str):
if dtype.endswith("?"):
dtype = dtype[:-1]
nullability = Type.NULLABILITY_NULLABLE
else:
nullability = Type.NULLABILITY_REQUIRED
if dtype == "boolean":
return ParameterizedType(bool=Type.Boolean(nullability=nullability))
elif dtype == "i8":
return ParameterizedType(i8=Type.I8(nullability=nullability))
elif dtype == "i16":
return ParameterizedType(i16=Type.I16(nullability=nullability))
elif dtype == "i32":
return ParameterizedType(i32=Type.I32(nullability=nullability))
elif dtype == "i64":
return ParameterizedType(i64=Type.I64(nullability=nullability))
elif dtype == "fp32":
return ParameterizedType(fp32=Type.FP32(nullability=nullability))
elif dtype == "fp64":
return ParameterizedType(fp64=Type.FP64(nullability=nullability))
elif dtype == "timestamp":
return ParameterizedType(timestamp=Type.Timestamp(nullability=nullability))
elif dtype == "timestamp_tz":
return ParameterizedType(timestamp_tz=Type.TimestampTZ(nullability=nullability))
elif dtype == "date":
return ParameterizedType(date=Type.Date(nullability=nullability))
elif dtype == "time":
return ParameterizedType(time=Type.Time(nullability=nullability))
elif dtype == "interval_year":
return ParameterizedType(
interval_year=Type.IntervalYear(nullability=nullability)
)
elif dtype.startswith("decimal") or dtype.startswith("DECIMAL"):
(_, precision, scale, _) = re.split(r"\W+", dtype)
return ParameterizedType(
decimal=ParameterizedType.ParameterizedDecimal(
scale=to_integer_option(scale),
precision=to_integer_option(precision),
nullability=nullability,
)
)
elif dtype.startswith("varchar"):
(_, length, _) = re.split(r"\W+", dtype)
return ParameterizedType(
varchar=ParameterizedType.ParameterizedVarChar(
length=to_integer_option(length), nullability=nullability
)
)
elif dtype.startswith("precision_timestamp"):
(_, precision, _) = re.split(r"\W+", dtype)
return ParameterizedType(
precision_timestamp=ParameterizedType.ParameterizedPrecisionTimestamp(
precision=to_integer_option(precision), nullability=nullability
)
)
elif dtype.startswith("precision_timestamp_tz"):
(_, precision, _) = re.split(r"\W+", dtype)
return ParameterizedType(
precision_timestamp_tz=ParameterizedType.ParameterizedPrecisionTimestampTZ(
precision=to_integer_option(precision), nullability=nullability
)
)
elif dtype.startswith("fixedchar"):
(_, length, _) = re.split(r"\W+", dtype)
return ParameterizedType(
fixed_char=ParameterizedType.ParameterizedFixedChar(
length=to_integer_option(length), nullability=nullability
)
)
elif dtype == "string":
return ParameterizedType(string=Type.String(nullability=nullability))
elif dtype.startswith("list"):
inner_dtype = dtype[5:-1]
return ParameterizedType(
list=ParameterizedType.ParameterizedList(
type=to_parameterized_type(inner_dtype), nullability=nullability
)
)
elif dtype.startswith("interval_day"):
(_, precision, _) = re.split(r"\W+", dtype)
return ParameterizedType(
interval_day=ParameterizedType.ParameterizedIntervalDay(
precision=to_integer_option(precision), nullability=nullability
)
)
elif dtype.startswith("any"):
return ParameterizedType(
type_parameter=ParameterizedType.TypeParameter(name=dtype)
)
elif dtype.startswith("u!") or dtype == "geometry":
return ParameterizedType(
user_defined=ParameterizedType.ParameterizedUserDefined(
nullability=nullability
)
)
else:
raise Exception(f"Unknown type - {dtype}")
def violates_integer_option(
actual: int, option: ParameterizedType.IntegerOption, parameters: dict
):
integer_type = option.WhichOneof("integer_type")
if integer_type == "literal" and actual != option.literal:
return True
else:
parameter_name = option.parameter.name
if parameter_name in parameters and parameters[parameter_name] != actual:
return True
else:
parameters[parameter_name] = actual
return False
def covers(dtype: Type, parameterized_type: ParameterizedType, parameters: dict):
expected_kind = parameterized_type.WhichOneof("kind")
if expected_kind == "type_parameter":
parameter_name = parameterized_type.type_parameter.name
if parameter_name == "any":
return True
else:
if parameter_name in parameters and parameters[
parameter_name
].SerializeToString(deterministic=True) != dtype.SerializeToString(
deterministic=True
):
return False
else:
parameters[parameter_name] = dtype
return True
kind = dtype.WhichOneof("kind")
if kind != expected_kind:
return False
if kind == "decimal":
if violates_integer_option(
dtype.decimal.scale, parameterized_type.decimal.scale, parameters
) or violates_integer_option(
dtype.decimal.precision, parameterized_type.decimal.precision, parameters
):
return False
# TODO handle all types
return True
class FunctionEntry:
def __init__(
self, uri: str, name: str, impl: Mapping[str, Any], anchor: int
) -> None:
self.name = name
self.normalized_inputs: list = []
self.uri: str = uri
self.anchor = anchor
self.arguments = []
self.rtn = impl["return"]
self.nullability = impl.get("nullability", False)
self.variadic = impl.get("variadic", False)
if input_args := impl.get("args", []):
for val in input_args:
if typ := val.get("value"):
self.arguments.append(to_parameterized_type(typ))
self.normalized_inputs.append(normalize_substrait_type_names(typ))
elif arg_name := val.get("name", None):
self.arguments.append(val.get("options"))
self.normalized_inputs.append("req")
def __repr__(self) -> str:
return f"{self.name}:{'_'.join(self.normalized_inputs)}"
def satisfies_signature(self, signature: tuple) -> Optional[str]:
if self.variadic:
min_args_allowed = self.variadic.get("min", 0)
if len(signature) < min_args_allowed:
return None
inputs = [self.arguments[0]] * len(signature)
else:
inputs = self.arguments
if len(inputs) != len(signature):
return None
zipped_args = list(zip(inputs, signature))
parameters = {}
for x, y in zipped_args:
if type(y) == str:
if y not in x:
return None
else:
if not covers(y, x, parameters):
return None
return evaluate(self.rtn, parameters)
class FunctionRegistry:
def __init__(self) -> None:
self._function_mapping: dict = defaultdict(dict)
self.id_generator = itertools.count(1)
self.uri_aliases = {}
for fpath in importlib_files("substrait.extensions").glob( # type: ignore
"functions*.yaml"
):
uri = f"{DEFAULT_URI_PREFIX}/{fpath.name}"
self.uri_aliases[fpath.name] = uri
self.register_extension_yaml(fpath, uri)
def register_extension_yaml(
self,
fname: Union[str, Path],
uri: str,
) -> None:
fname = Path(fname)
with open(fname) as f: # type: ignore
extension_definitions = yaml.safe_load(f)
self.register_extension_dict(extension_definitions, uri)
def register_extension_dict(self, definitions: dict, uri: str) -> None:
for named_functions in definitions.values():
for function in named_functions:
for impl in function.get("impls", []):
func = FunctionEntry(
uri, function["name"], impl, next(self.id_generator)
)
if (
func.uri in self._function_mapping
and function["name"] in self._function_mapping[func.uri]
):
self._function_mapping[func.uri][function["name"]].append(func)
else:
self._function_mapping[func.uri][function["name"]] = [func]
# TODO add an optional return type check
def lookup_function(
self, uri: str, function_name: str, signature: tuple
) -> Optional[tuple[FunctionEntry, Type]]:
uri = self.uri_aliases.get(uri, uri)
if (
uri not in self._function_mapping
or function_name not in self._function_mapping[uri]
):
return None
functions = self._function_mapping[uri][function_name]
for f in functions:
assert isinstance(f, FunctionEntry)
rtn = f.satisfies_signature(signature)
if rtn is not None:
return (f, rtn)
return None