Skip to content

Commit

Permalink
refactor(api): make ibis.where and ibis.ifelse equivalent
Browse files Browse the repository at this point in the history
  • Loading branch information
cpcloud committed Aug 23, 2022
1 parent 9de604f commit 1722aac
Show file tree
Hide file tree
Showing 7 changed files with 10 additions and 40 deletions.
2 changes: 1 addition & 1 deletion ibis/backends/clickhouse/tests/test_select.py
Original file line number Diff line number Diff line change
Expand Up @@ -221,7 +221,7 @@ def test_isnull_case_expr_rewrite_failure(db, alltypes):

result = ibis.clickhouse.compile(reduction)
expected = """\
SELECT sum(CASE WHEN isNull(`string_col`) THEN 1 ELSE 0 END) AS `sum`
SELECT sum(if(isNull(`string_col`), 1, 0)) AS `sum`
FROM {0}.`functional_alltypes`"""
assert result == expected.format(db.name)

Expand Down
4 changes: 2 additions & 2 deletions ibis/backends/impala/tests/test_case_exprs.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,12 +37,12 @@ def test_isnull_1_0(table):
expr = table.g.isnull().ifelse(1, 0)

result = translate(expr)
expected = 'CASE WHEN `g` IS NULL THEN 1 ELSE 0 END'
expected = 'if(`g` IS NULL, 1, 0)'
assert result == expected

# inside some other function
result = translate(expr.sum())
expected = 'sum(CASE WHEN `g` IS NULL THEN 1 ELSE 0 END)'
expected = 'sum(if(`g` IS NULL, 1, 0))'
assert result == expected


Expand Down
5 changes: 2 additions & 3 deletions ibis/backends/impala/tests/test_exprs.py
Original file line number Diff line number Diff line change
Expand Up @@ -704,8 +704,7 @@ def test_where_with_timestamp():
)
result = ibis.impala.compile(expr)
expected = """\
SELECT `uuid`,
min(CASE WHEN `search_level` = 1 THEN `ts` ELSE NULL END) AS `min_date`
SELECT `uuid`, min(if(`search_level` = 1, `ts`, NULL)) AS `min_date`
FROM t
GROUP BY 1"""
assert result == expected
Expand Down Expand Up @@ -766,7 +765,7 @@ def test_nunique_where():
t = ibis.table([('key', 'string'), ('value', 'double')], name='t0')
expr = t.key.nunique(where=t.value >= 1.0)
expected = """\
SELECT count(DISTINCT CASE WHEN `value` >= 1.0 THEN `key` ELSE NULL END) AS `nunique`
SELECT count(DISTINCT if(`value` >= 1.0, `key`, NULL)) AS `nunique`
FROM t0""" # noqa: E501
result = ibis.impala.compile(expr)
assert result == expected
6 changes: 1 addition & 5 deletions ibis/backends/impala/tests/test_unary_builtins.py
Original file line number Diff line number Diff line change
Expand Up @@ -161,11 +161,7 @@ def test_hash(table):
def test_reduction_where(table, expr_fn, func_name):
expr = expr_fn(table)
result = translate(expr)
expected = (
f'{func_name}'
'(CASE WHEN `bigint_col` < 70 THEN `double_col` '
'ELSE NULL END)'
)
expected = f'{func_name}(if(`bigint_col` < 70, `double_col`, NULL))'
assert result == expected


Expand Down
27 changes: 1 addition & 26 deletions ibis/expr/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -681,31 +681,6 @@ def _add_methods(klass, method_table):
setattr(klass, k, v)


def where(
boolean_expr: ir.BooleanValue,
true_expr: ir.Value,
false_null_expr: ir.Value,
) -> ir.Value:
"""Return `true_expr` if `boolean_expr` is `True` else `false_null_expr`.
Parameters
----------
boolean_expr
A boolean expression
true_expr
Value returned if `boolean_expr` is `True`
false_null_expr
Value returned if `boolean_expr` is `False` or `NULL`
Returns
-------
ir.Value
An expression
"""
op = ops.Where(boolean_expr, true_expr, false_null_expr)
return op.to_expr()


coalesce = ir.Value.coalesce
greatest = ir.Value.greatest
least = ir.Value.least
Expand Down Expand Up @@ -790,7 +765,7 @@ def category_label(
geo_y_min = ir.GeoSpatialValue.y_min
geo_unary_union = ir.GeoSpatialColumn.unary_union

ifelse = ir.BooleanValue.ifelse
where = ifelse = ir.BooleanValue.ifelse

# ----------------------------------------------------------------------
# Category API
Expand Down
4 changes: 2 additions & 2 deletions ibis/expr/types/logical.py
Original file line number Diff line number Diff line change
Expand Up @@ -41,12 +41,12 @@ def ifelse(
SELECT CASE WHEN `is_person` THEN 'yes' ELSE 'no' END AS `tmp`
FROM t
"""
import ibis
import ibis.expr.operations as ops

# Result will be the result of promotion of true/false exprs. These
# might be conflicting types; same type resolution as case expressions
# must be used.
return ibis.case().when(self, true_expr).else_(false_expr).end()
return ops.Where(self, true_expr, false_expr).to_expr()

def __and__(self, other: BooleanValue) -> BooleanValue:
from ibis.expr import operations as ops
Expand Down
2 changes: 1 addition & 1 deletion ibis/tests/sql/test_non_tabular_results.py
Original file line number Diff line number Diff line change
Expand Up @@ -135,6 +135,6 @@ def test_isnull_case_expr_rewrite_failure(alltypes):

result = Compiler.to_sql(reduction)
expected = """\
SELECT sum(CASE WHEN `g` IS NULL THEN 1 ELSE 0 END) AS `sum`
SELECT sum(if(`g` IS NULL, 1, 0)) AS `sum`
FROM alltypes"""
assert result == expected

0 comments on commit 1722aac

Please sign in to comment.