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

refactor: Check number of binary comparisons in join_where predicates #18608

Merged
merged 1 commit into from
Sep 8, 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
11 changes: 11 additions & 0 deletions crates/polars-plan/src/plans/conversion/join.rs
Original file line number Diff line number Diff line change
Expand Up @@ -130,6 +130,17 @@ fn resolve_join_where(
) -> PolarsResult<Node> {
check_join_keys(&predicates)?;

for e in &predicates {
let no_binary_comparisons = e
.into_iter()
.filter(|e| match e {
Expr::BinaryExpr { op, .. } => op.is_comparison(),
_ => false,
})
.count();
polars_ensure!(no_binary_comparisons == 1, InvalidOperation: "only 1 binary comparison allowed as join condition")
}

let owned = |e: Arc<Expr>| (*e).clone();

// Partition to:
Expand Down
11 changes: 11 additions & 0 deletions py-polars/polars/dataframe/frame.py
Original file line number Diff line number Diff line change
Expand Up @@ -7095,6 +7095,10 @@ def join_where(
"""
Perform a join based on one or multiple equality predicates.

.. warning::
This functionality is experimental. It may be
changed at any point without it being considered a breaking change.

A row from this table may be included in zero or multiple rows in the result,
and the relative order of rows may differ between the input and output tables.

Expand All @@ -7111,6 +7115,13 @@ def join_where(
suffix
Suffix to append to columns with a duplicate name.

Notes
-----
This method is strict about its equality expressions.
Only 1 equality expression is allowed per predicate, where
the lhs `pl.col` refers to the left table in the join, and the
rhs `pl.col` refers to the right table.

Examples
--------
>>> east = pl.DataFrame(
Expand Down
11 changes: 11 additions & 0 deletions py-polars/polars/lazyframe/frame.py
Original file line number Diff line number Diff line change
Expand Up @@ -4574,6 +4574,10 @@ def join_where(
A row from this table may be included in zero or multiple rows in the result,
and the relative order of rows may differ between the input and output tables.

.. warning::
This functionality is experimental. It may be
changed at any point without it being considered a breaking change.

Parameters
----------
other
Expand All @@ -4587,6 +4591,13 @@ def join_where(
suffix
Suffix to append to columns with a duplicate name.

Notes
-----
This method is strict about its equality expressions.
Only 1 equality expression is allowed per predicate, where
the lhs `pl.col` refers to the left table in the join, and the
rhs `pl.col` refers to the right table.

Examples
--------
>>> east = pl.LazyFrame(
Expand Down
8 changes: 8 additions & 0 deletions py-polars/tests/unit/operations/test_inequality_join.py
Original file line number Diff line number Diff line change
Expand Up @@ -447,3 +447,11 @@ def test_raise_on_suffixed_predicate_18604() -> None:
df = pl.DataFrame({"id": [1, 2]})
with pytest.raises(pl.exceptions.ColumnNotFoundError):
df.join_where(df, pl.col("id") >= pl.col("id_right"))


def test_raise_on_multiple_binary_comparisons() -> None:
df = pl.DataFrame({"id": [1, 2]})
with pytest.raises(pl.exceptions.InvalidOperationError):
df.join_where(
df, (pl.col("id") < pl.col("id")) & (pl.col("id") >= pl.col("id"))
)