diff --git a/ibis/backends/base/sqlglot/datatypes.py b/ibis/backends/base/sqlglot/datatypes.py index 7d59e48226ba..5dcc188eb1c4 100644 --- a/ibis/backends/base/sqlglot/datatypes.py +++ b/ibis/backends/base/sqlglot/datatypes.py @@ -6,6 +6,7 @@ import sqlglot as sg import sqlglot.expressions as sge +import ibis.common.exceptions as com import ibis.expr.datatypes as dt from ibis.common.collections import FrozenDict from ibis.formats import TypeMapper @@ -230,11 +231,20 @@ def _from_sqlglot_TIMESTAMPLTZ(cls, scale=None) -> dt.Timestamp: @classmethod def _from_sqlglot_INTERVAL( - cls, precision: sge.DataTypeParam | None = None + cls, precision_or_span: sge.DataTypeParam | sge.IntervalSpan | None = None ) -> dt.Interval: - if precision is None: - precision = cls.default_interval_precision - return dt.Interval(str(precision), nullable=cls.default_nullable) + nullable = cls.default_nullable + if precision_or_span is None: + precision_or_span = cls.default_interval_precision + + if isinstance(precision_or_span, str): + return dt.Interval(precision_or_span, nullable=nullable) + elif isinstance(precision_or_span, sge.DataTypeParam): + return dt.Interval(str(precision_or_span), nullable=nullable) + elif isinstance(precision_or_span, sge.IntervalSpan): + return dt.Interval(unit=precision_or_span.this.this, nullable=nullable) + else: + raise com.IbisTypeError(precision_or_span) @classmethod def _from_sqlglot_DECIMAL( @@ -264,7 +274,13 @@ def _from_sqlglot_GEOGRAPHY(cls) -> sge.DataType: @classmethod def _from_ibis_Interval(cls, dtype: dt.Interval) -> sge.DataType: - return sge.DataType(this=typecode.INTERVAL) + if (unit := dtype.unit) is None: + return sge.DataType(this=typecode.INTERVAL) + + return sge.DataType( + this=typecode.INTERVAL, + expressions=[sge.IntervalSpan(this=sge.Var(this=unit.name))], + ) @classmethod def _from_ibis_Array(cls, dtype: dt.Array) -> sge.DataType: diff --git a/ibis/backends/base/sqlglot/tests/test_datatypes.py b/ibis/backends/base/sqlglot/tests/test_datatypes.py index 4b1686107797..87d46245416f 100644 --- a/ibis/backends/base/sqlglot/tests/test_datatypes.py +++ b/ibis/backends/base/sqlglot/tests/test_datatypes.py @@ -42,6 +42,7 @@ def assert_dtype_roundtrip(ibis_type, sqlglot_expected=None): | its.geometry_dtypes(nullable=true) | its.geography_dtypes(nullable=true) | its.decimal_dtypes(nullable=true) + | its.interval_dtype(nullable=true) ) )