|
| 1 | +from typing import Optional |
| 2 | +from antlr4 import InputStream, CommonTokenStream |
| 3 | +from substrait.gen.antlr.SubstraitTypeLexer import SubstraitTypeLexer |
| 4 | +from substrait.gen.antlr.SubstraitTypeParser import SubstraitTypeParser |
| 5 | +from substrait.gen.proto.type_pb2 import Type |
| 6 | + |
| 7 | + |
| 8 | +def _evaluate(x, values: dict): |
| 9 | + if type(x) == SubstraitTypeParser.BinaryExprContext: |
| 10 | + left = _evaluate(x.left, values) |
| 11 | + right = _evaluate(x.right, values) |
| 12 | + |
| 13 | + if x.op.text == "+": |
| 14 | + return left + right |
| 15 | + elif x.op.text == "-": |
| 16 | + return left - right |
| 17 | + elif x.op.text == "*": |
| 18 | + return left * right |
| 19 | + elif x.op.text == ">": |
| 20 | + return left > right |
| 21 | + elif x.op.text == ">=": |
| 22 | + return left >= right |
| 23 | + elif x.op.text == "<": |
| 24 | + return left < right |
| 25 | + elif x.op.text == "<=": |
| 26 | + return left <= right |
| 27 | + else: |
| 28 | + raise Exception(f"Unknown binary op {x.op.text}") |
| 29 | + elif type(x) == SubstraitTypeParser.LiteralNumberContext: |
| 30 | + return int(x.number.text) |
| 31 | + elif type(x) == SubstraitTypeParser.TypeParamContext: |
| 32 | + return values[x.identifier.text] |
| 33 | + elif type(x) == SubstraitTypeParser.NumericParameterNameContext: |
| 34 | + return values[x.Identifier().symbol.text] |
| 35 | + elif type(x) == SubstraitTypeParser.ParenExpressionContext: |
| 36 | + return _evaluate(x.expr(), values) |
| 37 | + elif type(x) == SubstraitTypeParser.FunctionCallContext: |
| 38 | + exprs = [_evaluate(e, values) for e in x.expr()] |
| 39 | + func = x.Identifier().symbol.text |
| 40 | + |
| 41 | + if func == "min": |
| 42 | + return min(*exprs) |
| 43 | + elif func == "max": |
| 44 | + return max(*exprs) |
| 45 | + else: |
| 46 | + raise Exception(f"Unknown function {func}") |
| 47 | + elif type(x) == SubstraitTypeParser.TypeContext: |
| 48 | + scalar_type = x.scalarType() |
| 49 | + parametrized_type = x.parameterizedType() |
| 50 | + if scalar_type: |
| 51 | + if isinstance(scalar_type, SubstraitTypeParser.I8Context): |
| 52 | + return Type(i8=Type.I8()) |
| 53 | + elif isinstance(scalar_type, SubstraitTypeParser.I16Context): |
| 54 | + return Type(i16=Type.I16()) |
| 55 | + elif isinstance(scalar_type, SubstraitTypeParser.I32Context): |
| 56 | + return Type(i32=Type.I32()) |
| 57 | + elif isinstance(scalar_type, SubstraitTypeParser.I64Context): |
| 58 | + return Type(i64=Type.I64()) |
| 59 | + elif isinstance(scalar_type, SubstraitTypeParser.Fp32Context): |
| 60 | + return Type(fp32=Type.FP32()) |
| 61 | + elif isinstance(scalar_type, SubstraitTypeParser.Fp64Context): |
| 62 | + return Type(fp64=Type.FP64()) |
| 63 | + elif isinstance(scalar_type, SubstraitTypeParser.BooleanContext): |
| 64 | + return Type(bool=Type.Boolean()) |
| 65 | + else: |
| 66 | + raise Exception(f"Unknown scalar type {type(scalar_type)}") |
| 67 | + elif parametrized_type: |
| 68 | + if isinstance(parametrized_type, SubstraitTypeParser.DecimalContext): |
| 69 | + precision = _evaluate(parametrized_type.precision, values) |
| 70 | + scale = _evaluate(parametrized_type.scale, values) |
| 71 | + return Type(decimal=Type.Decimal(precision=precision, scale=scale)) |
| 72 | + raise Exception(f"Unknown parametrized type {type(parametrized_type)}") |
| 73 | + else: |
| 74 | + raise Exception("either scalar_type or parametrized_type is required") |
| 75 | + elif type(x) == SubstraitTypeParser.NumericExpressionContext: |
| 76 | + return _evaluate(x.expr(), values) |
| 77 | + elif type(x) == SubstraitTypeParser.TernaryContext: |
| 78 | + ifExpr = _evaluate(x.ifExpr, values) |
| 79 | + thenExpr = _evaluate(x.thenExpr, values) |
| 80 | + elseExpr = _evaluate(x.elseExpr, values) |
| 81 | + |
| 82 | + return thenExpr if ifExpr else elseExpr |
| 83 | + elif type(x) == SubstraitTypeParser.MultilineDefinitionContext: |
| 84 | + lines = zip(x.Identifier(), x.expr()) |
| 85 | + |
| 86 | + for i, e in lines: |
| 87 | + identifier = i.symbol.text |
| 88 | + expr_eval = _evaluate(e, values) |
| 89 | + values[identifier] = expr_eval |
| 90 | + |
| 91 | + return _evaluate(x.finalType, values) |
| 92 | + elif type(x) == SubstraitTypeParser.TypeLiteralContext: |
| 93 | + return _evaluate(x.type_(), values) |
| 94 | + else: |
| 95 | + raise Exception(f"Unknown token type {type(x)}") |
| 96 | + |
| 97 | + |
| 98 | +def evaluate(x: str, values: Optional[dict] = None): |
| 99 | + lexer = SubstraitTypeLexer(InputStream(x)) |
| 100 | + stream = CommonTokenStream(lexer) |
| 101 | + parser = SubstraitTypeParser(stream) |
| 102 | + return _evaluate(parser.expr(), values) |
0 commit comments