Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: Add missing operations for SQL Server - ExtractEpochSeconds, ExtractDayOfYear, ExtractWeekOfYear #870

Merged
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
33 changes: 33 additions & 0 deletions third_party/ibis/ibis_mssql/compiler.py
Original file line number Diff line number Diff line change
Expand Up @@ -371,10 +371,29 @@ def _day_of_week_name(t, expr):
(sa_arg,) = map(t.translate, expr.op().args)
return sa.func.trim(sa.func.format(sa_arg, 'dddd'))


def _string_join(t, expr):
sep, elements = expr.op().args
return sa.func.concat(*map(t.translate, elements))


def _timestamp_truncate(t, op):
helensilva14 marked this conversation as resolved.
Show resolved Hide resolved
arg = t.translate(op.arg)
unit = op.unit.short
if unit not in _truncate_precisions:
raise com.UnsupportedOperationError(f'Unsupported truncate unit {op.unit!r}')

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


def _timestamp_from_unix(x, unit='s'):
if 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 com.UnsupportedOperationError(f"{unit!r} unit is not supported!")


_operation_registry = alch._operation_registry.copy()

_operation_registry.update(
Expand Down Expand Up @@ -418,11 +437,25 @@ def _string_join(t, expr):
ops.ExtractYear: _extract('year'),
ops.ExtractMonth: _extract('month'),
ops.ExtractDay: _extract('day'),
ops.ExtractDayOfYear: _extract('dayofyear'),
ops.ExtractHour: _extract('hour'),
ops.ExtractMinute: _extract('minute'),
ops.ExtractSecond: _extract('second'),
ops.ExtractMillisecond: _extract('millisecond'),
ops.ExtractWeekOfYear: _extract('iso_week'),
ops.Strftime: _strftime,
ops.ExtractEpochSeconds: fixed_arity(
lambda x: sa.cast(
sa.func.datediff(sa.text('s'), '1970-01-01 00:00:00', x), sa.BIGINT
),
1,
),
ops.TimestampFromUNIX: lambda t, op: _timestamp_from_unix(
t.translate(op.arg), op.unit.short
),
ops.TimestampTruncate: _timestamp_truncate,
ops.DateTruncate: _timestamp_truncate,
ops.Hash: unary(sa.func.checksum),
helensilva14 marked this conversation as resolved.
Show resolved Hide resolved
# newly added
ops.Lag: _lag,
ops.Lead: _lead,
Expand Down