Skip to content

Commit

Permalink
fix(api): improve error message for bad arguments to Table.select
Browse files Browse the repository at this point in the history
  • Loading branch information
jcrist authored and cpcloud committed Sep 9, 2023
1 parent af2c232 commit 258a289
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 10 deletions.
26 changes: 16 additions & 10 deletions ibis/expr/types/relations.py
Original file line number Diff line number Diff line change
Expand Up @@ -1710,23 +1710,29 @@ def select(
import ibis.expr.analysis as an
from ibis.selectors import Selector

exprs = list(
itertools.chain(
itertools.chain.from_iterable(
util.promote_list(e.expand(self) if isinstance(e, Selector) else e)
for e in exprs
),
(
self._ensure_expr(expr).name(name)
for name, expr in named_exprs.items()
),
exprs = [
e
for expr in exprs
for e in (
expr.expand(self)
if isinstance(expr, Selector)
else map(self._ensure_expr, util.promote_list(expr))
)
]
exprs.extend(
self._ensure_expr(expr).name(name) for name, expr in named_exprs.items()
)

if not exprs:
raise com.IbisTypeError(
"You must select at least one column for a valid projection"
)
for ex in exprs:
if not isinstance(ex, Expr):
raise com.IbisTypeError(
"All arguments to `.select` must be coerceable to "
f"expressions - got {type(ex)!r}"
)

op = an.Projector(self, exprs).get_result()

Expand Down
10 changes: 10 additions & 0 deletions ibis/tests/expr/test_table.py
Original file line number Diff line number Diff line change
Expand Up @@ -243,6 +243,16 @@ def test_projection_no_expr(table, empty):
table.select(empty)


def test_projection_invalid_nested_list(table):
errmsg = "must be coerceable to expressions"
with pytest.raises(com.IbisTypeError, match=errmsg):
table.select(["a", ["b"]])
with pytest.raises(com.IbisTypeError, match=errmsg):
table[["a", ["b"]]]
with pytest.raises(com.IbisTypeError, match=errmsg):
table["a", ["b"]]


def test_mutate(table):
expr = table.mutate(
[
Expand Down

0 comments on commit 258a289

Please sign in to comment.