Skip to content

Commit 7596dc5

Browse files
TennyZhuangFokko
andauthored
Accept date in literal (#1618)
Co-authored-by: Fokko Driesprong <[email protected]>
1 parent 0aecc03 commit 7596dc5

File tree

3 files changed

+10
-3
lines changed

3 files changed

+10
-3
lines changed

pyiceberg/expressions/literals.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@
2323

2424
import struct
2525
from abc import ABC, abstractmethod
26-
from datetime import datetime
26+
from datetime import date, datetime
2727
from decimal import ROUND_HALF_UP, Decimal
2828
from functools import singledispatchmethod
2929
from math import isnan
@@ -50,6 +50,7 @@
5050
)
5151
from pyiceberg.utils.datetime import (
5252
date_str_to_days,
53+
date_to_days,
5354
datetime_to_micros,
5455
micros_to_days,
5556
time_str_to_micros,
@@ -149,6 +150,8 @@ def literal(value: L) -> Literal[L]:
149150
return DecimalLiteral(value)
150151
elif isinstance(value, datetime):
151152
return TimestampLiteral(datetime_to_micros(value)) # type: ignore
153+
elif isinstance(value, date):
154+
return DateLiteral(date_to_days(value)) # type: ignore
152155
else:
153156
raise TypeError(f"Invalid literal value: {repr(value)}")
154157

pyiceberg/typedef.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@
1717
from __future__ import annotations
1818

1919
from abc import abstractmethod
20-
from datetime import datetime
20+
from datetime import date, datetime
2121
from decimal import Decimal
2222
from functools import lru_cache
2323
from typing import (
@@ -94,7 +94,7 @@ def __missing__(self, key: K) -> V:
9494
"""A recursive dictionary type for nested structures in PyIceberg."""
9595

9696
# Represents the literal value
97-
L = TypeVar("L", str, bool, int, float, bytes, UUID, Decimal, datetime, covariant=True)
97+
L = TypeVar("L", str, bool, int, float, bytes, UUID, Decimal, datetime, date, covariant=True)
9898

9999

100100
@runtime_checkable

tests/expressions/test_literals.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -910,6 +910,10 @@ def test_literal_from_datetime() -> None:
910910
assert isinstance(literal(datetime.datetime.now()), TimestampLiteral)
911911

912912

913+
def test_literal_from_date() -> None:
914+
assert isinstance(literal(datetime.date.today()), DateLiteral)
915+
916+
913917
# __ __ ___
914918
# | \/ |_ _| _ \_ _
915919
# | |\/| | || | _/ || |

0 commit comments

Comments
 (0)