Skip to content

feat: expose between #868

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

Merged
merged 1 commit into from
Sep 11, 2024
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
16 changes: 16 additions & 0 deletions python/datafusion/expr.py
Original file line number Diff line number Diff line change
Expand Up @@ -393,6 +393,22 @@ def cast(

return Expr(self.expr.cast(to))

def between(self, low: Any, high: Any, negated: bool = False) -> Expr:
"""Returns ``True`` if this expression is between a given range.

Args:
low: lower bound of the range (inclusive).
high: higher bound of the range (inclusive).
negated: negates whether the expression is between a given range
"""
if not isinstance(low, Expr):
low = Expr.literal(low)

if not isinstance(high, Expr):
high = Expr.literal(high)

return Expr(self.expr.between(low.expr, high.expr, negated=negated))

def rex_type(self) -> RexType:
"""Return the Rex Type of this expression.

Expand Down
31 changes: 31 additions & 0 deletions python/datafusion/tests/test_functions.py
Original file line number Diff line number Diff line change
Expand Up @@ -1024,3 +1024,34 @@ def test_cast(df, python_datatype, name: str, expected):
result = df.collect()
result = result[0]
assert result.column(0) == result.column(1)


@pytest.mark.parametrize(
"negated, low, high, expected",
[
pytest.param(False, 3, 5, {"filtered": [4, 5]}),
pytest.param(False, 4, 5, {"filtered": [4, 5]}),
pytest.param(True, 3, 5, {"filtered": [6]}),
pytest.param(True, 4, 6, []),
],
)
def test_between(df, negated, low, high, expected):
df = df.filter(column("b").between(low, high, negated=negated)).select(
column("b").alias("filtered")
)

actual = df.collect()

if expected:
actual = actual[0].to_pydict()
assert actual == expected
else:
assert len(actual) == 0 # the rows are empty


def test_between_default(df):
df = df.filter(column("b").between(3, 5)).select(column("b").alias("filtered"))
expected = {"filtered": [4, 5]}

actual = df.collect()[0].to_pydict()
assert actual == expected
11 changes: 11 additions & 0 deletions src/expr.rs
Original file line number Diff line number Diff line change
Expand Up @@ -293,6 +293,17 @@ impl PyExpr {
expr.into()
}

#[pyo3(signature = (low, high, negated=false))]
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

With the introduction of the python wrappers, we've been preferring to avoid #[pyo3(signature = ...)] annotations for simple defaults and let the python wrappers handle them.

(I see that I left that out of the guidelines, I'll throw a quick PR for that.)

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

FYI for optionals we might have to have these signatures in when we upgrade to pyo3 0.22. It's part of the DF42 update PR that's up

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

So, do you folks think I should remove it?

Copy link
Contributor

@emgeee emgeee Sep 10, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think it should be kept in as we'll have to add it anyways once the pyo3 0.22 upgrade hits #867

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, leave it in for now.

pub fn between(&self, low: PyExpr, high: PyExpr, negated: bool) -> PyExpr {
let expr = Expr::Between(Between::new(
Box::new(self.expr.clone()),
negated,
Box::new(low.into()),
Box::new(high.into()),
));
expr.into()
}

/// A Rex (Row Expression) specifies a single row of data. That specification
/// could include user defined functions or types. RexType identifies the row
/// as one of the possible valid `RexTypes`.
Expand Down
Loading