-
Notifications
You must be signed in to change notification settings - Fork 9
feat: add registry for extension functions #68
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
1efb3f0
feat: add registry for extension functions
tokoko 050ca87
fix: remove global id_generator in function registry
tokoko e47abad
fix: improve signature name handling
tokoko 58bb259
fix: make default uri prefix a constant
tokoko 9770bb4
feat: add scalar function nullability handling
tokoko 00f5632
feat: add load_default_extensions arg to FunctionRegistry constructor
tokoko 739b7cf
feat: reimplement covers with antlr
tokoko File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,287 @@ | ||
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, _evaluate, _parse | ||
|
||
import yaml | ||
|
||
|
||
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: | ||
# Strip type specifiers | ||
typ = typ.split("<")[0] | ||
# First strip nullability marker | ||
typ = typ.strip("?").lower() | ||
|
||
if typ.startswith("any"): | ||
return "any" | ||
elif typ.startswith("u!"): | ||
return typ | ||
elif typ in _normalized_key_names: | ||
return _normalized_key_names[typ] | ||
else: | ||
raise Exception(f"Unrecognized substrait type {typ}") | ||
|
||
|
||
def violates_integer_option(actual: int, option, parameters: dict): | ||
if isinstance(option, SubstraitTypeParser.NumericLiteralContext): | ||
return actual != int(str(option.Number())) | ||
elif isinstance(option, SubstraitTypeParser.NumericParameterNameContext): | ||
parameter_name = str(option.Identifier()) | ||
if parameter_name in parameters and parameters[parameter_name] != actual: | ||
return True | ||
else: | ||
parameters[parameter_name] = actual | ||
else: | ||
raise Exception( | ||
f"Input should be either NumericLiteralContext or NumericParameterNameContext, got {type(option)} instead" | ||
) | ||
|
||
return False | ||
|
||
|
||
from substrait.gen.antlr.SubstraitTypeParser import SubstraitTypeParser | ||
|
||
|
||
def types_equal(type1: Type, type2: Type, check_nullability=False): | ||
if check_nullability: | ||
return type1 == type2 | ||
else: | ||
x, y = Type(), Type() | ||
x.CopyFrom(type1) | ||
y.CopyFrom(type2) | ||
x.__getattribute__( | ||
x.WhichOneof("kind") | ||
).nullability = Type.Nullability.NULLABILITY_UNSPECIFIED | ||
y.__getattribute__( | ||
y.WhichOneof("kind") | ||
).nullability = Type.Nullability.NULLABILITY_UNSPECIFIED | ||
return x == y | ||
|
||
|
||
def covers( | ||
covered: Type, | ||
covering: SubstraitTypeParser.TypeLiteralContext, | ||
parameters: dict, | ||
check_nullability=False, | ||
): | ||
if isinstance(covering, SubstraitTypeParser.TypeParamContext): | ||
parameter_name = str(covering.Identifier()) | ||
|
||
if parameter_name in parameters: | ||
covering = parameters[parameter_name] | ||
|
||
return types_equal(covering, covered, check_nullability) | ||
else: | ||
parameters[parameter_name] = covered | ||
return True | ||
|
||
covering = covering.type_() | ||
scalar_type = covering.scalarType() | ||
if scalar_type: | ||
covering = _evaluate(covering, {}) | ||
return types_equal(covering, covered, check_nullability) | ||
|
||
parameterized_type = covering.parameterizedType() | ||
if parameterized_type: | ||
if isinstance(parameterized_type, SubstraitTypeParser.DecimalContext): | ||
if covered.WhichOneof("kind") != "decimal": | ||
return False | ||
|
||
nullability = ( | ||
Type.NULLABILITY_NULLABLE | ||
if parameterized_type.isnull | ||
else Type.NULLABILITY_REQUIRED | ||
) | ||
|
||
if ( | ||
check_nullability | ||
and nullability | ||
!= covered.__getattribute__(covered.WhichOneof("kind")).nullability | ||
): | ||
return False | ||
|
||
return not ( | ||
violates_integer_option( | ||
covered.decimal.scale, parameterized_type.scale, parameters | ||
) | ||
or violates_integer_option( | ||
covered.decimal.precision, parameterized_type.precision, parameters | ||
) | ||
) | ||
else: | ||
raise Exception(f"Unhandled type {type(parameterized_type)}") | ||
|
||
any_type = covering.anyType() | ||
if any_type: | ||
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", "MIRROR") | ||
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(_parse(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, check_nullability=self.nullability == "DISCRETE" | ||
): | ||
return None | ||
|
||
output_type = evaluate(self.rtn, parameters) | ||
|
||
if self.nullability == "MIRROR": | ||
sig_contains_nullable = any( | ||
[ | ||
p.__getattribute__(p.WhichOneof("kind")).nullability | ||
== Type.NULLABILITY_NULLABLE | ||
for p in signature | ||
if type(p) == Type | ||
] | ||
) | ||
output_type.__getattribute__(output_type.WhichOneof("kind")).nullability = ( | ||
Type.NULLABILITY_NULLABLE | ||
if sig_contains_nullable | ||
else Type.NULLABILITY_REQUIRED | ||
) | ||
|
||
return output_type | ||
|
||
|
||
class FunctionRegistry: | ||
def __init__(self, load_default_extensions=True) -> None: | ||
self._function_mapping: dict = defaultdict(dict) | ||
self._id_generator = itertools.count(1) | ||
|
||
self._uri_aliases = {} | ||
|
||
if load_default_extensions: | ||
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 |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
What happens if the short name is used instead? Consider throwing a specific exception here so it's more clear what an end user should do.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Is using short names in extension yamls legal? I changed dict search to throw a specific exception.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
A quick glance of functions_arithmetic.yaml contains a lot of i8 and i16 arguments. If you put together a test which verifies that you pass all of the core YAML extensions you should be good to go.
My general practice when implementing a spec is to be exact as possible on output and lenient on input. But since we have the nearly full test set that should be enough.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
instantiating the registry loads all extensions by default, so they are all implicitly being tested. I'll work on more explicit tests, as well, though