Skip to content

Commit

Permalink
GH-38575: [Python] Include metadata when creating pa.schema from PyCa…
Browse files Browse the repository at this point in the history
…psule (#41538)

### Rationale for this change

Fixes the dropped `pa.schema` metadata reported in #38575, which was introduced in #37797.

### What changes are included in this PR?

Passes through the `metadata` to the short-circuited `Schema` created with `_import_from_c_capsule`.

### Are these changes tested?

Yes - added `metadata` to the existing test.

### Are there any user-facing changes?

I'm not sure this quite rises to the `(b) a bug that caused incorrect or invalid data to be produced,` condition, but I added that note to be safe since the resulting schema is "incorrect" (and broke some round-trip tests on my end after a pyarrow update):

**This PR contains a "Critical Fix".**

* GitHub Issue: #38575

Lead-authored-by: Jacob Hayes <jacob.r.hayes@gmail.com>
Co-authored-by: Joris Van den Bossche <jorisvandenbossche@gmail.com>
Signed-off-by: Joris Van den Bossche <jorisvandenbossche@gmail.com>
  • Loading branch information
JacobHayes and jorisvandenbossche committed May 17, 2024
1 parent dc973c2 commit 6a9e2d5
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 2 deletions.
5 changes: 4 additions & 1 deletion python/pyarrow/tests/test_types.py
Original file line number Diff line number Diff line change
Expand Up @@ -1331,10 +1331,13 @@ def __init__(self, schema):
def __arrow_c_schema__(self):
return self.schema.__arrow_c_schema__()

schema = pa.schema([pa.field("field_name", pa.int32())])
schema = pa.schema([pa.field("field_name", pa.int32())], metadata={"a": "b"})
assert schema.metadata == {b"a": b"b"}
wrapped_schema = Wrapper(schema)

assert pa.schema(wrapped_schema) == schema
assert pa.schema(wrapped_schema).metadata == {b"a": b"b"}
assert pa.schema(wrapped_schema, metadata={"a": "c"}).metadata == {b"a": b"c"}


def test_field_import_c_schema_interface():
Expand Down
5 changes: 4 additions & 1 deletion python/pyarrow/types.pxi
Original file line number Diff line number Diff line change
Expand Up @@ -5332,7 +5332,10 @@ def schema(fields, metadata=None):
if isinstance(fields, Mapping):
fields = fields.items()
elif hasattr(fields, "__arrow_c_schema__"):
return Schema._import_from_c_capsule(fields.__arrow_c_schema__())
result = Schema._import_from_c_capsule(fields.__arrow_c_schema__())
if metadata is not None:
result = result.with_metadata(metadata)
return result

for item in fields:
if isinstance(item, tuple):
Expand Down

0 comments on commit 6a9e2d5

Please sign in to comment.