Skip to content

Commit

Permalink
Refactor {year,month,day,hour} transform
Browse files Browse the repository at this point in the history
  • Loading branch information
Fokko committed Jan 22, 2025
1 parent 302c60a commit 993c382
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 @@ -419,11 +419,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 @@ -476,11 +482,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 @@ -539,11 +551,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 @@ -604,6 +622,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 993c382

Please sign in to comment.