Skip to content

Commit

Permalink
Support last_update_time in nanoseconds
Browse files Browse the repository at this point in the history
It was observed that in old versions of weaviate, the last_update_time
field is being retrieved in nanoseconds which causes a ValueError
exception.

fixes: gh-958
  • Loading branch information
jfrancoa committed Mar 15, 2024
1 parent fa0f4cd commit e56302e
Showing 1 changed file with 12 additions and 6 deletions.
18 changes: 12 additions & 6 deletions weaviate/collections/queries/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,16 @@ def __init__(
validate_arguments=self._validate_arguments,
)

def __retrieve_timestamp(
self,
timestamp: int,
) -> datetime.datetime:
# Handle the case in which last_update_time_unix is in nanoseconds or milliseconds, issue #958
if len(str(timestamp)) <= 13:
return datetime.datetime.fromtimestamp(timestamp / 1000, tz=datetime.timezone.utc)
else:
return datetime.datetime.fromtimestamp(timestamp / 1e9, tz=datetime.timezone.utc)

def __extract_metadata_for_object(
self,
add_props: "search_get_pb2.MetadataResult",
Expand All @@ -102,16 +112,12 @@ def __extract_metadata_for_object(
distance=add_props.distance if add_props.distance_present else None,
certainty=add_props.certainty if add_props.certainty_present else None,
creation_time=(
datetime.datetime.fromtimestamp(
add_props.creation_time_unix / 1000, tz=datetime.timezone.utc
)
self.__retrieve_timestamp(add_props.creation_time_unix)
if add_props.creation_time_unix_present
else None
),
last_update_time=(
datetime.datetime.fromtimestamp(
add_props.last_update_time_unix / 1000, tz=datetime.timezone.utc
)
self.__retrieve_timestamp(add_props.last_update_time_unix)
if add_props.last_update_time_unix_present
else None
),
Expand Down

0 comments on commit e56302e

Please sign in to comment.