|
| 1 | +import itertools |
| 2 | +import substrait.gen.proto.algebra_pb2 as stalg |
| 3 | +import substrait.gen.proto.type_pb2 as stp |
| 4 | +import substrait.gen.proto.extended_expression_pb2 as stee |
| 5 | +import substrait.gen.proto.extensions.extensions_pb2 as ste |
| 6 | +from substrait.function_registry import FunctionRegistry |
| 7 | +from substrait.utils import type_num_names, merge_extension_uris, merge_extension_declarations |
| 8 | +from substrait.type_inference import infer_extended_expression_schema |
| 9 | +from typing import Callable, Any, Union |
| 10 | + |
| 11 | +UnboundExpression = Callable[[stp.NamedStruct, FunctionRegistry], stee.ExtendedExpression] |
| 12 | + |
| 13 | +def literal(value: Any, type: stp.Type, alias: str = None) -> UnboundExpression: |
| 14 | + """Builds a resolver for ExtendedExpression containing a literal expression""" |
| 15 | + def resolve(base_schema: stp.NamedStruct, registry: FunctionRegistry) -> stee.ExtendedExpression: |
| 16 | + kind = type.WhichOneof('kind') |
| 17 | + |
| 18 | + if kind == "bool": |
| 19 | + literal = stalg.Expression.Literal(boolean=value, nullable=type.bool.nullability == stp.Type.NULLABILITY_NULLABLE) |
| 20 | + elif kind == "i8": |
| 21 | + literal = stalg.Expression.Literal(i8=value, nullable=type.i8.nullability == stp.Type.NULLABILITY_NULLABLE) |
| 22 | + elif kind == "i16": |
| 23 | + literal = stalg.Expression.Literal(i16=value, nullable=type.i16.nullability == stp.Type.NULLABILITY_NULLABLE) |
| 24 | + elif kind == "i32": |
| 25 | + literal = stalg.Expression.Literal(i32=value, nullable=type.i32.nullability == stp.Type.NULLABILITY_NULLABLE) |
| 26 | + elif kind == "i64": |
| 27 | + literal = stalg.Expression.Literal(i64=value, nullable=type.i64.nullability == stp.Type.NULLABILITY_NULLABLE) |
| 28 | + elif kind == "fp32": |
| 29 | + literal = stalg.Expression.Literal(fp32=value, nullable=type.fp32.nullability == stp.Type.NULLABILITY_NULLABLE) |
| 30 | + elif kind == "fp64": |
| 31 | + literal = stalg.Expression.Literal(fp64=value, nullable=type.fp64.nullability == stp.Type.NULLABILITY_NULLABLE) |
| 32 | + elif kind == "string": |
| 33 | + literal = stalg.Expression.Literal(string=value, nullable=type.string.nullability == stp.Type.NULLABILITY_NULLABLE) |
| 34 | + else: |
| 35 | + raise Exception(f"Unknown literal type - {type}") |
| 36 | + |
| 37 | + return stee.ExtendedExpression( |
| 38 | + referred_expr=[ |
| 39 | + stee.ExpressionReference( |
| 40 | + expression=stalg.Expression( |
| 41 | + literal=literal |
| 42 | + ), |
| 43 | + output_names=[alias if alias else f'literal_{kind}'], |
| 44 | + ) |
| 45 | + ], |
| 46 | + base_schema=base_schema, |
| 47 | + ) |
| 48 | + |
| 49 | + return resolve |
| 50 | + |
| 51 | +def column(field: Union[str, int]): |
| 52 | + """Builds a resolver for ExtendedExpression containing a FieldReference expression |
| 53 | + |
| 54 | + Accepts either an index or a field name of a desired field. |
| 55 | + """ |
| 56 | + def resolve(base_schema: stp.NamedStruct, registry: FunctionRegistry) -> stee.ExtendedExpression: |
| 57 | + if isinstance(field, str): |
| 58 | + column_index = list(base_schema.names).index(field) |
| 59 | + lengths = [type_num_names(t) for t in base_schema.struct.types] |
| 60 | + flat_indices = [0] + list(itertools.accumulate(lengths))[:-1] |
| 61 | + field_index = flat_indices.index(column_index) |
| 62 | + else: |
| 63 | + field_index = field |
| 64 | + |
| 65 | + names_start = flat_indices[field_index] |
| 66 | + names_end = ( |
| 67 | + flat_indices[field_index + 1] |
| 68 | + if len(flat_indices) > field_index + 1 |
| 69 | + else None |
| 70 | + ) |
| 71 | + |
| 72 | + return stee.ExtendedExpression( |
| 73 | + referred_expr=[ |
| 74 | + stee.ExpressionReference( |
| 75 | + expression=stalg.Expression( |
| 76 | + selection=stalg.Expression.FieldReference( |
| 77 | + root_reference=stalg.Expression.FieldReference.RootReference(), |
| 78 | + direct_reference=stalg.Expression.ReferenceSegment( |
| 79 | + struct_field=stalg.Expression.ReferenceSegment.StructField( |
| 80 | + field=field_index |
| 81 | + ) |
| 82 | + ), |
| 83 | + ) |
| 84 | + ), |
| 85 | + output_names=list(base_schema.names)[names_start:names_end], |
| 86 | + ) |
| 87 | + ], |
| 88 | + base_schema=base_schema, |
| 89 | + ) |
| 90 | + |
| 91 | + return resolve |
| 92 | + |
| 93 | +def scalar_function(uri: str, function: str, *expressions: UnboundExpression, alias: str = None): |
| 94 | + """Builds a resolver for ExtendedExpression containing a ScalarFunction expression""" |
| 95 | + def resolve(base_schema: stp.NamedStruct, registry: FunctionRegistry) -> stee.ExtendedExpression: |
| 96 | + bound_expressions: list[stee.ExtendedExpression] = [e(base_schema, registry) for e in expressions] |
| 97 | + |
| 98 | + expression_schemas = [infer_extended_expression_schema(b) for b in bound_expressions] |
| 99 | + |
| 100 | + signature = [typ for es in expression_schemas for typ in es.types] |
| 101 | + |
| 102 | + func = registry.lookup_function(uri, function, signature) |
| 103 | + |
| 104 | + if not func: |
| 105 | + raise Exception('') |
| 106 | + |
| 107 | + func_extension_uris = [ |
| 108 | + ste.SimpleExtensionURI( |
| 109 | + extension_uri_anchor=registry.lookup_uri(uri), |
| 110 | + uri=uri |
| 111 | + ) |
| 112 | + ] |
| 113 | + |
| 114 | + func_extensions = [ |
| 115 | + ste.SimpleExtensionDeclaration( |
| 116 | + extension_function=ste.SimpleExtensionDeclaration.ExtensionFunction( |
| 117 | + extension_uri_reference=registry.lookup_uri(uri), |
| 118 | + function_anchor=func[0].anchor, |
| 119 | + name=function |
| 120 | + ) |
| 121 | + ) |
| 122 | + ] |
| 123 | + |
| 124 | + extension_uris = merge_extension_uris( |
| 125 | + func_extension_uris, |
| 126 | + *[b.extension_uris for b in bound_expressions] |
| 127 | + ) |
| 128 | + |
| 129 | + extensions = merge_extension_declarations( |
| 130 | + func_extensions, |
| 131 | + *[b.extensions for b in bound_expressions] |
| 132 | + ) |
| 133 | + |
| 134 | + return stee.ExtendedExpression( |
| 135 | + referred_expr=[ |
| 136 | + stee.ExpressionReference( |
| 137 | + expression=stalg.Expression( |
| 138 | + scalar_function=stalg.Expression.ScalarFunction( |
| 139 | + function_reference=func[0].anchor, |
| 140 | + arguments=[ |
| 141 | + stalg.FunctionArgument( |
| 142 | + value=e.referred_expr[0].expression |
| 143 | + ) for e in bound_expressions |
| 144 | + ], |
| 145 | + output_type=func[1] |
| 146 | + ) |
| 147 | + ), |
| 148 | + output_names=[alias if alias else 'scalar_function'], |
| 149 | + ) |
| 150 | + ], |
| 151 | + base_schema=base_schema, |
| 152 | + extension_uris=extension_uris, |
| 153 | + extensions=extensions |
| 154 | + ) |
| 155 | + |
| 156 | + return resolve |
0 commit comments