Repository navigation
Fix partitioned appends of narrow integer, s/ms timestamp and large_binary columns #4421
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
sohailshk
wants to merge
4
commits into
apache:main
Choose a base branch
from
sohailshk:fix/partition-arrow-types
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+137
−18
Open
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
70ba74a
Fix partitioned appends of narrow integer, s/ms timestamp and large_b…
sohailshk 7c10d92
Update integration test for truncate on large_binary partitions
sohailshk 1ced59e
Merge remote-tracking branch 'origin/main' into fix/partition-arrow-t…
sohailshk cf7c806
Merge remote-tracking branch 'origin/main' into fix/partition-arrow-t…
sohailshk File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change | ||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
|
@@ -160,7 +160,7 @@ | |||||||||||
| from pyiceberg.table.metadata import TableMetadata | ||||||||||||
| from pyiceberg.table.metadata_columns import LAST_UPDATED_SEQUENCE_NUMBER, ROW_ID | ||||||||||||
| from pyiceberg.table.name_mapping import NameMapping, apply_name_mapping | ||||||||||||
| from pyiceberg.transforms import IdentityTransform, TruncateTransform | ||||||||||||
| from pyiceberg.transforms import IdentityTransform, Transform, TruncateTransform | ||||||||||||
| from pyiceberg.typedef import EMPTY_DICT, Properties, Record, TableVersion | ||||||||||||
| from pyiceberg.types import ( | ||||||||||||
| DEFAULT_GEOMETRY_CRS, | ||||||||||||
|
|
@@ -3471,6 +3471,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) | ||||||||||||
| elif ( | ||||||||||||
| isinstance(source_type, BinaryType) and pa.types.is_large_binary(arrow_type) and isinstance(transform, TruncateTransform) | ||||||||||||
| ): | ||||||||||||
| target = pa.binary() | ||||||||||||
|
|
||||||||||||
| if target is None or target == arrow_type: | ||||||||||||
| return array | ||||||||||||
| return array.cast(target) | ||||||||||||
|
|
||||||||||||
|
|
||||||||||||
| def _determine_partitions(spec: PartitionSpec, schema: Schema, arrow_table: pa.Table) -> Iterable[_TablePartition]: | ||||||||||||
| """Based on the iceberg table partition spec, filter the arrow table into partitions with their keys. | ||||||||||||
|
|
||||||||||||
|
|
@@ -3499,7 +3529,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 | ||||||||||||
| ) | ||||||||||||
|
Comment on lines
+3532
to
+3534
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I'm wondering if we could simplify it as:
Suggested change
|
||||||||||||
| partition_array = partition.transform.pyarrow_transform(source_field.field_type)(field_array) | ||||||||||||
| # PyArrow has no group by or equality kernels for extension types such as arrow.uuid, so use their storage type | ||||||||||||
| if isinstance(partition_array.type, pa.BaseExtensionType): | ||||||||||||
|
|
||||||||||||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Does the cast operation also scale from seconds to ms? Please check