Skip to content

Commit

Permalink
fix(mssql): fix ops.TimestampFromUNIX
Browse files Browse the repository at this point in the history
  • Loading branch information
krzysztof-kwitt authored and cpcloud committed Dec 29, 2022
1 parent 39e3962 commit ec28add
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 9 deletions.
12 changes: 9 additions & 3 deletions ibis/backends/mssql/registry.py
Original file line number Diff line number Diff line change
Expand Up @@ -83,8 +83,12 @@ def _round(t, op):
return sa.func.round(sa_arg, 0)


def _timestamp_from_unix(x):
return sa.func.dateadd(sa.text('s'), x, '1970-01-01 00:00:00')
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 ValueError(f"{unit!r} unit is not supported!")


operation_registry = sqlalchemy_operation_registry.copy()
Expand Down Expand Up @@ -152,7 +156,9 @@ def _timestamp_from_unix(x):
),
1,
),
ops.TimestampFromUNIX: fixed_arity(_timestamp_from_unix, 1),
ops.TimestampFromUNIX: lambda t, op: _timestamp_from_unix(
t.translate(op.arg), op.unit
),
ops.DateFromYMD: fixed_arity(sa.func.datefromparts, 3),
ops.TimestampFromYMDHMS: fixed_arity(
lambda y, m, d, h, min, s: sa.func.datetimefromparts(y, m, d, h, min, s, 0),
Expand Down
10 changes: 4 additions & 6 deletions ibis/backends/tests/test_temporal.py
Original file line number Diff line number Diff line change
Expand Up @@ -552,7 +552,7 @@ def test_strftime(backend, alltypes, df, expr_fn, pandas_pattern):
backend.assert_series_equal(result, expected)


unit_factors = {'s': int(1e9), 'ms': int(1e6), 'us': int(1e3), 'ns': 1}
unit_factors = {'s': 10**9, 'ms': 10**6, 'us': 10**3, 'ns': 1}


@pytest.mark.parametrize(
Expand All @@ -565,17 +565,15 @@ def test_strftime(backend, alltypes, df, expr_fn, pandas_pattern):
),
param(
'us',
marks=pytest.mark.notimpl(["clickhouse", "duckdb", "pyspark"]),
marks=pytest.mark.notimpl(["clickhouse", "duckdb", "pyspark", "mssql"]),
),
param(
'ns',
marks=pytest.mark.notimpl(["clickhouse", "duckdb", "pyspark"]),
marks=pytest.mark.notimpl(["clickhouse", "duckdb", "pyspark", "mssql"]),
),
],
)
@pytest.mark.notimpl(
["datafusion", "mysql", "postgres", "sqlite", "snowflake", "mssql"]
)
@pytest.mark.notimpl(["datafusion", "mysql", "postgres", "sqlite", "snowflake"])
def test_integer_to_timestamp(backend, con, unit):
backend_unit = backend.returned_timestamp_unit
factor = unit_factors[unit]
Expand Down

0 comments on commit ec28add

Please sign in to comment.