Skip to content
Open
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
32 changes: 22 additions & 10 deletions pyiceberg/table/inspect.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand All @@ -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.

Expand Down Expand Up @@ -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
Expand Down
23 changes: 22 additions & 1 deletion tests/table/test_inspect.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand All @@ -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


Expand Down Expand Up @@ -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"

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Minor: 100.00000000 renders the same with str(), so this test still passes if the upper bound keeps to_human_string, and nothing covers truncate. Small bounds such as -0.00000012 and 0.00000012 on an identity and a truncate[10] column would pin both.



@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
Expand Down