Skip to content

Commit c52d415

Browse files
committed
make exclusive ts instead of between patch
1 parent a595791 commit c52d415

File tree

4 files changed

+20
-26
lines changed

4 files changed

+20
-26
lines changed

sqlmesh/core/renderer.py

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,14 @@
1616
from sqlmesh.core import constants as c
1717
from sqlmesh.core import dialect as d
1818
from sqlmesh.core.macros import MacroEvaluator, RuntimeStage
19-
from sqlmesh.utils.date import TimeLike, date_dict, make_inclusive, to_datetime
19+
from sqlmesh.utils.date import (
20+
TimeLike,
21+
date_dict,
22+
make_inclusive,
23+
to_datetime,
24+
make_ts_exclusive,
25+
to_tstz,
26+
)
2027
from sqlmesh.utils.errors import (
2128
ConfigError,
2229
ParsetimeAdapterCallError,
@@ -213,7 +220,9 @@ def _resolve_table(table: str | exp.Table) -> str:
213220
for ref in all_refs:
214221
if ref.event_time_filter:
215222
ref.event_time_filter["start"] = render_kwargs["start_tstz"]
216-
ref.event_time_filter["end"] = render_kwargs["end_tstz"]
223+
ref.event_time_filter["end"] = to_tstz(
224+
make_ts_exclusive(render_kwargs["end_tstz"], dialect=self._dialect)
225+
)
217226
jinja_env = self._jinja_macro_registry.build_environment(**jinja_env_kwargs)
218227

219228
expressions = [self._expression]

sqlmesh/dbt/__init__.py

Lines changed: 0 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -2,25 +2,3 @@
22
create_builtin_filters as create_builtin_filters,
33
create_builtin_globals as create_builtin_globals,
44
)
5-
from sqlmesh.dbt.util import DBT_VERSION
6-
7-
8-
if DBT_VERSION >= (1, 9, 0):
9-
from dbt.adapters.base.relation import BaseRelation, EventTimeFilter
10-
11-
def _render_event_time_filtered_inclusive(
12-
self: BaseRelation, event_time_filter: EventTimeFilter
13-
) -> str:
14-
"""
15-
Returns "" if start and end are both None
16-
"""
17-
filter = ""
18-
if event_time_filter.start and event_time_filter.end:
19-
filter = f"{event_time_filter.field_name} BETWEEN '{event_time_filter.start}' and '{event_time_filter.end}'"
20-
elif event_time_filter.start:
21-
filter = f"{event_time_filter.field_name} >= '{event_time_filter.start}'"
22-
elif event_time_filter.end:
23-
filter = f"{event_time_filter.field_name} <= '{event_time_filter.end}'"
24-
return filter
25-
26-
BaseRelation._render_event_time_filtered = _render_event_time_filtered_inclusive # type: ignore

sqlmesh/utils/date.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -343,6 +343,13 @@ def make_exclusive(time: TimeLike) -> datetime:
343343
return dt
344344

345345

346+
def make_ts_exclusive(time: TimeLike, dialect: DialectType) -> datetime:
347+
ts = to_datetime(time)
348+
if dialect == "tsql":
349+
return to_utc_timestamp(ts) - pd.Timedelta(1, unit="ns")
350+
return ts + timedelta(microseconds=1)
351+
352+
346353
def to_utc_timestamp(time: datetime) -> pd.Timestamp:
347354
import pandas as pd
348355

tests/dbt/test_model.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -358,11 +358,11 @@ def test_load_microbatch_with_ref(
358358
context = Context(paths=project_dir)
359359
assert (
360360
context.render(microbatch_snapshot_fqn, start="2025-01-01", end="2025-01-10").sql()
361-
== 'SELECT "cola" AS "cola", "ds" AS "ds" FROM (SELECT * FROM "local"."my_source"."my_table" AS "my_table" WHERE "ds" BETWEEN \'2025-01-01 00:00:00+00:00\' AND \'2025-01-10 23:59:59.999999+00:00\') AS "_q_0"'
361+
== 'SELECT "cola" AS "cola", "ds" AS "ds" FROM (SELECT * FROM "local"."my_source"."my_table" AS "my_table" WHERE "ds" >= \'2025-01-01 00:00:00+00:00\' AND "ds" < \'2025-01-11 00:00:00+00:00\') AS "_q_0"'
362362
)
363363
assert (
364364
context.render(microbatch_two_snapshot_fqn, start="2025-01-01", end="2025-01-10").sql()
365-
== 'SELECT "_q_0"."cola" AS "cola", "_q_0"."ds" AS "ds" FROM (SELECT "microbatch"."cola" AS "cola", "microbatch"."ds" AS "ds" FROM "local"."main"."microbatch" AS "microbatch" WHERE "microbatch"."ds" <= \'2025-01-10 23:59:59.999999+00:00\' AND "microbatch"."ds" >= \'2025-01-01 00:00:00+00:00\') AS "_q_0"'
365+
== 'SELECT "_q_0"."cola" AS "cola", "_q_0"."ds" AS "ds" FROM (SELECT "microbatch"."cola" AS "cola", "microbatch"."ds" AS "ds" FROM "local"."main"."microbatch" AS "microbatch" WHERE "microbatch"."ds" < \'2025-01-11 00:00:00+00:00\' AND "microbatch"."ds" >= \'2025-01-01 00:00:00+00:00\') AS "_q_0"'
366366
)
367367

368368

0 commit comments

Comments
 (0)