From db228154f1f93e47342bd1e052f5177101b78b7c Mon Sep 17 00:00:00 2001 From: Anurag Singh Date: Wed, 11 Nov 2026 18:33:07 +0530 Subject: [PATCH 1/2] Decode dictionary arrays before applying Arrow partition transforms --- pyiceberg/transforms.py | 7 +++++-- tests/catalog/test_catalog_behaviors.py | 18 ++++++++++++++++++ tests/test_transforms.py | 18 ++++++++++++++++++ 3 files changed, 41 insertions(+), 2 deletions(-) diff --git a/pyiceberg/transforms.py b/pyiceberg/transforms.py index 74925c695f..71c0cb3d9b 100644 --- a/pyiceberg/transforms.py +++ b/pyiceberg/transforms.py @@ -132,12 +132,15 @@ def _cast_if_needed(arr: "ArrayLike") -> "ArrayLike": else: return arr + def _decode_if_dictionary(arr: "ArrayLike") -> "ArrayLike": + return arr.dictionary_decode() if pa.types.is_dictionary(arr.type) else arr + if isinstance(array, pa.Array): - return _cast_if_needed(transform_func(array, *args)) + return _cast_if_needed(transform_func(_decode_if_dictionary(array), *args)) elif isinstance(array, pa.ChunkedArray): result_chunks = [] for arr in array.iterchunks(): - result_chunks.append(_cast_if_needed(transform_func(arr, *args))) + result_chunks.append(_cast_if_needed(transform_func(_decode_if_dictionary(arr), *args))) return pa.chunked_array(result_chunks) else: raise ValueError(f"PyArrow array can only be of type pa.Array or pa.ChunkedArray, but found {type(array)}") diff --git a/tests/catalog/test_catalog_behaviors.py b/tests/catalog/test_catalog_behaviors.py index 1ed9ea4096..02700f7b21 100644 --- a/tests/catalog/test_catalog_behaviors.py +++ b/tests/catalog/test_catalog_behaviors.py @@ -338,6 +338,24 @@ def test_write_pyarrow_schema(catalog: Catalog, test_table_identifier: Identifie table.append(pyarrow_table) +def test_write_dictionary_encoded_column_with_bucket_partition(catalog: Catalog) -> None: + catalog.create_namespace("default") + schema = pa.schema([pa.field("id", pa.int64()), pa.field("s", pa.string())]) + table = catalog.create_table("default.dictionary_bucket", schema=schema) + with table.update_spec() as update: + update.add_field("s", "bucket[8]") + + plain = pa.table({"id": [1, 2], "s": ["a", "b"]}) + encoded = plain.cast(pa.schema([pa.field("id", pa.int64()), pa.field("s", pa.dictionary(pa.int32(), pa.string()))])) + table.append(encoded) + + assert sorted(table.scan().to_arrow().to_pylist(), key=lambda row: row["id"]) == [ + {"id": 1, "s": "a"}, + {"id": 2, "s": "b"}, + ] + assert len(table.inspect.partitions()) == 2 + + @pytest.mark.parametrize( "schema,expected", [ diff --git a/tests/test_transforms.py b/tests/test_transforms.py index 998d8a224d..a8dca09a3a 100644 --- a/tests/test_transforms.py +++ b/tests/test_transforms.py @@ -1701,6 +1701,24 @@ def test_bucket_pyarrow_transforms( assert expected == transform.pyarrow_transform(source_type)(input_arr) +def test_pyarrow_transform_decodes_dictionary_arrays() -> None: + transform: Transform[Any, Any] = BucketTransform(num_buckets=8) + plain = pa.array(["a", "b", None]) + encoded = plain.dictionary_encode() + + assert ( + transform.pyarrow_transform(StringType())(encoded).to_pylist() + == transform.pyarrow_transform(StringType())(plain).to_pylist() + ) + + chunked_encoded = pa.chunked_array([encoded, pa.array(["c"]).dictionary_encode()]) + chunked_plain = pa.chunked_array([plain, pa.array(["c"])]) + assert ( + transform.pyarrow_transform(StringType())(chunked_encoded).to_pylist() + == transform.pyarrow_transform(StringType())(chunked_plain).to_pylist() + ) + + def test_bucket_pyarrow_void_transform() -> None: input_arr = pa.chunked_array([pa.array([1, 2], type=pa.int32()), pa.array([3, 4], type=pa.int32())]) output_arr = pa.array([None, None, None, None], type=pa.int32()) From cf62de0dde6cc67eb0cee64482dd83dee2e568ca Mon Sep 17 00:00:00 2001 From: Anurag Singh Date: Sun, 29 Nov 2026 07:44:09 +0530 Subject: [PATCH 2/2] Core: Decode dictionary arrays in PyArrow transform methods Decode only in the transforms that need it instead of the shared Arrow wrapper, drop the catalog behavior test since it is not catalog specific, and parameterize the transform test to cover all transforms. --- pyiceberg/transforms.py | 35 +++++++++++++++------- tests/catalog/test_catalog_behaviors.py | 18 ----------- tests/test_transforms.py | 40 +++++++++++++++++-------- 3 files changed, 51 insertions(+), 42 deletions(-) diff --git a/pyiceberg/transforms.py b/pyiceberg/transforms.py index 71c0cb3d9b..2bccf312f0 100644 --- a/pyiceberg/transforms.py +++ b/pyiceberg/transforms.py @@ -132,15 +132,12 @@ def _cast_if_needed(arr: "ArrayLike") -> "ArrayLike": else: return arr - def _decode_if_dictionary(arr: "ArrayLike") -> "ArrayLike": - return arr.dictionary_decode() if pa.types.is_dictionary(arr.type) else arr - if isinstance(array, pa.Array): - return _cast_if_needed(transform_func(_decode_if_dictionary(array), *args)) + return _cast_if_needed(transform_func(array, *args)) elif isinstance(array, pa.ChunkedArray): result_chunks = [] for arr in array.iterchunks(): - result_chunks.append(_cast_if_needed(transform_func(_decode_if_dictionary(arr), *args))) + result_chunks.append(_cast_if_needed(transform_func(arr, *args))) return pa.chunked_array(result_chunks) else: raise ValueError(f"PyArrow array can only be of type pa.Array or pa.ChunkedArray, but found {type(array)}") @@ -148,6 +145,16 @@ def _decode_if_dictionary(arr: "ArrayLike") -> "ArrayLike": return _transform +def _decode_dictionary(array: "ArrayLike") -> "ArrayLike": + pa = try_import("pyarrow") + + if isinstance(array, pa.Array) and pa.types.is_dictionary(array.type): + return array.dictionary_decode() + if isinstance(array, pa.ChunkedArray) and pa.types.is_dictionary(array.type): + return pa.chunked_array([chunk.dictionary_decode() for chunk in array.iterchunks()]) + return array + + class Transform(IcebergRootModel[str], ABC, Generic[S, T]): """Transform base class for concrete transforms. @@ -386,7 +393,8 @@ def __repr__(self) -> str: def pyarrow_transform(self, source: IcebergType) -> "Callable[[pa.Array], pa.Array]": pyiceberg_core_transform = try_import("pyiceberg_core", extras_name="pyiceberg-core").transform - return _pyiceberg_transform_wrapper(pyiceberg_core_transform.bucket, self._num_buckets) + transform = _pyiceberg_transform_wrapper(pyiceberg_core_transform.bucket, self._num_buckets) + return lambda array: transform(_decode_dictionary(array)) class TimeResolution(IntEnum): @@ -501,7 +509,8 @@ def __repr__(self) -> str: def pyarrow_transform(self, source: IcebergType) -> "Callable[[pa.Array], pa.Array]": pa = try_import("pyarrow") pyiceberg_core_transform = try_import("pyiceberg_core", extras_name="pyiceberg-core").transform - return _pyiceberg_transform_wrapper(pyiceberg_core_transform.year, expected_type=pa.int32()) + transform = _pyiceberg_transform_wrapper(pyiceberg_core_transform.year, expected_type=pa.int32()) + return lambda array: transform(_decode_dictionary(array)) class MonthTransform(TimeTransform[S]): @@ -562,7 +571,8 @@ def pyarrow_transform(self, source: IcebergType) -> "Callable[[pa.Array], pa.Arr pa = try_import("pyarrow") pyiceberg_core_transform = try_import("pyiceberg_core", extras_name="pyiceberg-core").transform - return _pyiceberg_transform_wrapper(pyiceberg_core_transform.month, expected_type=pa.int32()) + transform = _pyiceberg_transform_wrapper(pyiceberg_core_transform.month, expected_type=pa.int32()) + return lambda array: transform(_decode_dictionary(array)) class DayTransform(TimeTransform[S]): @@ -631,7 +641,8 @@ def pyarrow_transform(self, source: IcebergType) -> "Callable[[pa.Array], pa.Arr pa = try_import("pyarrow", extras_name="pyarrow") pyiceberg_core_transform = try_import("pyiceberg_core", extras_name="pyiceberg-core").transform - return _pyiceberg_transform_wrapper(pyiceberg_core_transform.day, expected_type=pa.int32()) + transform = _pyiceberg_transform_wrapper(pyiceberg_core_transform.day, expected_type=pa.int32()) + return lambda array: transform(_decode_dictionary(array)) class HourTransform(TimeTransform[S]): @@ -683,7 +694,8 @@ def __repr__(self) -> str: def pyarrow_transform(self, source: IcebergType) -> "Callable[[pa.Array], pa.Array]": pyiceberg_core_transform = try_import("pyiceberg_core", extras_name="pyiceberg-core").transform - return _pyiceberg_transform_wrapper(pyiceberg_core_transform.hour) + transform = _pyiceberg_transform_wrapper(pyiceberg_core_transform.hour) + return lambda array: transform(_decode_dictionary(array)) def _base64encode(buffer: bytes) -> str: @@ -911,7 +923,8 @@ def __repr__(self) -> str: def pyarrow_transform(self, source: IcebergType) -> "Callable[[pa.Array], pa.Array]": pyiceberg_core_transform = try_import("pyiceberg_core", extras_name="pyiceberg-core").transform - return _pyiceberg_transform_wrapper(pyiceberg_core_transform.truncate, self._width) + transform = _pyiceberg_transform_wrapper(pyiceberg_core_transform.truncate, self._width) + return lambda array: transform(_decode_dictionary(array)) @singledispatch diff --git a/tests/catalog/test_catalog_behaviors.py b/tests/catalog/test_catalog_behaviors.py index 02700f7b21..1ed9ea4096 100644 --- a/tests/catalog/test_catalog_behaviors.py +++ b/tests/catalog/test_catalog_behaviors.py @@ -338,24 +338,6 @@ def test_write_pyarrow_schema(catalog: Catalog, test_table_identifier: Identifie table.append(pyarrow_table) -def test_write_dictionary_encoded_column_with_bucket_partition(catalog: Catalog) -> None: - catalog.create_namespace("default") - schema = pa.schema([pa.field("id", pa.int64()), pa.field("s", pa.string())]) - table = catalog.create_table("default.dictionary_bucket", schema=schema) - with table.update_spec() as update: - update.add_field("s", "bucket[8]") - - plain = pa.table({"id": [1, 2], "s": ["a", "b"]}) - encoded = plain.cast(pa.schema([pa.field("id", pa.int64()), pa.field("s", pa.dictionary(pa.int32(), pa.string()))])) - table.append(encoded) - - assert sorted(table.scan().to_arrow().to_pylist(), key=lambda row: row["id"]) == [ - {"id": 1, "s": "a"}, - {"id": 2, "s": "b"}, - ] - assert len(table.inspect.partitions()) == 2 - - @pytest.mark.parametrize( "schema,expected", [ diff --git a/tests/test_transforms.py b/tests/test_transforms.py index a8dca09a3a..0024d248c9 100644 --- a/tests/test_transforms.py +++ b/tests/test_transforms.py @@ -1701,22 +1701,36 @@ def test_bucket_pyarrow_transforms( assert expected == transform.pyarrow_transform(source_type)(input_arr) -def test_pyarrow_transform_decodes_dictionary_arrays() -> None: - transform: Transform[Any, Any] = BucketTransform(num_buckets=8) - plain = pa.array(["a", "b", None]) +@pytest.mark.parametrize( + "transform, source_type, plain", + [ + pytest.param(IdentityTransform(), StringType(), pa.array(["a", "b", None]), id="identity"), + pytest.param(VoidTransform(), StringType(), pa.array(["a", "b", None]), id="void"), + pytest.param(BucketTransform(num_buckets=8), StringType(), pa.array(["a", "b", None]), id="bucket"), + pytest.param(TruncateTransform(width=2), StringType(), pa.array(["abc", "def", None]), id="truncate"), + pytest.param(YearTransform(), DateType(), pa.array([date(2020, 1, 1), date(2021, 6, 1), None]), id="year"), + pytest.param(MonthTransform(), DateType(), pa.array([date(2020, 1, 1), date(2021, 6, 1), None]), id="month"), + pytest.param(DayTransform(), DateType(), pa.array([date(2020, 1, 1), date(2021, 6, 1), None]), id="day"), + pytest.param( + HourTransform(), + TimestampType(), + pa.array([datetime(2020, 1, 1), datetime(2021, 6, 1), None]), + id="hour", + ), + ], +) +def test_pyarrow_transform_decodes_dictionary_arrays( + transform: Transform[Any, Any], source_type: PrimitiveType, plain: pa.Array +) -> None: + transform_func = transform.pyarrow_transform(source_type) encoded = plain.dictionary_encode() - assert ( - transform.pyarrow_transform(StringType())(encoded).to_pylist() - == transform.pyarrow_transform(StringType())(plain).to_pylist() - ) + assert transform_func(encoded).to_pylist() == transform_func(plain).to_pylist() - chunked_encoded = pa.chunked_array([encoded, pa.array(["c"]).dictionary_encode()]) - chunked_plain = pa.chunked_array([plain, pa.array(["c"])]) - assert ( - transform.pyarrow_transform(StringType())(chunked_encoded).to_pylist() - == transform.pyarrow_transform(StringType())(chunked_plain).to_pylist() - ) + chunked_plain = pa.chunked_array([plain.slice(0, 1), plain.slice(1)]) + chunked_encoded = pa.chunked_array([chunk.dictionary_encode() for chunk in chunked_plain.iterchunks()]) + + assert transform_func(chunked_encoded).to_pylist() == transform_func(chunked_plain).to_pylist() def test_bucket_pyarrow_void_transform() -> None: