Skip to content

Commit 4e9624f

Browse files
committed
fix: raise ValueError for an out-of-range epoch in to_datetime
to_datetime parses number-like values as millisecond epochs via datetime.fromtimestamp(epoch / 1000). A non-finite value ('inf', '-inf') or one large enough to be out of range (1e30, a huge millis integer) passes float() but overflows fromtimestamp, leaking OverflowError or OSError. The function documents ValueError as its failure mode (and to_timestamp / to_date wrap it), so catch those and fall through to the existing ValueError.
1 parent ad2d113 commit 4e9624f

2 files changed

Lines changed: 18 additions & 1 deletion

File tree

sqlmesh/utils/date.py

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -194,7 +194,13 @@ def to_datetime(
194194
try:
195195
dt = datetime.strptime(str(value), DATE_INT_FMT)
196196
except ValueError:
197-
dt = datetime.fromtimestamp(epoch / 1000.0, tz=UTC)
197+
try:
198+
dt = datetime.fromtimestamp(epoch / 1000.0, tz=UTC)
199+
except (OverflowError, OSError, ValueError):
200+
# A non-finite or out-of-range epoch (e.g. "inf", 1e30, or a
201+
# huge millis value) overflows fromtimestamp. Fall through to
202+
# the ValueError below rather than leaking OverflowError/OSError.
203+
dt = None
198204

199205
if dt is None:
200206
raise ValueError(f"Could not convert `{value}` to datetime.")

tests/utils/test_date.py

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,17 @@ def test_to_datetime() -> None:
4141
assert to_datetime("31536000000") == target
4242

4343

44+
@pytest.mark.parametrize(
45+
"value",
46+
["inf", "-inf", "1e30", "99999999999999999999", float("inf")],
47+
)
48+
def test_to_datetime_out_of_range_raises(value: t.Any) -> None:
49+
# A non-finite or out-of-range epoch overflows fromtimestamp; it must raise
50+
# the documented ValueError rather than leaking OverflowError/OSError.
51+
with pytest.raises(ValueError):
52+
to_datetime(value)
53+
54+
4455
@pytest.mark.parametrize(
4556
"expression, result",
4657
[

0 commit comments

Comments
 (0)