Skip to content

Commit dbbc7a5

Browse files
committed
feat: add example for raw PyCapsule registration and dataset fallback
1 parent ac250a5 commit dbbc7a5

1 file changed

Lines changed: 46 additions & 0 deletions

File tree

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
"""Demonstrate how passing a raw PyCapsule triggers the dataset fallback.
2+
3+
Run with ``python examples/raw_capsule_registration_failure.py``.
4+
5+
This mirrors integrations that construct a :class:`datafusion.Table` directly
6+
from the FFI PyCapsule returned by ``__datafusion_table_provider__``. After the
7+
refactor that routes all inputs through ``RawTable`` the capsule is no longer
8+
recognized, so the constructor falls back to the PyArrow dataset path and raises
9+
``ValueError: dataset argument must be a pyarrow.dataset.Dataset object``.
10+
"""
11+
12+
from __future__ import annotations
13+
14+
import ctypes
15+
16+
from datafusion import SessionContext, Table
17+
18+
19+
def make_table_provider_capsule() -> object:
20+
"""Create a dummy PyCapsule with the expected table provider name."""
21+
22+
pycapsule_new = ctypes.pythonapi.PyCapsule_New
23+
pycapsule_new.restype = ctypes.py_object
24+
pycapsule_new.argtypes = [ctypes.c_void_p, ctypes.c_char_p, ctypes.c_void_p]
25+
return pycapsule_new(None, b"datafusion_table_provider", None)
26+
27+
28+
def main() -> None:
29+
"""Attempt to use the capsule the same way existing callers do."""
30+
31+
ctx = SessionContext()
32+
capsule = make_table_provider_capsule()
33+
34+
try:
35+
Table(capsule)
36+
except ValueError as err:
37+
print("Constructing Table(capsule) failed:", err)
38+
39+
try:
40+
ctx.register_table("capsule", capsule)
41+
except ValueError as err:
42+
print("Registering capsule with SessionContext failed:", err)
43+
44+
45+
if __name__ == "__main__":
46+
main()

0 commit comments

Comments
 (0)