tustvold commented on a change in pull request #1386: URL: https://github.com/apache/arrow-rs/pull/1386#discussion_r821937337
########## File path: arrow-flight/src/sql/server.rs ########## @@ -0,0 +1,585 @@ +// Licensed to the Apache Software Foundation (ASF) under one +// or more contributor license agreements. See the NOTICE file +// distributed with this work for additional information +// regarding copyright ownership. The ASF licenses this file +// to you under the Apache License, Version 2.0 (the +// "License"); you may not use this file except in compliance +// with the License. You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, +// software distributed under the License is distributed on an +// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY +// KIND, either express or implied. See the License for the +// specific language governing permissions and limitations +// under the License. + +use std::pin::Pin; + +use futures::Stream; +use prost::Message; +use tonic::{Request, Response, Status, Streaming}; + +use super::{ + super::{ + flight_service_server::FlightService, Action, ActionType, Criteria, Empty, + FlightData, FlightDescriptor, FlightInfo, HandshakeRequest, HandshakeResponse, + PutResult, SchemaResult, Ticket, + }, + ActionClosePreparedStatementRequest, ActionCreatePreparedStatementRequest, + CommandGetCatalogs, CommandGetCrossReference, CommandGetDbSchemas, + CommandGetExportedKeys, CommandGetImportedKeys, CommandGetPrimaryKeys, + CommandGetSqlInfo, CommandGetTableTypes, CommandGetTables, + CommandPreparedStatementQuery, CommandPreparedStatementUpdate, CommandStatementQuery, + CommandStatementUpdate, ProstAnyExt, SqlInfo, TicketStatementQuery, +}; + +static CREATE_PREPARED_STATEMENT: &str = "CreatePreparedStatement"; +static CLOSE_PREPARED_STATEMENT: &str = "ClosePreparedStatement"; + +/// Implements FlightSqlService to handle the flight sql protocol +#[tonic::async_trait] +pub trait FlightSqlService: + std::marker::Sync + std::marker::Send + std::marker::Sized + FlightService + 'static +{ + /// When impl FlightSqlService, you can always set FlightService to Self + type FlightService: FlightService; + + /// Get a FlightInfo for executing a SQL query. + async fn get_flight_info_statement( + &self, + query: CommandStatementQuery, + request: FlightDescriptor, + ) -> Result<Response<FlightInfo>, Status>; + + /// Get a FlightInfo for executing an already created prepared statement. + async fn get_flight_info_prepared_statement( + &self, + query: CommandPreparedStatementQuery, + request: FlightDescriptor, + ) -> Result<Response<FlightInfo>, Status>; + + /// Get a FlightInfo for listing catalogs. + async fn get_flight_info_catalogs( + &self, + query: CommandGetCatalogs, + request: FlightDescriptor, + ) -> Result<Response<FlightInfo>, Status>; + + /// Get a FlightInfo for listing schemas. + async fn get_flight_info_schemas( + &self, + query: CommandGetDbSchemas, + request: FlightDescriptor, + ) -> Result<Response<FlightInfo>, Status>; + + /// Get a FlightInfo for listing tables. + async fn get_flight_info_tables( + &self, + query: CommandGetTables, + request: FlightDescriptor, + ) -> Result<Response<FlightInfo>, Status>; + + /// Get a FlightInfo to extract information about the table types. + async fn get_flight_info_table_types( + &self, + query: CommandGetTableTypes, + request: FlightDescriptor, + ) -> Result<Response<FlightInfo>, Status>; + + /// Get a FlightInfo for retrieving other information (See SqlInfo). + async fn get_flight_info_sql_info( + &self, + query: CommandGetSqlInfo, + request: FlightDescriptor, + ) -> Result<Response<FlightInfo>, Status>; + + /// Get a FlightInfo to extract information about primary and foreign keys. + async fn get_flight_info_primary_keys( + &self, + query: CommandGetPrimaryKeys, + request: FlightDescriptor, + ) -> Result<Response<FlightInfo>, Status>; + + /// Get a FlightInfo to extract information about exported keys. + async fn get_flight_info_exported_keys( + &self, + query: CommandGetExportedKeys, + request: FlightDescriptor, + ) -> Result<Response<FlightInfo>, Status>; + + /// Get a FlightInfo to extract information about imported keys. + async fn get_flight_info_imported_keys( + &self, + query: CommandGetImportedKeys, + request: FlightDescriptor, + ) -> Result<Response<FlightInfo>, Status>; + + /// Get a FlightInfo to extract information about cross reference. + async fn get_flight_info_cross_reference( + &self, + query: CommandGetCrossReference, + request: FlightDescriptor, + ) -> Result<Response<FlightInfo>, Status>; + + // do_get + + /// Get a FlightInfo for executing an already created prepared statement. + async fn do_get_statement( + &self, + ticket: TicketStatementQuery, + ) -> Result<Response<<Self as FlightService>::DoGetStream>, Status>; + + /// Get a FlightDataStream containing the prepared statement query results. + async fn do_get_prepared_statement( + &self, + query: CommandPreparedStatementQuery, + ) -> Result<Response<<Self as FlightService>::DoGetStream>, Status>; + + /// Get a FlightDataStream containing the list of catalogs. + async fn do_get_catalogs( + &self, + query: CommandGetCatalogs, + ) -> Result<Response<<Self as FlightService>::DoGetStream>, Status>; + + /// Get a FlightDataStream containing the list of schemas. + async fn do_get_schemas( + &self, + query: CommandGetDbSchemas, + ) -> Result<Response<<Self as FlightService>::DoGetStream>, Status>; + + /// Get a FlightDataStream containing the list of tables. + async fn do_get_tables( + &self, + query: CommandGetTables, + ) -> Result<Response<<Self as FlightService>::DoGetStream>, Status>; + + /// Get a FlightDataStream containing the data related to the table types. + async fn do_get_table_types( + &self, + query: CommandGetTableTypes, + ) -> Result<Response<<Self as FlightService>::DoGetStream>, Status>; + + /// Get a FlightDataStream containing the list of SqlInfo results. + async fn do_get_sql_info( + &self, + query: CommandGetSqlInfo, + ) -> Result<Response<<Self as FlightService>::DoGetStream>, Status>; + + /// Get a FlightDataStream containing the data related to the primary and foreign keys. + async fn do_get_primary_keys( + &self, + query: CommandGetPrimaryKeys, + ) -> Result<Response<<Self as FlightService>::DoGetStream>, Status>; + + /// Get a FlightDataStream containing the data related to the exported keys. + async fn do_get_exported_keys( + &self, + query: CommandGetExportedKeys, + ) -> Result<Response<<Self as FlightService>::DoGetStream>, Status>; + + /// Get a FlightDataStream containing the data related to the imported keys. + async fn do_get_imported_keys( + &self, + query: CommandGetImportedKeys, + ) -> Result<Response<<Self as FlightService>::DoGetStream>, Status>; + + /// Get a FlightDataStream containing the data related to the cross reference. + async fn do_get_cross_reference( + &self, + query: CommandGetCrossReference, + ) -> Result<Response<<Self as FlightService>::DoGetStream>, Status>; + + // do_put + + /// Execute an update SQL statement. + async fn do_put_statement_update( + &self, + ticket: CommandStatementUpdate, + ) -> Result<Response<<Self as FlightService>::DoPutStream>, Status>; + + /// Bind parameters to given prepared statement. + async fn do_put_prepared_statement_query( + &self, + query: CommandPreparedStatementQuery, + ) -> Result<Response<<Self as FlightService>::DoPutStream>, Status>; + + /// Execute an update SQL prepared statement. + async fn do_put_prepared_statement_update( + &self, + query: CommandPreparedStatementUpdate, + ) -> Result<Response<<Self as FlightService>::DoPutStream>, Status>; + + // do_action + + /// Create a prepared statement from given SQL statement. + async fn do_action_create_prepared_statement( + &self, + query: ActionCreatePreparedStatementRequest, + ) -> Result<Response<<Self as FlightService>::DoActionStream>, Status>; + + /// Close a prepared statement. + async fn do_action_close_prepared_statement( + &self, + query: ActionClosePreparedStatementRequest, + ) -> Result<Response<<Self as FlightService>::DoActionStream>, Status>; + + /// Register a new SqlInfo result, making it available when calling GetSqlInfo. + async fn register_sql_info(&self, id: i32, result: &SqlInfo); +} + +/// Implements the lower level interface to handle FlightSQL +#[tonic::async_trait] +impl<T: 'static> FlightService for T +where + T: FlightSqlService + std::marker::Sync + std::marker::Send, Review comment: This blanket impl really confuses me, it implements `FlightService` for all T which implement `FlightSqlService` which by definition already implement `FlightService`... ########## File path: arrow-flight/src/sql/server.rs ########## @@ -0,0 +1,581 @@ +// Licensed to the Apache Software Foundation (ASF) under one +// or more contributor license agreements. See the NOTICE file +// distributed with this work for additional information +// regarding copyright ownership. The ASF licenses this file +// to you under the Apache License, Version 2.0 (the +// "License"); you may not use this file except in compliance +// with the License. You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, +// software distributed under the License is distributed on an +// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY +// KIND, either express or implied. See the License for the +// specific language governing permissions and limitations +// under the License. + +use std::pin::Pin; + +use futures::Stream; +use prost::Message; +use tonic::{Request, Response, Status, Streaming}; + +use super::{ + super::{ + flight_service_server::FlightService, Action, ActionType, Criteria, Empty, + FlightData, FlightDescriptor, FlightInfo, HandshakeRequest, HandshakeResponse, + PutResult, SchemaResult, Ticket, + }, + ActionClosePreparedStatementRequest, ActionCreatePreparedStatementRequest, + CommandGetCatalogs, CommandGetCrossReference, CommandGetDbSchemas, + CommandGetExportedKeys, CommandGetImportedKeys, CommandGetPrimaryKeys, + CommandGetSqlInfo, CommandGetTableTypes, CommandGetTables, + CommandPreparedStatementQuery, CommandPreparedStatementUpdate, CommandStatementQuery, + CommandStatementUpdate, ProstAnyExt, SqlInfo, TicketStatementQuery, +}; + +static CREATE_PREPARED_STATEMENT: &str = "CreatePreparedStatement"; +static CLOSE_PREPARED_STATEMENT: &str = "ClosePreparedStatement"; + +/// Implements FlightSqlService to handle the flight sql protocol +#[tonic::async_trait] +pub trait FlightSqlService: + std::marker::Sync + std::marker::Send + std::marker::Sized + FlightService + 'static +{ + /// When impl FlightSqlService, you can always set FlightService to Self + type FlightService: FlightService; + + /// Get a FlightInfo for executing a SQL query. + async fn get_flight_info_statement( + &self, + query: CommandStatementQuery, + request: FlightDescriptor, + ) -> Result<Response<FlightInfo>, Status>; + + /// Get a FlightInfo for executing an already created prepared statement. + async fn get_flight_info_prepared_statement( + &self, + query: CommandPreparedStatementQuery, + request: FlightDescriptor, + ) -> Result<Response<FlightInfo>, Status>; + + /// Get a FlightInfo for listing catalogs. + async fn get_flight_info_catalogs( + &self, + query: CommandGetCatalogs, + request: FlightDescriptor, + ) -> Result<Response<FlightInfo>, Status>; + + /// Get a FlightInfo for listing schemas. + async fn get_flight_info_schemas( + &self, + query: CommandGetDbSchemas, + request: FlightDescriptor, + ) -> Result<Response<FlightInfo>, Status>; + + /// Get a FlightInfo for listing tables. + async fn get_flight_info_tables( + &self, + query: CommandGetTables, + request: FlightDescriptor, + ) -> Result<Response<FlightInfo>, Status>; + + /// Get a FlightInfo to extract information about the table types. + async fn get_flight_info_table_types( + &self, + query: CommandGetTableTypes, + request: FlightDescriptor, + ) -> Result<Response<FlightInfo>, Status>; + + /// Get a FlightInfo for retrieving other information (See SqlInfo). + async fn get_flight_info_sql_info( + &self, + query: CommandGetSqlInfo, + request: FlightDescriptor, + ) -> Result<Response<FlightInfo>, Status>; + + /// Get a FlightInfo to extract information about primary and foreign keys. + async fn get_flight_info_primary_keys( + &self, + query: CommandGetPrimaryKeys, + request: FlightDescriptor, + ) -> Result<Response<FlightInfo>, Status>; + + /// Get a FlightInfo to extract information about exported keys. + async fn get_flight_info_exported_keys( + &self, + query: CommandGetExportedKeys, + request: FlightDescriptor, + ) -> Result<Response<FlightInfo>, Status>; + + /// Get a FlightInfo to extract information about imported keys. + async fn get_flight_info_imported_keys( + &self, + query: CommandGetImportedKeys, + request: FlightDescriptor, + ) -> Result<Response<FlightInfo>, Status>; + + /// Get a FlightInfo to extract information about cross reference. + async fn get_flight_info_cross_reference( + &self, + query: CommandGetCrossReference, + request: FlightDescriptor, + ) -> Result<Response<FlightInfo>, Status>; + + // do_get + + /// Get a FlightInfo for executing an already created prepared statement. + async fn do_get_statement( + &self, + ticket: TicketStatementQuery, + ) -> Result<Response<<Self as FlightService>::DoGetStream>, Status>; + + /// Get a FlightDataStream containing the prepared statement query results. + async fn do_get_prepared_statement( + &self, + query: CommandPreparedStatementQuery, + ) -> Result<Response<<Self as FlightService>::DoGetStream>, Status>; + + /// Get a FlightDataStream containing the list of catalogs. + async fn do_get_catalogs( + &self, + query: CommandGetCatalogs, + ) -> Result<Response<<Self as FlightService>::DoGetStream>, Status>; + + /// Get a FlightDataStream containing the list of schemas. + async fn do_get_schemas( + &self, + query: CommandGetDbSchemas, + ) -> Result<Response<<Self as FlightService>::DoGetStream>, Status>; + + /// Get a FlightDataStream containing the list of tables. + async fn do_get_tables( + &self, + query: CommandGetTables, + ) -> Result<Response<<Self as FlightService>::DoGetStream>, Status>; + + /// Get a FlightDataStream containing the data related to the table types. + async fn do_get_table_types( + &self, + query: CommandGetTableTypes, + ) -> Result<Response<<Self as FlightService>::DoGetStream>, Status>; + + /// Get a FlightDataStream containing the list of SqlInfo results. + async fn do_get_sql_info( + &self, + query: CommandGetSqlInfo, + ) -> Result<Response<<Self as FlightService>::DoGetStream>, Status>; + + /// Get a FlightDataStream containing the data related to the primary and foreign keys. + async fn do_get_primary_keys( + &self, + query: CommandGetPrimaryKeys, + ) -> Result<Response<<Self as FlightService>::DoGetStream>, Status>; + + /// Get a FlightDataStream containing the data related to the exported keys. + async fn do_get_exported_keys( + &self, + query: CommandGetExportedKeys, + ) -> Result<Response<<Self as FlightService>::DoGetStream>, Status>; + + /// Get a FlightDataStream containing the data related to the imported keys. + async fn do_get_imported_keys( + &self, + query: CommandGetImportedKeys, + ) -> Result<Response<<Self as FlightService>::DoGetStream>, Status>; + + /// Get a FlightDataStream containing the data related to the cross reference. + async fn do_get_cross_reference( + &self, + query: CommandGetCrossReference, + ) -> Result<Response<<Self as FlightService>::DoGetStream>, Status>; + + // do_put + + /// Execute an update SQL statement. + async fn do_put_statement_update( + &self, + ticket: CommandStatementUpdate, + ) -> Result<Response<<Self as FlightService>::DoPutStream>, Status>; + + /// Bind parameters to given prepared statement. + async fn do_put_prepared_statement_query( + &self, + query: CommandPreparedStatementQuery, + ) -> Result<Response<<Self as FlightService>::DoPutStream>, Status>; + + /// Execute an update SQL prepared statement. + async fn do_put_prepared_statement_update( + &self, + query: CommandPreparedStatementUpdate, + ) -> Result<Response<<Self as FlightService>::DoPutStream>, Status>; + + // do_action + + /// Create a prepared statement from given SQL statement. + async fn do_action_create_prepared_statement( + &self, + query: ActionCreatePreparedStatementRequest, + ) -> Result<Response<<Self as FlightService>::DoActionStream>, Status>; + + /// Close a prepared statement. + async fn do_action_close_prepared_statement( + &self, + query: ActionClosePreparedStatementRequest, + ) -> Result<Response<<Self as FlightService>::DoActionStream>, Status>; + + /// Register a new SqlInfo result, making it available when calling GetSqlInfo. + async fn register_sql_info(&self, id: i32, result: &SqlInfo); +} + +/// Implements the lower level interface to handle FlightSQL +#[tonic::async_trait] +impl<T: 'static> FlightService for T +where + T: FlightSqlService + std::marker::Sync + std::marker::Send, +{ + type HandshakeStream = Pin< + Box<dyn Stream<Item = Result<HandshakeResponse, Status>> + Send + Sync + 'static>, + >; + type ListFlightsStream = + Pin<Box<dyn Stream<Item = Result<FlightInfo, Status>> + Send + Sync + 'static>>; + type DoGetStream = + Pin<Box<dyn Stream<Item = Result<FlightData, Status>> + Send + Sync + 'static>>; + type DoPutStream = + Pin<Box<dyn Stream<Item = Result<PutResult, Status>> + Send + Sync + 'static>>; + type DoActionStream = Pin< + Box< + dyn Stream<Item = Result<super::super::Result, Status>> + + Send + + Sync + + 'static, + >, + >; + type ListActionsStream = + Pin<Box<dyn Stream<Item = Result<ActionType, Status>> + Send + Sync + 'static>>; + type DoExchangeStream = + Pin<Box<dyn Stream<Item = Result<FlightData, Status>> + Send + Sync + 'static>>; + + async fn handshake( + &self, + _request: Request<Streaming<HandshakeRequest>>, + ) -> Result<Response<Self::HandshakeStream>, Status> { + Err(Status::unimplemented("Not yet implemented")) + } + + async fn list_flights( + &self, + _request: Request<Criteria>, + ) -> Result<Response<Self::ListFlightsStream>, Status> { + Err(Status::unimplemented("Not yet implemented")) + } + + async fn get_flight_info( + &self, + request: Request<FlightDescriptor>, + ) -> Result<Response<FlightInfo>, Status> { + let request = request.into_inner(); + let any: prost_types::Any = prost::Message::decode(&*request.cmd) + .map_err(|_| Status::invalid_argument("Unable to parse command"))?; + + if any.is::<CommandStatementQuery>() { + return self + .get_flight_info_statement( + any.unpack::<CommandStatementQuery>().unwrap().unwrap(), + request, + ) + .await; + } + if any.is::<CommandPreparedStatementQuery>() { + return self + .get_flight_info_prepared_statement( + any.unpack::<CommandPreparedStatementQuery>() + .unwrap() + .unwrap(), + request, + ) + .await; + } + if any.is::<CommandGetCatalogs>() { + return self + .get_flight_info_catalogs( + any.unpack::<CommandGetCatalogs>().unwrap().unwrap(), + request, + ) + .await; + } + if any.is::<CommandGetDbSchemas>() { + return self + .get_flight_info_schemas( + any.unpack::<CommandGetDbSchemas>().unwrap().unwrap(), + request, + ) + .await; + } + if any.is::<CommandGetTables>() { + return self + .get_flight_info_tables( + any.unpack::<CommandGetTables>().unwrap().unwrap(), + request, + ) + .await; + } + if any.is::<CommandGetTableTypes>() { + return self + .get_flight_info_table_types( + any.unpack::<CommandGetTableTypes>().unwrap().unwrap(), + request, + ) + .await; + } + if any.is::<CommandGetSqlInfo>() { + return self + .get_flight_info_sql_info( + any.unpack::<CommandGetSqlInfo>().unwrap().unwrap(), + request, + ) + .await; + } + if any.is::<CommandGetPrimaryKeys>() { + return self + .get_flight_info_primary_keys( + any.unpack::<CommandGetPrimaryKeys>().unwrap().unwrap(), + request, + ) + .await; + } + if any.is::<CommandGetExportedKeys>() { + return self + .get_flight_info_exported_keys( + any.unpack::<CommandGetExportedKeys>().unwrap().unwrap(), + request, + ) + .await; + } + if any.is::<CommandGetImportedKeys>() { + return self + .get_flight_info_imported_keys( + any.unpack::<CommandGetImportedKeys>().unwrap().unwrap(), + request, + ) + .await; + } + if any.is::<CommandGetCrossReference>() { + return self + .get_flight_info_cross_reference( + any.unpack::<CommandGetCrossReference>().unwrap().unwrap(), + request, + ) + .await; + } + + Err(Status::unimplemented(format!( + "get_flight_info: The defined request is invalid: {:?}", + String::from_utf8(any.encode_to_vec()).unwrap() + ))) + } + + async fn get_schema( + &self, + _request: Request<FlightDescriptor>, + ) -> Result<Response<SchemaResult>, Status> { + Err(Status::unimplemented("Not yet implemented")) + } + + async fn do_get( + &self, + _request: Request<Ticket>, + ) -> Result<Response<Self::DoGetStream>, Status> { + let request = _request.into_inner(); + let any: prost_types::Any = prost::Message::decode(&*request.ticket) + .map_err(|_| Status::invalid_argument("Unable to parse command"))?; + + if any.is::<TicketStatementQuery>() { + return self + .do_get_statement(any.unpack::<TicketStatementQuery>().unwrap().unwrap()) + .await; + } + if any.is::<CommandPreparedStatementQuery>() { + return self + .do_get_prepared_statement( + any.unpack::<CommandPreparedStatementQuery>() + .unwrap() + .unwrap(), + ) + .await; + } + if any.is::<CommandGetCatalogs>() { + return self + .do_get_catalogs(any.unpack::<CommandGetCatalogs>().unwrap().unwrap()) + .await; + } + if any.is::<CommandGetDbSchemas>() { + return self + .do_get_schemas(any.unpack::<CommandGetDbSchemas>().unwrap().unwrap()) + .await; + } + if any.is::<CommandGetTables>() { + return self + .do_get_tables(any.unpack::<CommandGetTables>().unwrap().unwrap()) + .await; + } + if any.is::<CommandGetTableTypes>() { + return self + .do_get_table_types( + any.unpack::<CommandGetTableTypes>().unwrap().unwrap(), + ) + .await; + } + if any.is::<CommandGetSqlInfo>() { + return self + .do_get_sql_info(any.unpack::<CommandGetSqlInfo>().unwrap().unwrap()) + .await; + } + if any.is::<CommandGetPrimaryKeys>() { + return self + .do_get_primary_keys( + any.unpack::<CommandGetPrimaryKeys>().unwrap().unwrap(), + ) + .await; + } + if any.is::<CommandGetExportedKeys>() { + return self + .do_get_exported_keys( + any.unpack::<CommandGetExportedKeys>().unwrap().unwrap(), + ) + .await; + } + if any.is::<CommandGetImportedKeys>() { + return self + .do_get_imported_keys( + any.unpack::<CommandGetImportedKeys>().unwrap().unwrap(), + ) + .await; + } + if any.is::<CommandGetCrossReference>() { + return self + .do_get_cross_reference( + any.unpack::<CommandGetCrossReference>().unwrap().unwrap(), + ) + .await; + } + + Err(Status::unimplemented(format!( + "do_get: The defined request is invalid: {:?}", + String::from_utf8(request.ticket).unwrap() + ))) + } + + async fn do_put( + &self, + _request: Request<Streaming<FlightData>>, + ) -> Result<Response<Self::DoPutStream>, Status> { + let request = _request.into_inner().message().await?.unwrap(); + let any: prost_types::Any = + prost::Message::decode(&*request.flight_descriptor.unwrap().cmd) + .map_err(|_| Status::invalid_argument("Unable to parse command"))?; + if any.is::<CommandStatementUpdate>() { + return self + .do_put_statement_update( + any.unpack::<CommandStatementUpdate>().unwrap().unwrap(), + ) + .await; + } + if any.is::<CommandPreparedStatementQuery>() { + return self + .do_put_prepared_statement_query( + any.unpack::<CommandPreparedStatementQuery>() + .unwrap() + .unwrap(), + ) + .await; + } + if any.is::<CommandPreparedStatementUpdate>() { + return self + .do_put_prepared_statement_update( + any.unpack::<CommandPreparedStatementUpdate>() + .unwrap() + .unwrap(), + ) + .await; + } + + Err(Status::unimplemented(format!( + "do_put: The defined request is invalid: {:?}", + String::from_utf8(any.encode_to_vec()).unwrap() + ))) + } + + async fn list_actions( + &self, + _request: Request<Empty>, + ) -> Result<Response<Self::ListActionsStream>, Status> { + let create_prepared_statement_action_type = ActionType { + r#type: CREATE_PREPARED_STATEMENT.to_string(), + description: "Creates a reusable prepared statement resource on the server.\n + Request Message: ActionCreatePreparedStatementRequest\n + Response Message: ActionCreatePreparedStatementResult" + .into(), + }; + let close_prepared_statement_action_type = ActionType { + r#type: CLOSE_PREPARED_STATEMENT.to_string(), + description: "Closes a reusable prepared statement resource on the server.\n + Request Message: ActionClosePreparedStatementRequest\n + Response Message: N/A" + .into(), + }; + let _actions: Vec<Result<ActionType, Status>> = vec![ + Ok(create_prepared_statement_action_type), + Ok(close_prepared_statement_action_type), + ]; + // TODO: not sure why it's not work + // let output = futures::stream::iter(actions); + // Ok(Response::new(Box::pin(output) as Self::ListActionsStream)) Review comment: I'm not entirely sure I fully understand what the compiler is doing here, I don't really understand how recursive trait definitions work, but if you remove the recursive trait-definition, i.e. remove the `trait FlightSqlService: FlightService`, this compiles ########## File path: arrow-flight/src/sql/server.rs ########## @@ -0,0 +1,585 @@ +// Licensed to the Apache Software Foundation (ASF) under one +// or more contributor license agreements. See the NOTICE file +// distributed with this work for additional information +// regarding copyright ownership. The ASF licenses this file +// to you under the Apache License, Version 2.0 (the +// "License"); you may not use this file except in compliance +// with the License. You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, +// software distributed under the License is distributed on an +// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY +// KIND, either express or implied. See the License for the +// specific language governing permissions and limitations +// under the License. + +use std::pin::Pin; + +use futures::Stream; +use prost::Message; +use tonic::{Request, Response, Status, Streaming}; + +use super::{ + super::{ + flight_service_server::FlightService, Action, ActionType, Criteria, Empty, + FlightData, FlightDescriptor, FlightInfo, HandshakeRequest, HandshakeResponse, + PutResult, SchemaResult, Ticket, + }, + ActionClosePreparedStatementRequest, ActionCreatePreparedStatementRequest, + CommandGetCatalogs, CommandGetCrossReference, CommandGetDbSchemas, + CommandGetExportedKeys, CommandGetImportedKeys, CommandGetPrimaryKeys, + CommandGetSqlInfo, CommandGetTableTypes, CommandGetTables, + CommandPreparedStatementQuery, CommandPreparedStatementUpdate, CommandStatementQuery, + CommandStatementUpdate, ProstAnyExt, SqlInfo, TicketStatementQuery, +}; + +static CREATE_PREPARED_STATEMENT: &str = "CreatePreparedStatement"; +static CLOSE_PREPARED_STATEMENT: &str = "ClosePreparedStatement"; + +/// Implements FlightSqlService to handle the flight sql protocol +#[tonic::async_trait] +pub trait FlightSqlService: + std::marker::Sync + std::marker::Send + std::marker::Sized + FlightService + 'static +{ + /// When impl FlightSqlService, you can always set FlightService to Self + type FlightService: FlightService; Review comment: Why is this necessary? ########## File path: arrow-flight/src/sql/server.rs ########## @@ -0,0 +1,585 @@ +// Licensed to the Apache Software Foundation (ASF) under one +// or more contributor license agreements. See the NOTICE file +// distributed with this work for additional information +// regarding copyright ownership. The ASF licenses this file +// to you under the Apache License, Version 2.0 (the +// "License"); you may not use this file except in compliance +// with the License. You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, +// software distributed under the License is distributed on an +// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY +// KIND, either express or implied. See the License for the +// specific language governing permissions and limitations +// under the License. + +use std::pin::Pin; + +use futures::Stream; +use prost::Message; +use tonic::{Request, Response, Status, Streaming}; + +use super::{ + super::{ + flight_service_server::FlightService, Action, ActionType, Criteria, Empty, + FlightData, FlightDescriptor, FlightInfo, HandshakeRequest, HandshakeResponse, + PutResult, SchemaResult, Ticket, + }, + ActionClosePreparedStatementRequest, ActionCreatePreparedStatementRequest, + CommandGetCatalogs, CommandGetCrossReference, CommandGetDbSchemas, + CommandGetExportedKeys, CommandGetImportedKeys, CommandGetPrimaryKeys, + CommandGetSqlInfo, CommandGetTableTypes, CommandGetTables, + CommandPreparedStatementQuery, CommandPreparedStatementUpdate, CommandStatementQuery, + CommandStatementUpdate, ProstAnyExt, SqlInfo, TicketStatementQuery, +}; + +static CREATE_PREPARED_STATEMENT: &str = "CreatePreparedStatement"; +static CLOSE_PREPARED_STATEMENT: &str = "ClosePreparedStatement"; + +/// Implements FlightSqlService to handle the flight sql protocol +#[tonic::async_trait] +pub trait FlightSqlService: + std::marker::Sync + std::marker::Send + std::marker::Sized + FlightService + 'static +{ + /// When impl FlightSqlService, you can always set FlightService to Self + type FlightService: FlightService; + + /// Get a FlightInfo for executing a SQL query. + async fn get_flight_info_statement( + &self, + query: CommandStatementQuery, + request: FlightDescriptor, + ) -> Result<Response<FlightInfo>, Status>; + + /// Get a FlightInfo for executing an already created prepared statement. + async fn get_flight_info_prepared_statement( + &self, + query: CommandPreparedStatementQuery, + request: FlightDescriptor, + ) -> Result<Response<FlightInfo>, Status>; + + /// Get a FlightInfo for listing catalogs. + async fn get_flight_info_catalogs( + &self, + query: CommandGetCatalogs, + request: FlightDescriptor, + ) -> Result<Response<FlightInfo>, Status>; + + /// Get a FlightInfo for listing schemas. + async fn get_flight_info_schemas( + &self, + query: CommandGetDbSchemas, + request: FlightDescriptor, + ) -> Result<Response<FlightInfo>, Status>; + + /// Get a FlightInfo for listing tables. + async fn get_flight_info_tables( + &self, + query: CommandGetTables, + request: FlightDescriptor, + ) -> Result<Response<FlightInfo>, Status>; + + /// Get a FlightInfo to extract information about the table types. + async fn get_flight_info_table_types( + &self, + query: CommandGetTableTypes, + request: FlightDescriptor, + ) -> Result<Response<FlightInfo>, Status>; + + /// Get a FlightInfo for retrieving other information (See SqlInfo). + async fn get_flight_info_sql_info( + &self, + query: CommandGetSqlInfo, + request: FlightDescriptor, + ) -> Result<Response<FlightInfo>, Status>; + + /// Get a FlightInfo to extract information about primary and foreign keys. + async fn get_flight_info_primary_keys( + &self, + query: CommandGetPrimaryKeys, + request: FlightDescriptor, + ) -> Result<Response<FlightInfo>, Status>; + + /// Get a FlightInfo to extract information about exported keys. + async fn get_flight_info_exported_keys( + &self, + query: CommandGetExportedKeys, + request: FlightDescriptor, + ) -> Result<Response<FlightInfo>, Status>; + + /// Get a FlightInfo to extract information about imported keys. + async fn get_flight_info_imported_keys( + &self, + query: CommandGetImportedKeys, + request: FlightDescriptor, + ) -> Result<Response<FlightInfo>, Status>; + + /// Get a FlightInfo to extract information about cross reference. + async fn get_flight_info_cross_reference( + &self, + query: CommandGetCrossReference, + request: FlightDescriptor, + ) -> Result<Response<FlightInfo>, Status>; + + // do_get + + /// Get a FlightInfo for executing an already created prepared statement. + async fn do_get_statement( + &self, + ticket: TicketStatementQuery, + ) -> Result<Response<<Self as FlightService>::DoGetStream>, Status>; + + /// Get a FlightDataStream containing the prepared statement query results. + async fn do_get_prepared_statement( + &self, + query: CommandPreparedStatementQuery, + ) -> Result<Response<<Self as FlightService>::DoGetStream>, Status>; + + /// Get a FlightDataStream containing the list of catalogs. + async fn do_get_catalogs( + &self, + query: CommandGetCatalogs, + ) -> Result<Response<<Self as FlightService>::DoGetStream>, Status>; + + /// Get a FlightDataStream containing the list of schemas. + async fn do_get_schemas( + &self, + query: CommandGetDbSchemas, + ) -> Result<Response<<Self as FlightService>::DoGetStream>, Status>; + + /// Get a FlightDataStream containing the list of tables. + async fn do_get_tables( + &self, + query: CommandGetTables, + ) -> Result<Response<<Self as FlightService>::DoGetStream>, Status>; + + /// Get a FlightDataStream containing the data related to the table types. + async fn do_get_table_types( + &self, + query: CommandGetTableTypes, + ) -> Result<Response<<Self as FlightService>::DoGetStream>, Status>; + + /// Get a FlightDataStream containing the list of SqlInfo results. + async fn do_get_sql_info( + &self, + query: CommandGetSqlInfo, + ) -> Result<Response<<Self as FlightService>::DoGetStream>, Status>; + + /// Get a FlightDataStream containing the data related to the primary and foreign keys. + async fn do_get_primary_keys( + &self, + query: CommandGetPrimaryKeys, + ) -> Result<Response<<Self as FlightService>::DoGetStream>, Status>; + + /// Get a FlightDataStream containing the data related to the exported keys. + async fn do_get_exported_keys( + &self, + query: CommandGetExportedKeys, + ) -> Result<Response<<Self as FlightService>::DoGetStream>, Status>; + + /// Get a FlightDataStream containing the data related to the imported keys. + async fn do_get_imported_keys( + &self, + query: CommandGetImportedKeys, + ) -> Result<Response<<Self as FlightService>::DoGetStream>, Status>; + + /// Get a FlightDataStream containing the data related to the cross reference. + async fn do_get_cross_reference( + &self, + query: CommandGetCrossReference, + ) -> Result<Response<<Self as FlightService>::DoGetStream>, Status>; + + // do_put + + /// Execute an update SQL statement. + async fn do_put_statement_update( + &self, + ticket: CommandStatementUpdate, + ) -> Result<Response<<Self as FlightService>::DoPutStream>, Status>; + + /// Bind parameters to given prepared statement. + async fn do_put_prepared_statement_query( + &self, + query: CommandPreparedStatementQuery, + ) -> Result<Response<<Self as FlightService>::DoPutStream>, Status>; + + /// Execute an update SQL prepared statement. + async fn do_put_prepared_statement_update( + &self, + query: CommandPreparedStatementUpdate, + ) -> Result<Response<<Self as FlightService>::DoPutStream>, Status>; + + // do_action + + /// Create a prepared statement from given SQL statement. + async fn do_action_create_prepared_statement( + &self, + query: ActionCreatePreparedStatementRequest, + ) -> Result<Response<<Self as FlightService>::DoActionStream>, Status>; + + /// Close a prepared statement. + async fn do_action_close_prepared_statement( + &self, + query: ActionClosePreparedStatementRequest, + ) -> Result<Response<<Self as FlightService>::DoActionStream>, Status>; + + /// Register a new SqlInfo result, making it available when calling GetSqlInfo. + async fn register_sql_info(&self, id: i32, result: &SqlInfo); +} + +/// Implements the lower level interface to handle FlightSQL +#[tonic::async_trait] +impl<T: 'static> FlightService for T +where + T: FlightSqlService + std::marker::Sync + std::marker::Send, +{ + type HandshakeStream = Pin< + Box<dyn Stream<Item = Result<HandshakeResponse, Status>> + Send + Sync + 'static>, Review comment: The `Sync` is technically not required, we recently removed it from Datafusion - https://github.com/apache/arrow-datafusion/issues/1614 This would also allow you to use `futures::stream::BoxStream<'static, Result<ActionType, Status>>` which is perhaps more idiomatic ########## File path: arrow-flight/src/sql/server.rs ########## @@ -0,0 +1,585 @@ +// Licensed to the Apache Software Foundation (ASF) under one +// or more contributor license agreements. See the NOTICE file +// distributed with this work for additional information +// regarding copyright ownership. The ASF licenses this file +// to you under the Apache License, Version 2.0 (the +// "License"); you may not use this file except in compliance +// with the License. You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, +// software distributed under the License is distributed on an +// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY +// KIND, either express or implied. See the License for the +// specific language governing permissions and limitations +// under the License. + +use std::pin::Pin; + +use futures::Stream; +use prost::Message; +use tonic::{Request, Response, Status, Streaming}; + +use super::{ + super::{ + flight_service_server::FlightService, Action, ActionType, Criteria, Empty, + FlightData, FlightDescriptor, FlightInfo, HandshakeRequest, HandshakeResponse, + PutResult, SchemaResult, Ticket, + }, + ActionClosePreparedStatementRequest, ActionCreatePreparedStatementRequest, + CommandGetCatalogs, CommandGetCrossReference, CommandGetDbSchemas, + CommandGetExportedKeys, CommandGetImportedKeys, CommandGetPrimaryKeys, + CommandGetSqlInfo, CommandGetTableTypes, CommandGetTables, + CommandPreparedStatementQuery, CommandPreparedStatementUpdate, CommandStatementQuery, + CommandStatementUpdate, ProstAnyExt, SqlInfo, TicketStatementQuery, +}; + +static CREATE_PREPARED_STATEMENT: &str = "CreatePreparedStatement"; +static CLOSE_PREPARED_STATEMENT: &str = "ClosePreparedStatement"; + +/// Implements FlightSqlService to handle the flight sql protocol +#[tonic::async_trait] +pub trait FlightSqlService: + std::marker::Sync + std::marker::Send + std::marker::Sized + FlightService + 'static +{ + /// When impl FlightSqlService, you can always set FlightService to Self + type FlightService: FlightService; + + /// Get a FlightInfo for executing a SQL query. + async fn get_flight_info_statement( + &self, + query: CommandStatementQuery, + request: FlightDescriptor, + ) -> Result<Response<FlightInfo>, Status>; + + /// Get a FlightInfo for executing an already created prepared statement. + async fn get_flight_info_prepared_statement( + &self, + query: CommandPreparedStatementQuery, + request: FlightDescriptor, + ) -> Result<Response<FlightInfo>, Status>; + + /// Get a FlightInfo for listing catalogs. + async fn get_flight_info_catalogs( + &self, + query: CommandGetCatalogs, + request: FlightDescriptor, + ) -> Result<Response<FlightInfo>, Status>; + + /// Get a FlightInfo for listing schemas. + async fn get_flight_info_schemas( + &self, + query: CommandGetDbSchemas, + request: FlightDescriptor, + ) -> Result<Response<FlightInfo>, Status>; + + /// Get a FlightInfo for listing tables. + async fn get_flight_info_tables( + &self, + query: CommandGetTables, + request: FlightDescriptor, + ) -> Result<Response<FlightInfo>, Status>; + + /// Get a FlightInfo to extract information about the table types. + async fn get_flight_info_table_types( + &self, + query: CommandGetTableTypes, + request: FlightDescriptor, + ) -> Result<Response<FlightInfo>, Status>; + + /// Get a FlightInfo for retrieving other information (See SqlInfo). + async fn get_flight_info_sql_info( + &self, + query: CommandGetSqlInfo, + request: FlightDescriptor, + ) -> Result<Response<FlightInfo>, Status>; + + /// Get a FlightInfo to extract information about primary and foreign keys. + async fn get_flight_info_primary_keys( + &self, + query: CommandGetPrimaryKeys, + request: FlightDescriptor, + ) -> Result<Response<FlightInfo>, Status>; + + /// Get a FlightInfo to extract information about exported keys. + async fn get_flight_info_exported_keys( + &self, + query: CommandGetExportedKeys, + request: FlightDescriptor, + ) -> Result<Response<FlightInfo>, Status>; + + /// Get a FlightInfo to extract information about imported keys. + async fn get_flight_info_imported_keys( + &self, + query: CommandGetImportedKeys, + request: FlightDescriptor, + ) -> Result<Response<FlightInfo>, Status>; + + /// Get a FlightInfo to extract information about cross reference. + async fn get_flight_info_cross_reference( + &self, + query: CommandGetCrossReference, + request: FlightDescriptor, + ) -> Result<Response<FlightInfo>, Status>; + + // do_get + + /// Get a FlightInfo for executing an already created prepared statement. + async fn do_get_statement( + &self, + ticket: TicketStatementQuery, + ) -> Result<Response<<Self as FlightService>::DoGetStream>, Status>; + + /// Get a FlightDataStream containing the prepared statement query results. + async fn do_get_prepared_statement( + &self, + query: CommandPreparedStatementQuery, + ) -> Result<Response<<Self as FlightService>::DoGetStream>, Status>; + + /// Get a FlightDataStream containing the list of catalogs. + async fn do_get_catalogs( + &self, + query: CommandGetCatalogs, + ) -> Result<Response<<Self as FlightService>::DoGetStream>, Status>; + + /// Get a FlightDataStream containing the list of schemas. + async fn do_get_schemas( + &self, + query: CommandGetDbSchemas, + ) -> Result<Response<<Self as FlightService>::DoGetStream>, Status>; + + /// Get a FlightDataStream containing the list of tables. + async fn do_get_tables( + &self, + query: CommandGetTables, + ) -> Result<Response<<Self as FlightService>::DoGetStream>, Status>; + + /// Get a FlightDataStream containing the data related to the table types. + async fn do_get_table_types( + &self, + query: CommandGetTableTypes, + ) -> Result<Response<<Self as FlightService>::DoGetStream>, Status>; + + /// Get a FlightDataStream containing the list of SqlInfo results. + async fn do_get_sql_info( + &self, + query: CommandGetSqlInfo, + ) -> Result<Response<<Self as FlightService>::DoGetStream>, Status>; + + /// Get a FlightDataStream containing the data related to the primary and foreign keys. + async fn do_get_primary_keys( + &self, + query: CommandGetPrimaryKeys, + ) -> Result<Response<<Self as FlightService>::DoGetStream>, Status>; + + /// Get a FlightDataStream containing the data related to the exported keys. + async fn do_get_exported_keys( + &self, + query: CommandGetExportedKeys, + ) -> Result<Response<<Self as FlightService>::DoGetStream>, Status>; + + /// Get a FlightDataStream containing the data related to the imported keys. + async fn do_get_imported_keys( + &self, + query: CommandGetImportedKeys, + ) -> Result<Response<<Self as FlightService>::DoGetStream>, Status>; + + /// Get a FlightDataStream containing the data related to the cross reference. + async fn do_get_cross_reference( + &self, + query: CommandGetCrossReference, + ) -> Result<Response<<Self as FlightService>::DoGetStream>, Status>; + + // do_put + + /// Execute an update SQL statement. + async fn do_put_statement_update( + &self, + ticket: CommandStatementUpdate, + ) -> Result<Response<<Self as FlightService>::DoPutStream>, Status>; + + /// Bind parameters to given prepared statement. + async fn do_put_prepared_statement_query( + &self, + query: CommandPreparedStatementQuery, + ) -> Result<Response<<Self as FlightService>::DoPutStream>, Status>; + + /// Execute an update SQL prepared statement. + async fn do_put_prepared_statement_update( + &self, + query: CommandPreparedStatementUpdate, + ) -> Result<Response<<Self as FlightService>::DoPutStream>, Status>; + + // do_action + + /// Create a prepared statement from given SQL statement. + async fn do_action_create_prepared_statement( + &self, + query: ActionCreatePreparedStatementRequest, + ) -> Result<Response<<Self as FlightService>::DoActionStream>, Status>; + + /// Close a prepared statement. + async fn do_action_close_prepared_statement( + &self, + query: ActionClosePreparedStatementRequest, + ) -> Result<Response<<Self as FlightService>::DoActionStream>, Status>; + + /// Register a new SqlInfo result, making it available when calling GetSqlInfo. + async fn register_sql_info(&self, id: i32, result: &SqlInfo); +} + +/// Implements the lower level interface to handle FlightSQL +#[tonic::async_trait] +impl<T: 'static> FlightService for T +where + T: FlightSqlService + std::marker::Sync + std::marker::Send, +{ + type HandshakeStream = Pin< + Box<dyn Stream<Item = Result<HandshakeResponse, Status>> + Send + Sync + 'static>, + >; + type ListFlightsStream = + Pin<Box<dyn Stream<Item = Result<FlightInfo, Status>> + Send + Sync + 'static>>; + type DoGetStream = + Pin<Box<dyn Stream<Item = Result<FlightData, Status>> + Send + Sync + 'static>>; + type DoPutStream = + Pin<Box<dyn Stream<Item = Result<PutResult, Status>> + Send + Sync + 'static>>; + type DoActionStream = Pin< + Box< + dyn Stream<Item = Result<super::super::Result, Status>> + + Send + + Sync + + 'static, + >, + >; + type ListActionsStream = + Pin<Box<dyn Stream<Item = Result<ActionType, Status>> + Send + Sync + 'static>>; + type DoExchangeStream = + Pin<Box<dyn Stream<Item = Result<FlightData, Status>> + Send + Sync + 'static>>; + + async fn handshake( + &self, + _request: Request<Streaming<HandshakeRequest>>, + ) -> Result<Response<Self::HandshakeStream>, Status> { + Err(Status::unimplemented("Not yet implemented")) + } + + async fn list_flights( + &self, + _request: Request<Criteria>, + ) -> Result<Response<Self::ListFlightsStream>, Status> { + Err(Status::unimplemented("Not yet implemented")) + } + + async fn get_flight_info( + &self, + request: Request<FlightDescriptor>, + ) -> Result<Response<FlightInfo>, Status> { + let request = request.into_inner(); + let any: prost_types::Any = prost::Message::decode(&*request.cmd) + .map_err(|_| Status::invalid_argument("Unable to parse command"))?; + + if any.is::<CommandStatementQuery>() { + return self + .get_flight_info_statement( + any.unpack::<CommandStatementQuery>().unwrap().unwrap(), Review comment: I think the result should probably be returned as an error, e.g. `Status::invalid_argument` as above, vs panicking. It might be possible to pull this logic into a function so that `?` can be used instead of lots of manual `map_err` calls ########## File path: arrow-flight/src/sql/mod.rs ########## @@ -0,0 +1,183 @@ +// Licensed to the Apache Software Foundation (ASF) under one +// or more contributor license agreements. See the NOTICE file +// distributed with this work for additional information +// regarding copyright ownership. The ASF licenses this file +// to you under the Apache License, Version 2.0 (the +// "License"); you may not use this file except in compliance +// with the License. You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, +// software distributed under the License is distributed on an +// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY +// KIND, either express or implied. See the License for the +// specific language governing permissions and limitations +// under the License. + +use arrow::error::{ArrowError, Result as ArrowResult}; +use prost::Message; + +mod gen { + #![allow(clippy::all)] + include!("arrow.flight.protocol.sql.rs"); +} + +pub use gen::ActionClosePreparedStatementRequest; +pub use gen::ActionCreatePreparedStatementRequest; +pub use gen::ActionCreatePreparedStatementResult; +pub use gen::CommandGetCatalogs; +pub use gen::CommandGetCrossReference; +pub use gen::CommandGetDbSchemas; +pub use gen::CommandGetExportedKeys; +pub use gen::CommandGetImportedKeys; +pub use gen::CommandGetPrimaryKeys; +pub use gen::CommandGetSqlInfo; +pub use gen::CommandGetTableTypes; +pub use gen::CommandGetTables; +pub use gen::CommandPreparedStatementQuery; +pub use gen::CommandPreparedStatementUpdate; +pub use gen::CommandStatementQuery; +pub use gen::CommandStatementUpdate; +pub use gen::DoPutUpdateResult; +pub use gen::SqlInfo; +pub use gen::SqlNullOrdering; +pub use gen::SqlOuterJoinsSupportLevel; +pub use gen::SqlSupportedCaseSensitivity; +pub use gen::SqlSupportedElementActions; +pub use gen::SqlSupportedGroupBy; +pub use gen::SqlSupportedPositionedCommands; +pub use gen::SqlSupportedResultSetConcurrency; +pub use gen::SqlSupportedResultSetType; +pub use gen::SqlSupportedSubqueries; +pub use gen::SqlSupportedTransactions; +pub use gen::SqlSupportedUnions; +pub use gen::SqlSupportsConvert; +pub use gen::SqlTransactionIsolationLevel; +pub use gen::SupportedSqlGrammar; +pub use gen::TicketStatementQuery; +pub use gen::UpdateDeleteRules; + +pub mod server; + +/// ProstMessageExt are useful utility methods for prost::Message types +pub trait ProstMessageExt { + /// Item is the return value of prost_type::Any::unpack() + type Item: prost::Message + Default; + + /// type_url for this Message + fn type_url() -> &'static str; + + /// Convert this Message to prost_types::Any + fn as_any(&self) -> prost_types::Any; +} + +macro_rules! prost_message_ext { + ($($name:ty,)*) => { + $( + impl ProstMessageExt for $name { + type Item = $name; + fn type_url() -> &'static str { + concat!("type.googleapis.com/arrow.flight.protocol.sql.", stringify!($name)) + } + + fn as_any(&self) -> prost_types::Any { + prost_types::Any { + type_url: <$name>::type_url().to_string(), + value: self.encode_to_vec(), + } + } + } + )* + }; +} + +// Implement ProstMessageExt for all structs defined in FlightSql.proto +prost_message_ext!( + ActionClosePreparedStatementRequest, + ActionCreatePreparedStatementRequest, + ActionCreatePreparedStatementResult, + CommandGetCatalogs, + CommandGetCrossReference, + CommandGetDbSchemas, + CommandGetExportedKeys, + CommandGetImportedKeys, + CommandGetPrimaryKeys, + CommandGetSqlInfo, + CommandGetTableTypes, + CommandGetTables, + CommandPreparedStatementQuery, + CommandPreparedStatementUpdate, + CommandStatementQuery, + CommandStatementUpdate, + DoPutUpdateResult, + TicketStatementQuery, +); + +/// ProstAnyExt are useful utility methods for prost_types::Any +/// The API design is inspired by [rust-protobuf](https://github.com/stepancheg/rust-protobuf/blob/master/protobuf/src/well_known_types_util/any.rs) +pub trait ProstAnyExt { + /// Check if `Any` contains a message of given type. + fn is<M: ProstMessageExt>(&self) -> bool; + + /// Extract a message from this `Any`. + /// + /// # Returns + /// + /// * `Ok(None)` when message type mismatch + /// * `Err` when parse failed + fn unpack<M: ProstMessageExt>(&self) -> ArrowResult<Option<M::Item>>; + + /// Pack any message into `prost_types::Any` value. + fn pack<M: ProstMessageExt>(message: &M) -> ArrowResult<prost_types::Any>; +} + +impl ProstAnyExt for prost_types::Any { + fn is<M: ProstMessageExt>(&self) -> bool { + M::type_url() == self.type_url + } + + fn unpack<M: ProstMessageExt>(&self) -> ArrowResult<Option<M::Item>> { Review comment: Perhaps this could return `tonic::Status` given the fact it will be predominantely used in gRPC handlers? :thinking: ########## File path: arrow-flight/src/sql/mod.rs ########## @@ -0,0 +1,183 @@ +// Licensed to the Apache Software Foundation (ASF) under one +// or more contributor license agreements. See the NOTICE file +// distributed with this work for additional information +// regarding copyright ownership. The ASF licenses this file +// to you under the Apache License, Version 2.0 (the +// "License"); you may not use this file except in compliance +// with the License. You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, +// software distributed under the License is distributed on an +// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY +// KIND, either express or implied. See the License for the +// specific language governing permissions and limitations +// under the License. + +use arrow::error::{ArrowError, Result as ArrowResult}; +use prost::Message; + +mod gen { + #![allow(clippy::all)] + include!("arrow.flight.protocol.sql.rs"); +} + +pub use gen::ActionClosePreparedStatementRequest; +pub use gen::ActionCreatePreparedStatementRequest; +pub use gen::ActionCreatePreparedStatementResult; +pub use gen::CommandGetCatalogs; +pub use gen::CommandGetCrossReference; +pub use gen::CommandGetDbSchemas; +pub use gen::CommandGetExportedKeys; +pub use gen::CommandGetImportedKeys; +pub use gen::CommandGetPrimaryKeys; +pub use gen::CommandGetSqlInfo; +pub use gen::CommandGetTableTypes; +pub use gen::CommandGetTables; +pub use gen::CommandPreparedStatementQuery; +pub use gen::CommandPreparedStatementUpdate; +pub use gen::CommandStatementQuery; +pub use gen::CommandStatementUpdate; +pub use gen::DoPutUpdateResult; +pub use gen::SqlInfo; +pub use gen::SqlNullOrdering; +pub use gen::SqlOuterJoinsSupportLevel; +pub use gen::SqlSupportedCaseSensitivity; +pub use gen::SqlSupportedElementActions; +pub use gen::SqlSupportedGroupBy; +pub use gen::SqlSupportedPositionedCommands; +pub use gen::SqlSupportedResultSetConcurrency; +pub use gen::SqlSupportedResultSetType; +pub use gen::SqlSupportedSubqueries; +pub use gen::SqlSupportedTransactions; +pub use gen::SqlSupportedUnions; +pub use gen::SqlSupportsConvert; +pub use gen::SqlTransactionIsolationLevel; +pub use gen::SupportedSqlGrammar; +pub use gen::TicketStatementQuery; +pub use gen::UpdateDeleteRules; + +pub mod server; + +/// ProstMessageExt are useful utility methods for prost::Message types +pub trait ProstMessageExt { + /// Item is the return value of prost_type::Any::unpack() + type Item: prost::Message + Default; + + /// type_url for this Message + fn type_url() -> &'static str; + + /// Convert this Message to prost_types::Any + fn as_any(&self) -> prost_types::Any; +} + +macro_rules! prost_message_ext { Review comment: Nice :ok_hand: ########## File path: arrow-flight/src/sql/server.rs ########## @@ -0,0 +1,585 @@ +// Licensed to the Apache Software Foundation (ASF) under one +// or more contributor license agreements. See the NOTICE file +// distributed with this work for additional information +// regarding copyright ownership. The ASF licenses this file +// to you under the Apache License, Version 2.0 (the +// "License"); you may not use this file except in compliance +// with the License. You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, +// software distributed under the License is distributed on an +// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY +// KIND, either express or implied. See the License for the +// specific language governing permissions and limitations +// under the License. + +use std::pin::Pin; + +use futures::Stream; +use prost::Message; +use tonic::{Request, Response, Status, Streaming}; + +use super::{ + super::{ + flight_service_server::FlightService, Action, ActionType, Criteria, Empty, + FlightData, FlightDescriptor, FlightInfo, HandshakeRequest, HandshakeResponse, + PutResult, SchemaResult, Ticket, + }, + ActionClosePreparedStatementRequest, ActionCreatePreparedStatementRequest, + CommandGetCatalogs, CommandGetCrossReference, CommandGetDbSchemas, + CommandGetExportedKeys, CommandGetImportedKeys, CommandGetPrimaryKeys, + CommandGetSqlInfo, CommandGetTableTypes, CommandGetTables, + CommandPreparedStatementQuery, CommandPreparedStatementUpdate, CommandStatementQuery, + CommandStatementUpdate, ProstAnyExt, SqlInfo, TicketStatementQuery, +}; + +static CREATE_PREPARED_STATEMENT: &str = "CreatePreparedStatement"; +static CLOSE_PREPARED_STATEMENT: &str = "ClosePreparedStatement"; + +/// Implements FlightSqlService to handle the flight sql protocol +#[tonic::async_trait] +pub trait FlightSqlService: + std::marker::Sync + std::marker::Send + std::marker::Sized + FlightService + 'static Review comment: These types seem to be recursive, something implements `FlightSqlService` which must in turn implement `FlightService` and there is then a blanket impl of `FlightService` for all `FlightSqlService` ########## File path: arrow-flight/src/sql/mod.rs ########## @@ -0,0 +1,183 @@ +// Licensed to the Apache Software Foundation (ASF) under one +// or more contributor license agreements. See the NOTICE file +// distributed with this work for additional information +// regarding copyright ownership. The ASF licenses this file +// to you under the Apache License, Version 2.0 (the +// "License"); you may not use this file except in compliance +// with the License. You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, +// software distributed under the License is distributed on an +// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY +// KIND, either express or implied. See the License for the +// specific language governing permissions and limitations +// under the License. + +use arrow::error::{ArrowError, Result as ArrowResult}; +use prost::Message; + +mod gen { + #![allow(clippy::all)] + include!("arrow.flight.protocol.sql.rs"); +} + +pub use gen::ActionClosePreparedStatementRequest; +pub use gen::ActionCreatePreparedStatementRequest; +pub use gen::ActionCreatePreparedStatementResult; +pub use gen::CommandGetCatalogs; +pub use gen::CommandGetCrossReference; +pub use gen::CommandGetDbSchemas; +pub use gen::CommandGetExportedKeys; +pub use gen::CommandGetImportedKeys; +pub use gen::CommandGetPrimaryKeys; +pub use gen::CommandGetSqlInfo; +pub use gen::CommandGetTableTypes; +pub use gen::CommandGetTables; +pub use gen::CommandPreparedStatementQuery; +pub use gen::CommandPreparedStatementUpdate; +pub use gen::CommandStatementQuery; +pub use gen::CommandStatementUpdate; +pub use gen::DoPutUpdateResult; +pub use gen::SqlInfo; +pub use gen::SqlNullOrdering; +pub use gen::SqlOuterJoinsSupportLevel; +pub use gen::SqlSupportedCaseSensitivity; +pub use gen::SqlSupportedElementActions; +pub use gen::SqlSupportedGroupBy; +pub use gen::SqlSupportedPositionedCommands; +pub use gen::SqlSupportedResultSetConcurrency; +pub use gen::SqlSupportedResultSetType; +pub use gen::SqlSupportedSubqueries; +pub use gen::SqlSupportedTransactions; +pub use gen::SqlSupportedUnions; +pub use gen::SqlSupportsConvert; +pub use gen::SqlTransactionIsolationLevel; +pub use gen::SupportedSqlGrammar; +pub use gen::TicketStatementQuery; +pub use gen::UpdateDeleteRules; + +pub mod server; + +/// ProstMessageExt are useful utility methods for prost::Message types +pub trait ProstMessageExt { + /// Item is the return value of prost_type::Any::unpack() + type Item: prost::Message + Default; + + /// type_url for this Message + fn type_url() -> &'static str; + + /// Convert this Message to prost_types::Any + fn as_any(&self) -> prost_types::Any; +} + +macro_rules! prost_message_ext { + ($($name:ty,)*) => { + $( + impl ProstMessageExt for $name { + type Item = $name; + fn type_url() -> &'static str { + concat!("type.googleapis.com/arrow.flight.protocol.sql.", stringify!($name)) + } + + fn as_any(&self) -> prost_types::Any { + prost_types::Any { + type_url: <$name>::type_url().to_string(), + value: self.encode_to_vec(), + } + } + } + )* + }; +} + +// Implement ProstMessageExt for all structs defined in FlightSql.proto +prost_message_ext!( + ActionClosePreparedStatementRequest, + ActionCreatePreparedStatementRequest, + ActionCreatePreparedStatementResult, + CommandGetCatalogs, + CommandGetCrossReference, + CommandGetDbSchemas, + CommandGetExportedKeys, + CommandGetImportedKeys, + CommandGetPrimaryKeys, + CommandGetSqlInfo, + CommandGetTableTypes, + CommandGetTables, + CommandPreparedStatementQuery, + CommandPreparedStatementUpdate, + CommandStatementQuery, + CommandStatementUpdate, + DoPutUpdateResult, + TicketStatementQuery, +); + +/// ProstAnyExt are useful utility methods for prost_types::Any +/// The API design is inspired by [rust-protobuf](https://github.com/stepancheg/rust-protobuf/blob/master/protobuf/src/well_known_types_util/any.rs) +pub trait ProstAnyExt { + /// Check if `Any` contains a message of given type. + fn is<M: ProstMessageExt>(&self) -> bool; + + /// Extract a message from this `Any`. + /// + /// # Returns + /// + /// * `Ok(None)` when message type mismatch + /// * `Err` when parse failed + fn unpack<M: ProstMessageExt>(&self) -> ArrowResult<Option<M::Item>>; + + /// Pack any message into `prost_types::Any` value. + fn pack<M: ProstMessageExt>(message: &M) -> ArrowResult<prost_types::Any>; +} + +impl ProstAnyExt for prost_types::Any { + fn is<M: ProstMessageExt>(&self) -> bool { + M::type_url() == self.type_url + } + + fn unpack<M: ProstMessageExt>(&self) -> ArrowResult<Option<M::Item>> { + if !self.is::<M>() { + return Ok(None); + } + let m = prost::Message::decode(&*self.value).map_err(|err| { + ArrowError::ParseError(format!("Unable to decode Any value: {}", err)) + })?; + Ok(Some(m)) + } + + fn pack<M: ProstMessageExt>(message: &M) -> ArrowResult<prost_types::Any> { + Ok(message.as_any()) + } +} + +#[cfg(test)] +mod tests { + use super::*; + + #[test] + fn test_type_url() { + assert_eq!( + TicketStatementQuery::type_url(), + "type.googleapis.com/arrow.flight.protocol.sql.TicketStatementQuery" + ); + assert_eq!( + CommandStatementQuery::type_url(), + "type.googleapis.com/arrow.flight.protocol.sql.CommandStatementQuery" + ); + } + + #[test] + fn test_prost_any_pack_unpack() -> ArrowResult<()> { + let query = CommandStatementQuery { + query: "select 1".to_string(), + }; + let any = prost_types::Any::pack(&query)?; + assert!(any.is::<CommandStatementQuery>()); + let unpack_query: CommandStatementQuery = + any.unpack::<CommandStatementQuery>()?.unwrap(); Review comment: Is this a problem? The only way around this would be to have some macro that returns either an enumeration of the message types or some boxed trait, I'm not sure this would be an improvement? ########## File path: arrow-flight/src/sql/mod.rs ########## @@ -0,0 +1,183 @@ +// Licensed to the Apache Software Foundation (ASF) under one +// or more contributor license agreements. See the NOTICE file +// distributed with this work for additional information +// regarding copyright ownership. The ASF licenses this file +// to you under the Apache License, Version 2.0 (the +// "License"); you may not use this file except in compliance +// with the License. You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, +// software distributed under the License is distributed on an +// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY +// KIND, either express or implied. See the License for the +// specific language governing permissions and limitations +// under the License. + +use arrow::error::{ArrowError, Result as ArrowResult}; +use prost::Message; + +mod gen { + #![allow(clippy::all)] + include!("arrow.flight.protocol.sql.rs"); +} + +pub use gen::ActionClosePreparedStatementRequest; +pub use gen::ActionCreatePreparedStatementRequest; +pub use gen::ActionCreatePreparedStatementResult; +pub use gen::CommandGetCatalogs; +pub use gen::CommandGetCrossReference; +pub use gen::CommandGetDbSchemas; +pub use gen::CommandGetExportedKeys; +pub use gen::CommandGetImportedKeys; +pub use gen::CommandGetPrimaryKeys; +pub use gen::CommandGetSqlInfo; +pub use gen::CommandGetTableTypes; +pub use gen::CommandGetTables; +pub use gen::CommandPreparedStatementQuery; +pub use gen::CommandPreparedStatementUpdate; +pub use gen::CommandStatementQuery; +pub use gen::CommandStatementUpdate; +pub use gen::DoPutUpdateResult; +pub use gen::SqlInfo; +pub use gen::SqlNullOrdering; +pub use gen::SqlOuterJoinsSupportLevel; +pub use gen::SqlSupportedCaseSensitivity; +pub use gen::SqlSupportedElementActions; +pub use gen::SqlSupportedGroupBy; +pub use gen::SqlSupportedPositionedCommands; +pub use gen::SqlSupportedResultSetConcurrency; +pub use gen::SqlSupportedResultSetType; +pub use gen::SqlSupportedSubqueries; +pub use gen::SqlSupportedTransactions; +pub use gen::SqlSupportedUnions; +pub use gen::SqlSupportsConvert; +pub use gen::SqlTransactionIsolationLevel; +pub use gen::SupportedSqlGrammar; +pub use gen::TicketStatementQuery; +pub use gen::UpdateDeleteRules; + +pub mod server; + +/// ProstMessageExt are useful utility methods for prost::Message types +pub trait ProstMessageExt { + /// Item is the return value of prost_type::Any::unpack() + type Item: prost::Message + Default; + + /// type_url for this Message + fn type_url() -> &'static str; + + /// Convert this Message to prost_types::Any + fn as_any(&self) -> prost_types::Any; +} + +macro_rules! prost_message_ext { + ($($name:ty,)*) => { + $( + impl ProstMessageExt for $name { + type Item = $name; + fn type_url() -> &'static str { + concat!("type.googleapis.com/arrow.flight.protocol.sql.", stringify!($name)) + } + + fn as_any(&self) -> prost_types::Any { + prost_types::Any { + type_url: <$name>::type_url().to_string(), + value: self.encode_to_vec(), + } + } + } + )* + }; +} + +// Implement ProstMessageExt for all structs defined in FlightSql.proto +prost_message_ext!( + ActionClosePreparedStatementRequest, + ActionCreatePreparedStatementRequest, + ActionCreatePreparedStatementResult, + CommandGetCatalogs, + CommandGetCrossReference, + CommandGetDbSchemas, + CommandGetExportedKeys, + CommandGetImportedKeys, + CommandGetPrimaryKeys, + CommandGetSqlInfo, + CommandGetTableTypes, + CommandGetTables, + CommandPreparedStatementQuery, + CommandPreparedStatementUpdate, + CommandStatementQuery, + CommandStatementUpdate, + DoPutUpdateResult, + TicketStatementQuery, +); + +/// ProstAnyExt are useful utility methods for prost_types::Any +/// The API design is inspired by [rust-protobuf](https://github.com/stepancheg/rust-protobuf/blob/master/protobuf/src/well_known_types_util/any.rs) +pub trait ProstAnyExt { Review comment: This should possibly also be crate local ########## File path: arrow-flight/src/sql/mod.rs ########## @@ -0,0 +1,183 @@ +// Licensed to the Apache Software Foundation (ASF) under one +// or more contributor license agreements. See the NOTICE file +// distributed with this work for additional information +// regarding copyright ownership. The ASF licenses this file +// to you under the Apache License, Version 2.0 (the +// "License"); you may not use this file except in compliance +// with the License. You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, +// software distributed under the License is distributed on an +// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY +// KIND, either express or implied. See the License for the +// specific language governing permissions and limitations +// under the License. + +use arrow::error::{ArrowError, Result as ArrowResult}; +use prost::Message; + +mod gen { + #![allow(clippy::all)] + include!("arrow.flight.protocol.sql.rs"); +} + +pub use gen::ActionClosePreparedStatementRequest; +pub use gen::ActionCreatePreparedStatementRequest; +pub use gen::ActionCreatePreparedStatementResult; +pub use gen::CommandGetCatalogs; +pub use gen::CommandGetCrossReference; +pub use gen::CommandGetDbSchemas; +pub use gen::CommandGetExportedKeys; +pub use gen::CommandGetImportedKeys; +pub use gen::CommandGetPrimaryKeys; +pub use gen::CommandGetSqlInfo; +pub use gen::CommandGetTableTypes; +pub use gen::CommandGetTables; +pub use gen::CommandPreparedStatementQuery; +pub use gen::CommandPreparedStatementUpdate; +pub use gen::CommandStatementQuery; +pub use gen::CommandStatementUpdate; +pub use gen::DoPutUpdateResult; +pub use gen::SqlInfo; +pub use gen::SqlNullOrdering; +pub use gen::SqlOuterJoinsSupportLevel; +pub use gen::SqlSupportedCaseSensitivity; +pub use gen::SqlSupportedElementActions; +pub use gen::SqlSupportedGroupBy; +pub use gen::SqlSupportedPositionedCommands; +pub use gen::SqlSupportedResultSetConcurrency; +pub use gen::SqlSupportedResultSetType; +pub use gen::SqlSupportedSubqueries; +pub use gen::SqlSupportedTransactions; +pub use gen::SqlSupportedUnions; +pub use gen::SqlSupportsConvert; +pub use gen::SqlTransactionIsolationLevel; +pub use gen::SupportedSqlGrammar; +pub use gen::TicketStatementQuery; +pub use gen::UpdateDeleteRules; + +pub mod server; + +/// ProstMessageExt are useful utility methods for prost::Message types +pub trait ProstMessageExt { Review comment: I think this should at least be `pub(crate)`, it feels like something best kept as an implementation detail -- This is an automated message from the Apache Git Service. To respond to the message, please log on to GitHub and use the URL above to go to the specific comment. To unsubscribe, e-mail: [email protected] For queries about this service, please contact Infrastructure at: [email protected]
