forked from apache/arrow-rs
-
Notifications
You must be signed in to change notification settings - Fork 0
/
mod.rs
177 lines (159 loc) · 5.7 KB
/
mod.rs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
// 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_schema::ArrowError;
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: 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 {
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) -> Result<Option<M>, ArrowError>;
/// Pack any message into `prost_types::Any` value.
fn pack<M: ProstMessageExt>(message: &M) -> Result<prost_types::Any, ArrowError>;
}
impl ProstAnyExt for prost_types::Any {
fn is<M: ProstMessageExt>(&self) -> bool {
M::type_url() == self.type_url
}
fn unpack<M: ProstMessageExt>(&self) -> Result<Option<M>, ArrowError> {
if !self.is::<M>() {
return Ok(None);
}
let m = 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) -> Result<prost_types::Any, ArrowError> {
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() {
let query = CommandStatementQuery {
query: "select 1".to_string(),
};
let any = prost_types::Any::pack(&query).unwrap();
assert!(any.is::<CommandStatementQuery>());
let unpack_query: CommandStatementQuery = any.unpack().unwrap().unwrap();
assert_eq!(query, unpack_query);
}
}