From 7aa6edba9f5a345eb3fdbdadd907c7df1397ef5b Mon Sep 17 00:00:00 2001 From: Simon Lin Date: Tue, 28 May 2024 18:56:34 +1000 Subject: [PATCH] fix: Crash selecting columns after non-coalesced join --- .../optimizer/projection_pushdown/joins.rs | 3 +-- py-polars/tests/unit/test_projections.py | 19 +++++++++++++++++++ 2 files changed, 20 insertions(+), 2 deletions(-) diff --git a/crates/polars-plan/src/logical_plan/optimizer/projection_pushdown/joins.rs b/crates/polars-plan/src/logical_plan/optimizer/projection_pushdown/joins.rs index 0a663e9e9195..a30b674f8e39 100644 --- a/crates/polars-plan/src/logical_plan/optimizer/projection_pushdown/joins.rs +++ b/crates/polars-plan/src/logical_plan/optimizer/projection_pushdown/joins.rs @@ -258,8 +258,7 @@ pub(super) fn process_join( already_added_local_to_local_projected.insert(local_name); } // In full outer joins both columns remain. So `add_local=true` also for the right table - let add_local = matches!(options.args.how, JoinType::Full) - && !options.args.coalesce.coalesce(&options.args.how); + let add_local = !options.args.coalesce.coalesce(&options.args.how); for e in &right_on { // In case of full outer joins we also add the columns. // But before we do that we must check if the column wasn't already added by the lhs. diff --git a/py-polars/tests/unit/test_projections.py b/py-polars/tests/unit/test_projections.py index 976e05fb3e51..02b029a46a98 100644 --- a/py-polars/tests/unit/test_projections.py +++ b/py-polars/tests/unit/test_projections.py @@ -1,4 +1,7 @@ +from typing import Literal + import numpy as np +import pytest import polars as pl from polars.testing import assert_frame_equal @@ -434,3 +437,19 @@ def test_double_projection_pushdown_15895() -> None: "C": [[0]], "A": [[1]], } + + +@pytest.mark.parametrize("join_type", ["inner", "left", "full"]) +def test_non_coalesce_join_projection_pushdown_16515( + join_type: Literal["inner", "left", "full"], +) -> None: + left = pl.LazyFrame({"x": 1}) + right = pl.LazyFrame({"y": 1}) + + assert ( + left.join(right, how=join_type, left_on="x", right_on="y", coalesce=False) + .select("y") + .collect() + .item() + == 1 + )