Skip to content

Commit d3c3bad

Browse files
Magnus Wahlbergcpcloud
authored andcommitted
fix(polars): handle cross joins without passing join keys
Cross joins were failing with ValueError because join keys were being passed to polars' join method when they shouldn't be. fixes #11764
1 parent bb42a6b commit d3c3bad

File tree

2 files changed

+47
-0
lines changed

2 files changed

+47
-0
lines changed

ibis/backends/polars/compiler.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -313,6 +313,9 @@ def join(op, **kw):
313313
left, right = right, left
314314
elif how == "outer":
315315
how = "full"
316+
elif how == "cross":
317+
# Cross joins don't use join keys
318+
return left.join(right, how=how)
316319

317320
joined = left.join(right, on=on, how=how, coalesce=False)
318321
joined = joined.drop(*on)

ibis/backends/polars/tests/test_join.py

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,3 +25,47 @@ def test_memtable_join(con):
2525
left = result.sort_values("x").reset_index(drop=True)
2626
right = expected.sort_values("x").reset_index(drop=True)
2727
tm.assert_frame_equal(left, right)
28+
29+
30+
def test_memtable_join_left(con):
31+
t1 = ibis.memtable({"x": [1, 2, 3], "y": [4, 5, 6], "z": ["a", "b", "c"]})
32+
t2 = ibis.memtable({"x": [3, 2, 1], "y": [7, 8, 9], "z": ["d", "e", "f"]})
33+
expr = t1.join(t2, "x", how="left")
34+
35+
result = con.execute(expr)
36+
expected = pd.DataFrame(
37+
{
38+
"x": [1, 2, 3],
39+
"y": [4, 5, 6],
40+
"z": ["a", "b", "c"],
41+
"x_right": [1, 2, 3],
42+
"y_right": [9, 8, 7],
43+
"z_right": ["f", "e", "d"],
44+
}
45+
)
46+
47+
left = result.sort_values("x").reset_index(drop=True)
48+
right = expected.sort_values("x").reset_index(drop=True)
49+
tm.assert_frame_equal(left, right)
50+
51+
52+
def test_memtable_join_cross(con):
53+
t1 = ibis.memtable({"x": [1, 2, 3], "y": [4, 5, 6], "z": ["a", "b", "c"]})
54+
t2 = ibis.memtable({"x": [3, 2, 1], "y": [7, 8, 9], "z": ["d", "e", "f"]})
55+
expr = t1.join(t2, how="cross")
56+
57+
result = con.execute(expr)
58+
expected = pd.DataFrame(
59+
{
60+
"x": [1, 1, 1, 2, 2, 2, 3, 3, 3],
61+
"y": [4, 4, 4, 5, 5, 5, 6, 6, 6],
62+
"z": ["a", "a", "a", "b", "b", "b", "c", "c", "c"],
63+
"x_right": [1, 2, 3, 1, 2, 3, 1, 2, 3],
64+
"y_right": [9, 8, 7, 9, 8, 7, 9, 8, 7],
65+
"z_right": ["f", "e", "d", "f", "e", "d", "f", "e", "d"],
66+
}
67+
)
68+
69+
left = result.sort_values(["x", "x_right"]).reset_index(drop=True)
70+
right = expected.sort_values(["x", "x_right"]).reset_index(drop=True)
71+
tm.assert_frame_equal(left, right)

0 commit comments

Comments
 (0)