-
Notifications
You must be signed in to change notification settings - Fork 475
feat: Add support for rest scan planning #2864
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
Merged
Merged
Changes from 1 commit
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
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 |
|---|---|---|
|
|
@@ -145,6 +145,11 @@ | |
| from pyiceberg_core.datafusion import IcebergDataFusionTable | ||
|
|
||
| from pyiceberg.catalog import Catalog | ||
| from pyiceberg.catalog.rest.scan_planning import ( | ||
| RESTContentFile, | ||
| RESTDeleteFile, | ||
| RESTFileScanTask, | ||
| ) | ||
|
|
||
| ALWAYS_TRUE = AlwaysTrue() | ||
| DOWNCAST_NS_TIMESTAMP_TO_US_ON_WRITE = "downcast-ns-timestamp-to-us-on-write" | ||
|
|
@@ -1168,6 +1173,8 @@ def scan( | |
| snapshot_id=snapshot_id, | ||
| options=options, | ||
| limit=limit, | ||
| catalog=self.catalog, | ||
| table_identifier=self._identifier, | ||
| ) | ||
|
|
||
| @property | ||
|
|
@@ -1684,6 +1691,8 @@ class TableScan(ABC): | |
| snapshot_id: int | None | ||
| options: Properties | ||
| limit: int | None | ||
| catalog: Catalog | None | ||
| table_identifier: Identifier | None | ||
|
|
||
| def __init__( | ||
| self, | ||
|
|
@@ -1695,6 +1704,8 @@ def __init__( | |
| snapshot_id: int | None = None, | ||
| options: Properties = EMPTY_DICT, | ||
| limit: int | None = None, | ||
| catalog: Catalog | None = None, | ||
| table_identifier: Identifier | None = None, | ||
| ): | ||
| self.table_metadata = table_metadata | ||
| self.io = io | ||
|
|
@@ -1704,6 +1715,8 @@ def __init__( | |
| self.snapshot_id = snapshot_id | ||
| self.options = options | ||
| self.limit = limit | ||
| self.catalog = catalog | ||
| self.table_identifier = table_identifier | ||
|
|
||
| def snapshot(self) -> Snapshot | None: | ||
| if self.snapshot_id: | ||
|
|
@@ -1798,6 +1811,74 @@ def __init__( | |
| self.delete_files = delete_files or set() | ||
| self.residual = residual | ||
|
|
||
| @staticmethod | ||
| def from_rest_response( | ||
| rest_task: RESTFileScanTask, | ||
| delete_files: list[RESTDeleteFile], | ||
| ) -> 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. | ||
|
|
||
| Returns: | ||
| A FileScanTask with the converted data and delete files. | ||
|
|
||
| Raises: | ||
| NotImplementedError: If equality delete files are encountered. | ||
| """ | ||
| from pyiceberg.catalog.rest.scan_planning import RESTEqualityDeleteFile | ||
|
|
||
| data_file = _rest_file_to_data_file(rest_task.data_file) | ||
|
|
||
| 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)) | ||
|
|
||
| return FileScanTask( | ||
| data_file=data_file, | ||
| delete_files=resolved_deletes, | ||
| residual=rest_task.residual_filter if rest_task.residual_filter else ALWAYS_TRUE, | ||
|
geruh marked this conversation as resolved.
|
||
| ) | ||
|
|
||
|
|
||
| def _rest_file_to_data_file(rest_file: RESTContentFile) -> DataFile: | ||
| """Convert a REST content file to a manifest DataFile.""" | ||
| from pyiceberg.catalog.rest.scan_planning import CONTENT_TYPE_MAP, RESTDataFile | ||
|
|
||
| if isinstance(rest_file, RESTDataFile): | ||
| column_sizes = rest_file.column_sizes.to_dict() if rest_file.column_sizes else None | ||
| 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 | ||
| else: | ||
| column_sizes = None | ||
| value_counts = None | ||
| null_value_counts = None | ||
| nan_value_counts = None | ||
|
|
||
| data_file = DataFile.from_args( | ||
| content=CONTENT_TYPE_MAP[rest_file.content], | ||
| file_path=rest_file.file_path, | ||
| file_format=rest_file.file_format, | ||
| partition=Record(*rest_file.partition) if rest_file.partition else Record(), | ||
| record_count=rest_file.record_count, | ||
| file_size_in_bytes=rest_file.file_size_in_bytes, | ||
| column_sizes=column_sizes, | ||
| value_counts=value_counts, | ||
| null_value_counts=null_value_counts, | ||
| nan_value_counts=nan_value_counts, | ||
| split_offsets=rest_file.split_offsets, | ||
| sort_order_id=rest_file.sort_order_id, | ||
| ) | ||
| data_file.spec_id = rest_file.spec_id | ||
| return data_file | ||
|
|
||
|
|
||
| def _open_manifest( | ||
| io: FileIO, | ||
|
|
@@ -1970,12 +2051,35 @@ def scan_plan_helper(self) -> Iterator[list[ManifestEntry]]: | |
| ], | ||
| ) | ||
|
|
||
| def plan_files(self) -> Iterable[FileScanTask]: | ||
| """Plans the relevant files by filtering on the PartitionSpecs. | ||
| def _should_use_rest_planning(self) -> bool: | ||
| """Check if REST scan planning should be used for this scan.""" | ||
| from pyiceberg.catalog.rest import RestCatalog | ||
|
|
||
| if not isinstance(self.catalog, RestCatalog): | ||
| return False | ||
| return self.catalog.is_rest_scan_planning_enabled() | ||
|
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 would be inclined to create a method on the Have the This would also clean up |
||
|
|
||
| def _plan_files_rest(self) -> Iterable[FileScanTask]: | ||
|
geruh marked this conversation as resolved.
Outdated
|
||
| """Plan files using REST server-side scan planning.""" | ||
| from pyiceberg.catalog.rest import RestCatalog | ||
| from pyiceberg.catalog.rest.scan_planning import PlanTableScanRequest | ||
|
|
||
| if not isinstance(self.catalog, RestCatalog): | ||
| raise TypeError("REST scan planning requires a RestCatalog") | ||
| if self.table_identifier is None: | ||
| raise ValueError("REST scan planning requires a table identifier") | ||
|
|
||
| request = PlanTableScanRequest( | ||
| snapshot_id=self.snapshot_id, | ||
| select=list(self.selected_fields) if self.selected_fields != ("*",) else None, | ||
| filter=self.row_filter if self.row_filter != ALWAYS_TRUE else None, | ||
| case_sensitive=self.case_sensitive, | ||
| ) | ||
|
|
||
| Returns: | ||
| List of FileScanTasks that contain both data and delete files. | ||
| """ | ||
| return self.catalog.plan_scan(self.table_identifier, request) | ||
|
|
||
| def _plan_files_local(self) -> Iterable[FileScanTask]: | ||
| """Plan files locally by reading manifests.""" | ||
| data_entries: list[ManifestEntry] = [] | ||
| positional_delete_entries = SortedList(key=lambda entry: entry.sequence_number or INITIAL_SEQUENCE_NUMBER) | ||
|
|
||
|
|
@@ -2006,6 +2110,20 @@ def plan_files(self) -> Iterable[FileScanTask]: | |
| for data_entry in data_entries | ||
| ] | ||
|
|
||
| def plan_files(self) -> Iterable[FileScanTask]: | ||
| """Plans the relevant files by filtering on the PartitionSpecs. | ||
|
|
||
| If the table comes from a REST catalog with scan planning enabled, | ||
| this will use server-side scan planning. Otherwise, it falls back | ||
| to local planning. | ||
|
|
||
| Returns: | ||
| List of FileScanTasks that contain both data and delete files. | ||
| """ | ||
| if self._should_use_rest_planning(): | ||
| return self._plan_files_rest() | ||
| return self._plan_files_local() | ||
|
|
||
| def to_arrow(self) -> pa.Table: | ||
| """Read an Arrow table eagerly from this DataScan. | ||
|
|
||
|
|
||
Oops, something went wrong.
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.
Uh oh!
There was an error while loading. Please reload this page.