diff --git a/pyiceberg/table/inspect.py b/pyiceberg/table/inspect.py index f540a01d4e..c06b97ec8c 100644 --- a/pyiceberg/table/inspect.py +++ b/pyiceberg/table/inspect.py @@ -21,12 +21,12 @@ from datetime import UTC, datetime from typing import TYPE_CHECKING, Any -from pyiceberg.conversions import from_bytes +from pyiceberg.conversions import from_bytes, to_json from pyiceberg.expressions import AlwaysTrue, BooleanExpression from pyiceberg.manifest import DataFile, DataFileContent, ManifestContent, ManifestFile, PartitionFieldSummary from pyiceberg.partitioning import PartitionSpec from pyiceberg.table.snapshots import Snapshot, ancestors_of -from pyiceberg.types import IcebergType, PrimitiveType +from pyiceberg.types import DecimalType, IcebergType, PrimitiveType from pyiceberg.utils.concurrent import ExecutorFactory from pyiceberg.utils.singleton import _convert_to_hashable_type @@ -42,6 +42,18 @@ def _readable_bound(field_type: PrimitiveType, bound: bytes | None) -> Any | Non return from_bytes(field_type, bound) if bound is not None else None +def _partition_bound_to_human(partition_field_type: PrimitiveType, transform: Any, value: Any) -> str: + """Render a partition bound for display. + + Decimal values use the plain JSON notation (#4420): to_human_string falls + through to str(), which switches to scientific notation for small + magnitudes, so a partition value of 0.00000012 would display as 1.2E-7. + """ + if isinstance(partition_field_type, DecimalType): + return to_json(partition_field_type, value) + return transform.to_human_string(partition_field_type, value) + + class InspectTable: """Provides metadata inspection for Apache Iceberg tables. @@ -576,19 +588,19 @@ def _partition_summaries_to_rows( field = spec.fields[i] partition_field_type = partition_field_types[field.field_id] lower_bound = ( - ( - field.transform.to_human_string( - partition_field_type, from_bytes(partition_field_type, field_summary.lower_bound) - ) + _partition_bound_to_human( + partition_field_type, + field.transform, + from_bytes(partition_field_type, field_summary.lower_bound), ) if field_summary.lower_bound is not None else None ) upper_bound = ( - ( - field.transform.to_human_string( - partition_field_type, from_bytes(partition_field_type, field_summary.upper_bound) - ) + _partition_bound_to_human( + partition_field_type, + field.transform, + from_bytes(partition_field_type, field_summary.upper_bound), ) if field_summary.upper_bound is not None else None diff --git a/tests/table/test_inspect.py b/tests/table/test_inspect.py index 3d24549012..57a102322e 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 decimal import Decimal from pathlib import PosixPath from typing import Any @@ -29,7 +30,7 @@ from pyiceberg.table.snapshots import Snapshot from pyiceberg.transforms import IdentityTransform from pyiceberg.typedef import Record -from pyiceberg.types import DoubleType, FloatType, IntegerType, LongType, NestedField, PrimitiveType, StringType +from pyiceberg.types import DecimalType, DoubleType, FloatType, IntegerType, LongType, NestedField, PrimitiveType, StringType from tests.catalog.test_base import InMemoryCatalog @@ -104,6 +105,26 @@ def test_inspect_entries_and_files_with_data_file_without_metrics(catalog: InMem assert files[0]["column_sizes"] is None +@pytest.mark.filterwarnings("ignore:Falling back to pure Python Avro decoder") +def test_manifests_render_decimal_bounds_in_plain_notation(catalog: InMemoryCatalog) -> None: + schema = Schema(NestedField(1, "amount", DecimalType(20, 8), required=True)) + spec = PartitionSpec(PartitionField(source_id=1, field_id=1000, transform=IdentityTransform(), name="amount")) + tbl = catalog.create_table("default.dec_bounds", schema, partition_spec=spec) + tbl.append( + pa.table( + {"amount": pa.array([Decimal("0.00000012"), Decimal("100.00000000")], type=pa.decimal128(20, 8))}, + schema=tbl.schema().as_arrow(), + ) + ) + + summaries = tbl.inspect.manifests().to_pydict()["partition_summaries"][0] + + # Plain notation per the decimal JSON convention (#4420): str() would render + # the small magnitude as 1.2E-7 + assert summaries[0]["lower_bound"] == "0.00000012" + assert summaries[0]["upper_bound"] == "100.00000000" + + @pytest.mark.parametrize("newest_first", [False, True]) def test_partitions_last_updated_uses_latest_snapshot_regardless_of_order(newest_first: bool) -> None: # Manifest entries are visited in manifest order, which is not chronological, so the