Skip to content

Commit

Permalink
feat(datatype): enable inference of Decimal type
Browse files Browse the repository at this point in the history
  • Loading branch information
deepyaman authored and cpcloud committed Feb 14, 2023
1 parent 4994add commit 8761732
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 2 deletions.
14 changes: 12 additions & 2 deletions ibis/expr/datatypes/cast.py
Original file line number Diff line number Diff line change
Expand Up @@ -113,8 +113,18 @@ def can_cast_decimals(source: dt.Decimal, target: dt.Decimal, **kwargs) -> bool:
target_sc = target.scale
source_sc = source.scale
return (
target_prec is None or (source_prec is not None and target_prec >= source_prec)
) and (target_sc is None or (source_sc is not None and target_sc >= source_sc))
# If either sides precision and scale are both `None`, return `True`.
target_prec is None
and target_sc is None
or source_prec is None
and source_sc is None
# Otherwise, return `True` unless we are downcasting precision or scale.
or (
target_prec is None
or (source_prec is not None and target_prec >= source_prec)
)
and (target_sc is None or (source_sc is not None and target_sc >= source_sc))
)


@castable.register(dt.Interval, dt.Interval)
Expand Down
2 changes: 2 additions & 0 deletions ibis/expr/datatypes/tests/test_value.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import datetime
import decimal
import enum
from collections import OrderedDict

Expand Down Expand Up @@ -46,6 +47,7 @@ class Foo(enum.Enum):
(-32769, dt.int32),
(-2147483649, dt.int64),
(1.5, dt.double),
(decimal.Decimal(1.5), dt.decimal),
# parametric types
(list('abc'), dt.Array(dt.string)),
(set('abc'), dt.Set(dt.string)),
Expand Down
6 changes: 6 additions & 0 deletions ibis/expr/datatypes/value.py
Original file line number Diff line number Diff line change
Expand Up @@ -132,6 +132,12 @@ def infer_enum(_: enum.Enum) -> dt.String:
return dt.string


@infer.register(decimal.Decimal)
def infer_decimal(value: decimal.Decimal) -> dt.Decimal:
"""Infer the [`Decimal`][ibis.expr.datatypes.Decimal] type of `value`."""
return dt.decimal


@infer.register(bool)
def infer_boolean(value: bool) -> dt.Boolean:
return dt.boolean
Expand Down

0 comments on commit 8761732

Please sign in to comment.