-
Notifications
You must be signed in to change notification settings - Fork 411
feat: Add models for rest scan planning #2861
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
geruh
wants to merge
2
commits into
apache:main
Choose a base branch
from
geruh:rest-scan
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.
+658
−0
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
| Original file line number | Diff line number | Diff line change | ||||||
|---|---|---|---|---|---|---|---|---|
| @@ -0,0 +1,208 @@ | ||||||||
| # Licensed to the Apache Software Foundation (ASF) under one | ||||||||
| # or more contributor license agreements. See the NOTICE file | ||||||||
| # distributed with this work for additional information | ||||||||
| # regarding copyright ownership. The ASF licenses this file | ||||||||
| # to you under the Apache License, Version 2.0 (the | ||||||||
| # "License"); you may not use this file except in compliance | ||||||||
| # with the License. You may obtain a copy of the License at | ||||||||
| # | ||||||||
| # http://www.apache.org/licenses/LICENSE-2.0 | ||||||||
| # | ||||||||
| # Unless required by applicable law or agreed to in writing, | ||||||||
| # software distributed under the License is distributed on an | ||||||||
| # "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY | ||||||||
| # KIND, either express or implied. See the License for the | ||||||||
| # specific language governing permissions and limitations | ||||||||
| # under the License. | ||||||||
| from __future__ import annotations | ||||||||
|
|
||||||||
| from datetime import date, datetime, time | ||||||||
| from decimal import Decimal | ||||||||
| from typing import Annotated, Generic, Literal, TypeAlias, TypeVar | ||||||||
| from uuid import UUID | ||||||||
|
|
||||||||
| from pydantic import Field, model_validator | ||||||||
|
|
||||||||
| from pyiceberg.catalog.rest.response import ErrorResponseMessage | ||||||||
| from pyiceberg.expressions import BooleanExpression | ||||||||
| from pyiceberg.manifest import FileFormat | ||||||||
| from pyiceberg.typedef import IcebergBaseModel | ||||||||
|
|
||||||||
| # Primitive types that can appear in partition values and bounds | ||||||||
| PrimitiveTypeValue: TypeAlias = bool | int | float | str | Decimal | UUID | date | time | datetime | bytes | ||||||||
|
|
||||||||
| V = TypeVar("V") | ||||||||
|
|
||||||||
|
|
||||||||
| class KeyValueMap(IcebergBaseModel, Generic[V]): | ||||||||
| """Map serialized as parallel key/value arrays for column statistics.""" | ||||||||
|
|
||||||||
| keys: list[int] = Field(default_factory=list) | ||||||||
| values: list[V] = Field(default_factory=list) | ||||||||
|
|
||||||||
| @model_validator(mode="after") | ||||||||
| def _validate_lengths_match(self) -> KeyValueMap[V]: | ||||||||
| if len(self.keys) != len(self.values): | ||||||||
| raise ValueError(f"keys and values must have same length: {len(self.keys)} != {len(self.values)}") | ||||||||
| return self | ||||||||
|
|
||||||||
| def to_dict(self) -> dict[int, V]: | ||||||||
| """Convert to dictionary mapping field ID to value.""" | ||||||||
| return dict(zip(self.keys, self.values, strict=True)) | ||||||||
|
|
||||||||
|
|
||||||||
| class CountMap(KeyValueMap[int]): | ||||||||
| """Map of field IDs to counts.""" | ||||||||
|
|
||||||||
|
|
||||||||
| class ValueMap(KeyValueMap[PrimitiveTypeValue]): | ||||||||
| """Map of field IDs to primitive values (for lower/upper bounds).""" | ||||||||
|
|
||||||||
|
|
||||||||
| class StorageCredential(IcebergBaseModel): | ||||||||
| """Storage credential for accessing content files.""" | ||||||||
|
|
||||||||
| prefix: str = Field(description="Storage location prefix this credential applies to") | ||||||||
| config: dict[str, str] = Field(default_factory=dict) | ||||||||
|
|
||||||||
|
|
||||||||
| class RESTContentFile(IcebergBaseModel): | ||||||||
| """Base model for data and delete files from REST API.""" | ||||||||
|
|
||||||||
| spec_id: int = Field(alias="spec-id") | ||||||||
| partition: list[PrimitiveTypeValue] = Field(default_factory=list) | ||||||||
| content: Literal["data", "position-deletes", "equality-deletes"] | ||||||||
| file_path: str = Field(alias="file-path") | ||||||||
| file_format: FileFormat = Field(alias="file-format") | ||||||||
| file_size_in_bytes: int = Field(alias="file-size-in-bytes") | ||||||||
| record_count: int = Field(alias="record-count") | ||||||||
| key_metadata: str | None = Field(alias="key-metadata", default=None) | ||||||||
| split_offsets: list[int] | None = Field(alias="split-offsets", default=None) | ||||||||
| sort_order_id: int | None = Field(alias="sort-order-id", default=None) | ||||||||
|
|
||||||||
|
|
||||||||
| class RESTDataFile(RESTContentFile): | ||||||||
| """Data file from REST API.""" | ||||||||
|
|
||||||||
| content: Literal["data"] = Field(default="data") | ||||||||
| first_row_id: int | None = Field(alias="first-row-id", default=None) | ||||||||
| column_sizes: CountMap | None = Field(alias="column-sizes", default=None) | ||||||||
| value_counts: CountMap | None = Field(alias="value-counts", default=None) | ||||||||
| null_value_counts: CountMap | None = Field(alias="null-value-counts", default=None) | ||||||||
| nan_value_counts: CountMap | None = Field(alias="nan-value-counts", default=None) | ||||||||
| lower_bounds: ValueMap | None = Field(alias="lower-bounds", default=None) | ||||||||
| upper_bounds: ValueMap | None = Field(alias="upper-bounds", default=None) | ||||||||
|
|
||||||||
|
|
||||||||
| class RESTPositionDeleteFile(RESTContentFile): | ||||||||
| """Position delete file from REST API.""" | ||||||||
|
|
||||||||
| content: Literal["position-deletes"] = Field(default="position-deletes") | ||||||||
| referenced_data_file: str | None = Field(alias="referenced-data-file", default=None) | ||||||||
| content_offset: int | None = Field(alias="content-offset", default=None) | ||||||||
| content_size_in_bytes: int | None = Field(alias="content-size-in-bytes", default=None) | ||||||||
|
|
||||||||
|
|
||||||||
| class RESTEqualityDeleteFile(RESTContentFile): | ||||||||
| """Equality delete file from REST API.""" | ||||||||
|
|
||||||||
| content: Literal["equality-deletes"] = Field(default="equality-deletes") | ||||||||
| equality_ids: list[int] | None = Field(alias="equality-ids", default=None) | ||||||||
|
|
||||||||
|
|
||||||||
| # Discriminated union for delete files | ||||||||
| RESTDeleteFile = Annotated[ | ||||||||
| RESTPositionDeleteFile | RESTEqualityDeleteFile, | ||||||||
| Field(discriminator="content"), | ||||||||
| ] | ||||||||
|
|
||||||||
|
|
||||||||
| class RESTFileScanTask(IcebergBaseModel): | ||||||||
| """A file scan task from the REST server.""" | ||||||||
|
|
||||||||
| data_file: RESTDataFile = Field(alias="data-file") | ||||||||
| delete_file_references: list[int] | None = Field(alias="delete-file-references", default=None) | ||||||||
| residual_filter: BooleanExpression | None = Field(alias="residual-filter", default=None) | ||||||||
|
|
||||||||
|
|
||||||||
| class ScanTasks(IcebergBaseModel): | ||||||||
| """Container for scan tasks returned by the server.""" | ||||||||
|
|
||||||||
| delete_files: list[RESTDeleteFile] = Field(alias="delete-files", default_factory=list) | ||||||||
| file_scan_tasks: list[RESTFileScanTask] = Field(alias="file-scan-tasks", default_factory=list) | ||||||||
| plan_tasks: list[str] = Field(alias="plan-tasks", default_factory=list) | ||||||||
|
|
||||||||
| @model_validator(mode="after") | ||||||||
| def _validate_delete_file_references(self) -> ScanTasks: | ||||||||
| # validate delete file references are in bounds | ||||||||
| max_idx = len(self.delete_files) - 1 | ||||||||
| for task in self.file_scan_tasks: | ||||||||
| for idx in task.delete_file_references or []: | ||||||||
| if idx < 0 or idx > max_idx: | ||||||||
| raise ValueError(f"Invalid delete file reference: {idx} (valid range: 0-{max_idx})") | ||||||||
|
|
||||||||
| if self.delete_files and not self.file_scan_tasks: | ||||||||
| raise ValueError("Invalid response: deleteFiles should only be returned with fileScanTasks that reference them") | ||||||||
|
|
||||||||
| return self | ||||||||
|
|
||||||||
|
|
||||||||
| class PlanCompleted(ScanTasks): | ||||||||
| """Completed scan plan result.""" | ||||||||
|
|
||||||||
| status: Literal["completed"] = "completed" | ||||||||
| plan_id: str | None = Field(alias="plan-id", default=None) | ||||||||
| storage_credentials: list[StorageCredential] | None = Field(alias="storage-credentials", default=None) | ||||||||
|
|
||||||||
|
|
||||||||
| class PlanSubmitted(IcebergBaseModel): | ||||||||
| """Scan plan submitted, poll for completion.""" | ||||||||
|
|
||||||||
| status: Literal["submitted"] = "submitted" | ||||||||
| plan_id: str | None = Field(alias="plan-id", default=None) | ||||||||
|
|
||||||||
|
|
||||||||
| class PlanCancelled(IcebergBaseModel): | ||||||||
| """Planning was cancelled.""" | ||||||||
|
|
||||||||
| status: Literal["cancelled"] = "cancelled" | ||||||||
|
|
||||||||
|
|
||||||||
| class PlanFailed(IcebergBaseModel): | ||||||||
| """Planning failed with error.""" | ||||||||
|
|
||||||||
| status: Literal["failed"] = "failed" | ||||||||
| error: ErrorResponseMessage | ||||||||
|
|
||||||||
|
|
||||||||
| PlanningResponse = Annotated[ | ||||||||
| PlanCompleted | PlanSubmitted | PlanCancelled | PlanFailed, | ||||||||
| Field(discriminator="status"), | ||||||||
| ] | ||||||||
|
|
||||||||
|
|
||||||||
| class PlanTableScanRequest(IcebergBaseModel): | ||||||||
| """Request body for planning a REST scan.""" | ||||||||
|
|
||||||||
| snapshot_id: int | None = Field(alias="snapshot-id", default=None) | ||||||||
| select: list[str] | None = Field(default=None) | ||||||||
| filter: BooleanExpression | None = Field(default=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.
Suggested change
missing |
||||||||
| case_sensitive: bool = Field(alias="case-sensitive", default=True) | ||||||||
| use_snapshot_schema: bool = Field(alias="use-snapshot-schema", default=False) | ||||||||
| start_snapshot_id: int | None = Field(alias="start-snapshot-id", default=None) | ||||||||
| end_snapshot_id: int | None = Field(alias="end-snapshot-id", default=None) | ||||||||
| stats_fields: list[str] | None = Field(alias="stats-fields", default=None) | ||||||||
|
|
||||||||
| @model_validator(mode="after") | ||||||||
| def _validate_snapshot_fields(self) -> PlanTableScanRequest: | ||||||||
| if self.start_snapshot_id is not None and self.end_snapshot_id is None: | ||||||||
| raise ValueError("end-snapshot-id is required when start-snapshot-id is specified") | ||||||||
| if self.snapshot_id is not None and self.start_snapshot_id is not None: | ||||||||
| raise ValueError("Cannot specify both snapshot-id and start-snapshot-id") | ||||||||
| return self | ||||||||
|
|
||||||||
|
|
||||||||
| class FetchScanTasksRequest(IcebergBaseModel): | ||||||||
| """Request body for fetching scan tasks endpoint.""" | ||||||||
|
|
||||||||
| plan_task: str = Field(alias="plan-task") | ||||||||
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.
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.
PositionDeleteFiledoesnt have thishttps://github.com/apache/iceberg/blob/0651b8913d27c3b1c9aca4a9609bec521905fb36/open-api/rest-catalog-open-api.yaml#L4450-L4466
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.
Ahh yeah it's another gap in the spec. This was for v3 puffin file.