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

fix: Raise when join projects name with suffix that doesn't exist #15256

Merged
merged 2 commits into from
Mar 24, 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
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ pub(super) fn process_asof_join(
projections_seen: usize,
lp_arena: &mut Arena<ALogicalPlan>,
expr_arena: &mut Arena<AExpr>,
join_schema: &Schema,
) -> PolarsResult<ALogicalPlan> {
// n = 0 if no projections, so we don't allocate unneeded
let n = acc_projections.len() * 2;
Expand Down Expand Up @@ -152,6 +153,7 @@ pub(super) fn process_asof_join(
&mut local_projection,
add_local,
&options,
join_schema,
);
}
}
Expand Down Expand Up @@ -198,6 +200,7 @@ pub(super) fn process_join(
projections_seen: usize,
lp_arena: &mut Arena<ALogicalPlan>,
expr_arena: &mut Arena<AExpr>,
join_schema: &Schema,
) -> PolarsResult<ALogicalPlan> {
#[cfg(feature = "asof_join")]
if matches!(options.args.how, JoinType::AsOf(_)) {
Expand All @@ -213,6 +216,7 @@ pub(super) fn process_join(
projections_seen,
lp_arena,
expr_arena,
join_schema,
);
}

Expand Down Expand Up @@ -299,6 +303,7 @@ pub(super) fn process_join(
&mut local_projection,
add_local,
&options,
join_schema,
);
}
}
Expand Down Expand Up @@ -345,6 +350,7 @@ fn process_projection(
local_projection: &mut Vec<ColumnNode>,
add_local: bool,
options: &JoinOptions,
join_schema: &Schema,
) {
// Path for renamed columns due to the join. The column name of the left table
// stays as is, the column of the right will have the "_right" suffix.
Expand Down Expand Up @@ -373,7 +379,7 @@ fn process_projection(
let suffix = options.args.suffix();
// If _right suffix exists we need to push a projection down without this
// suffix.
if leaf_column_name.ends_with(suffix) {
if leaf_column_name.ends_with(suffix) && join_schema.contains(leaf_column_name.as_ref()) {
// downwards name is the name without the _right i.e. "foo".
let (downwards_name, _) =
leaf_column_name.split_at(leaf_column_name.len() - suffix.len());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -567,7 +567,7 @@ impl ProjectionPushDown {
left_on,
right_on,
options,
..
schema,
} => match options.args.how {
#[cfg(feature = "semi_anti_join")]
JoinType::Semi | JoinType::Anti => process_semi_anti_join(
Expand Down Expand Up @@ -595,6 +595,7 @@ impl ProjectionPushDown {
projections_seen,
lp_arena,
expr_arena,
&schema,
),
},
HStack {
Expand Down
12 changes: 12 additions & 0 deletions py-polars/tests/unit/operations/test_join.py
Original file line number Diff line number Diff line change
Expand Up @@ -803,3 +803,15 @@ def test_join_results_in_duplicate_names() -> None:
rhs = lhs.clone()
with pytest.raises(pl.DuplicateError, match="'c_right' already exists"):
lhs.join(rhs, on=["a", "b"], how="left")


def test_join_projection_invalid_name_contains_suffix_15243() -> None:
df1 = pl.DataFrame({"a": [1, 2, 3]}).lazy()
df2 = pl.DataFrame({"a": [1, 2, 3], "b": [4, 5, 6]}).lazy()

with pytest.raises(pl.ColumnNotFoundError):
(
df1.join(df2, on="a")
.select(pl.col("b").filter(pl.col("b") == pl.col("foo_right")))
.collect()
)
Loading