|
| 1 | +# Licensed to the Apache Software Foundation (ASF) under one |
| 2 | +# or more contributor license agreements. See the NOTICE file |
| 3 | +# distributed with this work for additional information |
| 4 | +# regarding copyright ownership. The ASF licenses this file |
| 5 | +# to you under the Apache License, Version 2.0 (the |
| 6 | +# "License"); you may not use this file except in compliance |
| 7 | +# with the License. You may obtain a copy of the License at |
| 8 | +# |
| 9 | +# http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | +# |
| 11 | +# Unless required by applicable law or agreed to in writing, |
| 12 | +# software distributed under the License is distributed on an |
| 13 | +# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY |
| 14 | +# KIND, either express or implied. See the License for the |
| 15 | +# specific language governing permissions and limitations |
| 16 | +# under the License. |
| 17 | +from typing import TYPE_CHECKING, Tuple |
| 18 | + |
| 19 | +from pyiceberg.table.statistics import StatisticsFile |
| 20 | +from pyiceberg.table.update import ( |
| 21 | + RemoveStatisticsUpdate, |
| 22 | + SetStatisticsUpdate, |
| 23 | + TableUpdate, |
| 24 | + UpdatesAndRequirements, |
| 25 | + UpdateTableMetadata, |
| 26 | +) |
| 27 | + |
| 28 | +if TYPE_CHECKING: |
| 29 | + from pyiceberg.table import Transaction |
| 30 | + |
| 31 | + |
| 32 | +class UpdateStatistics(UpdateTableMetadata["UpdateStatistics"]): |
| 33 | + """ |
| 34 | + Run statistics management operations using APIs. |
| 35 | +
|
| 36 | + APIs include set_statistics and remove statistics operations. |
| 37 | +
|
| 38 | + Use table.update_statistics().<operation>().commit() to run a specific operation. |
| 39 | + Use table.update_statistics().<operation-one>().<operation-two>().commit() to run multiple operations. |
| 40 | +
|
| 41 | + Pending changes are applied on commit. |
| 42 | +
|
| 43 | + We can also use context managers to make more changes. For example: |
| 44 | +
|
| 45 | + with table.update_statistics() as update: |
| 46 | + update.set_statistics(snapshot_id=1, statistics_file=statistics_file) |
| 47 | + update.remove_statistics(snapshot_id=2) |
| 48 | + """ |
| 49 | + |
| 50 | + _updates: Tuple[TableUpdate, ...] = () |
| 51 | + |
| 52 | + def __init__(self, transaction: "Transaction") -> None: |
| 53 | + super().__init__(transaction) |
| 54 | + |
| 55 | + def set_statistics(self, snapshot_id: int, statistics_file: StatisticsFile) -> "UpdateStatistics": |
| 56 | + self._updates += ( |
| 57 | + SetStatisticsUpdate( |
| 58 | + snapshot_id=snapshot_id, |
| 59 | + statistics=statistics_file, |
| 60 | + ), |
| 61 | + ) |
| 62 | + |
| 63 | + return self |
| 64 | + |
| 65 | + def remove_statistics(self, snapshot_id: int) -> "UpdateStatistics": |
| 66 | + self._updates = ( |
| 67 | + RemoveStatisticsUpdate( |
| 68 | + snapshot_id=snapshot_id, |
| 69 | + ), |
| 70 | + ) |
| 71 | + |
| 72 | + return self |
| 73 | + |
| 74 | + def _commit(self) -> UpdatesAndRequirements: |
| 75 | + return self._updates, () |
0 commit comments