Fokko commented on code in PR #4421:
URL: https://github.com/apache/iceberg-python/pull/4421#discussion_r4705999413
##########
pyiceberg/io/pyarrow.py:
##########
@@ -3322,6 +3322,36 @@ class _TablePartition:
arrow_table_partition: pa.Table
+def _cast_to_transform_input_type(array: pa.Array, source_type: IcebergType,
transform: Transform[Any, Any]) -> pa.Array:
+ """Cast a partition source column to the Arrow type that the partition
transform kernels accept.
+
+ A write accepts Arrow columns that can be converted to the Iceberg type,
for example int16 for an int column,
+ millisecond timestamps for a timestamp column or large_binary for a binary
column. The bucket, truncate,
+ day and hour kernels of pyiceberg-core only handle the Arrow type that
matches the Iceberg type exactly.
+ Only the truncate kernel lacks large_binary support, so large_binary is
cast to binary for truncate alone.
+ """
+ arrow_type = array.type
+ target: pa.DataType | None = None
+ if isinstance(source_type, IntegerType) and
pa.types.is_integer(arrow_type):
+ target = pa.int32()
+ elif isinstance(source_type, LongType) and pa.types.is_integer(arrow_type):
+ target = pa.int64()
+ elif (
+ isinstance(source_type, (TimestampType, TimestamptzType))
+ and pa.types.is_timestamp(arrow_type)
+ and arrow_type.unit in ("s", "ms")
+ ):
+ target = pa.timestamp("us", tz=arrow_type.tz)
Review Comment:
It feels to me that we're duplicating logic here. This is actually
converting Iceberg types to Arrow, and I don't think it is correct, since
TimestampType should not have a timezone at all. Please check my suggestion
below.
##########
pyiceberg/io/pyarrow.py:
##########
@@ -3355,7 +3385,9 @@ def _determine_partitions(spec: PartitionSpec, schema:
Schema, arrow_table: pa.T
full_field_name = schema.find_column_name(partition.source_id)
if full_field_name is None:
raise ValueError(f"Could not find column name for field ID:
{partition.source_id}")
- field_array = _get_field_from_arrow_table(arrow_table, full_field_name)
+ field_array = _cast_to_transform_input_type(
+ _get_field_from_arrow_table(arrow_table, full_field_name),
source_field.field_type, partition.transform
+ )
Review Comment:
I'm wondering if we could simplify it as:
```suggestion
field_array = _cast_to_transform_input_type(
_get_field_from_arrow_table(arrow_table,
full_field_name).cast(schema_to_pyarrow(source_field.field_type))
```
##########
pyiceberg/io/pyarrow.py:
##########
@@ -3322,6 +3322,36 @@ class _TablePartition:
arrow_table_partition: pa.Table
+def _cast_to_transform_input_type(array: pa.Array, source_type: IcebergType,
transform: Transform[Any, Any]) -> pa.Array:
+ """Cast a partition source column to the Arrow type that the partition
transform kernels accept.
+
+ A write accepts Arrow columns that can be converted to the Iceberg type,
for example int16 for an int column,
+ millisecond timestamps for a timestamp column or large_binary for a binary
column. The bucket, truncate,
+ day and hour kernels of pyiceberg-core only handle the Arrow type that
matches the Iceberg type exactly.
+ Only the truncate kernel lacks large_binary support, so large_binary is
cast to binary for truncate alone.
+ """
+ arrow_type = array.type
+ target: pa.DataType | None = None
+ if isinstance(source_type, IntegerType) and
pa.types.is_integer(arrow_type):
+ target = pa.int32()
+ elif isinstance(source_type, LongType) and pa.types.is_integer(arrow_type):
+ target = pa.int64()
+ elif (
+ isinstance(source_type, (TimestampType, TimestamptzType))
+ and pa.types.is_timestamp(arrow_type)
+ and arrow_type.unit in ("s", "ms")
+ ):
+ target = pa.timestamp("us", tz=arrow_type.tz)
Review Comment:
Does the cast operation also scale from seconds to ms? Please check
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: [email protected]
For queries about this service, please contact Infrastructure at:
[email protected]
---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]