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
4 changes: 3 additions & 1 deletion pyiceberg/io/pyarrow.py
Original file line number Diff line number Diff line change
Expand Up @@ -3519,7 +3519,9 @@ def _determine_partitions(spec: PartitionSpec, schema: Schema, arrow_table: pa.T
partition_array = partition_array.cast(partition_array.type.storage_type)
arrow_table = arrow_table.append_column(name, partition_array)

unique_partition_fields = arrow_table.select(partition_fields).group_by(partition_fields).aggregate([])
# group_by cannot combine chunks with different dictionaries (https://github.com/apache/arrow/issues/14955);
# combine_chunks unifies them, and unlike unify_dictionaries keeps a shared dictionary that fills its index type
unique_partition_fields = arrow_table.select(partition_fields).combine_chunks().group_by(partition_fields).aggregate([])

for unique_partition in unique_partition_fields.to_pylist():
partition_key = PartitionKey(
Expand Down
87 changes: 87 additions & 0 deletions tests/io/test_pyarrow.py
Original file line number Diff line number Diff line change
Expand Up @@ -3676,6 +3676,93 @@ def test_determine_partitions_with_uuid_column() -> None:
assert all(p.arrow_table_partition.schema.field("uuid").type == pa.uuid() for p in partitions)


def test_determine_partitions_with_shared_dictionary_at_index_limit() -> None:
schema = Schema(
NestedField(id=1, name="id", field_type=LongType(), required=False),
NestedField(id=2, name="region", field_type=StringType(), required=False),
)
spec = PartitionSpec(PartitionField(source_id=2, field_id=1000, transform=IdentityTransform(), name="region"))
# Both chunks share one dictionary whose 128 values fill the int8 indices, which appends on main
dictionary = pa.array([f"r{i}" for i in range(128)])
region = pa.chunked_array(
[
pa.DictionaryArray.from_arrays(pa.array([0, 1], pa.int8()), dictionary),
pa.DictionaryArray.from_arrays(pa.array([127, 1], pa.int8()), dictionary),
]
)
arrow_table = pa.table({"id": pa.array([1, 2, 3, 4], pa.int64()), "region": region})

partitions = list(_determine_partitions(spec, schema, arrow_table))

rows_by_partition = {p.partition_key.partition[0]: sorted(p.arrow_table_partition["id"].to_pylist()) for p in partitions}
assert rows_by_partition == {"r0": [1], "r1": [2, 4], "r127": [3]}
assert all(p.arrow_table_partition.schema.field("region").type == pa.dictionary(pa.int8(), pa.string()) for p in partitions)


def test_determine_partitions_with_nested_dictionary_chunks() -> None:
schema = Schema(
NestedField(id=1, name="id", field_type=LongType(), required=False),
NestedField(
id=2,
name="info",
field_type=StructType(NestedField(id=3, name="region", field_type=StringType(), required=False)),
required=False,
),
)
spec = PartitionSpec(PartitionField(source_id=3, field_id=1000, transform=IdentityTransform(), name="region"))
arrow_schema = pa.schema(
[pa.field("id", pa.int64()), pa.field("info", pa.struct([pa.field("region", pa.dictionary(pa.int8(), pa.string()))]))]
)
# Each batch has its own dictionary for the partition source in the struct
arrow_table = pa.Table.from_batches(
[
pa.RecordBatch.from_pylist(
[{"id": 1, "info": {"region": "us"}}, {"id": 2, "info": {"region": "eu"}}, {"id": 3, "info": {"region": None}}],
schema=arrow_schema,
),
pa.RecordBatch.from_pylist(
[{"id": 4, "info": {"region": "ap"}}, {"id": 5, "info": {"region": "eu"}}], schema=arrow_schema
),
]
)

partitions = list(_determine_partitions(spec, schema, arrow_table))

rows_by_partition = {p.partition_key.partition[0]: sorted(p.arrow_table_partition["id"].to_pylist()) for p in partitions}
assert rows_by_partition == {"us": [1], "eu": [2, 5], None: [3], "ap": [4]}
written = pa.concat_tables([p.arrow_table_partition for p in partitions])
assert sorted(written.to_pylist(), key=lambda row: row["id"]) == arrow_table.to_pylist()
# The written data keeps the dictionary encoding
assert all(p.arrow_table_partition.schema == arrow_schema for p in partitions)


def test_append_dictionary_chunks_to_partitioned_table(catalog: InMemoryCatalog) -> None:
schema = Schema(
NestedField(id=1, name="id", field_type=LongType(), required=False),
NestedField(id=2, name="region", field_type=StringType(), required=False),
)
spec = PartitionSpec(PartitionField(source_id=2, field_id=1000, transform=IdentityTransform(), name="region"))
catalog.create_namespace("default")
table = catalog.create_table("default.test_append_dictionary_chunks", schema=schema, partition_spec=spec)
# Each chunk has its own dictionary, as after pa.concat_tables of tables with pandas categoricals
int8_dictionary = pa.dictionary(pa.int8(), pa.string())
arrow_table = pa.table(
{
"id": pa.array([1, 2, 3, 4, 5], type=pa.int64()),
"region": pa.chunked_array([pa.array(["us", "eu", None], int8_dictionary), pa.array(["ap", "eu"], int8_dictionary)]),
}
)
table.append(arrow_table)

assert sorted(table.scan().to_arrow().to_pylist(), key=lambda row: row["id"]) == arrow_table.to_pylist()
tasks = list(table.scan().plan_files())
assert {task.file.partition[0]: task.file.record_count for task in tasks} == {"us": 1, "eu": 2, None: 1, "ap": 1}
# The data files keep the dictionary encoding, which pyarrow 18 reads back with int32 indices
for task in tasks:
with table.io.new_input(task.file.file_path).open() as f:
assert pa.types.is_dictionary(pq.read_schema(f).field("region").type)


def test_append_uuid_to_table_partitioned_on_uuid(catalog: InMemoryCatalog) -> None:
schema = Schema(
NestedField(id=1, name="id", field_type=LongType(), required=False),
Expand Down
Loading