Skip to content

Commit

Permalink
feat(mssql): add support for timestamp bucket
Browse files Browse the repository at this point in the history
  • Loading branch information
jcrist committed Oct 18, 2023
1 parent bd62065 commit 73d1c86
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 6 deletions.
30 changes: 27 additions & 3 deletions ibis/backends/mssql/registry.py
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ def _timestamp_from_unix(x, unit="s"):
raise com.UnsupportedOperationError(f"{unit!r} unit is not supported!")


_truncate_precisions = {
_interval_units = {
"us": "microsecond",
"ms": "millisecond",
"s": "second",
Expand All @@ -100,10 +100,33 @@ def _timestamp_from_unix(x, unit="s"):
def _timestamp_truncate(t, op):
arg = t.translate(op.arg)
unit = op.unit.short
if unit not in _truncate_precisions:
if unit not in _interval_units:
raise com.UnsupportedOperationError(f"Unsupported truncate unit {op.unit!r}")

return sa.func.datetrunc(sa.text(_truncate_precisions[unit]), arg)
return sa.func.datetrunc(sa.text(_interval_units[unit]), arg)


def _timestamp_bucket(t, op):
unit = op.interval.dtype.unit.short
if not isinstance(op.interval, ops.Literal):
raise com.UnsupportedOperationError(
"Only literal interval values are supported"
)
if unit == "us" or unit not in _interval_units:
raise com.UnsupportedOperationError(
f"Unsupported bucket interval {op.interval!r}"
)
if op.offset is not None:
raise com.UnsupportedOperationError(
"Timestamp bucket with offset is not supported"
)

part = sa.literal_column(_interval_units[unit])
value = sa.literal_column(str(op.interval.value))
arg = t.translate(op.arg)
origin = sa.literal_column("CAST('1970-01-01' AS DATETIME2)")

return sa.func.DATE_BUCKET(part, value, arg, origin)


def _temporal_delta(t, op):
Expand Down Expand Up @@ -210,6 +233,7 @@ def _not(t, op):
),
ops.TimestampTruncate: _timestamp_truncate,
ops.DateTruncate: _timestamp_truncate,
ops.TimestampBucket: _timestamp_bucket,
ops.Hash: unary(sa.func.checksum),
ops.ExtractMicrosecond: fixed_arity(
lambda arg: sa.func.datepart(sa.literal_column("microsecond"), arg), 1
Expand Down
4 changes: 1 addition & 3 deletions ibis/backends/tests/test_temporal.py
Original file line number Diff line number Diff line change
Expand Up @@ -2507,7 +2507,6 @@ def test_delta(con, start, end, unit, expected):
"druid",
"flink",
"impala",
"mssql",
"mysql",
"oracle",
"pandas",
Expand Down Expand Up @@ -2552,7 +2551,6 @@ def test_timestamp_bucket(backend, kws, pd_freq):
"druid",
"flink",
"impala",
"mssql",
"mysql",
"oracle",
"pandas",
Expand All @@ -2565,7 +2563,7 @@ def test_timestamp_bucket(backend, kws, pd_freq):
raises=com.OperationNotDefinedError,
)
@pytest.mark.notimpl(
["clickhouse"],
["clickhouse", "mssql"],
reason="offset arg not supported",
raises=com.UnsupportedOperationError,
)
Expand Down

0 comments on commit 73d1c86

Please sign in to comment.