Skip to content

Error reading table after appending pyarrow table #1798

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
3 tasks
p1c2u opened this issue Mar 17, 2025 · 7 comments · Fixed by #1867
Closed
3 tasks

Error reading table after appending pyarrow table #1798

p1c2u opened this issue Mar 17, 2025 · 7 comments · Fixed by #1867

Comments

@p1c2u
Copy link

p1c2u commented Mar 17, 2025

Apache Iceberg version

None

Please describe the bug 🐞

Hi,

I have iceberg table created by pyiceberg and appended some data in pyarrow table format. When I try to read the table now I get error:

ValueError: Parquet file does not have field-ids and the Iceberg table does not have 'schema.name-mapping.default' defined

I have to covert my pyarrow table to pandas dataframe to make it work.

Willingness to contribute

  • I can contribute a fix for this bug independently
  • I would be willing to contribute a fix for this bug with guidance from the Iceberg community
  • I cannot contribute a fix for this bug at this time
@kevinjqliu
Copy link
Contributor

I have iceberg table created by pyiceberg and appended some data in pyarrow table format

can you provide the code you use to do this?

@p1c2u
Copy link
Author

p1c2u commented Mar 19, 2025

@kevinjqliu

after investigation I found out it happens after I append pyarrow table without list field specified in schema as optional.

Example table with schema

from pyiceberg.catalog import load_catalog

catalog = load_catalog(**dict(type="in-memory"))

from pyiceberg.schema import Schema
from pyiceberg.types import NestedField, StringType, ListType

schema = Schema(
    NestedField(field_id=1, name="name", field_type=StringType(), required=False),
    NestedField(
        field_id=3,
        name="my_list",
        field_type=ListType(
            element_id=45, element=StringType(), element_required=False
        ),
        required=False,
    ),
)
catalog.create_namespace_if_not_exists("test")
catalog.create_table_if_not_exists("test.table", schema)

I append dataset with my_list:

import pyarrow as pa

df_1 = pa.Table.from_pylist([
    {"name": "one", "my_list": ["test"]},
    {"name": "another", "my_list": ["test"]},
])
catalog.load_table("test.table").append(df_1)

Read works

catalog.load_table("test.table").scan().to_arrow()

I append dataset without my_list:

import pyarrow as pa

df_2 = pa.Table.from_pylist([
    {"name": "one"},
    {"name": "another"},
])
catalog.load_table("test.table").append(df_2)

This time it won't work

catalog.load_table("test.table").scan().to_arrow()

it will throw

ValueError: Parquet file does not have field-ids and the Iceberg table does not have 'schema.name-mapping.default' defined

@kevinjqliu
Copy link
Contributor

kevinjqliu commented Mar 30, 2025

interesting, thanks for the code! I can reproduce the issue.

heres a working version, note how the schema used for create table and append are all aligned

# working
from pyiceberg.catalog import load_catalog
import pyarrow as pa

catalog = load_catalog(**dict(type="in-memory"))

df_1 = pa.Table.from_pylist([
    {"name": "one", "my_list": ["test"]},
    {"name": "another", "my_list": ["test"]},
])
pyarrow_schema = df_1.schema

# create table
catalog.create_namespace_if_not_exists("test")
catalog.create_table_if_not_exists("test.table", pyarrow_schema)

# append data
catalog.load_table("test.table").append(df_1)
catalog.load_table("test.table").scan().to_arrow()

# append more data
df_2 = pa.Table.from_pylist([
    {"name": "one"},
    {"name": "another"},
], schema=pyarrow_schema)
catalog.load_table("test.table").append(df_2)
catalog.load_table("test.table").scan().to_arrow()

@kevinjqliu
Copy link
Contributor

I suspect the issue is with the schema definition

schema = Schema(
    NestedField(field_id=1, name="name", field_type=StringType(), required=False),
    NestedField(
        field_id=3,
        name="my_list",
        field_type=ListType(
            element_id=45, element=StringType(), element_required=False
        ),
        required=False,
    ),
)

or how we handle the schema conversion internally, between iceberg schema and pyarrow schema.

For example, using the example iceberg schema provided, i get a schema mismatch


# not working
from pyiceberg.catalog import load_catalog
import pyarrow as pa
from pyiceberg.schema import Schema
from pyiceberg.types import NestedField, StringType, ListType
from pyiceberg.io.pyarrow import schema_to_pyarrow

catalog = load_catalog(**dict(type="in-memory"))

schema = Schema(
    NestedField(field_id=1, name="name", field_type=StringType(), required=False),
    NestedField(
        field_id=3,
        name="my_list",
        field_type=ListType(
            element_id=45, element=StringType(), element_required=False
        ),
        required=False,
    ),
)
pyarrow_schema = schema_to_pyarrow(schema)

# create table
catalog.create_namespace_if_not_exists("test")
catalog.create_table_if_not_exists("test.table", pyarrow_schema)

# append data
df_1 = pa.Table.from_pylist([
    {"name": "one", "my_list": ["test"]},
    {"name": "another", "my_list": ["test"]},
], schema=pyarrow_schema)
catalog.load_table("test.table").append(df_1)
catalog.load_table("test.table").scan().to_arrow()

# append more data
df_2 = pa.Table.from_pylist([
    {"name": "one"},
    {"name": "another"},
], schema=pyarrow_schema)
catalog.load_table("test.table").append(df_2)
catalog.load_table("test.table").scan().to_arrow()

ValueError: Mismatch in fields:
┏━━━━┳━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━┳━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━┓
┃    ┃ Table field                       ┃ Dataframe field                   ┃
┡━━━━╇━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━╇━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━┩
│ ✅ │ 1: name: optional string          │ 1: name: optional string          │
│ ✅ │ 2: my_list: optional list<string> │ Missing                           │
│ ❌ │ 3: element: optional string       │ 3: my_list: optional list<string> │
└────┴───────────────────────────────────┴───────────────────────────────────┘

@kevinjqliu
Copy link
Contributor

kevinjqliu commented Mar 30, 2025

there's a bug somewhere in the schema translation between pyarrow schema and iceberg schema.

Note the iceberg table schema, has an extra field_id=2

Output:

>>> schema
Schema(NestedField(field_id=1, name='name', field_type=StringType(), required=False), NestedField(field_id=3, name='my_list', field_type=ListType(type='list', element_id=45, element_type=StringType(), element_required=False), required=False), schema_id=0, identifier_field_ids=[])

>>> pyarrow_schema
name: large_string
  -- field metadata --
  PARQUET:field_id: '1'
my_list: large_list<element: large_string>
  child 0, element: large_string
    -- field metadata --
    PARQUET:field_id: '45'
  -- field metadata --
  PARQUET:field_id: '3'

>>> catalog.load_table("test.table").schema()
Schema(NestedField(field_id=1, name='name', field_type=StringType(), required=False), NestedField(field_id=2, name='my_list', field_type=ListType(type='list', element_id=3, element_type=StringType(), element_required=False), required=False), schema_id=0, identifier_field_ids=[])

>>> from pyiceberg.io.pyarrow import pyarrow_to_schema
>>> pyarrow_to_schema(pyarrow_schema, name_mapping=schema.name_mapping)
Schema(NestedField(field_id=1, name='name', field_type=StringType(), required=False), NestedField(field_id=3, name='my_list', field_type=ListType(type='list', element_id=45, element_type=StringType(), element_required=False), required=False), schema_id=0, identifier_field_ids=[])

Reproduce:

# schema difference
from pyiceberg.catalog import load_catalog
import pyarrow as pa
from pyiceberg.schema import Schema
from pyiceberg.types import NestedField, StringType, ListType
from pyiceberg.io.pyarrow import schema_to_pyarrow

catalog = load_catalog(**dict(type="in-memory"))

schema = Schema(
    NestedField(field_id=1, name="name", field_type=StringType(), required=False),
    NestedField(
        field_id=3,
        name="my_list",
        field_type=ListType(
            element_id=45, element=StringType(), element_required=False
        ),
        required=False,
    ),
)
pyarrow_schema = schema_to_pyarrow(schema)

# create table
catalog.create_namespace_if_not_exists("test")
catalog.create_table_if_not_exists("test.table", pyarrow_schema)

# iceberg schema
catalog.load_table("test.table").schema()

# pyarrow to iceberg schema
from pyiceberg.io.pyarrow import pyarrow_to_schema
pyarrow_to_schema(pyarrow_schema, name_mapping=schema.name_mapping)

@kevinjqliu
Copy link
Contributor

kevinjqliu commented Mar 31, 2025

ok heres a working version, which supplies a pyarrow schema to when creating the pyarrow table.

The difference is the parquet field-id (see PYARROW_PARQUET_FIELD_ID_KEY)

from pyiceberg.catalog import load_catalog

catalog = load_catalog(**dict(type="in-memory"))

from pyiceberg.schema import Schema
from pyiceberg.types import NestedField, StringType, ListType

schema = Schema(
    NestedField(field_id=1, name="name", field_type=StringType(), required=False),
    NestedField(
        field_id=3,
        name="my_list",
        field_type=ListType(
            element_id=45, element=StringType(), element_required=False
        ),
        required=False,
    ),
)
catalog.create_namespace_if_not_exists("test")
tbl = catalog.create_table_if_not_exists("test.table", schema)

import pyarrow as pa

df_1 = pa.Table.from_pylist([
    {"name": "one", "my_list": ["test"]},
    {"name": "another", "my_list": ["test"]},
], tbl.schema().as_arrow())
catalog.load_table("test.table").append(df_1)
catalog.load_table("test.table").scan().to_arrow()

import pyarrow as pa

df_2 = pa.Table.from_pylist([
    {"name": "one"},
    {"name": "another"},
], tbl.schema().as_arrow())
catalog.load_table("test.table").append(df_2)
catalog.load_table("test.table").scan().to_arrow()

@kevinjqliu
Copy link
Contributor

kevinjqliu commented Mar 31, 2025

alternatively, this can be resolved by setting the table's name-mapping. This is interesting because name-mapping is not set by default.

But Spark ensure that it is set

from pyiceberg.catalog import load_catalog

catalog = load_catalog(**dict(type="in-memory"))

from pyiceberg.schema import Schema
from pyiceberg.types import NestedField, StringType, ListType
from pyiceberg.table import TableProperties

schema = Schema(
    NestedField(field_id=1, name="name", field_type=StringType(), required=False),
    NestedField(
        field_id=3,
        name="my_list",
        field_type=ListType(
            element_id=45, element=StringType(), element_required=False
        ),
        required=False,
    ),
)
catalog.create_namespace_if_not_exists("test")
tbl = catalog.create_table_if_not_exists("test.table", schema)
print(f"name-mapping: {tbl.metadata.name_mapping()}")

if tbl.metadata.name_mapping() is None:
    with tbl.transaction() as txn:
        txn.set_properties(
            **{TableProperties.DEFAULT_NAME_MAPPING: tbl.metadata.schema().name_mapping.model_dump_json()}
        )

print(f"name-mapping: {tbl.metadata.name_mapping()}")

import pyarrow as pa
df_1 = pa.Table.from_pylist([
    {"name": "one", "my_list": ["test"]},
    {"name": "another", "my_list": ["test"]},
])
catalog.load_table("test.table").append(df_1)
catalog.load_table("test.table").scan().to_arrow()

import pyarrow as pa
df_2 = pa.Table.from_pylist([
    {"name": "one"},
    {"name": "another"},
])
catalog.load_table("test.table").append(df_2)
catalog.load_table("test.table").scan().to_arrow()

Fokko added a commit to Fokko/iceberg-python that referenced this issue Mar 31, 2025
Fixes apache#1798
@Fokko Fokko closed this as completed in 3d08776 Apr 1, 2025
Fokko added a commit that referenced this issue Apr 17, 2025
Fixes #1798

<!--
Thanks for opening a pull request!
-->

<!-- In the case this PR will resolve an issue, please replace
${GITHUB_ISSUE_ID} below with the actual Github issue id. -->
<!-- Closes #${GITHUB_ISSUE_ID} -->

# Rationale for this change

# Are these changes tested?

# Are there any user-facing changes?

<!-- In the case of user-facing changes, please add the changelog label.
-->
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 a pull request may close this issue.

2 participants