Skip to content

Commit

Permalink
feat: support datetime objects in literal instantiation
Browse files Browse the repository at this point in the history
  • Loading branch information
jayceslesar committed Jan 20, 2025
1 parent b15934d commit 200e6c8
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 0 deletions.
4 changes: 4 additions & 0 deletions pyiceberg/expressions/literals.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@

import struct
from abc import ABC, abstractmethod
from datetime import datetime
from decimal import ROUND_HALF_UP, Decimal
from functools import singledispatchmethod
from math import isnan
Expand All @@ -49,6 +50,7 @@
)
from pyiceberg.utils.datetime import (
date_str_to_days,
datetime_to_micros,
micros_to_days,
time_str_to_micros,
timestamp_to_micros,
Expand Down Expand Up @@ -145,6 +147,8 @@ def literal(value: L) -> Literal[L]:
return BinaryLiteral(value)
elif isinstance(value, Decimal):
return DecimalLiteral(value)
elif isinstance(value, datetime):
return TimestampLiteral(datetime_to_micros(value))
else:
raise TypeError(f"Invalid literal value: {repr(value)}")

Expand Down
4 changes: 4 additions & 0 deletions tests/expressions/test_literals.py
Original file line number Diff line number Diff line change
Expand Up @@ -906,6 +906,10 @@ def test_uuid_to_binary() -> None:
assert isinstance(binary_literal, BinaryLiteral) # type: ignore


def test_literal_from_datetime() -> None:
assert isinstance(literal(datetime.datetime.now()), TimestampLiteral)


# __ __ ___
# | \/ |_ _| _ \_ _
# | |\/| | || | _/ || |
Expand Down

0 comments on commit 200e6c8

Please sign in to comment.