From 226074e20648419c831d46971e3e630818ae063a Mon Sep 17 00:00:00 2001 From: Gil Forsyth Date: Wed, 2 Nov 2022 12:43:43 -0400 Subject: [PATCH] feat: add support for ops.Log if `base` isn't specified we default to the value of `e` --- ibis_substrait/compiler/mapping.py | 1 + ibis_substrait/compiler/translate.py | 24 ++++++++++++++++++++++++ 2 files changed, 25 insertions(+) diff --git a/ibis_substrait/compiler/mapping.py b/ibis_substrait/compiler/mapping.py index af78e7ee..23d60079 100644 --- a/ibis_substrait/compiler/mapping.py +++ b/ibis_substrait/compiler/mapping.py @@ -32,6 +32,7 @@ "Less": "lt", "LessEqual": "lte", "Ln": "ln", + "Log": "logb", "Log2": "log2", "Log10": "log10", "Lowercase": "lower", diff --git a/ibis_substrait/compiler/translate.py b/ibis_substrait/compiler/translate.py index a25c9d21..dd0d1c4a 100644 --- a/ibis_substrait/compiler/translate.py +++ b/ibis_substrait/compiler/translate.py @@ -11,10 +11,12 @@ import decimal import functools import itertools +import math import operator import uuid from typing import Any, Mapping, MutableMapping, Sequence, TypeVar +import ibis import ibis.expr.datatypes as dt import ibis.expr.operations as ops import ibis.expr.schema as sch @@ -1054,3 +1056,25 @@ def _extractdatefield( scalar_func.arguments.add(enum=stalg.FunctionArgument.Enum(specified=span)) scalar_func.arguments.extend(arguments) return stalg.Expression(scalar_function=scalar_func) + + +@translate.register(ops.Log) +def _log( + op: ops.Log, + expr: ir.TableExpr, + compiler: SubstraitCompiler, + **kwargs: Any, +) -> stalg.Expression: + arg = stalg.FunctionArgument(value=translate(op.arg, compiler, **kwargs)) + base = stalg.FunctionArgument( + value=translate( + op.base if op.base is not None else ibis.literal(math.e), compiler, **kwargs + ) + ) + + scalar_func = stalg.Expression.ScalarFunction( + function_reference=compiler.function_id(expr), + output_type=translate(expr.type()), + arguments=[arg, base], + ) + return stalg.Expression(scalar_function=scalar_func)