Bind Flight ListFlights server call to Python

This commit is contained in:
David Li authored and Antoine Pitrou committed 2019-02-26 19:24:40 +01:00
1 parent 4a5cbaf521
commit c09b138de4
3 files changed
+43

No files matched your search

+14
View File
@@ -33,6 +33,20 @@ class FlightServer(pyarrow.flight.FlightServerBase):
return (descriptor.descriptor_type.value, descriptor.command,
tuple(descriptor.path or tuple()))
def list_flights(self, criteria):
for key, table in self.flights.items():
if key[1] is not None:
descriptor = pyarrow.flight.FlightDescriptor.for_command(key[1])
else:
descriptor = pyarrow.flight.FlightDescriptor.for_path(*key[2])
endpoints = [
pyarrow.flight.FlightEndpoint(repr(key), [('localhost', 5005)]),
]
yield pyarrow.flight.FlightInfo(table.schema,
descriptor, endpoints,
table.num_rows, 0)
def get_flight_info(self, descriptor):
key = FlightServer.descriptor_to_key(descriptor)
if key in self.flights:
+18
View File
@@ -433,6 +433,20 @@ cdef class RecordBatchStream(FlightDataStream):
raise TypeError("Expected RecordBatchReader or Table, but got: {}".format(type(data_source)))
cdef void _list_flights(void* self, const CCriteria* c_criteria,
unique_ptr[CFlightListing]* listing):
"""Callback for implementing ListFlights in Python."""
cdef:
vector[CFlightInfo] flights
result = (<object> self).list_flights(c_criteria.expression)
for info in result:
if not isinstance(info, FlightInfo):
raise TypeError("FlightServerBase.list_flights must return "
"FlightInfo instances, but got {}".format(type(info)))
flights.push_back(CFlightInfo(deref((<FlightInfo> info).info.get())))
listing.reset(new CSimpleFlightListing(flights))
cdef void _get_flight_info(void* self, CFlightDescriptor c_descriptor,
unique_ptr[CFlightInfo]* info):
"""Callback for implementing Flight servers in Python."""
@@ -480,6 +494,7 @@ cdef class FlightServerBase:
cdef:
PyFlightServerVtable vtable = PyFlightServerVtable()
int c_port = port
vtable.list_flights = &_list_flights
vtable.get_flight_info = &_get_flight_info
vtable.do_put = &_do_put
vtable.do_get = &_do_get
@@ -487,6 +502,9 @@ cdef class FlightServerBase:
with nogil:
self.server.get().Run(c_port)
def list_flights(self, criteria):
raise NotImplementedError
def get_flight_info(self, descriptor):
raise NotImplementedError
@@ -58,6 +58,10 @@ cdef extern from "arrow/flight/api.h" namespace "arrow" nogil:
CTicket()
c_string ticket
cdef cppclass CCriteria" arrow::flight::Criteria":
CCriteria()
c_string expression
cdef cppclass CLocation" arrow::flight::Location":
CLocation()
@@ -71,6 +75,7 @@ cdef extern from "arrow/flight/api.h" namespace "arrow" nogil:
vector[CLocation] locations
cdef cppclass CFlightInfo" arrow::flight::FlightInfo":
CFlightInfo(CFlightInfo info)
uint64_t total_records()
uint64_t total_bytes()
CStatus GetSchema(shared_ptr[CSchema]* out)
@@ -80,6 +85,9 @@ cdef extern from "arrow/flight/api.h" namespace "arrow" nogil:
cdef cppclass CFlightListing" arrow::flight::FlightListing":
CStatus Next(unique_ptr[CFlightInfo]* info)
cdef cppclass CSimpleFlightListing" arrow::flight::SimpleFlightListing":
CSimpleFlightListing(vector[CFlightInfo]&& info)
cdef cppclass CFlightMessageReader \
" arrow::flight::FlightMessageReader"(CRecordBatchReader):
CFlightDescriptor& descriptor()
@@ -112,6 +120,8 @@ cdef extern from "arrow/flight/api.h" namespace "arrow" nogil:
# Callbacks for implementing Flight servers
# Use typedef to emulate syntax for std::function<void(...)>
ctypedef void cb_list_flights(object, const CCriteria*,
unique_ptr[CFlightListing]*)
ctypedef void cb_get_flight_info(object, const CFlightDescriptor&,
unique_ptr[CFlightInfo]*)
ctypedef void cb_do_put(object, unique_ptr[CFlightMessageReader])
@@ -121,6 +131,7 @@ ctypedef void cb_do_get(object, const CTicket&,
cdef extern from "arrow/python/flight.h" namespace "arrow::py::flight" nogil:
cdef cppclass PyFlightServerVtable:
PyFlightServerVtable()
function[cb_list_flights] list_flights
function[cb_get_flight_info] get_flight_info
function[cb_do_put] do_put
function[cb_do_get] do_get