Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 9 additions & 2 deletions pyiceberg/partitioning.py
Original file line number Diff line number Diff line change
Expand Up @@ -546,8 +546,15 @@ def _(type: IcebergType, value: int | date | None) -> int | None:


@_to_partition_representation.register(TimeType)
def _(type: IcebergType, value: time | None) -> int | None:
return time_to_micros(value) if value is not None else None
def _(type: IcebergType, value: int | time | None) -> int | None:
if value is None:
return None
elif isinstance(value, int):
return value
elif isinstance(value, time):
return time_to_micros(value)
else:
raise ValueError(f"Type not recognized: {value}")


@_to_partition_representation.register(UUIDType)
Expand Down
23 changes: 22 additions & 1 deletion tests/io/test_pyarrow.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@
import zlib
from collections.abc import Iterator
from dataclasses import replace
from datetime import UTC, date, datetime
from datetime import UTC, date, datetime, time
from decimal import Decimal
from pathlib import Path
from typing import Any, Literal
Expand Down Expand Up @@ -3474,6 +3474,27 @@ def test_append_view_types_to_partitioned_table(catalog: InMemoryCatalog) -> Non
assert sorted(table.scan().to_arrow().to_pylist(), key=lambda row: row["id"]) == arrow_table.to_pylist()


def test_append_to_table_bucketed_by_time(catalog: InMemoryCatalog) -> None:
schema = Schema(
NestedField(id=1, name="id", field_type=LongType(), required=False),
NestedField(id=2, name="event_time", field_type=TimeType(), required=False),
)
spec = PartitionSpec(PartitionField(source_id=2, field_id=1000, transform=BucketTransform(4), name="event_time_bucket"))
catalog.create_namespace("default")
table = catalog.create_table("default.test_append_bucketed_by_time", schema=schema, partition_spec=spec)

arrow_table = pa.table(
{
"id": pa.array([1, 2, 3], type=pa.int64()),
"event_time": pa.array([time(9, 30), time(22, 31, 8), None], type=pa.time64("us")),
}
)
table.append(arrow_table)

assert sorted(table.scan().to_arrow().to_pylist(), key=lambda row: row["id"]) == arrow_table.to_pylist()
assert table.scan(row_filter=EqualTo("event_time", "22:31:08")).to_arrow()["id"].to_pylist() == [2]


def test_inspect_partition_for_nested_field(catalog: InMemoryCatalog) -> None:
schema = Schema(
NestedField(id=1, name="foo", field_type=StringType(), required=True),
Expand Down
11 changes: 10 additions & 1 deletion tests/test_transforms.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
# under the License.
# pylint: disable=eval-used,protected-access,redefined-outer-name
from collections.abc import Callable
from datetime import date, datetime
from datetime import date, datetime, time
from decimal import Decimal
from typing import Annotated, Any
from uuid import UUID
Expand Down Expand Up @@ -1643,11 +1643,20 @@ def test_to_partition_representation_timestamps(source_type: PrimitiveType, valu
assert _to_partition_representation(source_type, value) == expected


def test_to_partition_representation_time() -> None:
assert _to_partition_representation(TimeType(), None) is None
assert _to_partition_representation(TimeType(), time(22, 31, 8)) == 81068000000
# The bucket transform returns an int, which is already the partition representation
assert _to_partition_representation(TimeType(), 3) == 3


def test_to_partition_representation_unrecognized_type_raises() -> None:
with pytest.raises(ValueError, match="Type not recognized"):
_to_partition_representation(TimestampType(), "not-a-datetime-or-int")
with pytest.raises(ValueError, match="Type not recognized"):
_to_partition_representation(TimestampNanoType(), "not-a-datetime-or-int")
with pytest.raises(ValueError, match="Type not recognized"):
_to_partition_representation(TimeType(), "not-a-time-or-int")


@pytest.mark.parametrize(
Expand Down
Loading