Skip to content

Commit

Permalink
fix(selectors): error when trying to select a non-existent column wit…
Browse files Browse the repository at this point in the history
…h `s.c`
  • Loading branch information
cpcloud committed Aug 17, 2023
1 parent 5e11529 commit ae3e76e
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 4 deletions.
12 changes: 11 additions & 1 deletion ibis/selectors.py
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@

from public import public

import ibis.common.exceptions as exc
import ibis.expr.datatypes as dt
import ibis.expr.types as ir
from ibis import util
Expand Down Expand Up @@ -367,7 +368,16 @@ def all_of(*predicates: str | Predicate) -> Predicate:
def c(*names: str) -> Predicate:
"""Select specific column names."""
names = frozenset(names)
return where(lambda col: col.get_name() in names)

def func(col: ir.Value) -> bool:
schema = col.op().table.schema
if extra_cols := (names - schema.keys()):
raise exc.IbisInputError(
f"Columns {extra_cols} are not present in {schema.names}"
)
return col.get_name() in names

return where(func)


class Across(Selector):
Expand Down
20 changes: 20 additions & 0 deletions ibis/tests/expr/test_selectors.py
Original file line number Diff line number Diff line change
Expand Up @@ -451,3 +451,23 @@ def test_any_of_string_list(penguins):
"bill_length_mm", "flipper_length_mm", "body_mass_g", "year"
)
assert expr.equals(expected)


def test_c_error_on_misspelled_column(penguins):
match = "Columns .+ are not present"

sel = s.c("inland")
with pytest.raises(exc.IbisInputError, match=match):
penguins.select(sel)

sel = s.any_of(s.c("inland"), s.c("island"))
with pytest.raises(exc.IbisInputError, match=match):
penguins.select(sel)

sel = s.any_of(s.c("island"), s.c("inland"))
with pytest.raises(exc.IbisInputError, match=match):
penguins.select(sel)

sel = s.any_of(s.c("island", "inland"))
with pytest.raises(exc.IbisInputError, match=match):
penguins.select(sel)
4 changes: 1 addition & 3 deletions ibis/tests/expr/test_table.py
Original file line number Diff line number Diff line change
Expand Up @@ -1797,9 +1797,7 @@ def test_pivot_wider():
names=["Release", "Lisbon"], names_from="station", values_from="seen"
)
assert res.schema().names == ("fish", "Release", "Lisbon")
with pytest.raises(
com.IbisInputError, match="No matching names columns in `names_from`"
):
with pytest.raises(com.IbisInputError, match="Columns .+ are not present in"):
fish.pivot_wider(names=["Release", "Lisbon"], values_from="seen")


Expand Down

0 comments on commit ae3e76e

Please sign in to comment.