alamb commented on code in PR #3887:
URL: https://github.com/apache/arrow-rs/pull/3887#discussion_r1143137588
##########
arrow-flight/src/sql/server.rs:
##########
@@ -315,90 +315,56 @@ where
let message =
Any::decode(&*request.get_ref().cmd).map_err(decode_error_to_status)?;
- if message.is::<CommandStatementQuery>() {
- let token = message
- .unpack()
- .map_err(arrow_error_to_status)?
- .expect("unreachable");
- return self.get_flight_info_statement(token, request).await;
- }
- if message.is::<CommandPreparedStatementQuery>() {
- let handle = message
- .unpack()
- .map_err(arrow_error_to_status)?
- .expect("unreachable");
- return self
- .get_flight_info_prepared_statement(handle, request)
- .await;
- }
- if message.is::<CommandGetCatalogs>() {
- let token = message
- .unpack()
- .map_err(arrow_error_to_status)?
- .expect("unreachable");
- return self.get_flight_info_catalogs(token, request).await;
- }
- if message.is::<CommandGetDbSchemas>() {
- let token = message
- .unpack()
- .map_err(arrow_error_to_status)?
- .expect("unreachable");
- return self.get_flight_info_schemas(token, request).await;
- }
- if message.is::<CommandGetTables>() {
- let token = message
- .unpack()
- .map_err(arrow_error_to_status)?
- .expect("unreachable");
- return self.get_flight_info_tables(token, request).await;
- }
- if message.is::<CommandGetTableTypes>() {
- let token = message
- .unpack()
- .map_err(arrow_error_to_status)?
- .expect("unreachable");
- return self.get_flight_info_table_types(token, request).await;
+ let command =
Command::try_from(message).map_err(arrow_error_to_status)?;
+
+ match command {
+ Command::CommandStatementQuery(token) => {
+ self.get_flight_info_statement(token, request).await
+ }
+ Command::CommandPreparedStatementQuery(handle) => {
+ self.get_flight_info_prepared_statement(handle, request)
+ .await
+ }
+ Command::CommandGetCatalogs(token) => {
+ self.get_flight_info_catalogs(token, request).await
+ }
+ Command::CommandGetDbSchemas(token) => {
+ return self.get_flight_info_schemas(token, request).await
+ }
+ Command::CommandGetTables(token) => {
+ self.get_flight_info_tables(token, request).await
+ }
+ Command::CommandGetTableTypes(token) => {
+ self.get_flight_info_table_types(token, request).await
+ }
+
+ Command::CommandGetSqlInfo(token) => {
+ self.get_flight_info_sql_info(token, request).await
+ }
+ Command::CommandGetPrimaryKeys(token) => {
+ self.get_flight_info_primary_keys(token, request).await
+ }
+ Command::CommandGetExportedKeys(token) => {
+ self.get_flight_info_exported_keys(token, request).await
+ }
+ Command::CommandGetImportedKeys(token) => {
+ self.get_flight_info_imported_keys(token, request).await
+ }
+ Command::CommandGetCrossReference(token) => {
+ self.get_flight_info_cross_reference(token, request).await
+ }
+ Command::ActionClosePreparedStatementRequest(_)
+ | Command::ActionCreatePreparedStatementRequest(_)
+ | Command::CommandPreparedStatementUpdate(_)
+ | Command::CommandStatementUpdate(_)
+ | Command::DoPutUpdateResult(_)
+ | Command::TicketStatementQuery(_)
+ | Command::ActionCreatePreparedStatementResult(_) => {
+ Err(Status::unimplemented(format!(
+ "get_flight_info: The defined request is invalid:
{command:?}",
Review Comment:
```suggestion
"get_flight_info: The defined request is invalid: {}",
command.type_url(),
```
##########
arrow-flight/src/sql/mod.rs:
##########
@@ -71,22 +72,104 @@ pub trait ProstMessageExt: prost::Message + Default {
fn as_any(&self) -> Any;
}
+/// Macro to coerce a token to an item, specifically
+/// to build the `Commands` enum.
+///
+/// See: <https://danielkeep.github.io/tlborm/book/blk-ast-coercion.html>
+macro_rules! as_item {
+ ($i:item) => {
+ $i
+ };
+}
+
macro_rules! prost_message_ext {
- ($($name:ty,)*) => {
- $(
- impl ProstMessageExt for $name {
- fn type_url() -> &'static str {
- concat!("type.googleapis.com/arrow.flight.protocol.sql.",
stringify!($name))
+ ($($name:tt,)*) => {
+ paste! {
+ $(
+ const [<$name:snake:upper _TYPE_URL>]: &'static str =
concat!("type.googleapis.com/arrow.flight.protocol.sql.", stringify!($name));
+ )*
+
+ as_item! {
+ /// Helper to convert to/from protobuf [`Any`] to a strongly
typed enum.
+ ///
+ /// # Example
+ /// ```rust
+ /// # use arrow_flight::sql::{Any, CommandStatementQuery,
Command};
+ /// let flightsql_message = CommandStatementQuery {
+ /// query: "SELECT * FROM foo".to_string(),
+ /// };
+ ///
+ /// // Given a packed FlightSQL Any message
+ /// let any_message = Any::pack(&flightsql_message).unwrap();
+ ///
+ /// // decode it to Commands:
+ /// match Command::try_from(any_message).unwrap() {
+ /// Command::CommandStatementQuery(decoded) => {
+ /// assert_eq!(flightsql_message, decoded);
+ /// }
+ /// _ => panic!("Unexpected decoded message"),
+ /// }
+ /// ```
+ #[derive(Clone, Debug, PartialEq)]
+ pub enum Command {
+ $($name($name),)*
+ }
+ }
+
+ impl Command {
+ /// Convert the command to [`Any`].
+ pub fn into_any(self) -> Any {
+ match self {
+ $(
+ Self::$name(cmd) => cmd.as_any(),
+ )*
+ }
}
- fn as_any(&self) -> Any {
- Any {
- type_url: <$name>::type_url().to_string(),
- value: self.encode_to_vec().into(),
+ /// Get the URL for the command.
Review Comment:
👍 this is great
--
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]