Skip to content

Commit

Permalink
feat(api): raise better error on column name collision in joins
Browse files Browse the repository at this point in the history
  • Loading branch information
jcrist authored and cpcloud committed May 20, 2023
1 parent 69899ca commit e04c38c
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 1 deletion.
20 changes: 19 additions & 1 deletion ibis/expr/operations/relations.py
Original file line number Diff line number Diff line change
Expand Up @@ -648,7 +648,25 @@ def _dedup_join_columns(expr, lname: str, rname: str):
for column in right.columns
if column not in equal
]
return expr.select(left_projections + right_projections)
projections = left_projections + right_projections

# Certain configurations can result in the renamed columns still colliding,
# here we check for duplicates again, and raise a nicer error message if
# any exist.
seen = set()
collisions = set()
for column in projections:
name = column.get_name()
if name in seen:
collisions.add(name)
seen.add(name)
if collisions:
raise com.IntegrityError(
f"Joining with `lname={lname!r}, rname={rname!r}` resulted in multiple "
f"columns mapping to the following names `{sorted(collisions)}`. Please "
f"adjust `lname` and/or `rname` accordingly"
)
return expr.select(projections)


public(ExistsSubquery=ExistsSubquery, NotExistsSubquery=NotExistsSubquery)
12 changes: 12 additions & 0 deletions ibis/tests/expr/test_table.py
Original file line number Diff line number Diff line change
Expand Up @@ -1592,6 +1592,18 @@ def test_join_lname_rname(how):
assert expr.columns == ["left_id", "first_name", "right_id", "last_name"]


def test_join_lname_rname_still_collide():
t1 = ibis.table({"id": "int64", "col1": "int64", "col2": "int64"})
t2 = ibis.table({"id": "int64", "col1": "int64", "col2": "int64"})
t3 = ibis.table({"id": "int64", "col1": "int64", "col2": "int64"})

with pytest.raises(com.IntegrityError) as rec:
t1.left_join(t2, "id").left_join(t3, "id")

assert "`['col1_right', 'col2_right', 'id_right']`" in str(rec.value)
assert "`lname='', rname='{name}_right'`" in str(rec.value)


def test_drop():
t = ibis.table(dict.fromkeys("abcd", "int"))

Expand Down

0 comments on commit e04c38c

Please sign in to comment.