From f5a441c32f6e9e9565b59baa8cc2a1140e3c4327 Mon Sep 17 00:00:00 2001 From: rahulsmahadev Date: Wed, 18 Nov 2026 17:19:10 +0000 Subject: [PATCH 1/3] Compute partition statistics files in PyIceberg Add table.maintenance.compute_partition_statistics(), which computes the partition statistics of a snapshot from its manifests, writes them to a Parquet file and registers it, so users need not build the file by hand (follow-up to #4397). Full compute only, as in Java's PartitionStatsHandler. Co-authored-by: Isaac --- mkdocs/docs/api.md | 15 + pyiceberg/table/__init__.py | 2 +- pyiceberg/table/maintenance.py | 45 +++ pyiceberg/table/partition_stats.py | 237 +++++++++++++ tests/table/test_statistics.py | 515 +++++++++++++++++++++++++++++ 5 files changed, 813 insertions(+), 1 deletion(-) create mode 100644 pyiceberg/table/partition_stats.py diff --git a/mkdocs/docs/api.md b/mkdocs/docs/api.md index 078157dc8f..6c64be1673 100644 --- a/mkdocs/docs/api.md +++ b/mkdocs/docs/api.md @@ -1594,6 +1594,21 @@ with table.manage_snapshots() as ms: PyIceberg provides table maintenance operations through the `table.maintenance` API. This provides a clean interface for performing maintenance tasks like snapshot expiration. +### Compute Partition Statistics + +Compute the partition statistics of a snapshot, write them to a Parquet file in the metadata directory, and register the file in the table metadata. The current snapshot is used by default: + +```python +partition_statistics_file = table.maintenance.compute_partition_statistics() + +# Or for a specific snapshot +partition_statistics_file = table.maintenance.compute_partition_statistics(snapshot_id=12345) +``` + +When no statistics file is registered for the requested snapshot, statistics are computed from its manifests; files from earlier snapshots are not reused. The `total_record_count` column is left empty. If the snapshot already has a partition statistics file, that file is returned and nothing is written. + +UUID partition fields currently raise `ArrowNotImplementedError` before a statistics file is registered; see #4358 for the related inspection limitation. Historical partition fields that share a name but have different field IDs raise `ValueError` before registration because the unified schema requires unique field names. + ### Snapshot Expiration Expire old snapshots to clean up table metadata and reduce storage costs: diff --git a/pyiceberg/table/__init__.py b/pyiceberg/table/__init__.py index 84925fd025..9b5e2669e2 100644 --- a/pyiceberg/table/__init__.py +++ b/pyiceberg/table/__init__.py @@ -1726,7 +1726,7 @@ def update_partition_statistics(self) -> UpdatePartitionStatistics: We can also use context managers to make more changes. For example: with table.update_partition_statistics() as update: - update.set_partition_statistics(partition_statistics_file=partition_statistics_file) + update.set_partition_statistics(snapshot_id=1, statistics_path="s3://bucket/stats.parquet", file_size_in_bytes=124) update.remove_partition_statistics(snapshot_id=2) """ return UpdatePartitionStatistics(transaction=Transaction(self, autocommit=True)) diff --git a/pyiceberg/table/maintenance.py b/pyiceberg/table/maintenance.py index 0fcda35ae9..2107211338 100644 --- a/pyiceberg/table/maintenance.py +++ b/pyiceberg/table/maintenance.py @@ -19,6 +19,8 @@ import logging from typing import TYPE_CHECKING +from pyiceberg.table.statistics import PartitionStatisticsFile + logger = logging.getLogger(__name__) @@ -33,6 +35,49 @@ class MaintenanceTable: def __init__(self, tbl: Table) -> None: self.tbl = tbl + def compute_partition_statistics(self, snapshot_id: int | None = None) -> PartitionStatisticsFile | None: + """Compute the partition statistics of a snapshot, write them to a file and register the file in the table metadata. + + The statistics are computed from the manifests of the snapshot. A partition statistics file that is already + registered for the snapshot is returned as is, and nothing is written. + + Args: + snapshot_id: The ID of the snapshot to compute the statistics for. Defaults to the current snapshot. + + Returns: + The registered partition statistics file, or None if the table has no snapshot or the snapshot has no + manifest entries. + + Raises: + ValueError: If the table is not partitioned, the snapshot does not exist, the table's default file format + has no writer, or historical partition fields have conflicting output names. + NotImplementedError: If the table is format version 3 or later. + ArrowNotImplementedError: If the unified partition type contains a UUID field. + """ + from pyiceberg.table.partition_stats import compute_and_write_partition_statistics_file + + if snapshot_id is None: + if (current_snapshot := self.tbl.current_snapshot()) is None: + return None + snapshot_id = current_snapshot.snapshot_id + + # A registered file can outlive its snapshot, so the snapshot is checked before the file is returned + if self.tbl.snapshot_by_id(snapshot_id) is None: + raise ValueError(f"Snapshot not found: {snapshot_id}") + + registered = next((file for file in self.tbl.metadata.partition_statistics if file.snapshot_id == snapshot_id), None) + if registered is not None: + return registered + + statistics_file = compute_and_write_partition_statistics_file(self.tbl, snapshot_id) + if statistics_file is not None: + self.tbl.update_partition_statistics().set_partition_statistics( + snapshot_id=statistics_file.snapshot_id, + statistics_path=statistics_file.statistics_path, + file_size_in_bytes=statistics_file.file_size_in_bytes, + ).commit() + return statistics_file + def expire_snapshots(self) -> ExpireSnapshots: """Return an ExpireSnapshots builder for snapshot expiration operations. diff --git a/pyiceberg/table/partition_stats.py b/pyiceberg/table/partition_stats.py new file mode 100644 index 0000000000..9aa0d4dbd8 --- /dev/null +++ b/pyiceberg/table/partition_stats.py @@ -0,0 +1,237 @@ +# 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. +"""Computes the partition statistics file of a snapshot, mirroring Java's PartitionStatsHandler.""" + +from __future__ import annotations + +import math +import uuid +from dataclasses import dataclass +from typing import TYPE_CHECKING, Any + +from pyiceberg.io.fileformat import FileFormatFactory +from pyiceberg.manifest import DataFile, DataFileContent, FileFormat, ManifestEntryStatus +from pyiceberg.schema import Schema +from pyiceberg.table.statistics import PartitionStatisticsFile +from pyiceberg.transforms import VoidTransform +from pyiceberg.types import IntegerType, LongType, NestedField, StructType + +if TYPE_CHECKING: + from pyiceberg.table import Table + from pyiceberg.table.metadata import TableMetadata + from pyiceberg.table.snapshots import Snapshot + + +@dataclass +class _PartitionStatistics: + """The statistics of the files of one partition of one spec.""" + + spec_id: int + partition: tuple[Any, ...] + data_record_count: int = 0 + data_file_count: int = 0 + total_data_file_size_in_bytes: int = 0 + position_delete_record_count: int = 0 + position_delete_file_count: int = 0 + equality_delete_record_count: int = 0 + equality_delete_file_count: int = 0 + last_updated_at: int | None = None + last_updated_snapshot_id: int | None = None + + +def compute_and_write_partition_statistics_file(table: Table, snapshot_id: int) -> PartitionStatisticsFile | None: + """Compute the partition statistics of a snapshot and write them to a new Parquet file. + + The file is not registered in the table metadata. The statistics are computed from the manifests of the snapshot. + + Args: + table: The table to compute the statistics for. + snapshot_id: The ID of the snapshot to compute the statistics for. + + Returns: + The partition statistics file, or None if the snapshot has no manifest entries. + + Raises: + ValueError: If the table is not partitioned, the snapshot does not exist, the table's default file format + has no writer, or historical partition fields have conflicting output names. + NotImplementedError: If the table is format version 3 or later. + ArrowNotImplementedError: If the unified partition type contains a UUID field. + """ + partition_type = _partition_type(table.metadata) + if not partition_type.fields: + raise ValueError("Table must be partitioned") + if table.metadata.format_version >= 3: + raise NotImplementedError("Partition statistics are not supported for format version 3 tables yet") + if (snapshot := table.snapshot_by_id(snapshot_id)) is None: + raise ValueError(f"Snapshot not found: {snapshot_id}") + + rows = _collect_partition_statistics(table, snapshot, partition_type) + if not rows: + return None + # Rows are sorted by partition, NULL first in each field + rows.sort(key=lambda row: tuple(_partition_value_key(value) for value in row.partition)) + return _write_partition_statistics_file(table, snapshot_id, partition_type, rows) + + +def _partition_type(metadata: TableMetadata) -> StructType: + """Return the unified partition type, as Java's Partitioning.partitionType does. + + It holds the partition fields of all specs whose source column is still in the current schema, sorted by field ID. + Fields of dropped source columns are left out, unlike TableMetadata.specs_struct. + """ + schema = metadata.schema() + fields: dict[int, NestedField] = {} + # Specs are visited in ascending order, so the name from the latest spec wins + for spec in sorted(metadata.specs().values(), key=lambda spec: spec.spec_id): + for field in spec.fields: + if schema.find_column_name(field.source_id) is not None: + previous = fields.get(field.field_id) + # A v1 spec drops a partition field by replacing its transform with void, which keeps the output type + # of the older transform, as Java's Partitioning does + if isinstance(field.transform, VoidTransform) and previous is not None: + result_type = previous.field_type + else: + result_type = field.transform.result_type(schema.find_type(field.source_id)) + fields[field.field_id] = NestedField(field.field_id, field.name, result_type, required=False) + return StructType(*(fields[field_id] for field_id in sorted(fields))) + + +def _partition_value_key(value: Any) -> tuple[Any, ...]: + """Return a key that groups and orders partition values as Iceberg does. + + NULL sorts first, NaN values are equal and sort after every other float, and -0.0 and 0.0 are different values. + """ + if value is None: + return (False,) + if isinstance(value, float): + if math.isnan(value): + return (True, True) + return (True, False, value, math.copysign(1.0, value)) + return (True, value) + + +def _collect_partition_statistics(table: Table, snapshot: Snapshot, partition_type: StructType) -> list[_PartitionStatistics]: + """Collect the statistics of all partitions in the manifests of a snapshot, as Java's full compute does. + + Live files add to the counts. Every entry, deleted ones included, can move the last update of its partition forward. + """ + specs = table.metadata.specs() + snapshots_by_id = {snap.snapshot_id: snap for snap in table.snapshots()} + positions = {field.field_id: index for index, field in enumerate(partition_type.fields)} + stats_by_key: dict[tuple[int, tuple[Any, ...]], _PartitionStatistics] = {} + + for manifest in snapshot.manifests(table.io, table.encryption_manager): + spec = specs[manifest.partition_spec_id] + # Maps each partition value of the spec to its position in the unified partition type + spec_positions = [ + (pos, positions[field.field_id]) for pos, field in enumerate(spec.fields) if field.field_id in positions + ] + for entry in manifest.fetch_manifest_entry(table.io, discard_deleted=False): + partition: list[Any] = [None] * len(partition_type.fields) + for pos, index in spec_positions: + partition[index] = entry.data_file.partition[pos] + key = (spec.spec_id, tuple(_partition_value_key(value) for value in partition)) + if (stats := stats_by_key.get(key)) is None: + stats = stats_by_key[key] = _PartitionStatistics(spec.spec_id, tuple(partition)) + + if entry.status != ManifestEntryStatus.DELETED: + _add_live_file(stats, entry.data_file) + if entry.snapshot_id is not None and (entry_snapshot := snapshots_by_id.get(entry.snapshot_id)) is not None: + _update_last_updated(stats, entry_snapshot) + + return list(stats_by_key.values()) + + +def _add_live_file(stats: _PartitionStatistics, data_file: DataFile) -> None: + if data_file.content == DataFileContent.DATA: + stats.data_record_count += data_file.record_count + stats.data_file_count += 1 + stats.total_data_file_size_in_bytes += data_file.file_size_in_bytes + elif data_file.content == DataFileContent.POSITION_DELETES: + stats.position_delete_record_count += data_file.record_count + stats.position_delete_file_count += 1 + elif data_file.content == DataFileContent.EQUALITY_DELETES: + stats.equality_delete_record_count += data_file.record_count + stats.equality_delete_file_count += 1 + + +def _update_last_updated(stats: _PartitionStatistics, snapshot: Snapshot) -> None: + if stats.last_updated_at is None or stats.last_updated_at < snapshot.timestamp_ms: + stats.last_updated_at = snapshot.timestamp_ms + stats.last_updated_snapshot_id = snapshot.snapshot_id + + +def _partition_statistics_schema(partition_type: StructType) -> Schema: + """Return the version 2 schema of the partition statistics file, with the field IDs of the Iceberg spec.""" + return Schema( + NestedField(1, "partition", partition_type, required=True), + NestedField(2, "spec_id", IntegerType(), required=True), + NestedField(3, "data_record_count", LongType(), required=True), + NestedField(4, "data_file_count", IntegerType(), required=True), + NestedField(5, "total_data_file_size_in_bytes", LongType(), required=True), + NestedField(6, "position_delete_record_count", LongType(), required=False), + NestedField(7, "position_delete_file_count", IntegerType(), required=False), + NestedField(8, "equality_delete_record_count", LongType(), required=False), + NestedField(9, "equality_delete_file_count", IntegerType(), required=False), + NestedField(10, "total_record_count", LongType(), required=False), + NestedField(11, "last_updated_at", LongType(), required=False), + NestedField(12, "last_updated_snapshot_id", LongType(), required=False), + ) + + +def _write_partition_statistics_file( + table: Table, snapshot_id: int, partition_type: StructType, rows: list[_PartitionStatistics] +) -> PartitionStatisticsFile: + import pyarrow as pa + + # Importing pyiceberg.io.pyarrow registers the Parquet format model + from pyiceberg.io.pyarrow import schema_to_pyarrow + from pyiceberg.table import TableProperties + + file_schema = _partition_statistics_schema(partition_type) + partition_names = [field.name for field in partition_type.fields] + arrow_table = pa.Table.from_pylist( + [ + { + "partition": dict(zip(partition_names, row.partition, strict=True)), + "spec_id": row.spec_id, + "data_record_count": row.data_record_count, + "data_file_count": row.data_file_count, + "total_data_file_size_in_bytes": row.total_data_file_size_in_bytes, + "position_delete_record_count": row.position_delete_record_count, + "position_delete_file_count": row.position_delete_file_count, + "equality_delete_record_count": row.equality_delete_record_count, + "equality_delete_file_count": row.equality_delete_file_count, + "total_record_count": None, + "last_updated_at": row.last_updated_at, + "last_updated_snapshot_id": row.last_updated_snapshot_id, + } + for row in rows + ], + schema=schema_to_pyarrow(file_schema), + ) + + file_format = FileFormat(table.properties.get(TableProperties.WRITE_FILE_FORMAT, TableProperties.WRITE_FILE_FORMAT_DEFAULT)) + format_model = FileFormatFactory.get(file_format) + location = table.location_provider().new_metadata_location( + f"partition-stats-{snapshot_id}-{uuid.uuid4()}.{format_model.file_extension()}" + ) + output = table.io.new_output(location) + with format_model.create_writer(output, file_schema, table.properties) as writer: + writer.write(arrow_table) + + return PartitionStatisticsFile(snapshot_id=snapshot_id, statistics_path=location, file_size_in_bytes=len(output)) diff --git a/tests/table/test_statistics.py b/tests/table/test_statistics.py index 4161c44725..b35e93d83d 100644 --- a/tests/table/test_statistics.py +++ b/tests/table/test_statistics.py @@ -14,12 +14,35 @@ # KIND, either express or implied. See the License for the # specific language governing permissions and limitations # under the License. +from datetime import date, datetime +from decimal import Decimal +from typing import Any + import pyarrow as pa +import pyarrow.parquet as pq import pytest from pyiceberg.catalog import Catalog +from pyiceberg.manifest import DataFile, DataFileContent, FileFormat, ManifestEntryStatus +from pyiceberg.partitioning import PartitionField, PartitionSpec +from pyiceberg.schema import Schema from pyiceberg.table import Table +from pyiceberg.table.snapshots import Snapshot from pyiceberg.table.statistics import BlobMetadata, PartitionStatisticsFile, StatisticsFile +from pyiceberg.transforms import BucketTransform, IdentityTransform, YearTransform +from pyiceberg.typedef import Record +from pyiceberg.types import ( + BinaryType, + BooleanType, + DateType, + DecimalType, + DoubleType, + FloatType, + LongType, + NestedField, + StringType, + TimestampType, +) def test_partition_statistics_file() -> None: @@ -189,3 +212,495 @@ def test_remove_partition_statistics_without_file_is_noop(catalog: Catalog) -> N assert table.metadata == metadata_before assert catalog.load_table(table.name()).metadata.partition_statistics == [stats_2] + + +def _create_category_table(catalog: Catalog, name: str) -> Table: + schema = Schema(NestedField(1, "category", StringType()), NestedField(2, "id", LongType())) + spec = PartitionSpec(PartitionField(source_id=1, field_id=1000, transform=IdentityTransform(), name="category")) + return catalog.create_table(name, schema=schema, partition_spec=spec) + + +def _read_partition_statistics(table: Table, statistics_file: PartitionStatisticsFile) -> list[dict[str, Any]]: + with table.io.new_input(statistics_file.statistics_path).open() as stream: + return pq.read_table(stream).to_pylist() + + +def _expected_partition_statistics_row(spec_id: int, partition: dict[str, Any], **values: Any) -> dict[str, Any]: + row: dict[str, Any] = { + "partition": partition, + "spec_id": spec_id, + "data_record_count": 0, + "data_file_count": 0, + "total_data_file_size_in_bytes": 0, + "position_delete_record_count": 0, + "position_delete_file_count": 0, + "equality_delete_record_count": 0, + "equality_delete_file_count": 0, + "total_record_count": None, + "last_updated_at": None, + "last_updated_snapshot_id": None, + } + return {**row, **values} + + +def test_compute_partition_statistics(catalog: Catalog) -> None: + catalog.create_namespace("default") + table = _create_category_table(catalog, "default.test_compute_partition_statistics") + arrow_schema = table.schema().as_arrow() + table.append(pa.Table.from_pylist([{"category": "a", "id": 1}, {"category": None, "id": 2}], schema=arrow_schema)) + snapshot_1 = table.current_snapshot() + assert snapshot_1 is not None + table.append(pa.Table.from_pylist([{"category": "a", "id": 3}], schema=arrow_schema)) + snapshot_2 = table.current_snapshot() + assert snapshot_2 is not None + # The overwrite deletes both files of partition "a" and adds one file + table.overwrite(pa.Table.from_pylist([{"category": "a", "id": 5}], schema=arrow_schema), overwrite_filter="category = 'a'") + snapshot_3 = table.current_snapshot() + assert snapshot_3 is not None + table.append(pa.Table.from_pylist([{"category": "b", "id": 4}], schema=arrow_schema)) + table.delete("category = 'b'") + snapshot_5 = table.current_snapshot() + assert snapshot_5 is not None + + statistics_file = table.maintenance.compute_partition_statistics() + + assert statistics_file is not None + assert statistics_file.snapshot_id == snapshot_5.snapshot_id + assert statistics_file.statistics_path.startswith(f"{table.metadata.location}/metadata/partition-stats-") + assert statistics_file.file_size_in_bytes == len(table.io.new_input(statistics_file.statistics_path)) + assert table.metadata.partition_statistics == [statistics_file] + assert catalog.load_table(table.name()).metadata.partition_statistics == [statistics_file] + + # Partitions are sorted with NULL first. The deleted partition "b" keeps a row with zero counts, as in Java. + size_of = {row["partition"]["category"]: row["file_size_in_bytes"] for row in table.inspect.data_files().to_pylist()} + assert _read_partition_statistics(table, statistics_file) == [ + _expected_partition_statistics_row( + 0, + {"category": None}, + data_record_count=1, + data_file_count=1, + total_data_file_size_in_bytes=size_of[None], + last_updated_at=snapshot_1.timestamp_ms, + last_updated_snapshot_id=snapshot_1.snapshot_id, + ), + _expected_partition_statistics_row( + 0, + {"category": "a"}, + data_record_count=1, + data_file_count=1, + total_data_file_size_in_bytes=size_of["a"], + last_updated_at=snapshot_3.timestamp_ms, + last_updated_snapshot_id=snapshot_3.snapshot_id, + ), + _expected_partition_statistics_row( + 0, + {"category": "b"}, + last_updated_at=snapshot_5.timestamp_ms, + last_updated_snapshot_id=snapshot_5.snapshot_id, + ), + ] + + # Statistics of an older snapshot are registered for that snapshot, next to the current one + statistics_file_2 = table.maintenance.compute_partition_statistics(snapshot_id=snapshot_2.snapshot_id) + assert statistics_file_2 is not None + assert [row["partition"]["category"] for row in _read_partition_statistics(table, statistics_file_2)] == [None, "a"] + assert table.metadata.partition_statistics == [statistics_file, statistics_file_2] + + +def test_compute_partition_statistics_file_layout(catalog: Catalog) -> None: + catalog.create_namespace("default") + table = _create_category_table(catalog, "default.test_compute_partition_statistics_file_layout") + table.append(pa.Table.from_pylist([{"category": "a", "id": 1}], schema=table.schema().as_arrow())) + + statistics_file = table.maintenance.compute_partition_statistics() + assert statistics_file is not None + + with table.io.new_input(statistics_file.statistics_path).open() as stream: + arrow_table = pq.read_table(stream) + # The version 2 layout, with the field IDs of the spec and the required fields of the spec as not nullable + assert [(field.name, int(field.metadata[b"PARQUET:field_id"]), not field.nullable) for field in arrow_table.schema] == [ + ("partition", 1, True), + ("spec_id", 2, True), + ("data_record_count", 3, True), + ("data_file_count", 4, True), + ("total_data_file_size_in_bytes", 5, True), + ("position_delete_record_count", 6, False), + ("position_delete_file_count", 7, False), + ("equality_delete_record_count", 8, False), + ("equality_delete_file_count", 9, False), + ("total_record_count", 10, False), + ("last_updated_at", 11, False), + ("last_updated_snapshot_id", 12, False), + ] + partition_field = arrow_table.schema.field("partition").type.field("category") + assert partition_field.metadata == {b"PARQUET:field_id": b"1000"} + + +def test_compute_partition_statistics_across_partition_specs(catalog: Catalog) -> None: + catalog.create_namespace("default") + schema = Schema(NestedField(1, "dt", DateType()), NestedField(2, "category", StringType()), NestedField(3, "id", LongType())) + spec = PartitionSpec(PartitionField(source_id=1, field_id=1000, transform=IdentityTransform(), name="dt")) + table = catalog.create_table("default.test_compute_partition_statistics_specs", schema=schema, partition_spec=spec) + arrow_schema = table.schema().as_arrow() + table.append(pa.Table.from_pylist([{"dt": date(2025, 1, 2), "category": None, "id": 1}], schema=arrow_schema)) + table.update_spec().add_identity("category").commit() + table.append(pa.Table.from_pylist([{"dt": date(2025, 1, 1), "category": "x", "id": 2}], schema=arrow_schema)) + + statistics_file = table.maintenance.compute_partition_statistics() + assert statistics_file is not None + + # The unified partition type holds the fields of both specs, and rows of the first spec have NULL for the new field + rows = _read_partition_statistics(table, statistics_file) + assert [(row["spec_id"], row["partition"], row["data_record_count"]) for row in rows] == [ + (1, {"dt": date(2025, 1, 1), "category": "x"}, 1), + (0, {"dt": date(2025, 1, 2), "category": None}, 1), + ] + + +def test_compute_partition_statistics_skips_dropped_source_columns(catalog: Catalog) -> None: + catalog.create_namespace("default") + schema = Schema(NestedField(1, "a", LongType()), NestedField(2, "b", StringType()), NestedField(3, "id", LongType())) + spec = PartitionSpec(PartitionField(source_id=1, field_id=1000, transform=IdentityTransform(), name="a")) + table = catalog.create_table("default.test_compute_partition_statistics_dropped", schema=schema, partition_spec=spec) + table.append( + pa.Table.from_pylist([{"a": 1, "b": "x", "id": 1}, {"a": 2, "b": "x", "id": 2}], schema=table.schema().as_arrow()) + ) + table.update_spec().remove_field("a").add_identity("b").commit() + table.update_schema().delete_column("a").commit() + table.append(pa.Table.from_pylist([{"b": "y", "id": 3}], schema=table.schema().as_arrow())) + + statistics_file = table.maintenance.compute_partition_statistics() + assert statistics_file is not None + + # The field of the dropped source column is left out of the partition type, so the two partitions of the first spec + # that differ only in "a" share one row with NULL for "b". Java keeps them in separate rows; this is a known difference. + rows = _read_partition_statistics(table, statistics_file) + assert [(row["spec_id"], row["partition"], row["data_record_count"], row["data_file_count"]) for row in rows] == [ + (0, {"b": None}, 2, 2), + (1, {"b": "y"}, 1, 1), + ] + + +def test_compute_partition_statistics_partition_value_types(catalog: Catalog) -> None: + catalog.create_namespace("default") + cases: list[tuple[Any, Any]] = [ + (LongType(), 1), + (StringType(), "a"), + (DateType(), date(2025, 1, 1)), + (TimestampType(), datetime(2025, 1, 1, 12, 30)), + (BooleanType(), True), + (DecimalType(10, 2), Decimal("1.50")), + (BinaryType(), b"ab"), + ] + for index, (field_type, value) in enumerate(cases): + schema = Schema(NestedField(1, "p", field_type), NestedField(2, "id", LongType())) + spec = PartitionSpec(PartitionField(source_id=1, field_id=1000, transform=IdentityTransform(), name="p")) + table = catalog.create_table( + f"default.test_compute_partition_statistics_types_{index}", schema=schema, partition_spec=spec + ) + table.append(pa.Table.from_pylist([{"p": value, "id": 1}, {"p": None, "id": 2}], schema=table.schema().as_arrow())) + + statistics_file = table.maintenance.compute_partition_statistics() + assert statistics_file is not None + assert [row["partition"]["p"] for row in _read_partition_statistics(table, statistics_file)] == [None, value], field_type + + +def test_compute_partition_statistics_returns_registered_file(catalog: Catalog) -> None: + catalog.create_namespace("default") + table = _create_category_table(catalog, "default.test_compute_partition_statistics_registered") + table.append(pa.Table.from_pylist([{"category": "a", "id": 1}], schema=table.schema().as_arrow())) + statistics_file = table.maintenance.compute_partition_statistics() + metadata_location = table.metadata_location + + # The file of the snapshot is returned as is, without writing a file or committing the table again + assert table.maintenance.compute_partition_statistics() == statistics_file + assert table.metadata_location == metadata_location + assert table.metadata.partition_statistics == [statistics_file] + + +def test_compute_partition_statistics_without_statistics(catalog: Catalog) -> None: + catalog.create_namespace("default") + table = _create_category_table(catalog, "default.test_compute_partition_statistics_empty") + # A table without snapshots has nothing to compute + assert table.maintenance.compute_partition_statistics() is None + assert table.metadata.partition_statistics == [] + + +def test_compute_partition_statistics_errors(catalog: Catalog) -> None: + catalog.create_namespace("default") + unpartitioned = catalog.create_table( + "default.test_compute_partition_statistics_unpartitioned", schema=Schema(NestedField(1, "id", LongType())) + ) + unpartitioned.append(pa.Table.from_pylist([{"id": 1}], schema=unpartitioned.schema().as_arrow())) + with pytest.raises(ValueError, match="Table must be partitioned"): + unpartitioned.maintenance.compute_partition_statistics() + + table = _create_category_table(catalog, "default.test_compute_partition_statistics_errors") + table.append(pa.Table.from_pylist([{"category": "a", "id": 1}], schema=table.schema().as_arrow())) + with pytest.raises(ValueError, match="Snapshot not found: 42"): + table.maintenance.compute_partition_statistics(snapshot_id=42) + + # The file is written in the default file format of the table, and PyIceberg only has a Parquet writer + table.transaction().set_properties({"write.format.default": "orc"}).commit_transaction() + with pytest.raises(ValueError, match="No writer registered"): + table.maintenance.compute_partition_statistics() + assert table.metadata.partition_statistics == [] + + # PyIceberg cannot create format version 3 tables, so the format version of a loaded v2 table is changed for this check + table.metadata = table.metadata.model_copy(update={"format_version": 3}) + with pytest.raises(NotImplementedError, match="format version 3"): + table.maintenance.compute_partition_statistics() + + +def test_compute_partition_statistics_float_partition_identity_and_order(catalog: Catalog) -> None: + catalog.create_namespace("default") + # Each value is appended on its own, so each value is one data file + values: list[float | None] = [float("nan"), float("nan"), -0.0, 0.0, float("inf"), float("-inf"), None] + for index, field_type in enumerate([FloatType(), DoubleType()]): + schema = Schema(NestedField(1, "p", field_type), NestedField(2, "id", LongType())) + spec = PartitionSpec(PartitionField(source_id=1, field_id=1000, transform=IdentityTransform(), name="p")) + table = catalog.create_table( + f"default.test_compute_partition_statistics_float_{index}", schema=schema, partition_spec=spec + ) + for value in values: + table.append(pa.Table.from_pylist([{"p": value, "id": 1}], schema=table.schema().as_arrow())) + + statistics_file = table.maintenance.compute_partition_statistics() + assert statistics_file is not None + # NULL first. NaN values form one partition that sorts after the other floats, and -0.0 and 0.0 are two partitions. + rows = _read_partition_statistics(table, statistics_file) + assert [(repr(row["partition"]["p"]), row["data_record_count"], row["data_file_count"]) for row in rows] == [ + ("None", 1, 1), + ("-inf", 1, 1), + ("-0.0", 1, 1), + ("0.0", 1, 1), + ("inf", 1, 1), + ("nan", 2, 2), + ] + + +def test_compute_partition_statistics_keeps_output_type_of_removed_v1_partition_field(catalog: Catalog) -> None: + catalog.create_namespace("default") + cases: list[tuple[str, Any, Any, Any]] = [ + ("bucket", BucketTransform(8), StringType(), "abc"), + ("year", YearTransform(), DateType(), date(2025, 1, 1)), + ] + for name, transform, source_type, value in cases: + schema = Schema(NestedField(1, "a", source_type), NestedField(2, "id", LongType())) + spec = PartitionSpec(PartitionField(source_id=1, field_id=1000, transform=transform, name="p")) + table = catalog.create_table( + f"default.test_compute_partition_statistics_v1_{name}", + schema=schema, + partition_spec=spec, + properties={"format-version": "1"}, + ) + table.append(pa.Table.from_pylist([{"a": value, "id": 1}], schema=table.schema().as_arrow())) + # In v1, removing a partition field replaces its transform with void, under the same field ID + table.update_spec().remove_field("p").commit() + + statistics_file = table.maintenance.compute_partition_statistics() + assert statistics_file is not None + with table.io.new_input(statistics_file.statistics_path).open() as stream: + arrow_table = pq.read_table(stream) + # The value keeps the output type of the transform in the manifest, not the type of the source column + assert arrow_table.schema.field("partition").type.field("p").type == pa.int32() + assert arrow_table.column("partition").to_pylist() == [{"p": transform.transform(source_type)(value)}] + + +def test_compute_partition_statistics_delete_files(catalog: Catalog) -> None: + catalog.create_namespace("default") + table = _create_category_table(catalog, "default.test_compute_partition_statistics_deletes") + table.append(pa.Table.from_pylist([{"category": "a", "id": 1}], schema=table.schema().as_arrow())) + snapshot = _commit_delete_manifest( + table, + [ + (_delete_file(table, "a", DataFileContent.POSITION_DELETES, record_count=2), ManifestEntryStatus.ADDED), + (_delete_file(table, "a", DataFileContent.EQUALITY_DELETES, record_count=3), ManifestEntryStatus.ADDED), + # A deleted delete file is not counted, but its partition still gets a row + (_delete_file(table, "b", DataFileContent.POSITION_DELETES, record_count=9), ManifestEntryStatus.DELETED), + ], + ) + + statistics_file = table.maintenance.compute_partition_statistics() + assert statistics_file is not None + size_of = _data_file_sizes(table) + assert _read_partition_statistics(table, statistics_file) == [ + _expected_partition_statistics_row( + 0, + {"category": "a"}, + data_record_count=1, + data_file_count=1, + total_data_file_size_in_bytes=size_of["a"], + position_delete_record_count=2, + position_delete_file_count=1, + equality_delete_record_count=3, + equality_delete_file_count=1, + last_updated_at=snapshot.timestamp_ms, + last_updated_snapshot_id=snapshot.snapshot_id, + ), + _expected_partition_statistics_row( + 0, + {"category": "b"}, + last_updated_at=snapshot.timestamp_ms, + last_updated_snapshot_id=snapshot.snapshot_id, + ), + ] + + +def test_compute_partition_statistics_empty_snapshot(catalog: Catalog) -> None: + catalog.create_namespace("default") + table = _create_category_table(catalog, "default.test_compute_partition_statistics_empty_snapshot") + with table.transaction() as transaction: + with transaction.update_snapshot().fast_append(): + pass + assert table.current_snapshot() is not None + + # A snapshot without manifest entries has no statistics to write + assert table.maintenance.compute_partition_statistics() is None + assert table.metadata.partition_statistics == [] + + +def test_compute_partition_statistics_entries_of_expired_snapshot(catalog: Catalog) -> None: + catalog.create_namespace("default") + table = _create_category_table(catalog, "default.test_compute_partition_statistics_expired_entries") + table.append(pa.Table.from_pylist([{"category": "a", "id": 1}], schema=table.schema().as_arrow())) + expired = table.current_snapshot() + assert expired is not None + table.append(pa.Table.from_pylist([{"category": "b", "id": 2}], schema=table.schema().as_arrow())) + current = table.current_snapshot() + assert current is not None + table.maintenance.expire_snapshots().by_id(expired.snapshot_id).commit() + + statistics_file = table.maintenance.compute_partition_statistics() + assert statistics_file is not None + size_of = _data_file_sizes(table) + # The entries of partition "a" come from the expired snapshot, so they set no last update, as in Java + assert _read_partition_statistics(table, statistics_file) == [ + _expected_partition_statistics_row( + 0, {"category": "a"}, data_record_count=1, data_file_count=1, total_data_file_size_in_bytes=size_of["a"] + ), + _expected_partition_statistics_row( + 0, + {"category": "b"}, + data_record_count=1, + data_file_count=1, + total_data_file_size_in_bytes=size_of["b"], + last_updated_at=current.timestamp_ms, + last_updated_snapshot_id=current.snapshot_id, + ), + ] + + +def test_compute_partition_statistics_rejects_registered_file_of_expired_snapshot(catalog: Catalog) -> None: + catalog.create_namespace("default") + table = _create_category_table(catalog, "default.test_compute_partition_statistics_stale") + table.append(pa.Table.from_pylist([{"category": "a", "id": 1}], schema=table.schema().as_arrow())) + statistics_file = table.maintenance.compute_partition_statistics() + assert statistics_file is not None + table.append(pa.Table.from_pylist([{"category": "b", "id": 2}], schema=table.schema().as_arrow())) + table.maintenance.expire_snapshots().by_id(statistics_file.snapshot_id).commit() + + # Simulate a stale descriptor retained by an older writer (see #4364). + table.metadata = table.metadata.model_copy(update={"partition_statistics": [statistics_file]}) + with pytest.raises(ValueError, match=f"Snapshot not found: {statistics_file.snapshot_id}"): + table.maintenance.compute_partition_statistics(snapshot_id=statistics_file.snapshot_id) + + +def test_compute_partition_statistics_same_partition_under_different_specs(catalog: Catalog) -> None: + catalog.create_namespace("default") + schema = Schema(NestedField(1, "dt", DateType()), NestedField(2, "category", StringType()), NestedField(3, "id", LongType())) + spec = PartitionSpec(PartitionField(source_id=1, field_id=1000, transform=IdentityTransform(), name="dt")) + table = catalog.create_table("default.test_compute_partition_statistics_same_tuple", schema=schema, partition_spec=spec) + arrow_schema = table.schema().as_arrow() + table.append(pa.Table.from_pylist([{"dt": date(2025, 1, 1), "category": None, "id": 1}], schema=arrow_schema)) + table.update_spec().add_identity("category").commit() + table.append(pa.Table.from_pylist([{"dt": date(2025, 1, 1), "category": None, "id": 2}], schema=arrow_schema)) + + statistics_file = table.maintenance.compute_partition_statistics() + assert statistics_file is not None + # Both specs have the unified partition (2025-01-01, NULL), and each spec keeps its own row + rows = _read_partition_statistics(table, statistics_file) + assert sorted((row["spec_id"], row["partition"]["dt"], row["partition"]["category"]) for row in rows) == [ + (0, date(2025, 1, 1), None), + (1, date(2025, 1, 1), None), + ] + + +def _data_file_sizes(table: Table) -> dict[str, int]: + return {row["partition"]["category"]: row["file_size_in_bytes"] for row in table.inspect.data_files().to_pylist()} + + +def _delete_file(table: Table, category: str, content: DataFileContent, record_count: int) -> DataFile: + import uuid + + return DataFile.from_args( + content=content, + file_path=table.location_provider().new_data_location(f"{content.name.lower()}-{uuid.uuid4()}.parquet"), + file_format=FileFormat.PARQUET, + partition=Record(category), + record_count=record_count, + file_size_in_bytes=1, + equality_ids=[2] if content == DataFileContent.EQUALITY_DELETES else None, + ) + + +def _commit_delete_manifest(table: Table, entries: list[tuple[DataFile, ManifestEntryStatus]]) -> Snapshot: + """Commit a snapshot that adds a manifest of delete files, written as other engines write them. + + PyIceberg cannot write delete files. The snapshot keeps the manifests of the current snapshot and adds this one. + """ + import uuid + + from pyiceberg.manifest import ManifestContent, ManifestEntry, ManifestWriterV2, write_manifest_list + from pyiceberg.table.refs import SnapshotRefType + from pyiceberg.table.snapshots import Operation, Summary + from pyiceberg.table.update import AddSnapshotUpdate, SetSnapshotRefUpdate + + class DeleteManifestWriter(ManifestWriterV2): + def content(self) -> ManifestContent: + return ManifestContent.DELETES + + @property + def _meta(self) -> dict[str, str]: + return {**super()._meta, "content": "deletes"} + + parent = table.current_snapshot() + assert parent is not None + snapshot_id = parent.snapshot_id + 1 + sequence_number = table.last_sequence_number + 1 + manifest_path = table.location_provider().new_metadata_location(f"delete-manifest-{uuid.uuid4()}.avro") + with DeleteManifestWriter(table.spec(), table.schema(), table.io.new_output(manifest_path), snapshot_id, "null") as writer: + for data_file, status in entries: + writer.add_entry( + ManifestEntry.from_args( + status=status, + snapshot_id=snapshot_id, + sequence_number=sequence_number, + file_sequence_number=sequence_number, + data_file=data_file, + ) + ) + + list_path = table.location_provider().new_metadata_location(f"snap-{snapshot_id}-{uuid.uuid4()}.avro") + with write_manifest_list( + 2, table.io.new_output(list_path), snapshot_id, parent.snapshot_id, sequence_number, "null" + ) as manifests: + manifests.add_manifests([*parent.manifests(table.io), writer.to_manifest_file()]) + + snapshot = Snapshot( + snapshot_id=snapshot_id, + parent_snapshot_id=parent.snapshot_id, + sequence_number=sequence_number, + timestamp_ms=parent.timestamp_ms + 1, + manifest_list=list_path, + summary=Summary(Operation.OVERWRITE), + schema_id=table.schema().schema_id, + ) + with table.transaction() as transaction: + transaction._apply( + ( + AddSnapshotUpdate(snapshot=snapshot), + SetSnapshotRefUpdate(ref_name="main", type=SnapshotRefType.BRANCH, snapshot_id=snapshot_id), + ) + ) + return snapshot From 9bc2d6221b3c34654316f61ea5c3db943d36af4f Mon Sep 17 00:00:00 2001 From: rahulsmahadev Date: Tue, 1 Dec 2026 03:06:15 +0000 Subject: [PATCH 2/3] Use specs_struct() as the partition type of partition statistics Drop the forked _partition_type. specs_struct() keeps fields whose source column was dropped, so partitions that only differ in such a field keep their own rows instead of being merged. Give specs_struct() Java's v1 rule: a void field keeps the type of its last other transform. Tests reuse commit_partition_position_delete and fold the value type and empty-snapshot cases. Co-authored-by: Isaac --- mkdocs/docs/api.md | 4 +- pyiceberg/table/metadata.py | 10 +- pyiceberg/table/partition_stats.py | 35 +--- tests/table/test_metadata.py | 16 ++ tests/table/test_statistics.py | 283 ++++++++--------------------- 5 files changed, 104 insertions(+), 244 deletions(-) diff --git a/mkdocs/docs/api.md b/mkdocs/docs/api.md index 3a7d8b788e..847a5fe71b 100644 --- a/mkdocs/docs/api.md +++ b/mkdocs/docs/api.md @@ -1630,9 +1630,9 @@ partition_statistics_file = table.maintenance.compute_partition_statistics() partition_statistics_file = table.maintenance.compute_partition_statistics(snapshot_id=12345) ``` -When no statistics file is registered for the requested snapshot, statistics are computed from its manifests; files from earlier snapshots are not reused. The `total_record_count` column is left empty. If the snapshot already has a partition statistics file, that file is returned and nothing is written. +If the snapshot already has a partition statistics file, that file is returned and nothing is written. Otherwise the statistics are computed from the manifests of the snapshot, without reusing the file of an earlier snapshot, and `total_record_count` is left empty. -UUID partition fields currently raise `ArrowNotImplementedError` before a statistics file is registered; see #4358 for the related inspection limitation. Historical partition fields that share a name but have different field IDs raise `ValueError` before registration because the unified schema requires unique field names. +Format version 3 tables, UUID partition fields and tables whose `write.format.default` is not Parquet are not supported yet. ### Snapshot Expiration diff --git a/pyiceberg/table/metadata.py b/pyiceberg/table/metadata.py index fc2eba0c4a..79ef802c5f 100644 --- a/pyiceberg/table/metadata.py +++ b/pyiceberg/table/metadata.py @@ -39,6 +39,7 @@ assign_fresh_sort_order_ids, ) from pyiceberg.table.statistics import PartitionStatisticsFile, StatisticsFile +from pyiceberg.transforms import VoidTransform from pyiceberg.typedef import ( EMPTY_DICT, IcebergBaseModel, @@ -325,13 +326,20 @@ def specs_struct(self, spec_ids: Iterable[int] | None = None) -> StructType: # Collect all the fields struct_fields = {field.field_id: field for spec in selected_specs for field in spec.fields} + # A v1 table removes a partition field by making it void, so its type comes from the last transform that is not void + transforms = { + field.field_id: field.transform + for spec in selected_specs + for field in spec.fields + if not isinstance(field.transform, VoidTransform) + } nested_fields = [] # Sort them by field_id in order to get a deterministic output for field_id in sorted(struct_fields): field = struct_fields[field_id] source_type = self._resolve_field_type(field.source_id) - result_type = field.transform.result_type(source_type) + result_type = transforms.get(field_id, field.transform).result_type(source_type) nested_fields.append(NestedField(field_id=field.field_id, name=field.name, type=result_type, required=False)) return StructType(*nested_fields) diff --git a/pyiceberg/table/partition_stats.py b/pyiceberg/table/partition_stats.py index 9aa0d4dbd8..e9bdc0e347 100644 --- a/pyiceberg/table/partition_stats.py +++ b/pyiceberg/table/partition_stats.py @@ -27,12 +27,10 @@ from pyiceberg.manifest import DataFile, DataFileContent, FileFormat, ManifestEntryStatus from pyiceberg.schema import Schema from pyiceberg.table.statistics import PartitionStatisticsFile -from pyiceberg.transforms import VoidTransform from pyiceberg.types import IntegerType, LongType, NestedField, StructType if TYPE_CHECKING: from pyiceberg.table import Table - from pyiceberg.table.metadata import TableMetadata from pyiceberg.table.snapshots import Snapshot @@ -71,7 +69,7 @@ def compute_and_write_partition_statistics_file(table: Table, snapshot_id: int) NotImplementedError: If the table is format version 3 or later. ArrowNotImplementedError: If the unified partition type contains a UUID field. """ - partition_type = _partition_type(table.metadata) + partition_type = table.metadata.specs_struct() if not partition_type.fields: raise ValueError("Table must be partitioned") if table.metadata.format_version >= 3: @@ -87,29 +85,6 @@ def compute_and_write_partition_statistics_file(table: Table, snapshot_id: int) return _write_partition_statistics_file(table, snapshot_id, partition_type, rows) -def _partition_type(metadata: TableMetadata) -> StructType: - """Return the unified partition type, as Java's Partitioning.partitionType does. - - It holds the partition fields of all specs whose source column is still in the current schema, sorted by field ID. - Fields of dropped source columns are left out, unlike TableMetadata.specs_struct. - """ - schema = metadata.schema() - fields: dict[int, NestedField] = {} - # Specs are visited in ascending order, so the name from the latest spec wins - for spec in sorted(metadata.specs().values(), key=lambda spec: spec.spec_id): - for field in spec.fields: - if schema.find_column_name(field.source_id) is not None: - previous = fields.get(field.field_id) - # A v1 spec drops a partition field by replacing its transform with void, which keeps the output type - # of the older transform, as Java's Partitioning does - if isinstance(field.transform, VoidTransform) and previous is not None: - result_type = previous.field_type - else: - result_type = field.transform.result_type(schema.find_type(field.source_id)) - fields[field.field_id] = NestedField(field.field_id, field.name, result_type, required=False) - return StructType(*(fields[field_id] for field_id in sorted(fields))) - - def _partition_value_key(value: Any) -> tuple[Any, ...]: """Return a key that groups and orders partition values as Iceberg does. @@ -136,13 +111,11 @@ def _collect_partition_statistics(table: Table, snapshot: Snapshot, partition_ty for manifest in snapshot.manifests(table.io, table.encryption_manager): spec = specs[manifest.partition_spec_id] - # Maps each partition value of the spec to its position in the unified partition type - spec_positions = [ - (pos, positions[field.field_id]) for pos, field in enumerate(spec.fields) if field.field_id in positions - ] + # The position of each partition value of the spec in the unified partition type + spec_positions = [positions[field.field_id] for field in spec.fields] for entry in manifest.fetch_manifest_entry(table.io, discard_deleted=False): partition: list[Any] = [None] * len(partition_type.fields) - for pos, index in spec_positions: + for pos, index in enumerate(spec_positions): partition[index] = entry.data_file.partition[pos] key = (spec.spec_id, tuple(_partition_value_key(value) for value in partition)) if (stats := stats_by_key.get(key)) is None: diff --git a/tests/table/test_metadata.py b/tests/table/test_metadata.py index d4e0dc651f..77ef1c310e 100644 --- a/tests/table/test_metadata.py +++ b/tests/table/test_metadata.py @@ -1108,6 +1108,22 @@ def test_specs_struct_resolves_dropped_partition_source_from_historical_schemas( ) +def test_specs_struct_keeps_type_of_removed_v1_partition_field(example_table_metadata_v1: dict[str, Any]) -> None: + # A v1 table removes a partition field by replacing its transform with void, under the same field ID + table_metadata = TableMetadataV1( + **{ + **example_table_metadata_v1, + "partition-specs": [ + {"spec-id": 0, "fields": [{"name": "x_bucket", "transform": "bucket[8]", "source-id": 1, "field-id": 1000}]}, + {"spec-id": 1, "fields": [{"name": "x_bucket", "transform": "void", "source-id": 1, "field-id": 1000}]}, + ], + "default-spec-id": 1, + } + ) + + assert table_metadata.specs_struct() == StructType(NestedField(1000, "x_bucket", IntegerType(), required=False)) + + def test_specs_struct_falls_back_to_unknown_when_source_missing_from_all_schemas() -> None: table_metadata = TableMetadataV2( location="s3://bucket/test/location", diff --git a/tests/table/test_statistics.py b/tests/table/test_statistics.py index b35e93d83d..250ebf115e 100644 --- a/tests/table/test_statistics.py +++ b/tests/table/test_statistics.py @@ -23,11 +23,11 @@ import pytest from pyiceberg.catalog import Catalog -from pyiceberg.manifest import DataFile, DataFileContent, FileFormat, ManifestEntryStatus +from pyiceberg.manifest import DataFile, DataFileContent from pyiceberg.partitioning import PartitionField, PartitionSpec from pyiceberg.schema import Schema from pyiceberg.table import Table -from pyiceberg.table.snapshots import Snapshot +from pyiceberg.table.partition_stats import _add_live_file, _PartitionStatistics from pyiceberg.table.statistics import BlobMetadata, PartitionStatisticsFile, StatisticsFile from pyiceberg.transforms import BucketTransform, IdentityTransform, YearTransform from pyiceberg.typedef import Record @@ -43,6 +43,7 @@ StringType, TimestampType, ) +from tests.table.test_commit_retry import commit_partition_position_delete def test_partition_statistics_file() -> None: @@ -300,6 +301,11 @@ def test_compute_partition_statistics(catalog: Catalog) -> None: ), ] + # The file of the snapshot is returned as is, without writing a file or committing the table again + metadata_location = table.metadata_location + assert table.maintenance.compute_partition_statistics() == statistics_file + assert table.metadata_location == metadata_location + # Statistics of an older snapshot are registered for that snapshot, next to the current one statistics_file_2 = table.maintenance.compute_partition_statistics(snapshot_id=snapshot_2.snapshot_id) assert statistics_file_2 is not None @@ -344,27 +350,33 @@ def test_compute_partition_statistics_across_partition_specs(catalog: Catalog) - arrow_schema = table.schema().as_arrow() table.append(pa.Table.from_pylist([{"dt": date(2025, 1, 2), "category": None, "id": 1}], schema=arrow_schema)) table.update_spec().add_identity("category").commit() - table.append(pa.Table.from_pylist([{"dt": date(2025, 1, 1), "category": "x", "id": 2}], schema=arrow_schema)) + rows = [{"dt": date(2025, 1, 1), "category": "x", "id": 2}, {"dt": date(2025, 1, 2), "category": None, "id": 3}] + table.append(pa.Table.from_pylist(rows, schema=arrow_schema)) statistics_file = table.maintenance.compute_partition_statistics() assert statistics_file is not None - # The unified partition type holds the fields of both specs, and rows of the first spec have NULL for the new field - rows = _read_partition_statistics(table, statistics_file) - assert [(row["spec_id"], row["partition"], row["data_record_count"]) for row in rows] == [ - (1, {"dt": date(2025, 1, 1), "category": "x"}, 1), - (0, {"dt": date(2025, 1, 2), "category": None}, 1), + # The unified partition type holds the fields of both specs, and rows of the first spec have NULL for the new field. + # Both specs have the partition (2025-01-02, NULL), and each spec keeps its own row. + statistics = _read_partition_statistics(table, statistics_file) + assert [row["partition"] for row in statistics] == [ + {"dt": date(2025, 1, 1), "category": "x"}, + {"dt": date(2025, 1, 2), "category": None}, + {"dt": date(2025, 1, 2), "category": None}, + ] + assert sorted((row["spec_id"], row["partition"]["dt"]) for row in statistics) == [ + (0, date(2025, 1, 2)), + (1, date(2025, 1, 1)), + (1, date(2025, 1, 2)), ] -def test_compute_partition_statistics_skips_dropped_source_columns(catalog: Catalog) -> None: +def test_compute_partition_statistics_after_dropping_partition_source_column(catalog: Catalog) -> None: catalog.create_namespace("default") schema = Schema(NestedField(1, "a", LongType()), NestedField(2, "b", StringType()), NestedField(3, "id", LongType())) spec = PartitionSpec(PartitionField(source_id=1, field_id=1000, transform=IdentityTransform(), name="a")) table = catalog.create_table("default.test_compute_partition_statistics_dropped", schema=schema, partition_spec=spec) - table.append( - pa.Table.from_pylist([{"a": 1, "b": "x", "id": 1}, {"a": 2, "b": "x", "id": 2}], schema=table.schema().as_arrow()) - ) + table.append(pa.Table.from_pylist([{"a": a, "b": "x", "id": 1} for a in [1, 2, 2]], schema=table.schema().as_arrow())) table.update_spec().remove_field("a").add_identity("b").commit() table.update_schema().delete_column("a").commit() table.append(pa.Table.from_pylist([{"b": "y", "id": 3}], schema=table.schema().as_arrow())) @@ -372,61 +384,63 @@ def test_compute_partition_statistics_skips_dropped_source_columns(catalog: Cata statistics_file = table.maintenance.compute_partition_statistics() assert statistics_file is not None - # The field of the dropped source column is left out of the partition type, so the two partitions of the first spec - # that differ only in "a" share one row with NULL for "b". Java keeps them in separate rows; this is a known difference. + # The field of the dropped column stays in the partition type, so the partitions a=1 and a=2 keep their own rows rows = _read_partition_statistics(table, statistics_file) - assert [(row["spec_id"], row["partition"], row["data_record_count"], row["data_file_count"]) for row in rows] == [ - (0, {"b": None}, 2, 2), - (1, {"b": "y"}, 1, 1), + assert [(row["spec_id"], row["partition"], row["data_record_count"]) for row in rows] == [ + (1, {"a": None, "b": "y"}, 1), + (0, {"a": 1, "b": None}, 1), + (0, {"a": 2, "b": None}, 2), ] def test_compute_partition_statistics_partition_value_types(catalog: Catalog) -> None: catalog.create_namespace("default") - cases: list[tuple[Any, Any]] = [ - (LongType(), 1), - (StringType(), "a"), - (DateType(), date(2025, 1, 1)), - (TimestampType(), datetime(2025, 1, 1, 12, 30)), - (BooleanType(), True), - (DecimalType(10, 2), Decimal("1.50")), - (BinaryType(), b"ab"), + # The values in the order of their rows: NaN sorts after every other float, and -0.0 and 0.0 are two partitions + floats = [float("-inf"), -0.0, 0.0, float("inf"), float("nan")] + cases: list[tuple[Any, list[Any]]] = [ + (LongType(), [1]), + (StringType(), ["a"]), + (DateType(), [date(2025, 1, 1)]), + (TimestampType(), [datetime(2025, 1, 1, 12, 30)]), + (BooleanType(), [True]), + (DecimalType(10, 2), [Decimal("1.50")]), + (BinaryType(), [b"ab"]), + (FloatType(), floats), + (DoubleType(), floats), ] - for index, (field_type, value) in enumerate(cases): + for index, (field_type, values) in enumerate(cases): schema = Schema(NestedField(1, "p", field_type), NestedField(2, "id", LongType())) spec = PartitionSpec(PartitionField(source_id=1, field_id=1000, transform=IdentityTransform(), name="p")) table = catalog.create_table( f"default.test_compute_partition_statistics_types_{index}", schema=schema, partition_spec=spec ) - table.append(pa.Table.from_pylist([{"p": value, "id": 1}, {"p": None, "id": 2}], schema=table.schema().as_arrow())) + # Each value is appended twice in reverse order, as a data file of its own each time + for value in [*reversed(values), None] * 2: + table.append(pa.Table.from_pylist([{"p": value, "id": 1}], schema=table.schema().as_arrow())) statistics_file = table.maintenance.compute_partition_statistics() assert statistics_file is not None - assert [row["partition"]["p"] for row in _read_partition_statistics(table, statistics_file)] == [None, value], field_type - - -def test_compute_partition_statistics_returns_registered_file(catalog: Catalog) -> None: - catalog.create_namespace("default") - table = _create_category_table(catalog, "default.test_compute_partition_statistics_registered") - table.append(pa.Table.from_pylist([{"category": "a", "id": 1}], schema=table.schema().as_arrow())) - statistics_file = table.maintenance.compute_partition_statistics() - metadata_location = table.metadata_location - - # The file of the snapshot is returned as is, without writing a file or committing the table again - assert table.maintenance.compute_partition_statistics() == statistics_file - assert table.metadata_location == metadata_location - assert table.metadata.partition_statistics == [statistics_file] + # NULL first, and the two data files of a value share its row + rows = _read_partition_statistics(table, statistics_file) + assert [(repr(row["partition"]["p"]), row["data_file_count"]) for row in rows] == [ + (repr(value), 2) for value in [None, *values] + ], field_type -def test_compute_partition_statistics_without_statistics(catalog: Catalog) -> None: +def test_compute_partition_statistics_without_entries(catalog: Catalog) -> None: catalog.create_namespace("default") table = _create_category_table(catalog, "default.test_compute_partition_statistics_empty") - # A table without snapshots has nothing to compute + # Neither a table without snapshots nor a snapshot without manifest entries has statistics to write + assert table.maintenance.compute_partition_statistics() is None + with table.transaction() as transaction: + with transaction.update_snapshot().fast_append(): + pass + assert table.current_snapshot() is not None assert table.maintenance.compute_partition_statistics() is None assert table.metadata.partition_statistics == [] -def test_compute_partition_statistics_errors(catalog: Catalog) -> None: +def test_compute_partition_statistics_errors(catalog: Catalog, table_v3: Table) -> None: catalog.create_namespace("default") unpartitioned = catalog.create_table( "default.test_compute_partition_statistics_unpartitioned", schema=Schema(NestedField(1, "id", LongType())) @@ -446,37 +460,8 @@ def test_compute_partition_statistics_errors(catalog: Catalog) -> None: table.maintenance.compute_partition_statistics() assert table.metadata.partition_statistics == [] - # PyIceberg cannot create format version 3 tables, so the format version of a loaded v2 table is changed for this check - table.metadata = table.metadata.model_copy(update={"format_version": 3}) with pytest.raises(NotImplementedError, match="format version 3"): - table.maintenance.compute_partition_statistics() - - -def test_compute_partition_statistics_float_partition_identity_and_order(catalog: Catalog) -> None: - catalog.create_namespace("default") - # Each value is appended on its own, so each value is one data file - values: list[float | None] = [float("nan"), float("nan"), -0.0, 0.0, float("inf"), float("-inf"), None] - for index, field_type in enumerate([FloatType(), DoubleType()]): - schema = Schema(NestedField(1, "p", field_type), NestedField(2, "id", LongType())) - spec = PartitionSpec(PartitionField(source_id=1, field_id=1000, transform=IdentityTransform(), name="p")) - table = catalog.create_table( - f"default.test_compute_partition_statistics_float_{index}", schema=schema, partition_spec=spec - ) - for value in values: - table.append(pa.Table.from_pylist([{"p": value, "id": 1}], schema=table.schema().as_arrow())) - - statistics_file = table.maintenance.compute_partition_statistics() - assert statistics_file is not None - # NULL first. NaN values form one partition that sorts after the other floats, and -0.0 and 0.0 are two partitions. - rows = _read_partition_statistics(table, statistics_file) - assert [(repr(row["partition"]["p"]), row["data_record_count"], row["data_file_count"]) for row in rows] == [ - ("None", 1, 1), - ("-inf", 1, 1), - ("-0.0", 1, 1), - ("0.0", 1, 1), - ("inf", 1, 1), - ("nan", 2, 2), - ] + table_v3.maintenance.compute_partition_statistics() def test_compute_partition_statistics_keeps_output_type_of_removed_v1_partition_field(catalog: Catalog) -> None: @@ -510,54 +495,32 @@ def test_compute_partition_statistics_keeps_output_type_of_removed_v1_partition_ def test_compute_partition_statistics_delete_files(catalog: Catalog) -> None: catalog.create_namespace("default") table = _create_category_table(catalog, "default.test_compute_partition_statistics_deletes") - table.append(pa.Table.from_pylist([{"category": "a", "id": 1}], schema=table.schema().as_arrow())) - snapshot = _commit_delete_manifest( - table, - [ - (_delete_file(table, "a", DataFileContent.POSITION_DELETES, record_count=2), ManifestEntryStatus.ADDED), - (_delete_file(table, "a", DataFileContent.EQUALITY_DELETES, record_count=3), ManifestEntryStatus.ADDED), - # A deleted delete file is not counted, but its partition still gets a row - (_delete_file(table, "b", DataFileContent.POSITION_DELETES, record_count=9), ManifestEntryStatus.DELETED), - ], - ) + table.append(pa.Table.from_pylist([{"category": "a", "id": 1}, {"category": "a", "id": 2}], schema=table.schema().as_arrow())) + data_file = next(iter(table.scan().plan_files())).file + commit_partition_position_delete(table, Record("a"), data_file.file_path, pos=0) + snapshot = table.current_snapshot() + assert snapshot is not None statistics_file = table.maintenance.compute_partition_statistics() assert statistics_file is not None - size_of = _data_file_sizes(table) assert _read_partition_statistics(table, statistics_file) == [ _expected_partition_statistics_row( 0, {"category": "a"}, - data_record_count=1, + data_record_count=2, data_file_count=1, - total_data_file_size_in_bytes=size_of["a"], - position_delete_record_count=2, + total_data_file_size_in_bytes=data_file.file_size_in_bytes, + position_delete_record_count=1, position_delete_file_count=1, - equality_delete_record_count=3, - equality_delete_file_count=1, - last_updated_at=snapshot.timestamp_ms, - last_updated_snapshot_id=snapshot.snapshot_id, - ), - _expected_partition_statistics_row( - 0, - {"category": "b"}, last_updated_at=snapshot.timestamp_ms, last_updated_snapshot_id=snapshot.snapshot_id, - ), + ) ] - -def test_compute_partition_statistics_empty_snapshot(catalog: Catalog) -> None: - catalog.create_namespace("default") - table = _create_category_table(catalog, "default.test_compute_partition_statistics_empty_snapshot") - with table.transaction() as transaction: - with transaction.update_snapshot().fast_append(): - pass - assert table.current_snapshot() is not None - - # A snapshot without manifest entries has no statistics to write - assert table.maintenance.compute_partition_statistics() is None - assert table.metadata.partition_statistics == [] + # PyIceberg cannot write equality delete files, so one is counted directly + stats = _PartitionStatistics(spec_id=0, partition=()) + _add_live_file(stats, DataFile.from_args(content=DataFileContent.EQUALITY_DELETES, record_count=3, file_size_in_bytes=1)) + assert stats == _PartitionStatistics(0, (), equality_delete_record_count=3, equality_delete_file_count=1) def test_compute_partition_statistics_entries_of_expired_snapshot(catalog: Catalog) -> None: @@ -573,7 +536,7 @@ def test_compute_partition_statistics_entries_of_expired_snapshot(catalog: Catal statistics_file = table.maintenance.compute_partition_statistics() assert statistics_file is not None - size_of = _data_file_sizes(table) + size_of = {row["partition"]["category"]: row["file_size_in_bytes"] for row in table.inspect.data_files().to_pylist()} # The entries of partition "a" come from the expired snapshot, so they set no last update, as in Java assert _read_partition_statistics(table, statistics_file) == [ _expected_partition_statistics_row( @@ -604,103 +567,3 @@ def test_compute_partition_statistics_rejects_registered_file_of_expired_snapsho table.metadata = table.metadata.model_copy(update={"partition_statistics": [statistics_file]}) with pytest.raises(ValueError, match=f"Snapshot not found: {statistics_file.snapshot_id}"): table.maintenance.compute_partition_statistics(snapshot_id=statistics_file.snapshot_id) - - -def test_compute_partition_statistics_same_partition_under_different_specs(catalog: Catalog) -> None: - catalog.create_namespace("default") - schema = Schema(NestedField(1, "dt", DateType()), NestedField(2, "category", StringType()), NestedField(3, "id", LongType())) - spec = PartitionSpec(PartitionField(source_id=1, field_id=1000, transform=IdentityTransform(), name="dt")) - table = catalog.create_table("default.test_compute_partition_statistics_same_tuple", schema=schema, partition_spec=spec) - arrow_schema = table.schema().as_arrow() - table.append(pa.Table.from_pylist([{"dt": date(2025, 1, 1), "category": None, "id": 1}], schema=arrow_schema)) - table.update_spec().add_identity("category").commit() - table.append(pa.Table.from_pylist([{"dt": date(2025, 1, 1), "category": None, "id": 2}], schema=arrow_schema)) - - statistics_file = table.maintenance.compute_partition_statistics() - assert statistics_file is not None - # Both specs have the unified partition (2025-01-01, NULL), and each spec keeps its own row - rows = _read_partition_statistics(table, statistics_file) - assert sorted((row["spec_id"], row["partition"]["dt"], row["partition"]["category"]) for row in rows) == [ - (0, date(2025, 1, 1), None), - (1, date(2025, 1, 1), None), - ] - - -def _data_file_sizes(table: Table) -> dict[str, int]: - return {row["partition"]["category"]: row["file_size_in_bytes"] for row in table.inspect.data_files().to_pylist()} - - -def _delete_file(table: Table, category: str, content: DataFileContent, record_count: int) -> DataFile: - import uuid - - return DataFile.from_args( - content=content, - file_path=table.location_provider().new_data_location(f"{content.name.lower()}-{uuid.uuid4()}.parquet"), - file_format=FileFormat.PARQUET, - partition=Record(category), - record_count=record_count, - file_size_in_bytes=1, - equality_ids=[2] if content == DataFileContent.EQUALITY_DELETES else None, - ) - - -def _commit_delete_manifest(table: Table, entries: list[tuple[DataFile, ManifestEntryStatus]]) -> Snapshot: - """Commit a snapshot that adds a manifest of delete files, written as other engines write them. - - PyIceberg cannot write delete files. The snapshot keeps the manifests of the current snapshot and adds this one. - """ - import uuid - - from pyiceberg.manifest import ManifestContent, ManifestEntry, ManifestWriterV2, write_manifest_list - from pyiceberg.table.refs import SnapshotRefType - from pyiceberg.table.snapshots import Operation, Summary - from pyiceberg.table.update import AddSnapshotUpdate, SetSnapshotRefUpdate - - class DeleteManifestWriter(ManifestWriterV2): - def content(self) -> ManifestContent: - return ManifestContent.DELETES - - @property - def _meta(self) -> dict[str, str]: - return {**super()._meta, "content": "deletes"} - - parent = table.current_snapshot() - assert parent is not None - snapshot_id = parent.snapshot_id + 1 - sequence_number = table.last_sequence_number + 1 - manifest_path = table.location_provider().new_metadata_location(f"delete-manifest-{uuid.uuid4()}.avro") - with DeleteManifestWriter(table.spec(), table.schema(), table.io.new_output(manifest_path), snapshot_id, "null") as writer: - for data_file, status in entries: - writer.add_entry( - ManifestEntry.from_args( - status=status, - snapshot_id=snapshot_id, - sequence_number=sequence_number, - file_sequence_number=sequence_number, - data_file=data_file, - ) - ) - - list_path = table.location_provider().new_metadata_location(f"snap-{snapshot_id}-{uuid.uuid4()}.avro") - with write_manifest_list( - 2, table.io.new_output(list_path), snapshot_id, parent.snapshot_id, sequence_number, "null" - ) as manifests: - manifests.add_manifests([*parent.manifests(table.io), writer.to_manifest_file()]) - - snapshot = Snapshot( - snapshot_id=snapshot_id, - parent_snapshot_id=parent.snapshot_id, - sequence_number=sequence_number, - timestamp_ms=parent.timestamp_ms + 1, - manifest_list=list_path, - summary=Summary(Operation.OVERWRITE), - schema_id=table.schema().schema_id, - ) - with table.transaction() as transaction: - transaction._apply( - ( - AddSnapshotUpdate(snapshot=snapshot), - SetSnapshotRefUpdate(ref_name="main", type=SnapshotRefType.BRANCH, snapshot_id=snapshot_id), - ) - ) - return snapshot From d71bfb4d094dc60001efa59d5218e7ee3eac5039 Mon Sep 17 00:00:00 2001 From: rahulsmahadev Date: Tue, 1 Dec 2026 03:43:27 +0000 Subject: [PATCH 3/3] Test last updates after a dropped column and inspect after a v1 removal The dropped-column test appends each old partition in its own snapshot and checks each row's last update, with manifest merging off: merging old-spec manifests after the column is dropped writes an unreadable manifest. Add an inspect test for the v1 void rule in specs_struct() and document the ValueError for a reused field name. Co-authored-by: Isaac --- mkdocs/docs/api.md | 2 +- tests/table/test_inspect.py | 28 ++++++++++++++++++++++++++-- tests/table/test_statistics.py | 25 ++++++++++++++++++------- 3 files changed, 45 insertions(+), 10 deletions(-) diff --git a/mkdocs/docs/api.md b/mkdocs/docs/api.md index 847a5fe71b..7b9e9eb188 100644 --- a/mkdocs/docs/api.md +++ b/mkdocs/docs/api.md @@ -1632,7 +1632,7 @@ partition_statistics_file = table.maintenance.compute_partition_statistics(snaps If the snapshot already has a partition statistics file, that file is returned and nothing is written. Otherwise the statistics are computed from the manifests of the snapshot, without reusing the file of an earlier snapshot, and `total_record_count` is left empty. -Format version 3 tables, UUID partition fields and tables whose `write.format.default` is not Parquet are not supported yet. +Format version 3 tables, UUID partition fields and tables whose `write.format.default` is not Parquet are not supported yet. Historical partition fields that share a name under different field IDs raise `ValueError`. ### Snapshot Expiration diff --git a/tests/table/test_inspect.py b/tests/table/test_inspect.py index 3d24549012..15712cb37f 100644 --- a/tests/table/test_inspect.py +++ b/tests/table/test_inspect.py @@ -15,6 +15,7 @@ # specific language governing permissions and limitations # under the License. +from datetime import date from pathlib import PosixPath from typing import Any @@ -27,9 +28,9 @@ from pyiceberg.schema import Schema from pyiceberg.table.inspect import InspectTable, _readable_bound from pyiceberg.table.snapshots import Snapshot -from pyiceberg.transforms import IdentityTransform +from pyiceberg.transforms import BucketTransform, IdentityTransform, YearTransform from pyiceberg.typedef import Record -from pyiceberg.types import DoubleType, FloatType, IntegerType, LongType, NestedField, PrimitiveType, StringType +from pyiceberg.types import DateType, DoubleType, FloatType, IntegerType, LongType, NestedField, PrimitiveType, StringType from tests.catalog.test_base import InMemoryCatalog @@ -224,6 +225,29 @@ def test_inspect_files_after_v1_partition_field_readd(catalog: InMemoryCatalog) assert all(part["p"] is None for part in entry_partitions) +def test_inspect_after_removing_v1_partition_fields(catalog: InMemoryCatalog) -> None: + schema = Schema( + NestedField(1, "id", LongType(), required=False), + NestedField(2, "s", StringType(), required=False), + NestedField(3, "d", DateType(), required=False), + ) + spec = PartitionSpec( + PartitionField(2, 1000, BucketTransform(8), "s_bucket"), PartitionField(3, 1001, YearTransform(), "d_year") + ) + tbl = catalog.create_table("default.v1_partition_remove", schema, partition_spec=spec, properties={"format-version": "1"}) + data = pa.table({"id": [1], "s": ["abc"], "d": [date(2025, 1, 1)]}) + tbl.append(data) + with tbl.update_spec() as spec_update: + spec_update.remove_field("s_bucket").remove_field("d_year") + tbl.append(data) + + # In v1 the removed fields become void, and keep the int type of their transforms for the files of the first spec + for inspect_table in [tbl.inspect.files(), tbl.inspect.partitions()]: + assert inspect_table.schema.field("partition").type == pa.struct([("s_bucket", pa.int32()), ("d_year", pa.int32())]) + partitions = sorted(inspect_table.column("partition").to_pylist(), key=lambda part: part["s_bucket"] is None) + assert partitions == [{"s_bucket": 2, "d_year": 55}, {"s_bucket": None, "d_year": None}] + + def test_inspect_after_dropping_old_partition_column(catalog: InMemoryCatalog) -> None: schema = Schema( NestedField(1, "id", LongType(), required=False), diff --git a/tests/table/test_statistics.py b/tests/table/test_statistics.py index 250ebf115e..bd7a171cfd 100644 --- a/tests/table/test_statistics.py +++ b/tests/table/test_statistics.py @@ -375,21 +375,32 @@ def test_compute_partition_statistics_after_dropping_partition_source_column(cat catalog.create_namespace("default") schema = Schema(NestedField(1, "a", LongType()), NestedField(2, "b", StringType()), NestedField(3, "id", LongType())) spec = PartitionSpec(PartitionField(source_id=1, field_id=1000, transform=IdentityTransform(), name="a")) - table = catalog.create_table("default.test_compute_partition_statistics_dropped", schema=schema, partition_spec=spec) - table.append(pa.Table.from_pylist([{"a": a, "b": "x", "id": 1} for a in [1, 2, 2]], schema=table.schema().as_arrow())) + # Merging the manifests of a spec whose source column was dropped writes an unreadable manifest, a separate bug + properties = {"commit.manifest-merge.enabled": "false"} + table = catalog.create_table( + "default.test_compute_partition_statistics_dropped", schema=schema, partition_spec=spec, properties=properties + ) + table.append(pa.Table.from_pylist([{"a": 1, "b": "x", "id": 1}], schema=table.schema().as_arrow())) + table.append( + pa.Table.from_pylist([{"a": 2, "b": "x", "id": 2}, {"a": 2, "b": "x", "id": 3}], schema=table.schema().as_arrow()) + ) table.update_spec().remove_field("a").add_identity("b").commit() table.update_schema().delete_column("a").commit() - table.append(pa.Table.from_pylist([{"b": "y", "id": 3}], schema=table.schema().as_arrow())) + table.append(pa.Table.from_pylist([{"b": "y", "id": 4}], schema=table.schema().as_arrow())) statistics_file = table.maintenance.compute_partition_statistics() assert statistics_file is not None # The field of the dropped column stays in the partition type, so the partitions a=1 and a=2 keep their own rows + first, second, third = [(snapshot.timestamp_ms, snapshot.snapshot_id) for snapshot in table.snapshots()] rows = _read_partition_statistics(table, statistics_file) - assert [(row["spec_id"], row["partition"], row["data_record_count"]) for row in rows] == [ - (1, {"a": None, "b": "y"}, 1), - (0, {"a": 1, "b": None}, 1), - (0, {"a": 2, "b": None}, 2), + assert [ + (row["spec_id"], row["partition"], row["data_record_count"], (row["last_updated_at"], row["last_updated_snapshot_id"])) + for row in rows + ] == [ + (1, {"a": None, "b": "y"}, 1, third), + (0, {"a": 1, "b": None}, 1, first), + (0, {"a": 2, "b": None}, 2, second), ]