Skip to content

Commit

Permalink
feat(api): add and_ and or_ helpers
Browse files Browse the repository at this point in the history
  • Loading branch information
jcrist authored and cpcloud committed Sep 12, 2022
1 parent 0152f5e commit 94bd4df
Show file tree
Hide file tree
Showing 3 changed files with 64 additions and 1 deletion.
2 changes: 2 additions & 0 deletions docs/api/expressions/top_level.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ These methods and objects are available directly in the `ibis` module.

`NA` is the null scalar.

::: ibis.and*
::: ibis.array
::: ibis.asc
::: ibis.case
Expand All @@ -24,6 +25,7 @@ These methods and objects are available directly in the `ibis` module.
::: ibis.negate
::: ibis.now
::: ibis.null
::: ibis.or*
::: ibis.param
::: ibis.show_sql
::: ibis.random
Expand Down
43 changes: 42 additions & 1 deletion ibis/expr/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import datetime
import functools
import itertools
import operator
from typing import Iterable, Mapping, Sequence
from typing import Tuple as _Tuple
from typing import TypeVar
Expand Down Expand Up @@ -115,7 +116,9 @@

__all__ = (
'aggregate',
'and_',
'array',
'asc',
'case',
'coalesce',
'connect',
Expand All @@ -124,7 +127,6 @@
'date',
'desc',
'difference',
'asc',
'e',
'Expr',
'geo_area',
Expand Down Expand Up @@ -194,6 +196,7 @@
'negate',
'now',
'null',
'or_',
'param',
'pi',
'prevent_rewrite',
Expand Down Expand Up @@ -551,6 +554,44 @@ def asc(expr: ir.Column | str) -> ir.SortExpr | ops.DeferredSortKey:
return ops.SortKey(expr).to_expr()


def and_(*predicates: ir.BooleanValue) -> ir.BooleanValue:
"""Combine multiple predicates using `&`.
Parameters
----------
predicates
Boolean value expressions
Returns
-------
BooleanValue
A new predicate that evaluates to True if all composing predicates are
True. If no predicates were provided, returns True.
"""
if not predicates:
return literal(True)
return functools.reduce(operator.and_, predicates)


def or_(*predicates: ir.BooleanValue) -> ir.BooleanValue:
"""Combine multiple predicates using `|`.
Parameters
----------
predicates
Boolean value expressions
Returns
-------
BooleanValue
A new predicate that evaluates to True if any composing predicates are
True. If no predicates were provided, returns False.
"""
if not predicates:
return literal(False)
return functools.reduce(operator.or_, predicates)


@functools.singledispatch
def timestamp(
value,
Expand Down
20 changes: 20 additions & 0 deletions ibis/tests/expr/test_value_exprs.py
Original file line number Diff line number Diff line change
Expand Up @@ -600,6 +600,26 @@ def test_boolean_logical_ops(table, operation):
assert isinstance(result, ir.BooleanScalar)


def test_and_(table):
p1 = table.a > 1
p2 = table.b > 1
p3 = table.c > 1
assert ibis.and_().equals(ibis.literal(True))
assert ibis.and_(p1).equals(p1)
assert ibis.and_(p1, p2).equals(p1 & p2)
assert ibis.and_(p1, p2, p3).equals(p1 & p2 & p3)


def test_or_(table):
p1 = table.a > 1
p2 = table.b > 1
p3 = table.c > 1
assert ibis.or_().equals(ibis.literal(False))
assert ibis.or_(p1).equals(p1)
assert ibis.or_(p1, p2).equals(p1 | p2)
assert ibis.or_(p1, p2, p3).equals(p1 | p2 | p3)


def test_null_column():
t = ibis.table([('a', 'string')], name='t')
s = t.mutate(b=ibis.NA)
Expand Down

0 comments on commit 94bd4df

Please sign in to comment.