Skip to content

Commit

Permalink
refactor(backend): normalize exceptions
Browse files Browse the repository at this point in the history
BREAKING CHANGE: Backends now raise `ibis.common.exceptions.UnsupportedOperationError` in more places during compilation. You may need to catch this error type instead of the previous type, which differed between backends.
  • Loading branch information
krzysztof-kwitt authored and cpcloud committed Mar 8, 2023
1 parent c5081cf commit 065b66d
Show file tree
Hide file tree
Showing 5 changed files with 18 additions and 13 deletions.
2 changes: 1 addition & 1 deletion ibis/backends/clickhouse/compiler/values.py
Original file line number Diff line number Diff line change
Expand Up @@ -533,7 +533,7 @@ def _table_array_view(op, *, cache, **kw):
def _timestamp_from_unix(op, **kw):
arg = translate_val(op.arg, **kw)
if (unit := op.unit) in {"ms", "us", "ns"}:
raise ValueError(f"`{unit}` unit is not supported!")
raise com.UnsupportedOperationError(f"{unit!r} unit is not supported!")

return f"toDateTime({arg})"

Expand Down
11 changes: 7 additions & 4 deletions ibis/backends/duckdb/registry.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
fixed_arity,
operation_registry,
)
from ibis.common.exceptions import UnsupportedOperationError

operation_registry = {
op: operation_registry[op]
Expand Down Expand Up @@ -80,7 +81,7 @@ def _timestamp_from_unix(t, op):
elif unit == "s":
return sa.func.to_timestamp(arg)
else:
raise ValueError(f"`{unit}` unit is not supported!")
raise UnsupportedOperationError(f"{unit!r} unit is not supported!")


class struct_pack(GenericFunction):
Expand Down Expand Up @@ -177,21 +178,23 @@ def _json_get_item(left, path):
def _strftime(t, op):
format_str = op.format_str
if not isinstance(format_str_op := format_str, ops.Literal):
raise TypeError(
raise UnsupportedOperationError(
f"DuckDB format_str must be a literal `str`; got {type(format_str)}"
)
return sa.func.strftime(t.translate(op.arg), sa.text(repr(format_str_op.value)))


def _arbitrary(t, op):
if (how := op.how) == "heavy":
raise ValueError(f"how={how!r} not supported in the DuckDB backend")
raise UnsupportedOperationError(
f"how={how!r} not supported in the DuckDB backend"
)
return t._reduction(getattr(sa.func, how), op)


def _string_agg(t, op):
if not isinstance(op.sep, ops.Literal):
raise TypeError(
raise UnsupportedOperationError(
"Separator argument to group_concat operation must be a constant"
)
agg = sa.func.string_agg(t.translate(op.arg), sa.text(repr(op.sep.value)))
Expand Down
2 changes: 1 addition & 1 deletion ibis/backends/mssql/registry.py
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ def _timestamp_from_unix(x, unit='s'):
return sa.func.dateadd(sa.text('s'), x, '1970-01-01 00:00:00')
if unit == 'ms':
return sa.func.dateadd(sa.text('s'), x / 1_000, '1970-01-01 00:00:00')
raise ValueError(f"{unit!r} unit is not supported!")
raise com.UnsupportedOperationError(f"{unit!r} unit is not supported!")


_truncate_precisions = {
Expand Down
8 changes: 4 additions & 4 deletions ibis/backends/tests/test_temporal.py
Original file line number Diff line number Diff line change
Expand Up @@ -1394,7 +1394,7 @@ def test_interval_add_cast_column(backend, alltypes, df):
),
pytest.mark.notyet(
["duckdb"],
raises=TypeError,
raises=com.UnsupportedOperationError,
reason=(
"DuckDB format_str must be a literal `str`; got "
"<class 'ibis.expr.operations.strings.StringConcat'>"
Expand Down Expand Up @@ -1447,7 +1447,7 @@ def test_strftime(backend, alltypes, df, expr_fn, pandas_pattern):
),
pytest.mark.notimpl(
["clickhouse"],
raises=ValueError,
raises=com.UnsupportedOperationError,
reason="`ms` unit is not supported!",
),
],
Expand All @@ -1462,7 +1462,7 @@ def test_strftime(backend, alltypes, df, expr_fn, pandas_pattern):
),
pytest.mark.notimpl(
["duckdb", "mssql", "clickhouse"],
raises=ValueError,
raises=com.UnsupportedOperationError,
reason="`us` unit is not supported!",
),
],
Expand All @@ -1477,7 +1477,7 @@ def test_strftime(backend, alltypes, df, expr_fn, pandas_pattern):
),
pytest.mark.notimpl(
["duckdb", "mssql", "clickhouse"],
raises=ValueError,
raises=com.UnsupportedOperationError,
reason="`ms` unit is not supported!",
),
],
Expand Down
8 changes: 5 additions & 3 deletions ibis/backends/trino/registry.py
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ def _literal(t, op):

def _arbitrary(t, op):
if op.how == "heavy":
raise ValueError('Trino does not support how="heavy"')
raise com.UnsupportedOperationError('Trino does not support how="heavy"')
return reduction(sa.func.arbitrary)(t, op)


Expand All @@ -62,7 +62,9 @@ def _json_get_item(t, op):

def _group_concat(t, op):
if not isinstance(op.sep, ops.Literal):
raise com.IbisTypeError("Trino group concat separator must be a literal value")
raise com.UnsupportedOperationError(
"Trino group concat separator must be a literal value"
)

arg = sa.func.array_agg(t.translate(op.arg))
if (where := op.where) is not None:
Expand Down Expand Up @@ -119,7 +121,7 @@ def _timestamp_from_unix(t, op):
elif unit == "ns":
return sa.func.from_unixtime_nanos(arg - (arg % 1_000_000_000))
else:
raise ValueError(f"{unit!r} unit is not supported")
raise com.UnsupportedOperationError(f"{unit!r} unit is not supported")


def _neg_idx_to_pos(array, idx):
Expand Down

0 comments on commit 065b66d

Please sign in to comment.