Skip to content

PyArrow: Preserve field IDs on dictionary batch reads - #4475

Open
1fanwang wants to merge 1 commit into
apache:mainfrom
1fanwang:1fanwang/preserve-streaming-dictionary-field-ids
Open

1fanwang wants to merge 1 commit into
apache:mainfrom
1fanwang:1fanwang/preserve-streaming-dictionary-field-ids

Conversation

@1fanwang

Copy link
Copy Markdown
Contributor

Rationale for this change

Requesting dictionary encoding on a streaming scan dropped the Iceberg field ID for that column. Saving the result to Parquet preserved the values but lost the column's identity. The same export without dictionary encoding kept the ID.

The reader now takes the dictionary type from the first batch and retains the field from the projected schema. The encoding fix in #3595 still applies.

Are these changes tested?

End-to-end testing

With the PyArrow and SQLite extras installed, run the reproducer from the checkout:

PYTHONPATH=. python -B repro.py

Before, the dictionary-enabled column could not be converted back to an Iceberg field. Output and final exception:

dictionary=False, field ID=b'2'
round-trip field ID=2, rows=2
dictionary=True, field ID=None
ValueError: Parquet file does not have field-ids and the Iceberg table does not have 'schema.name-mapping.default' defined

After:

dictionary=False, field ID=b'2'
round-trip field ID=2, rows=2
dictionary=True, field ID=b'2'
round-trip field ID=2, rows=2
Reproducer source: repro.py
from pathlib import Path
from tempfile import TemporaryDirectory

import pyarrow as pa
import pyarrow.parquet as pq
from pyiceberg.catalog.sql import SqlCatalog
from pyiceberg.io.pyarrow import PYARROW_PARQUET_FIELD_ID_KEY, pyarrow_to_schema
from pyiceberg.schema import Schema
from pyiceberg.types import IntegerType, NestedField, StringType

with TemporaryDirectory() as tmp:
    directory = Path(tmp)
    catalog = SqlCatalog("demo", uri="sqlite://", warehouse=directory.as_uri())
    catalog.create_namespace("demo")
    table = catalog.create_table(
        identifier="demo.samples",
        schema=Schema(NestedField(1, "id", IntegerType()), NestedField(2, "kind", StringType())),
    )
    table.append(pa.table({"id": pa.array([1, 2], type=pa.int32()), "kind": ["a", "b"]}))
    for columns in [(), ("kind",)]:
        rows = table.scan().to_arrow_batch_reader(dictionary_columns=columns).read_all()
        field = rows.schema.field("kind")
        print(f"dictionary={pa.types.is_dictionary(field.type)}, field ID={(field.metadata or {}).get(PYARROW_PARQUET_FIELD_ID_KEY)}")
        path = directory / "scan.parquet"
        pq.write_table(table=rows, where=path)
        converted = pyarrow_to_schema(pq.read_schema(path))
        print(f"round-trip field ID={converted.find_field('kind').field_id}, rows={rows.num_rows}")

Are there any user-facing changes?

Parquet exports from dictionary-enabled batch scans keep the same field IDs as plain scans.

AI disclosure: GitHub Copilot CLI drafted the fix and regression assertion.

Signed-off-by: 1fanwang <1fannnw@gmail.com>
Copilot AI balanced review requested due to automatic review settings November 24, 2026 02:02

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🟢 Approval recommended

No active review issues are recorded for the change preserving Iceberg field metadata in dictionary-enabled PyArrow scans.

No open issues 🎉

What changed in this PR

Preserves Iceberg field IDs in dictionary-enabled PyArrow batch scans so Parquet exports retain column identity without losing dictionary encoding.

Changes:

  • Takes dictionary types from the first batch while preserving projected field metadata and nullability.
  • Adds a regression assertion that dictionary-column metadata matches the projected schema.
File Description
tests/​io/​test_pyarrow.py Asserts metadata is preserved for dictionary-encoded columns.
pyiceberg/​table/​__init__.py Updates projected field types without replacing their metadata or nullability.

Review effort: Balanced

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants