Skip to content

fix: raise ValueError for an out-of-range epoch in to_datetime - #5951

Open
eeshsaxena wants to merge 1 commit into
SQLMesh:mainfrom
eeshsaxena:fix/to-datetime-out-of-range
Open

fix: raise ValueError for an out-of-range epoch in to_datetime#5951
eeshsaxena wants to merge 1 commit into
SQLMesh:mainfrom
eeshsaxena:fix/to-datetime-out-of-range

Conversation

@eeshsaxena

Copy link
Copy Markdown

to_datetime (and therefore to_timestamp/to_date) leaks OverflowError/OSError on a number-like value that is out of range, instead of the ValueError it documents:

from sqlmesh.utils.date import to_datetime

to_datetime("inf")                    # OverflowError: timestamp out of range for platform time_t
to_datetime("1e30")                   # OverflowError
to_datetime("99999999999999999999")  # OSError: [Errno 22] Invalid argument

In the number branch, the value passes float() (float("inf") and large floats do not raise), strptime fails, and datetime.fromtimestamp(epoch / 1000.0) then overflows. Only ValueError was expected there, so OverflowError/OSError escape. The docstring states 'Raises: ValueError if value cannot be converted to a datetime', and "nan" already raises ValueError via the same path, so this makes the out-of-range cases consistent.

The fix catches OverflowError/OSError/ValueError around fromtimestamp and falls through to the existing raise ValueError(...). Added a parametrized regression test in tests/utils/test_date.py.

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.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant