Skip to content

Commit

Permalink
Refactor {year,month,day,hour} transform (#1563)
Browse files Browse the repository at this point in the history
Similar to #1562
  • Loading branch information
Fokko authored Jan 23, 2025
1 parent 60d6e53 commit a0cc563
Showing 1 changed file with 21 additions and 0 deletions.
21 changes: 21 additions & 0 deletions pyiceberg/transforms.py
Original file line number Diff line number Diff line change
Expand Up @@ -444,11 +444,17 @@ def transform(self, source: IcebergType) -> Callable[[Optional[S]], Optional[int
if isinstance(source, DateType):

def year_func(v: Any) -> int:
if isinstance(v, py_datetime.date):
v = datetime.date_to_days(v)

return datetime.days_to_years(v)

elif isinstance(source, (TimestampType, TimestamptzType)):

def year_func(v: Any) -> int:
if isinstance(v, py_datetime.datetime):
v = datetime.datetime_to_micros(v)

return datetime.micros_to_years(v)

else:
Expand Down Expand Up @@ -501,11 +507,17 @@ def transform(self, source: IcebergType) -> Callable[[Optional[S]], Optional[int
if isinstance(source, DateType):

def month_func(v: Any) -> int:
if isinstance(v, py_datetime.date):
v = datetime.date_to_days(v)

return datetime.days_to_months(v)

elif isinstance(source, (TimestampType, TimestamptzType)):

def month_func(v: Any) -> int:
if isinstance(v, py_datetime.datetime):
v = datetime.datetime_to_micros(v)

return datetime.micros_to_months(v)

else:
Expand Down Expand Up @@ -564,11 +576,17 @@ def transform(self, source: IcebergType) -> Callable[[Optional[S]], Optional[int
if isinstance(source, DateType):

def day_func(v: Any) -> int:
if isinstance(v, py_datetime.date):
v = datetime.date_to_days(v)

return v

elif isinstance(source, (TimestampType, TimestamptzType)):

def day_func(v: Any) -> int:
if isinstance(v, py_datetime.datetime):
v = datetime.datetime_to_micros(v)

return datetime.micros_to_days(v)

else:
Expand Down Expand Up @@ -629,6 +647,9 @@ def transform(self, source: IcebergType) -> Callable[[Optional[S]], Optional[int
if isinstance(source, (TimestampType, TimestamptzType)):

def hour_func(v: Any) -> int:
if isinstance(v, py_datetime.datetime):
v = datetime.datetime_to_micros(v)

return datetime.micros_to_hours(v)

else:
Expand Down

0 comments on commit a0cc563

Please sign in to comment.