Skip to content

Commit 6a280fd

Browse files
committed
fix(date): import pandas locally in make_ts_exclusive
make_ts_exclusive uses pd.Timedelta in its tsql branch, but pandas is only imported under TYPE_CHECKING (pandas is banned as a module-level import). Unlike its siblings make_inclusive_end and to_utc_timestamp, it never imports pandas at runtime, so make_ts_exclusive(time, dialect="tsql") raises NameError: name 'pd' is not defined. This is reachable when rendering an INCREMENTAL_BY_TIME_RANGE model with a jinja source/ref event-time filter on the tsql dialect (renderer.py), which surfaces as a ConfigError that the model could not be rendered. Add a local `import pandas as pd` inside make_ts_exclusive, mirroring the existing runtime-pandas helpers, and add a regression test covering both the tsql and non-tsql paths. Signed-off-by: Anas Khan <83116240+anxkhn@users.noreply.github.com>
1 parent b0bc176 commit 6a280fd

2 files changed

Lines changed: 15 additions & 0 deletions

File tree

sqlmesh/utils/date.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -344,6 +344,8 @@ def make_exclusive(time: TimeLike) -> datetime:
344344

345345

346346
def make_ts_exclusive(time: TimeLike, dialect: DialectType) -> datetime:
347+
import pandas as pd
348+
347349
ts = to_datetime(time)
348350
if dialect == "tsql":
349351
return to_utc_timestamp(ts) - pd.Timedelta(1, unit="ns")

tests/utils/test_date.py

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,11 +14,13 @@
1414
is_categorical_relative_expression,
1515
is_relative,
1616
make_inclusive,
17+
make_ts_exclusive,
1718
to_datetime,
1819
to_time_column,
1920
to_timestamp,
2021
to_ts,
2122
to_tstz,
23+
to_utc_timestamp,
2224
)
2325

2426

@@ -141,6 +143,17 @@ def test_make_inclusive_tsql(start_in, end_in, start_out, end_out, dialect) -> N
141143
)
142144

143145

146+
def test_make_ts_exclusive() -> None:
147+
# tsql subtracts 1ns from the UTC timestamp; must not raise NameError (pd not imported)
148+
assert make_ts_exclusive("2020-01-01 12:00:00", dialect="tsql") == to_utc_timestamp(
149+
to_datetime("2020-01-01 12:00:00")
150+
) - pd.Timedelta(1, unit="ns")
151+
# non-tsql dialects add 1 microsecond
152+
assert make_ts_exclusive("2020-01-01 12:00:00", dialect="duckdb") == to_datetime(
153+
"2020-01-01 12:00:00.000001"
154+
)
155+
156+
144157
@pytest.mark.parametrize(
145158
"expression, result",
146159
[

0 commit comments

Comments
 (0)