Skip to content

Commit 620f114

Browse files
committed
Fix: make start_date an actual date and add start_dt
1 parent cf5948a commit 620f114

File tree

3 files changed

+13
-3
lines changed

3 files changed

+13
-3
lines changed

docs/concepts/macros.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,11 @@ Variables:
5050
* @end_date
5151
* @latest_date
5252

53+
* datetime
54+
* @start_dt
55+
* @end_dt
56+
* @latest_dt
57+
5358
* ds
5459
* @start_ds
5560
* @end_ds

sqlmesh/utils/date.py

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -158,7 +158,7 @@ def to_date(value: TimeLike, relative_base: t.Optional[datetime] = None) -> date
158158

159159
def date_dict(
160160
start: TimeLike, end: TimeLike, latest: TimeLike, only_latest: bool = False
161-
) -> t.Dict[str, t.Union[str, datetime, float, int]]:
161+
) -> t.Dict[str, t.Union[str, datetime, date, float, int]]:
162162
"""Creates a kwarg dictionary of datetime variables for use in SQL Contexts.
163163
164164
Keys are like start_date, start_ds, end_date, end_ds...
@@ -172,7 +172,7 @@ def date_dict(
172172
Returns:
173173
A dictionary with various keys pointing to datetime formats.
174174
"""
175-
kwargs: t.Dict[str, t.Union[str, datetime, float, int]] = {}
175+
kwargs: t.Dict[str, t.Union[str, datetime, date, float, int]] = {}
176176

177177
prefixes = [("latest", to_datetime(latest))]
178178

@@ -183,7 +183,8 @@ def date_dict(
183183
for prefix, time_like in prefixes:
184184
dt = to_datetime(time_like)
185185
millis = to_timestamp(time_like)
186-
kwargs[f"{prefix}_date"] = dt
186+
kwargs[f"{prefix}_dt"] = dt
187+
kwargs[f"{prefix}_date"] = to_date(dt)
187188
kwargs[f"{prefix}_ds"] = to_ds(time_like)
188189
kwargs[f"{prefix}_ts"] = dt.isoformat()
189190
kwargs[f"{prefix}_epoch"] = millis / 1000

tests/core/test_model.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -550,8 +550,10 @@ def test_render_query(assert_exp_eq):
550550
FROM x AS x
551551
WHERE
552552
x.y <= '2020-10-28'
553+
AND x.y <= DATE_STR_TO_DATE('2020-10-28')
553554
AND x.y <= TIME_STR_TO_TIME('2020-10-28T23:59:59.999000+00:00')
554555
AND x.y >= '2020-10-28'
556+
AND x.y >= DATE_STR_TO_DATE('2020-10-28')
555557
AND x.y >= TIME_STR_TO_TIME('2020-10-28T00:00:00+00:00')
556558
""",
557559
)
@@ -563,8 +565,10 @@ def test_render_query(assert_exp_eq):
563565
FROM x AS x
564566
WHERE
565567
x.y <= '2020-10-28'
568+
AND x.y <= DATE_STR_TO_DATE('2020-10-28')
566569
AND x.y <= TIME_STR_TO_TIME('2020-10-28T23:59:59.999000+00:00')
567570
AND x.y >= '2020-10-28'
571+
AND x.y >= DATE_STR_TO_DATE('2020-10-28')
568572
AND x.y >= TIME_STR_TO_TIME('2020-10-28T00:00:00+00:00')
569573
""",
570574
)

0 commit comments

Comments
 (0)