Repository navigation
feat(rest): preserve column bounds and key metadata during REST scan planning #4544
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
SurenderguliaEnginner
wants to merge
2
commits into
apache:main
Choose a base branch
from
SurenderguliaEnginner:fix/4528-rest-scan-planning-metadata
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.
+91
−12
Open
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
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
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 | ||||
|---|---|---|---|---|---|---|
|
|
@@ -16,6 +16,7 @@ | |||||
| # under the License. | ||||||
| from __future__ import annotations | ||||||
|
|
||||||
| import base64 | ||||||
| import itertools | ||||||
| import logging | ||||||
| import math | ||||||
|
|
@@ -34,6 +35,7 @@ | |||||
| from cachetools import LRUCache | ||||||
| from pydantic import Field | ||||||
|
|
||||||
| from pyiceberg.conversions import to_bytes | ||||||
| from pyiceberg.encryption.kms import KeyManagementClient | ||||||
| from pyiceberg.exceptions import CommitFailedException, CommitStateUnknownException, ValidationException | ||||||
| from pyiceberg.expressions import AlwaysFalse, AlwaysTrue, And, BooleanExpression, EqualTo, IsNaN, IsNull, Or, Reference | ||||||
|
|
@@ -116,7 +118,7 @@ | |||||
| Record, | ||||||
| TableVersion, | ||||||
| ) | ||||||
| from pyiceberg.types import strtobool | ||||||
| from pyiceberg.types import PrimitiveType, strtobool | ||||||
| from pyiceberg.utils.concurrent import ExecutorFactory | ||||||
| from pyiceberg.utils.config import Config | ||||||
| from pyiceberg.utils.properties import property_as_bool, property_as_int | ||||||
|
|
@@ -2424,13 +2426,15 @@ def from_rest_response( | |||||
| delete_files: list[RESTDeleteFile], | ||||||
| *, | ||||||
| fallback_residual: BooleanExpression = ALWAYS_TRUE, | ||||||
| table_schema: Schema | None = None, | ||||||
| ) -> FileScanTask: | ||||||
| """Convert a RESTFileScanTask to a FileScanTask. | ||||||
|
|
||||||
| Args: | ||||||
| rest_task: The REST file scan task. | ||||||
| delete_files: The list of delete files from the ScanTasks response. | ||||||
| fallback_residual: The scan filter to use when the server omits the residual. | ||||||
| table_schema: The table schema used to serialize column bounds. | ||||||
|
|
||||||
| Returns: | ||||||
| A FileScanTask with the converted data and delete files. | ||||||
|
|
@@ -2440,15 +2444,19 @@ def from_rest_response( | |||||
| """ | ||||||
| from pyiceberg.catalog.rest.scan_planning import RESTEqualityDeleteFile | ||||||
|
|
||||||
| data_file = _rest_file_to_data_file(rest_task.data_file) | ||||||
| data_file = _rest_file_to_data_file(rest_task.data_file, table_schema=table_schema) | ||||||
|
|
||||||
| resolved_deletes: set[DataFile] = set() | ||||||
| if rest_task.delete_file_references: | ||||||
| for idx in rest_task.delete_file_references: | ||||||
| delete_file = delete_files[idx] | ||||||
| if isinstance(delete_file, RESTEqualityDeleteFile): | ||||||
| raise NotImplementedError(f"PyIceberg does not yet support equality deletes: {delete_file.file_path}") | ||||||
| resolved_deletes.add(_rest_file_to_data_file(delete_file, default_referenced_data_file=data_file.file_path)) | ||||||
| resolved_deletes.add( | ||||||
| _rest_file_to_data_file( | ||||||
| delete_file, default_referenced_data_file=data_file.file_path, table_schema=table_schema | ||||||
| ) | ||||||
| ) | ||||||
|
|
||||||
| return FileScanTask( | ||||||
| data_file=data_file, | ||||||
|
|
@@ -2457,7 +2465,25 @@ def from_rest_response( | |||||
| ) | ||||||
|
|
||||||
|
|
||||||
| def _rest_file_to_data_file(rest_file: RESTContentFile, default_referenced_data_file: str | None = None) -> DataFile: | ||||||
| def _serialize_bounds(raw_bounds: dict[int, Any] | None, schema: Schema | None) -> dict[int, bytes] | None: | ||||||
| if not raw_bounds: | ||||||
| return None | ||||||
| serialized: dict[int, bytes] = {} | ||||||
| for field_id, val in raw_bounds.items(): | ||||||
| if isinstance(val, bytes): | ||||||
| serialized[field_id] = val | ||||||
| elif schema is not None: | ||||||
| field = schema.find_field(field_id) | ||||||
| if field is not None and isinstance(field.field_type, PrimitiveType): | ||||||
| serialized[field_id] = to_bytes(field.field_type, val) | ||||||
| return serialized or None | ||||||
|
|
||||||
|
|
||||||
| def _rest_file_to_data_file( | ||||||
| rest_file: RESTContentFile, | ||||||
| default_referenced_data_file: str | None = None, | ||||||
| table_schema: Schema | None = None, | ||||||
| ) -> DataFile: | ||||||
| """Convert a REST content file to a manifest DataFile.""" | ||||||
| from pyiceberg.catalog.rest.scan_planning import RESTDataFile, RESTPositionDeleteFile | ||||||
|
|
||||||
|
|
@@ -2466,11 +2492,17 @@ def _rest_file_to_data_file(rest_file: RESTContentFile, default_referenced_data_ | |||||
| value_counts = rest_file.value_counts.to_dict() if rest_file.value_counts else None | ||||||
| null_value_counts = rest_file.null_value_counts.to_dict() if rest_file.null_value_counts else None | ||||||
| nan_value_counts = rest_file.nan_value_counts.to_dict() if rest_file.nan_value_counts else None | ||||||
| lower_bounds = _serialize_bounds(rest_file.lower_bounds.to_dict() if rest_file.lower_bounds else None, table_schema) | ||||||
| upper_bounds = _serialize_bounds(rest_file.upper_bounds.to_dict() if rest_file.upper_bounds else None, table_schema) | ||||||
| else: | ||||||
| column_sizes = None | ||||||
| value_counts = None | ||||||
| null_value_counts = None | ||||||
| nan_value_counts = None | ||||||
| lower_bounds = None | ||||||
| upper_bounds = None | ||||||
|
|
||||||
| key_metadata = base64.b64decode(rest_file.key_metadata, validate=True) if rest_file.key_metadata else None | ||||||
|
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. The REST spec types
Suggested change
|
||||||
|
|
||||||
| referenced_data_file = None | ||||||
| if isinstance(rest_file, RESTPositionDeleteFile): | ||||||
|
|
@@ -2494,6 +2526,9 @@ def _rest_file_to_data_file(rest_file: RESTContentFile, default_referenced_data_ | |||||
| value_counts=value_counts, | ||||||
| null_value_counts=null_value_counts, | ||||||
| nan_value_counts=nan_value_counts, | ||||||
| lower_bounds=lower_bounds, | ||||||
| upper_bounds=upper_bounds, | ||||||
| key_metadata=key_metadata, | ||||||
| split_offsets=rest_file.split_offsets, | ||||||
| sort_order_id=rest_file.sort_order_id, | ||||||
| referenced_data_file=referenced_data_file, | ||||||
|
|
@@ -2643,7 +2678,7 @@ def _plan_files_server_side(self) -> Iterable[FileScanTask]: | |||||
| case_sensitive=self.case_sensitive, | ||||||
| ) | ||||||
|
|
||||||
| result = self.catalog._plan_scan_result(self.table_identifier, request) | ||||||
| result = self.catalog._plan_scan_result(self.table_identifier, request, table_schema=self.table_metadata.schema()) | ||||||
| location = result.tasks[0].file.file_path if result.tasks else None | ||||||
| plan_io = self.catalog._file_io_from_plan(self.io.properties, result.storage_credentials, location) | ||||||
| if plan_io is not None: | ||||||
|
|
||||||
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.
One detail for the hex version:
RestCatalogparses responses withvalidate_json, where pydantic turns a 32-digit hex value into aUUID, so 16-byte bounds need astr-valued map. WithKeyValueMap[str]forlower_bounds/upper_boundsand{field_id: bytes.fromhex(value) for ...}, a scratch copy got the bytes pyiceberg writes in manifests for all 14 primitive types, including uuid, decimal and fixed[16].