Skip to content

Apache DataFusion SQL Query Engine

ARROW-7947: [Rust] [Flight] [DataFusion] Implement get_schema example

Andy Grove0db9d8ab4144
diff --git a/rust/datafusion/examples/flight-client.rs b/rust/datafusion/examples/flight-client.rs
index 97db793462..128d6f56c1 100644
--- a/rust/datafusion/examples/flight-client.rs
+++ b/rust/datafusion/examples/flight-client.rs
@@ -21,13 +21,30 @@ use std::sync::Arc;
use arrow::array::Int32Array;
use arrow::datatypes::Schema;
use arrow::flight::flight_data_to_batch;
+use flight::flight_descriptor;
use flight::flight_service_client::FlightServiceClient;
-use flight::Ticket;
+use flight::{FlightDescriptor, Ticket};
#[tokio::main]
async fn main() -> Result<(), Box<dyn std::error::Error>> {
+ let testdata =
+ std::env::var("PARQUET_TEST_DATA").expect("PARQUET_TEST_DATA not defined");
+
+ // Create Flight client
let mut client = FlightServiceClient::connect("http://localhost:50051").await?;
+ // Call get_schema to get the schema of a Parquet file
+ let request = tonic::Request::new(FlightDescriptor {
+ r#type: flight_descriptor::DescriptorType::Path as i32,
+ cmd: vec![],
+ path: vec![format!("{}/alltypes_plain.parquet", testdata)],
+ });
+
+ let schema_result = client.get_schema(request).await?.into_inner();
+ let schema = Schema::try_from(&schema_result)?;
+ println!("Schema: {:?}", schema);
+
+ // Call do_get to execute a SQL query and receive results
let request = tonic::Request::new(Ticket {
ticket: "SELECT id FROM alltypes_plain".into(),
});
diff --git a/rust/datafusion/examples/flight-server.rs b/rust/datafusion/examples/flight-server.rs
index e6cc22ac50..30afa5d537 100644
--- a/rust/datafusion/examples/flight-server.rs
+++ b/rust/datafusion/examples/flight-server.rs
@@ -21,6 +21,8 @@ use futures::Stream;
use tonic::transport::Server;
use tonic::{Request, Response, Status, Streaming};
+use datafusion::datasource::parquet::ParquetTable;
+use datafusion::datasource::TableProvider;
use datafusion::execution::context::ExecutionContext;
use flight::{
@@ -32,6 +34,10 @@ use flight::{
#[derive(Clone)]
pub struct FlightServiceImpl {}
+/**
+ * Example Flight Server wrapping DataFusion that supports looking up schema information for
+ * Parquet files and executing SQL queries against Parquet files.
+ */
#[tonic::async_trait]
impl FlightService for FlightServiceImpl {
type HandshakeStream = Pin<
@@ -49,6 +55,17 @@ impl FlightService for FlightServiceImpl {
type ListActionsStream =
Pin<Box<dyn Stream<Item = Result<ActionType, Status>> + Send + Sync + 'static>>;
+ async fn get_schema(
+ &self,
+ request: Request<FlightDescriptor>,
+ ) -> Result<Response<SchemaResult>, Status> {
+ let request = request.into_inner();
+
+ let table = ParquetTable::try_new(&request.path[0]).unwrap();
+
+ Ok(Response::new(SchemaResult::from(table.schema().as_ref())))
+ }
+
async fn do_get(
&self,
request: Request<Ticket>,
@@ -126,13 +143,6 @@ impl FlightService for FlightServiceImpl {
Err(Status::unimplemented("Not yet implemented"))
}
- async fn get_schema(
- &self,
- _request: Request<FlightDescriptor>,
- ) -> Result<Response<SchemaResult>, Status> {
- Err(Status::unimplemented("Not yet implemented"))
- }
-
async fn do_put(
&self,
_request: Request<Streaming<FlightData>>,