From 40eb3dfea0a13fad25d71a8de64be2b87d64121e Mon Sep 17 00:00:00 2001 From: 1fanwang <1fannnw@gmail.com> Date: Mon, 23 Nov 2026 14:42:56 -0500 Subject: [PATCH] PyArrow: Preserve field IDs on dictionary batch reads Signed-off-by: 1fanwang <1fannnw@gmail.com> --- pyiceberg/table/__init__.py | 11 +++++------ tests/io/test_pyarrow.py | 1 + 2 files changed, 6 insertions(+), 6 deletions(-) diff --git a/pyiceberg/table/__init__.py b/pyiceberg/table/__init__.py index a8fcdb16e6..4fc3333f9f 100644 --- a/pyiceberg/table/__init__.py +++ b/pyiceberg/table/__init__.py @@ -2537,17 +2537,16 @@ def _to_arrow_batch_reader_via_file_scan_tasks( ).to_record_batches(tasks) if dictionary_columns: - # schema_to_pyarrow returns plain types, but ArrowScan yields dictionary-encoded - # batches for the requested columns. Peek at the first batch to pick up the actual - # dictionary field Arrow produced, rather than assuming an index type, and put the - # batch back on the stream. Without this, .cast(target_schema) would silently - # convert the dictionary arrays back to their plain value type and erase the encoding. + # Arrow determines the dictionary type; the projection owns field IDs and nullability. dict_col_set = set(dictionary_columns) first_batch = next(batches, None) if first_batch is not None: batches = chain([first_batch], batches) target_schema = pa.schema( - [first_batch.schema.field(field.name) if field.name in dict_col_set else field for field in target_schema], + [ + field.with_type(first_batch.schema.field(field.name).type) if field.name in dict_col_set else field + for field in target_schema + ], metadata=target_schema.metadata, ) diff --git a/tests/io/test_pyarrow.py b/tests/io/test_pyarrow.py index e1e99be4c7..941c864ffc 100644 --- a/tests/io/test_pyarrow.py +++ b/tests/io/test_pyarrow.py @@ -6740,6 +6740,7 @@ def __init__(self) -> None: assert pa.types.is_dictionary(result.schema.field("label").type), ( f"expected dictionary type, got {result.schema.field('label').type}" ) + assert result.schema.field("label").metadata == arrow_schema.field("label").metadata # id is not in dictionary_columns — must remain int32 assert result.schema.field("id").type == pa.int32() # Values must be identical to the source data