Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion pyiceberg/table/update/statistics.py
Original file line number Diff line number Diff line change
Expand Up @@ -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,
),
Expand Down
110 changes: 110 additions & 0 deletions tests/table/test_update_statistics.py
Original file line number Diff line number Diff line change
@@ -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]
Loading