From 2b74f642bdf5dac1483dc38245cfe4434c5ddb2d Mon Sep 17 00:00:00 2001 From: xgbtxy <115146404+xgbtxy@users.noreply.github.com> Date: Thu, 26 Nov 2026 00:43:17 +0800 Subject: [PATCH] Fix UpdateStatistics.remove_statistics overwriting prior queued updates UpdateStatistics.remove_statistics() assigned self._updates = (...) instead of appending via self._updates += (...), silently discarding any preceding set_statistics() or remove_statistics() calls queued in the same UpdateStatistics batch or context manager. Append the RemoveStatisticsUpdate to self._updates so chained and context-manager statistics updates preserve all pending operations, matching the documented UpdateStatistics behavior. --- pyiceberg/table/update/statistics.py | 2 +- tests/table/test_update_statistics.py | 110 ++++++++++++++++++++++++++ 2 files changed, 111 insertions(+), 1 deletion(-) create mode 100644 tests/table/test_update_statistics.py diff --git a/pyiceberg/table/update/statistics.py b/pyiceberg/table/update/statistics.py index 76fe2cb07b..42e5e47c65 100644 --- a/pyiceberg/table/update/statistics.py +++ b/pyiceberg/table/update/statistics.py @@ -62,7 +62,7 @@ def set_statistics(self, statistics_file: StatisticsFile) -> "UpdateStatistics": return self def remove_statistics(self, snapshot_id: int) -> "UpdateStatistics": - self._updates = ( + self._updates += ( RemoveStatisticsUpdate( snapshot_id=snapshot_id, ), diff --git a/tests/table/test_update_statistics.py b/tests/table/test_update_statistics.py new file mode 100644 index 0000000000..05017e852f --- /dev/null +++ b/tests/table/test_update_statistics.py @@ -0,0 +1,110 @@ +# 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. +import pyarrow as pa + +from pyiceberg.catalog import Catalog +from pyiceberg.table import Table +from pyiceberg.table.statistics import BlobMetadata, StatisticsFile + + +def _create_table_with_two_snapshots(catalog: Catalog) -> tuple[Table, int, int]: + arrow_table = pa.table({"id": [1, 2, 3]}) + catalog.create_namespace("default") + table = catalog.create_table("default.test_update_statistics", schema=arrow_table.schema) + table.append(arrow_table) + table.append(arrow_table) + snapshot_id_1, snapshot_id_2 = [snapshot.snapshot_id for snapshot in table.snapshots()] + return table, snapshot_id_1, snapshot_id_2 + + +def _statistics_file(snapshot_id: int, statistics_path: str) -> StatisticsFile: + return StatisticsFile( + snapshot_id=snapshot_id, + statistics_path=statistics_path, + file_size_in_bytes=124, + file_footer_size_in_bytes=27, + blob_metadata=[ + BlobMetadata( + type="apache-datasketches-theta-v1", + snapshot_id=snapshot_id, + sequence_number=1, + fields=[1], + properties={}, + ) + ], + ) + + +def test_update_statistics_set_and_remove_in_one_update(catalog: Catalog) -> None: + table, snapshot_id_1, snapshot_id_2 = _create_table_with_two_snapshots(catalog) + stats_1 = _statistics_file(snapshot_id_1, "s3://bucket/stats-1.puffin") + stats_2 = _statistics_file(snapshot_id_2, "s3://bucket/stats-2.puffin") + + table.update_statistics().set_statistics(stats_1).commit() + assert table.metadata.statistics == [stats_1] + + # Chaining set_statistics followed by remove_statistics must preserve both updates + table.update_statistics().set_statistics(stats_2).remove_statistics(snapshot_id_1).commit() + assert table.metadata.statistics == [stats_2] + assert catalog.load_table(table.name()).metadata.statistics == [stats_2] + + # Context manager with set_statistics followed by remove_statistics (docstring pattern) + with table.update_statistics() as update: + update.set_statistics(stats_1) + update.remove_statistics(snapshot_id_2) + assert table.metadata.statistics == [stats_1] + assert catalog.load_table(table.name()).metadata.statistics == [stats_1] + + # Reverse order: remove_statistics followed by set_statistics + table.update_statistics().remove_statistics(snapshot_id_1).set_statistics(stats_2).commit() + assert table.metadata.statistics == [stats_2] + assert catalog.load_table(table.name()).metadata.statistics == [stats_2] + + +def test_update_statistics_multiple_removals_in_one_update(catalog: Catalog) -> None: + table, snapshot_id_1, snapshot_id_2 = _create_table_with_two_snapshots(catalog) + stats_1 = _statistics_file(snapshot_id_1, "s3://bucket/stats-1.puffin") + stats_2 = _statistics_file(snapshot_id_2, "s3://bucket/stats-2.puffin") + + with table.update_statistics() as update: + update.set_statistics(stats_1) + update.set_statistics(stats_2) + assert table.metadata.statistics == [stats_1, stats_2] + + with table.update_statistics() as update: + update.remove_statistics(snapshot_id_1) + update.remove_statistics(snapshot_id_2) + assert table.metadata.statistics == [] + assert catalog.load_table(table.name()).metadata.statistics == [] + + +def test_update_statistics_set_and_remove_in_transaction(catalog: Catalog) -> None: + table, snapshot_id_1, snapshot_id_2 = _create_table_with_two_snapshots(catalog) + stats_1 = _statistics_file(snapshot_id_1, "s3://bucket/stats-1.puffin") + stats_2 = _statistics_file(snapshot_id_2, "s3://bucket/stats-2.puffin") + + table.update_statistics().set_statistics(stats_1).commit() + + with table.transaction() as transaction: + with transaction.update_statistics() as update: + update.set_statistics(stats_2) + update.remove_statistics(snapshot_id_1) + assert transaction.table_metadata.statistics == [stats_2] + assert table.metadata.statistics == [stats_1] + + assert table.metadata.statistics == [stats_2] + assert catalog.load_table(table.name()).metadata.statistics == [stats_2]