Skip to content

Commit 28bb49c

Browse files
committed
add functions to convert mlarrow <-> pyarrow
1 parent 3ebcf4e commit 28bb49c

File tree

3 files changed

+42
-0
lines changed

3 files changed

+42
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
from pyarrow.cffi import ffi
2+
3+
class FFIWrapper:
4+
def __init__(self, cdata):
5+
self.cdata = cdata
6+
7+
def getAddress(self):
8+
address = int(ffi.cast("uintptr_t", self.cdata))
9+
return address
10+
11+
cArray = ffi.new("struct ArrowArray*")
12+
cSchema = ffi.new("struct ArrowSchema*")
13+
14+
cArrayWrapper = FFIWrapper(cArray)
15+
cSchemaWrapper = FFIWrapper(cSchema)
16+
17+
18+
19+
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
function pyarrowArray = mlarrow2pyarrow(mlarrowArray)
2+
[cArrayWrapper, cSchemaWrapper] = pyrunfile(fullfile(pwd, "+internal/FFIWrapper.py"), ["cArrayWrapper" "cSchemaWrapper"]);
3+
4+
cArrayAddress = uint64(cArrayWrapper.getAddress());
5+
cSchemaAdress = uint64(cSchemaWrapper.getAddress());
6+
7+
mlarrowArray.exportToC(cArrayAddress, cSchemaAdress);
8+
9+
dummyArray = py.pyarrow.array([1 2]);
10+
importFunc = py.getattr(dummyArray, "_import_from_c");
11+
pyarrowArray = importFunc(cArrayAddress, cSchemaAdress);
12+
end
13+
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
function mlarrowArray = pyarrow2mlarrow(pyarrowArray)
2+
cArray = arrow.c.ArrayCStruct;
3+
cSchema = arrow.c.SchemaCStruct;
4+
5+
exportFunc = py.getattr(pyarrowArray, "_export_to_c");
6+
exportFunc(cArray.Address, cSchema.Address);
7+
8+
mlarrowArray = arrow.array.Array.importFromC(cArray, cSchema);
9+
10+
end

0 commit comments

Comments
 (0)