From 50298f8316bae5daf69dee29cdf371651af983e6 Mon Sep 17 00:00:00 2001 From: Gedalia Kliger <117381134+gedaliakliger@users.noreply.github.com> Date: Thu, 19 Nov 2026 18:25:45 +0000 Subject: [PATCH] Encode * and ~ in partition paths the way Java does partition_to_path uses quote_plus to match java.net.URLEncoder, but the two differ on "*" (Java keeps it) and "~" (Java escapes it), so the same partition got a different path from PyIceberg than from Java. --- pyiceberg/partitioning.py | 9 +++++++-- tests/table/test_partitioning.py | 8 ++++++++ 2 files changed, 15 insertions(+), 2 deletions(-) diff --git a/pyiceberg/partitioning.py b/pyiceberg/partitioning.py index 6d79dce18a..f557d4a7e2 100644 --- a/pyiceberg/partitioning.py +++ b/pyiceberg/partitioning.py @@ -68,6 +68,11 @@ PARTITION_FIELD_ID_START: int = 1000 +def _url_encode(value: str) -> str: + # Unlike quote_plus, java.net.URLEncoder leaves "*" as is and escapes "~" + return quote_plus(value, safe="*").replace("~", "%7E") + + class PartitionField(IcebergBaseModel): """PartitionField represents how one partition value is derived from the source column via transformation. @@ -248,8 +253,8 @@ def partition_to_path(self, data: Record, schema: Schema) -> str: partition_field = self.fields[pos] value_str = partition_field.transform.to_human_string(field_types[pos].field_type, value=data[pos]) - value_strs.append(quote_plus(value_str, safe="")) - field_strs.append(quote_plus(partition_field.name, safe="")) + value_strs.append(_url_encode(value_str)) + field_strs.append(_url_encode(partition_field.name)) path = "/".join([field_str + "=" + value_str for field_str, value_str in zip(field_strs, value_strs, strict=True)]) return path diff --git a/tests/table/test_partitioning.py b/tests/table/test_partitioning.py index 17b465e7e4..1f24cfccd8 100644 --- a/tests/table/test_partitioning.py +++ b/tests/table/test_partitioning.py @@ -172,6 +172,14 @@ def test_partition_spec_to_path() -> None: assert spec.partition_to_path(record, schema) == "my%23str%25bucket=my%2Bstr/other+str%2Bbucket=%28+%29/my%21int%3Abucket=10" +def test_partition_spec_to_path_asterisk_and_tilde() -> None: + schema = Schema(NestedField(field_id=1, name="str", field_type=StringType(), required=False)) + spec = PartitionSpec(PartitionField(source_id=1, field_id=1000, transform=IdentityTransform(), name="my*str~field")) + + # java.net.URLEncoder leaves "*" as is and escapes "~" + assert spec.partition_to_path(Record("a*b~c-d_e.f"), schema) == "my*str%7Efield=a*b%7Ec-d_e.f" + + def test_partition_spec_to_path_dropped_source_id() -> None: schema = Schema( NestedField(field_id=1, name="str", field_type=StringType(), required=False),