Skip to content

Commit

Permalink
fix linting; add decimal input transform
Browse files Browse the repository at this point in the history
  • Loading branch information
jqin61 committed Feb 21, 2024
1 parent 132599b commit 6e16690
Show file tree
Hide file tree
Showing 2 changed files with 531 additions and 496 deletions.
19 changes: 9 additions & 10 deletions pyiceberg/partitioning.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
Optional,
Tuple,
)
from urllib.parse import quote

from pydantic import (
BeforeValidator,
Expand Down Expand Up @@ -208,7 +209,6 @@ def partition_to_path(self, data: Record, schema: Schema) -> str:

partition_field = self.fields[pos] # partition field
value_str = partition_field.transform.to_human_string(field_types[pos].field_type, value=value)
from urllib.parse import quote

value_str = quote(value_str, safe='')
value_strs.append(value_str)
Expand Down Expand Up @@ -250,10 +250,9 @@ class PartitionFieldValue:

@dataclass(frozen=True)
class PartitionKey:
raw_partition_field_values: list[PartitionFieldValue]
raw_partition_field_values: List[PartitionFieldValue]
partition_spec: PartitionSpec
schema: Schema
from functools import cached_property

@cached_property
def partition(self) -> Record: # partition key in iceberg type
Expand All @@ -263,8 +262,8 @@ def partition(self) -> Record: # partition key in iceberg type
assert len(partition_fields) == 1
partition_field = partition_fields[0]
iceberg_type = self.schema.find_field(name_or_id=raw_partition_field_value.field.source_id).field_type
_iceberg_typed_value = iceberg_typed_value(iceberg_type, raw_partition_field_value.value)
transformed_value = partition_field.transform.transform(iceberg_type)(_iceberg_typed_value)
iceberg_typed_value = _to_iceberg_type(iceberg_type, raw_partition_field_value.value)
transformed_value = partition_field.transform.transform(iceberg_type)(iceberg_typed_value)
iceberg_typed_key_values[partition_field.name] = transformed_value
return Record(**iceberg_typed_key_values)

Expand All @@ -273,21 +272,21 @@ def to_path(self) -> str:


@singledispatch
def iceberg_typed_value(type: IcebergType, value: Any) -> Any:
def _to_iceberg_type(type: IcebergType, value: Any) -> Any:
return TypeError(f"Unsupported partition field type: {type}")


@iceberg_typed_value.register(TimestampType)
@iceberg_typed_value.register(TimestamptzType)
@_to_iceberg_type.register(TimestampType)
@_to_iceberg_type.register(TimestamptzType)
def _(type: IcebergType, value: Optional[datetime]) -> Optional[int]:
return datetime_to_micros(value) if value is not None else None


@iceberg_typed_value.register(DateType)
@_to_iceberg_type.register(DateType)
def _(type: IcebergType, value: Optional[date]) -> Optional[int]:
return date_to_days(value) if value is not None else None


@iceberg_typed_value.register(PrimitiveType)
@_to_iceberg_type.register(PrimitiveType)
def _(type: IcebergType, value: Optional[Any]) -> Optional[Any]:
return value
Loading

0 comments on commit 6e16690

Please sign in to comment.