From 200e6c860eeabbf04b16b4b46c9ae70df3c362ee Mon Sep 17 00:00:00 2001 From: Jayce Date: Mon, 20 Jan 2025 16:54:49 -0500 Subject: [PATCH] feat: support datetime objects in literal instantiation --- pyiceberg/expressions/literals.py | 4 ++++ tests/expressions/test_literals.py | 4 ++++ 2 files changed, 8 insertions(+) diff --git a/pyiceberg/expressions/literals.py b/pyiceberg/expressions/literals.py index d1c170d0dd..d5c92206b2 100644 --- a/pyiceberg/expressions/literals.py +++ b/pyiceberg/expressions/literals.py @@ -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 @@ -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, @@ -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)}") diff --git a/tests/expressions/test_literals.py b/tests/expressions/test_literals.py index 59c2a3deaa..6a64f8a038 100644 --- a/tests/expressions/test_literals.py +++ b/tests/expressions/test_literals.py @@ -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) + + # __ __ ___ # | \/ |_ _| _ \_ _ # | |\/| | || | _/ || |