Skip to content

Set field-id when needed #1867

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

Merged
merged 2 commits into from
Apr 1, 2025
Merged
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
2 changes: 1 addition & 1 deletion pyiceberg/io/pyarrow.py
Original file line number Diff line number Diff line change
Expand Up @@ -1777,7 +1777,7 @@ def struct(
field_arrays.append(array)
fields.append(self._construct_field(field, array.type))
elif field.optional:
arrow_type = schema_to_pyarrow(field.field_type, include_field_ids=False)
arrow_type = schema_to_pyarrow(field.field_type, include_field_ids=self._include_field_ids)
Copy link
Contributor

@kevinjqliu kevinjqliu Mar 31, 2025

Choose a reason for hiding this comment

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

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Yes, we missed this in a review 🤦 Field-IDs are superior over name-mapping, for example: dropping a field, and then adding a new field with the same name is not supported by name-mapping because it re-uses the name. In the case of field-IDs, a new ID is assigned and it will look like a new column 👍

Copy link
Contributor

Choose a reason for hiding this comment

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

I found 3 other places where include_field_ids=False

  • in to_table, this is fine since we're just materializing the table from record batches
  • in _to_requested_schema, the 2 places where _to_requested_schema is called sets include_field_ids=True (1, 2)
  • in ArrowProjectionVisitor , but this is only called here get uses the include_field_ids from _to_requested_schema, which sets include_field_ids=True

field_arrays.append(pa.nulls(len(struct_array), type=arrow_type))
fields.append(self._construct_field(field, arrow_type))
else:
Expand Down
36 changes: 36 additions & 0 deletions tests/integration/test_writes/test_writes.py
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@
DateType,
DoubleType,
IntegerType,
ListType,
LongType,
NestedField,
StringType,
Expand Down Expand Up @@ -1647,3 +1648,38 @@ def test_abort_table_transaction_on_exception(

# Validate the transaction is aborted and no partial update is applied
assert len(tbl.scan().to_pandas()) == table_size # type: ignore


@pytest.mark.integration
def test_write_optional_list(session_catalog: Catalog) -> None:
identifier = "default.test_write_optional_list"
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,
),
)
session_catalog.create_table_if_not_exists(identifier, schema)

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

assert len(session_catalog.load_table(identifier).scan().to_arrow()) == 2

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

assert len(session_catalog.load_table(identifier).scan().to_arrow()) == 4