Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add python filters is_null, is_not_null, and Filter.not_() #3736

Merged
merged 2 commits into from
Apr 26, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
42 changes: 37 additions & 5 deletions py/server/deephaven/filters.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,14 @@ def j_object(self) -> jpy.JType:
def __init__(self, j_filter):
self.j_filter = j_filter

def not_(self):
"""Creates a new filter that evaluates to the opposite of what this filter evaluates to.

Returns:
a new not Filter
"""
return Filter(j_filter=_JFilterNot.of(self.j_filter))

@classmethod
def from_(cls, conditions: Union[str, List[str]]) -> Union[Filter, List[Filter]]:
"""Creates filter(s) from the given condition(s).
Expand Down Expand Up @@ -68,7 +76,7 @@ def or_(filters: List[Filter]) -> Filter:
filters (List[filter]): the component filters

Returns:
a new Filter
a new or Filter
"""
return Filter(j_filter=_JFilterOr.of(*[f.j_filter for f in filters]))

Expand All @@ -80,21 +88,45 @@ def and_(filters: List[Filter]) -> Filter:
filters (List[filter]): the component filters

Returns:
a new Filter
a new and Filter
"""
return Filter(j_filter=_JFilterAnd.of(*[f.j_filter for f in filters]))


def not_(filter_: Filter) -> Filter:
"""Creates a new filter that evaluates to true when the given filter evaluates to false.
"""Creates a new filter that evaluates to the opposite of what filter_ evaluates to.

Args:
filter_ (Filter): the filter to negate with

Returns:
a new Filter
a new not Filter
"""
return filter_.not_()


def is_null(col: str) -> Filter:
devinrsmith marked this conversation as resolved.
Show resolved Hide resolved
"""Creates a new filter that evaluates to true when the col is null, and evaluates to false when col is not null.

Args:
col (str): the column name

Returns:
a new is-null Filter
"""
return Filter(j_filter=_JFilter.isNull(_JColumnName.of(col)))


def is_not_null(col: str) -> Filter:
"""Creates a new filter that evaluates to true when the col is not null, and evaluates to false when col is null.

Args:
col (str): the column name

Returns:
a new is-not-null Filter
"""
return Filter(j_filter=_JFilterNot.of(filter_.j_filter))
return Filter(j_filter=_JFilter.isNotNull(_JColumnName.of(col)))


class PatternMode(Enum):
Expand Down
18 changes: 16 additions & 2 deletions py/server/tests/test_filters.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,9 @@

import unittest

from deephaven import read_csv, DHError
from deephaven.filters import Filter, PatternMode, and_, or_, not_, pattern
from deephaven import new_table, read_csv, DHError
from deephaven.column import string_col
from deephaven.filters import Filter, PatternMode, and_, is_not_null, is_null, or_, not_, pattern
from tests.testbase import BaseTestCase


Expand Down Expand Up @@ -51,6 +52,19 @@ def test_filter(self):
filtered_table_not = self.test_table.where(filter_not)
self.assertEqual(filtered_table_or.size + filtered_table_not.size, self.test_table.size)

def test_is_null(self):
x = new_table([string_col("X", ["a", "b", "c", None, "e", "f"])])
x_is_null = new_table([string_col("X", [None])])
x_not_is_null = new_table([string_col("X", ["a", "b", "c", "e", "f"])])
self.assert_table_equals(x.where(is_null("X")), x_is_null)
self.assert_table_equals(x.where(not_(is_null("X"))), x_not_is_null)

def test_is_not_null(self):
x = new_table([string_col("X", ["a", "b", "c", None, "e", "f"])])
x_is_not_null = new_table([string_col("X", ["a", "b", "c", "e", "f"])])
x_not_is_not_null = new_table([string_col("X", [None])])
self.assert_table_equals(x.where(is_not_null("X")), x_is_not_null)
self.assert_table_equals(x.where(not_(is_not_null("X"))), x_not_is_not_null)

if __name__ == '__main__':
unittest.main()