From 0c314795099270a6012b49bed374856ef9f63567 Mon Sep 17 00:00:00 2001 From: Daniel Mesejo Date: Tue, 25 Nov 2025 21:28:55 +0100 Subject: [PATCH] fix: asof_join for columns with different names closes #11780 --- ibis/backends/tests/test_asof_join.py | 10 +++++++++- ibis/expr/types/joins.py | 6 +++--- 2 files changed, 12 insertions(+), 4 deletions(-) diff --git a/ibis/backends/tests/test_asof_join.py b/ibis/backends/tests/test_asof_join.py index c57993a3b605..a6873bbbcb0c 100644 --- a/ibis/backends/tests/test_asof_join.py +++ b/ibis/backends/tests/test_asof_join.py @@ -219,6 +219,7 @@ def test_keyed_asof_join( @pytest.mark.parametrize( ("direction", "op"), [("backward", operator.ge), ("forward", operator.le)] ) +@pytest.mark.parametrize("right_column_key", [None, "on_right_time"]) @pytest.mark.notimpl( ["clickhouse"], raises=AssertionError, reason="`time` is truncated to seconds" ) @@ -251,8 +252,15 @@ def test_keyed_asof_join_with_tolerance( time_keyed_df2, direction, op, + right_column_key, ): - on = op(time_keyed_left["time"], time_keyed_right["time"]) + left_key = right_key = "time" + if right_column_key: + right_key = right_column_key + time_keyed_right = time_keyed_right.rename({right_column_key: "time"}) + assert left_key != right_key + + on = op(time_keyed_left[left_key], time_keyed_right[right_key]) expr = time_keyed_left.asof_join( time_keyed_right, on, "key", tolerance=ibis.interval(days=2) ) diff --git a/ibis/expr/types/joins.py b/ibis/expr/types/joins.py index d357f78bde67..816e0fc246b4 100644 --- a/ibis/expr/types/joins.py +++ b/ibis/expr/types/joins.py @@ -301,8 +301,8 @@ def asof_join( # 2. filter the asof join result using the `tolerance` parameter and # the `on` parameter # 3. perform a left join between the original left table and the - # filtered asof join result using the `on` parameter but this - # time as an equality predicate + # filtered asof join result using the left operand of the `on` and the corresponding column on + # the filtered asof join if isinstance(on, str): # self is always a JoinChain so reference one of the join tables left_on = self.op().values[on].to_expr() @@ -323,7 +323,7 @@ def asof_join( filtered = joined.filter( left_on <= right_on + tolerance, left_on >= right_on - tolerance ) - right_on = right_on.op().replace({right.op(): filtered.op()}).to_expr() + (right_on,) = filtered.bind(left_on) # without joining twice the table would not contain the rows from # the left table that do not match any row from the right table