|
2 | 2 | import substrait.gen.proto.algebra_pb2 as stalg |
3 | 3 | import substrait.gen.proto.type_pb2 as stp |
4 | 4 | import substrait.gen.proto.extended_expression_pb2 as stee |
5 | | -from substrait.utils import type_num_names |
| 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 |
6 | 10 |
|
| 11 | +UnboundExpression = Callable[[stp.NamedStruct, FunctionRegistry], stee.ExtendedExpression] |
| 12 | + |
| 13 | +def literal(value: Any, type: stp.Type, alias: str = None) -> UnboundExpression: |
| 14 | + def resolve(base_schema: stp.NamedStruct, registry: FunctionRegistry) -> stee.ExtendedExpression: |
| 15 | + kind = type.WhichOneof('kind') |
| 16 | + |
| 17 | + if kind == "bool": |
| 18 | + literal = stalg.Expression.Literal(boolean=value, nullable=type.bool.nullability == stp.Type.NULLABILITY_NULLABLE) |
| 19 | + elif kind == "i8": |
| 20 | + literal = stalg.Expression.Literal(i8=value, nullable=type.i8.nullability == stp.Type.NULLABILITY_NULLABLE) |
| 21 | + elif kind == "i16": |
| 22 | + literal = stalg.Expression.Literal(i16=value, nullable=type.i16.nullability == stp.Type.NULLABILITY_NULLABLE) |
| 23 | + elif kind == "i32": |
| 24 | + literal = stalg.Expression.Literal(i32=value, nullable=type.i32.nullability == stp.Type.NULLABILITY_NULLABLE) |
| 25 | + elif kind == "i64": |
| 26 | + literal = stalg.Expression.Literal(i64=value, nullable=type.i64.nullability == stp.Type.NULLABILITY_NULLABLE) |
| 27 | + elif kind == "fp32": |
| 28 | + literal = stalg.Expression.Literal(fp32=value, nullable=type.fp32.nullability == stp.Type.NULLABILITY_NULLABLE) |
| 29 | + elif kind == "fp64": |
| 30 | + literal = stalg.Expression.Literal(fp64=value, nullable=type.fp64.nullability == stp.Type.NULLABILITY_NULLABLE) |
| 31 | + elif kind == "string": |
| 32 | + literal = stalg.Expression.Literal(string=value, nullable=type.string.nullability == stp.Type.NULLABILITY_NULLABLE) |
| 33 | + else: |
| 34 | + raise Exception(f"Unknown literal type - {type}") |
| 35 | + |
| 36 | + return stee.ExtendedExpression( |
| 37 | + referred_expr=[ |
| 38 | + stee.ExpressionReference( |
| 39 | + expression=stalg.Expression( |
| 40 | + literal=literal |
| 41 | + ), |
| 42 | + output_names=[alias if alias else f'literal_{kind}'], |
| 43 | + ) |
| 44 | + ], |
| 45 | + base_schema=base_schema, |
| 46 | + ) |
| 47 | + |
| 48 | + return resolve |
7 | 49 |
|
8 | 50 | def column(name: str): |
9 | | - def resolve(base_schema: stp.NamedStruct) -> stee.ExtendedExpression: |
| 51 | + def resolve(base_schema: stp.NamedStruct, registry: FunctionRegistry) -> stee.ExtendedExpression: |
10 | 52 | column_index = list(base_schema.names).index(name) |
11 | 53 | lengths = [type_num_names(t) for t in base_schema.struct.types] |
12 | 54 | flat_indices = [0] + list(itertools.accumulate(lengths))[:-1] |
@@ -39,3 +81,67 @@ def resolve(base_schema: stp.NamedStruct) -> stee.ExtendedExpression: |
39 | 81 | ) |
40 | 82 |
|
41 | 83 | return resolve |
| 84 | + |
| 85 | +def scalar_function(uri: str, function: str, *expressions: UnboundExpression, alias: str = None): |
| 86 | + def resolve(base_schema: stp.NamedStruct, registry: FunctionRegistry) -> stee.ExtendedExpression: |
| 87 | + bound_expressions: list[stee.ExtendedExpression] = [e(base_schema, registry) for e in expressions] |
| 88 | + |
| 89 | + expression_schemas = [infer_extended_expression_schema(b) for b in bound_expressions] |
| 90 | + |
| 91 | + signature = [typ for es in expression_schemas for typ in es.types] |
| 92 | + |
| 93 | + func = registry.lookup_function(uri, function, signature) |
| 94 | + |
| 95 | + if not func: |
| 96 | + raise Exception('') |
| 97 | + |
| 98 | + func_extension_uris = [ |
| 99 | + ste.SimpleExtensionURI( |
| 100 | + extension_uri_anchor=registry.lookup_uri(uri), |
| 101 | + uri=uri |
| 102 | + ) |
| 103 | + ] |
| 104 | + |
| 105 | + func_extensions = [ |
| 106 | + ste.SimpleExtensionDeclaration( |
| 107 | + extension_function=ste.SimpleExtensionDeclaration.ExtensionFunction( |
| 108 | + extension_uri_reference=registry.lookup_uri(uri), |
| 109 | + function_anchor=func[0].anchor, |
| 110 | + name=function |
| 111 | + ) |
| 112 | + ) |
| 113 | + ] |
| 114 | + |
| 115 | + extension_uris = merge_extension_uris( |
| 116 | + func_extension_uris, |
| 117 | + *[b.extension_uris for b in bound_expressions] |
| 118 | + ) |
| 119 | + |
| 120 | + extensions = merge_extension_declarations( |
| 121 | + func_extensions, |
| 122 | + *[b.extensions for b in bound_expressions] |
| 123 | + ) |
| 124 | + |
| 125 | + return stee.ExtendedExpression( |
| 126 | + referred_expr=[ |
| 127 | + stee.ExpressionReference( |
| 128 | + expression=stalg.Expression( |
| 129 | + scalar_function=stalg.Expression.ScalarFunction( |
| 130 | + function_reference=func[0].anchor, |
| 131 | + arguments=[ |
| 132 | + stalg.FunctionArgument( |
| 133 | + value=e.referred_expr[0].expression |
| 134 | + ) for e in bound_expressions |
| 135 | + ], |
| 136 | + output_type=func[1] |
| 137 | + ) |
| 138 | + ), |
| 139 | + output_names=[alias if alias else 'scalar_function'], |
| 140 | + ) |
| 141 | + ], |
| 142 | + base_schema=base_schema, |
| 143 | + extension_uris=extension_uris, |
| 144 | + extensions=extensions |
| 145 | + ) |
| 146 | + |
| 147 | + return resolve |
0 commit comments