-
Notifications
You must be signed in to change notification settings - Fork 297
Add table statistics #1285
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
+488
−2
Merged
Add table statistics #1285
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
b6d7509
Add table statistics update
ndrluis 011f647
Update pyiceberg/table/statistics.py
ndrluis 0f7190b
Update mkdocs/docs/api.md
ndrluis 0ad4aa5
Update mkdocs/docs/api.md
ndrluis e328524
Add Literal import
ndrluis fb9b2a2
Rewrite tests
ndrluis 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 |
---|---|---|
@@ -0,0 +1,45 @@ | ||
# 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 typing import Dict, List, Literal, Optional | ||
|
||
from pydantic import Field | ||
|
||
from pyiceberg.typedef import IcebergBaseModel | ||
|
||
|
||
class BlobMetadata(IcebergBaseModel): | ||
type: Literal["apache-datasketches-theta-v1", "deletion-vector-v1"] | ||
snapshot_id: int = Field(alias="snapshot-id") | ||
sequence_number: int = Field(alias="sequence-number") | ||
fields: List[int] | ||
properties: Optional[Dict[str, str]] = None | ||
|
||
|
||
class StatisticsFile(IcebergBaseModel): | ||
snapshot_id: int = Field(alias="snapshot-id") | ||
statistics_path: str = Field(alias="statistics-path") | ||
file_size_in_bytes: int = Field(alias="file-size-in-bytes") | ||
file_footer_size_in_bytes: int = Field(alias="file-footer-size-in-bytes") | ||
key_metadata: Optional[str] = Field(alias="key-metadata", default=None) | ||
blob_metadata: List[BlobMetadata] = Field(alias="blob-metadata") | ||
ndrluis marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
|
||
def filter_statistics_by_snapshot_id( | ||
statistics: List[StatisticsFile], | ||
reject_snapshot_id: int, | ||
) -> List[StatisticsFile]: | ||
return [stat for stat in statistics if stat.snapshot_id != reject_snapshot_id] |
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 |
---|---|---|
|
@@ -36,6 +36,7 @@ | |
SnapshotLogEntry, | ||
) | ||
from pyiceberg.table.sorting import SortOrder | ||
from pyiceberg.table.statistics import StatisticsFile, filter_statistics_by_snapshot_id | ||
from pyiceberg.typedef import ( | ||
IcebergBaseModel, | ||
Properties, | ||
|
@@ -174,6 +175,17 @@ class RemovePropertiesUpdate(IcebergBaseModel): | |
removals: List[str] | ||
|
||
|
||
class SetStatisticsUpdate(IcebergBaseModel): | ||
action: Literal["set-statistics"] = Field(default="set-statistics") | ||
snapshot_id: int = Field(alias="snapshot-id") | ||
statistics: StatisticsFile | ||
|
||
|
||
class RemoveStatisticsUpdate(IcebergBaseModel): | ||
action: Literal["remove-statistics"] = Field(default="remove-statistics") | ||
snapshot_id: int = Field(alias="snapshot-id") | ||
|
||
|
||
TableUpdate = Annotated[ | ||
Union[ | ||
AssignUUIDUpdate, | ||
|
@@ -191,6 +203,8 @@ class RemovePropertiesUpdate(IcebergBaseModel): | |
SetLocationUpdate, | ||
SetPropertiesUpdate, | ||
RemovePropertiesUpdate, | ||
SetStatisticsUpdate, | ||
RemoveStatisticsUpdate, | ||
], | ||
Field(discriminator="action"), | ||
] | ||
|
@@ -475,6 +489,28 @@ def _( | |
return base_metadata.model_copy(update={"default_sort_order_id": new_sort_order_id}) | ||
|
||
|
||
@_apply_table_update.register(SetStatisticsUpdate) | ||
def _(update: SetStatisticsUpdate, base_metadata: TableMetadata, context: _TableMetadataUpdateContext) -> TableMetadata: | ||
if update.snapshot_id != update.statistics.snapshot_id: | ||
raise ValueError("Snapshot id in statistics does not match the snapshot id in the update") | ||
Comment on lines
+494
to
+495
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. It's a bit of an awkward check, but something that we have to live with I guess. |
||
|
||
statistics = filter_statistics_by_snapshot_id(base_metadata.statistics, update.snapshot_id) | ||
context.add_update(update) | ||
|
||
return base_metadata.model_copy(update={"statistics": statistics + [update.statistics]}) | ||
|
||
|
||
@_apply_table_update.register(RemoveStatisticsUpdate) | ||
def _(update: RemoveStatisticsUpdate, base_metadata: TableMetadata, context: _TableMetadataUpdateContext) -> TableMetadata: | ||
if not any(stat.snapshot_id == update.snapshot_id for stat in base_metadata.statistics): | ||
raise ValueError(f"Statistics with snapshot id {update.snapshot_id} does not exist") | ||
|
||
statistics = filter_statistics_by_snapshot_id(base_metadata.statistics, update.snapshot_id) | ||
context.add_update(update) | ||
|
||
return base_metadata.model_copy(update={"statistics": statistics}) | ||
|
||
|
||
def update_table_metadata( | ||
base_metadata: TableMetadata, | ||
updates: Tuple[TableUpdate, ...], | ||
|
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,75 @@ | ||
# 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 typing import TYPE_CHECKING, Tuple | ||
|
||
from pyiceberg.table.statistics import StatisticsFile | ||
from pyiceberg.table.update import ( | ||
RemoveStatisticsUpdate, | ||
SetStatisticsUpdate, | ||
TableUpdate, | ||
UpdatesAndRequirements, | ||
UpdateTableMetadata, | ||
) | ||
|
||
if TYPE_CHECKING: | ||
from pyiceberg.table import Transaction | ||
|
||
|
||
class UpdateStatistics(UpdateTableMetadata["UpdateStatistics"]): | ||
""" | ||
Run statistics management operations using APIs. | ||
|
||
APIs include set_statistics and remove statistics operations. | ||
|
||
Use table.update_statistics().<operation>().commit() to run a specific operation. | ||
Use table.update_statistics().<operation-one>().<operation-two>().commit() to run multiple operations. | ||
|
||
Pending changes are applied on commit. | ||
|
||
We can also use context managers to make more changes. For example: | ||
|
||
with table.update_statistics() as update: | ||
update.set_statistics(snapshot_id=1, statistics_file=statistics_file) | ||
update.remove_statistics(snapshot_id=2) | ||
""" | ||
|
||
_updates: Tuple[TableUpdate, ...] = () | ||
|
||
def __init__(self, transaction: "Transaction") -> None: | ||
super().__init__(transaction) | ||
|
||
def set_statistics(self, snapshot_id: int, statistics_file: StatisticsFile) -> "UpdateStatistics": | ||
self._updates += ( | ||
SetStatisticsUpdate( | ||
snapshot_id=snapshot_id, | ||
statistics=statistics_file, | ||
), | ||
) | ||
|
||
return self | ||
|
||
def remove_statistics(self, snapshot_id: int) -> "UpdateStatistics": | ||
self._updates = ( | ||
RemoveStatisticsUpdate( | ||
snapshot_id=snapshot_id, | ||
), | ||
) | ||
|
||
return self | ||
|
||
def _commit(self) -> UpdatesAndRequirements: | ||
return self._updates, () |
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
Oops, something went wrong.
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.