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
11 changes: 5 additions & 6 deletions pyiceberg/table/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -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,
)

Expand Down
1 change: 1 addition & 0 deletions tests/io/test_pyarrow.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Loading