Skip to content

Commit

Permalink
feat(sqlalchemy): implement clip
Browse files Browse the repository at this point in the history
  • Loading branch information
cpcloud committed May 27, 2022
1 parent 2cad85f commit 8c02639
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 11 deletions.
17 changes: 17 additions & 0 deletions ibis/backends/base/sql/alchemy/registry.py
Original file line number Diff line number Diff line change
Expand Up @@ -502,6 +502,22 @@ def _nth_value(t, expr):
return sa.func.nth_value(t.translate(op.arg), t.translate(op.nth) + 1)


def _clip(*, min_func, max_func):
def translate(t, expr):
op = expr.op()
arg = t.translate(op.arg)

if (upper := op.upper) is not None:
arg = min_func(t.translate(upper), arg)

if (lower := op.lower) is not None:
arg = max_func(t.translate(lower), arg)

return arg

return translate


sqlalchemy_operation_registry: Dict[Any, Any] = {
ops.Alias: _alias,
ops.And: fixed_arity(operator.and_, 2),
Expand Down Expand Up @@ -616,6 +632,7 @@ def _nth_value(t, expr):
ops.IdenticalTo: fixed_arity(
sa.sql.expression.ColumnElement.is_not_distinct_from, 2
),
ops.Clip: _clip(min_func=sa.func.least, max_func=sa.func.greatest),
}


Expand Down
3 changes: 2 additions & 1 deletion ibis/backends/sqlite/registry.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
varargs,
variance_reduction,
)
from ibis.backends.base.sql.alchemy.registry import _gen_string_find
from ibis.backends.base.sql.alchemy.registry import _clip, _gen_string_find

operation_registry = sqlalchemy_operation_registry.copy()
operation_registry.update(sqlalchemy_window_functions_registry)
Expand Down Expand Up @@ -358,5 +358,6 @@ def _time_from_hms(t, expr):
ops.BitXor: reduction(sa.func._ibis_sqlite_bit_xor),
ops.Degrees: unary(sa.func._ibis_sqlite_degrees),
ops.Radians: unary(sa.func._ibis_sqlite_radians),
ops.Clip: _clip(min_func=sa.func.min, max_func=sa.func.max),
}
)
11 changes: 1 addition & 10 deletions ibis/backends/tests/test_numeric.py
Original file line number Diff line number Diff line change
Expand Up @@ -562,16 +562,7 @@ def test_random(con):
),
],
)
@pytest.mark.notimpl(
[
"duckdb",
"datafusion",
"impala",
"mysql",
"postgres",
"sqlite",
]
)
@pytest.mark.notimpl(["datafusion", "impala"])
def test_clip(alltypes, df, ibis_func, pandas_func):
result = ibis_func(alltypes.int_col).execute()
expected = pandas_func(df.int_col).astype(result.dtype)
Expand Down

0 comments on commit 8c02639

Please sign in to comment.