diff --git a/sqlmesh/utils/date.py b/sqlmesh/utils/date.py index bdc15125d4..5358719abe 100644 --- a/sqlmesh/utils/date.py +++ b/sqlmesh/utils/date.py @@ -344,6 +344,8 @@ def make_exclusive(time: TimeLike) -> datetime: def make_ts_exclusive(time: TimeLike, dialect: DialectType) -> datetime: + import pandas as pd + ts = to_datetime(time) if dialect == "tsql": return to_utc_timestamp(ts) - pd.Timedelta(1, unit="ns") diff --git a/tests/utils/test_date.py b/tests/utils/test_date.py index cb35a6973c..bbd5bdf63a 100644 --- a/tests/utils/test_date.py +++ b/tests/utils/test_date.py @@ -14,11 +14,13 @@ is_categorical_relative_expression, is_relative, make_inclusive, + make_ts_exclusive, to_datetime, to_time_column, to_timestamp, to_ts, to_tstz, + to_utc_timestamp, ) @@ -141,6 +143,17 @@ def test_make_inclusive_tsql(start_in, end_in, start_out, end_out, dialect) -> N ) +def test_make_ts_exclusive() -> None: + # tsql subtracts 1ns from the UTC timestamp; must not raise NameError (pd not imported) + assert make_ts_exclusive("2020-01-01 12:00:00", dialect="tsql") == to_utc_timestamp( + to_datetime("2020-01-01 12:00:00") + ) - pd.Timedelta(1, unit="ns") + # non-tsql dialects add 1 microsecond + assert make_ts_exclusive("2020-01-01 12:00:00", dialect="duckdb") == to_datetime( + "2020-01-01 12:00:00.000001" + ) + + @pytest.mark.parametrize( "expression, result", [