Skip to content

Commit 4a3f17d

Browse files
committed
feat: add test for Arrow C stream schema selection in DataFrame
1 parent 03e530c commit 4a3f17d

File tree

1 file changed

+40
-0
lines changed

1 file changed

+40
-0
lines changed

python/tests/test_dataframe.py

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,8 @@
4646
from datafusion.expr import Window
4747
from pyarrow.csv import write_csv
4848

49+
pa_cffi = pytest.importorskip("pyarrow.cffi")
50+
4951
MB = 1024 * 1024
5052

5153

@@ -1637,6 +1639,44 @@ def test_arrow_c_stream_reader(df):
16371639
assert table.equals(expected)
16381640

16391641

1642+
def test_arrow_c_stream_schema_selection(fail_collect):
1643+
ctx = SessionContext()
1644+
1645+
batch = pa.RecordBatch.from_arrays(
1646+
[
1647+
pa.array([1, 2]),
1648+
pa.array([3, 4]),
1649+
pa.array([5, 6]),
1650+
],
1651+
names=["a", "b", "c"],
1652+
)
1653+
df = ctx.create_dataframe([[batch]])
1654+
1655+
requested_schema = pa.schema([("c", pa.int64()), ("a", pa.int64())])
1656+
1657+
c_schema = pa_cffi.ffi.new("struct ArrowSchema*")
1658+
address = int(pa_cffi.ffi.cast("uintptr_t", c_schema))
1659+
requested_schema._export_to_c(address)
1660+
capsule_new = ctypes.pythonapi.PyCapsule_New
1661+
capsule_new.restype = ctypes.py_object
1662+
capsule_new.argtypes = [ctypes.c_void_p, ctypes.c_char_p, ctypes.c_void_p]
1663+
schema_capsule = capsule_new(ctypes.c_void_p(address), b"arrow_schema", None)
1664+
1665+
reader = pa.RecordBatchReader._import_from_c_capsule(
1666+
df.__arrow_c_stream__(schema_capsule)
1667+
)
1668+
1669+
assert reader.schema == requested_schema
1670+
1671+
batches = list(reader)
1672+
1673+
assert len(batches) == 1
1674+
expected_batch = pa.record_batch(
1675+
[pa.array([5, 6]), pa.array([1, 2])], names=["c", "a"]
1676+
)
1677+
assert batches[0].equals(expected_batch)
1678+
1679+
16401680
def test_to_pylist(df):
16411681
# Convert datafusion dataframe to Python list
16421682
pylist = df.to_pylist()

0 commit comments

Comments
 (0)