Skip to content

Commit 4937b48

Browse files
cpcloudgforsyth
authored andcommitted
fix(memtables): disallow duplicate column names when constructing memtables
1 parent d8d622c commit 4937b48

File tree

2 files changed

+19
-1
lines changed

2 files changed

+19
-1
lines changed

ibis/expr/api.py

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
import functools
88
import numbers
99
import operator
10+
from collections import Counter
1011
from typing import TYPE_CHECKING, Any, NamedTuple, overload
1112

1213
import ibis.expr.builders as bl
@@ -476,6 +477,16 @@ def _memtable_from_dataframe(
476477
(f"col{i:d}" for i in builtins.range(len(cols))),
477478
)
478479
df = df.rename(columns=dict(zip(cols, newcols)))
480+
481+
# verify that the DataFrame has no duplicate column names because ibis
482+
# doesn't allow that
483+
cols = df.columns
484+
dupes = [name for name, count in Counter(cols).items() if count > 1]
485+
if dupes:
486+
raise IbisInputError(
487+
f"Duplicate column names found in DataFrame when constructing memtable: {dupes}"
488+
)
489+
479490
op = ops.InMemoryTable(
480491
name=name if name is not None else util.gen_name("pandas_memtable"),
481492
schema=sch.infer(df) if schema is None else schema,

ibis/expr/tests/test_api.py

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
import ibis.expr.datatypes as dt
1212
import ibis.expr.schema as sch
1313
from ibis import _
14-
from ibis.common.exceptions import IntegrityError
14+
from ibis.common.exceptions import IbisInputError, IntegrityError
1515

1616

1717
def test_schema_from_names_types():
@@ -117,3 +117,10 @@ def test_repr_deferred_with_exprs(f, sol):
117117
expr = f(t)
118118
res = repr(expr)
119119
assert res == sol
120+
121+
122+
def test_duplicate_columns_in_memtable_not_allowed():
123+
df = pd.DataFrame([[1, 2], [3, 4]], columns=["a", "a"])
124+
125+
with pytest.raises(IbisInputError, match="Duplicate column names"):
126+
ibis.memtable(df)

0 commit comments

Comments
 (0)