Skip to content
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

Implement column projection #1443

Open
wants to merge 7 commits into
base: main
Choose a base branch
from

Conversation

gabeiglio
Copy link

@gabeiglio gabeiglio commented Dec 18, 2024

This is a fix for issue #1401. In which table scans needed to infer partition column by following the column projection rules

Fixes #1401

@Fokko Fokko self-requested a review December 18, 2024 20:38
@gabeiglio gabeiglio marked this pull request as ready for review December 19, 2024 15:12
Copy link
Contributor

@kevinjqliu kevinjqliu left a comment

Choose a reason for hiding this comment

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

Added a few comments, please take a look! The PR looks great already. Thanks for working on this!

pyiceberg/io/pyarrow.py Outdated Show resolved Hide resolved
tests/io/test_pyarrow.py Outdated Show resolved Hide resolved
pyiceberg/io/pyarrow.py Show resolved Hide resolved
pyiceberg/io/pyarrow.py Outdated Show resolved Hide resolved
pyiceberg/io/pyarrow.py Outdated Show resolved Hide resolved
@kevinjqliu kevinjqliu self-requested a review December 23, 2024 19:04
…tion logic to helper method, changed test to use high-level table scan
file: DataFile,
projected_schema: Schema,
projected_field_ids: Set[int],
file_project_schema: Schema,
Copy link
Contributor

Choose a reason for hiding this comment

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

Since we only need the IDs, I'd rather just pass in those for clarity:

Suggested change
file_project_schema: Schema,
file_project_schema: Set[int],

if partition_spec is not None:
for partition_field in partition_spec.fields_by_source_id(field_id):
if isinstance(partition_field.transform, IdentityTransform) and partition_field.name in file.partition.__dict__:
projected_missing_fields[partition_field.name] = file.partition.__dict__[partition_field.name]
Copy link
Contributor

Choose a reason for hiding this comment

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

This is my mistake, the lookup should never be done by name, but by field-id. I've added the lookup by name to the record, but this should not be used outside of tests.

Instead, we want to create a lookup table from the field:

def build_position_accessors(schema_or_type: Union[Schema, IcebergType]) -> Dict[int, Accessor]:
"""Generate an index of field IDs to schema position accessors.
Args:
schema_or_type (Union[Schema, IcebergType]): A schema or type to index.
Returns:
Dict[int, Accessor]: An index of field IDs to accessors.
"""
return visit(schema_or_type, _BuildPositionAccessors())

It should be used something like:

accessors = build_position_accessors(table_schema)
projected_missing_fields[partition_field.name] = record[accessors[partition_field.field_id]]

Copy link
Author

Choose a reason for hiding this comment

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

Thanks for the input! thats right, the partition field shouldn't be accessed with the name. But, if I'm understanding correctly, the partition record position in the manifest file is not the same as the position of the field in the schema.

For example, lets say I have this schema

schema {
  1 field1: string
  2 partition_field: int
}

And a DataFile like this:

DataFile {
  ...
  partition: Record(partittion_field: 1)
  ...
}

Running build_position_accessor with the schema defined earlier will result in:

{
 1: Accessor(position=0,inner=None),
 2: Accessor(position=1,inner=None),
}

Then, doing accessor[partition_field.id] will result in Accessor(position=1,inner=None) but the partition record in the manifest would be in position 0.

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.

API table.scan does not conform to Iceberg spec for identity partition columns
3 participants