forked from datafusion-contrib/datafusion-federation
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtable_reference.rs
More file actions
290 lines (261 loc) · 10.2 KB
/
table_reference.rs
File metadata and controls
290 lines (261 loc) · 10.2 KB
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
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
use std::sync::Arc;
use datafusion::{
error::DataFusionError,
sql::{
sqlparser::{
self,
ast::{FunctionArg, ObjectNamePart},
dialect::{Dialect, GenericDialect},
tokenizer::Token,
},
TableReference,
},
};
/// A multipart identifier to a remote table, view or parameterized view.
///
/// RemoteTableRef can be created by parsing from a string representing a table object with optional
/// ```rust
/// use datafusion_federation::sql::RemoteTableRef;
/// use datafusion::sql::sqlparser::dialect::PostgreSqlDialect;
///
/// RemoteTableRef::try_from("myschema.table");
/// RemoteTableRef::try_from(r#"myschema."Table""#);
/// RemoteTableRef::try_from("myschema.view('obj')");
///
/// RemoteTableRef::parse_with_dialect("myschema.view(name = 'obj')", &PostgreSqlDialect {});
/// ```
#[derive(Debug, Clone, PartialEq, Eq, Hash)]
pub struct RemoteTableRef {
pub table_ref: TableReference,
pub args: Option<Arc<[FunctionArg]>>,
}
impl RemoteTableRef {
/// Get quoted_string representation for the table it is referencing, this is same as calling to_quoted_string on the inner table reference.
pub fn to_quoted_string(&self) -> String {
self.table_ref.to_quoted_string()
}
/// Create new using general purpose dialect. Prefer [`Self::parse_with_dialect`] if the dialect is known beforehand
pub fn parse_with_default_dialect(s: &str) -> Result<Self, DataFusionError> {
Self::parse_with_dialect(s, &GenericDialect {})
}
/// Create new using a specific instance of dialect.
pub fn parse_with_dialect(s: &str, dialect: &dyn Dialect) -> Result<Self, DataFusionError> {
let mut parser = sqlparser::parser::Parser::new(dialect).try_with_sql(s)?;
let name = parser.parse_object_name(true)?;
let args = if parser.consume_token(&Token::LParen) {
parser.parse_optional_args()?
} else {
vec![]
};
let table_ref = match (name.0.first(), name.0.get(1), name.0.get(2)) {
(
Some(ObjectNamePart::Identifier(catalog)),
Some(ObjectNamePart::Identifier(schema)),
Some(ObjectNamePart::Identifier(table)),
) => TableReference::full(
catalog.value.clone(),
schema.value.clone(),
table.value.clone(),
),
(
Some(ObjectNamePart::Identifier(schema)),
Some(ObjectNamePart::Identifier(table)),
None,
) => TableReference::partial(schema.value.clone(), table.value.clone()),
(Some(ObjectNamePart::Identifier(table)), None, None) => {
TableReference::bare(table.value.clone())
}
_ => {
return Err(DataFusionError::NotImplemented(
"Unable to parse string into TableReference".to_string(),
))
}
};
if !args.is_empty() {
Ok(RemoteTableRef {
table_ref,
args: Some(args.into()),
})
} else {
Ok(RemoteTableRef {
table_ref,
args: None,
})
}
}
pub fn table_ref(&self) -> &TableReference {
&self.table_ref
}
pub fn args(&self) -> Option<&[FunctionArg]> {
self.args.as_deref()
}
}
impl From<TableReference> for RemoteTableRef {
fn from(table_ref: TableReference) -> Self {
RemoteTableRef {
table_ref,
args: None,
}
}
}
impl From<RemoteTableRef> for TableReference {
fn from(remote_table_ref: RemoteTableRef) -> Self {
remote_table_ref.table_ref
}
}
impl From<&RemoteTableRef> for TableReference {
fn from(remote_table_ref: &RemoteTableRef) -> Self {
remote_table_ref.table_ref.clone()
}
}
impl From<(TableReference, Vec<FunctionArg>)> for RemoteTableRef {
fn from((table_ref, args): (TableReference, Vec<FunctionArg>)) -> Self {
RemoteTableRef {
table_ref,
args: Some(args.into()),
}
}
}
impl TryFrom<&str> for RemoteTableRef {
type Error = DataFusionError;
fn try_from(s: &str) -> Result<Self, Self::Error> {
Self::parse_with_default_dialect(s)
}
}
impl TryFrom<String> for RemoteTableRef {
type Error = DataFusionError;
fn try_from(s: String) -> Result<Self, Self::Error> {
Self::parse_with_default_dialect(&s)
}
}
impl TryFrom<&String> for RemoteTableRef {
type Error = DataFusionError;
fn try_from(s: &String) -> Result<Self, Self::Error> {
Self::parse_with_default_dialect(s)
}
}
#[cfg(test)]
mod tests {
use super::*;
use sqlparser::{
ast::{self, Expr, FunctionArgOperator, Ident, Value},
dialect,
};
#[test]
fn bare_table_reference() {
let table_ref = RemoteTableRef::parse_with_default_dialect("table").unwrap();
let expected = RemoteTableRef::from(TableReference::bare("table"));
assert_eq!(table_ref, expected);
let table_ref = RemoteTableRef::parse_with_default_dialect("Table").unwrap();
let expected = RemoteTableRef::from(TableReference::bare("Table"));
assert_eq!(table_ref, expected);
}
#[test]
fn bare_table_reference_with_args() {
let table_ref = RemoteTableRef::parse_with_default_dialect("table(1, 2)").unwrap();
let expected = RemoteTableRef::from((
TableReference::bare("table"),
vec![
FunctionArg::Unnamed(Expr::value(Value::Number("1".to_string(), false)).into()),
FunctionArg::Unnamed(Expr::value(Value::Number("2".to_string(), false)).into()),
],
));
assert_eq!(table_ref, expected);
let table_ref = RemoteTableRef::parse_with_default_dialect("Table(1, 2)").unwrap();
let expected = RemoteTableRef::from((
TableReference::bare("Table"),
vec![
FunctionArg::Unnamed(Expr::value(Value::Number("1".to_string(), false)).into()),
FunctionArg::Unnamed(Expr::value(Value::Number("2".to_string(), false)).into()),
],
));
assert_eq!(table_ref, expected);
}
#[test]
fn bare_table_reference_with_args_and_whitespace() {
let table_ref = RemoteTableRef::parse_with_default_dialect("table (1, 2)").unwrap();
let expected = RemoteTableRef::from((
TableReference::bare("table"),
vec![
FunctionArg::Unnamed(Expr::value(Value::Number("1".to_string(), false)).into()),
FunctionArg::Unnamed(Expr::value(Value::Number("2".to_string(), false)).into()),
],
));
assert_eq!(table_ref, expected);
let table_ref = RemoteTableRef::parse_with_default_dialect("Table (1, 2)").unwrap();
let expected = RemoteTableRef::from((
TableReference::bare("Table"),
vec![
FunctionArg::Unnamed(Expr::value(Value::Number("1".to_string(), false)).into()),
FunctionArg::Unnamed(Expr::value(Value::Number("2".to_string(), false)).into()),
],
));
assert_eq!(table_ref, expected);
}
#[test]
fn multi_table_reference_with_no_args() {
let table_ref = RemoteTableRef::parse_with_default_dialect("schema.table").unwrap();
let expected = RemoteTableRef::from(TableReference::partial("schema", "table"));
assert_eq!(table_ref, expected);
let table_ref = RemoteTableRef::parse_with_default_dialect("schema.Table").unwrap();
let expected = RemoteTableRef::from(TableReference::partial("schema", "Table"));
assert_eq!(table_ref, expected);
}
#[test]
fn multi_table_reference_with_args() {
let table_ref = RemoteTableRef::parse_with_default_dialect("schema.table(1, 2)").unwrap();
let expected = RemoteTableRef::from((
TableReference::partial("schema", "table"),
vec![
FunctionArg::Unnamed(Expr::value(Value::Number("1".to_string(), false)).into()),
FunctionArg::Unnamed(Expr::value(Value::Number("2".to_string(), false)).into()),
],
));
assert_eq!(table_ref, expected);
let table_ref = RemoteTableRef::parse_with_default_dialect("schema.Table(1, 2)").unwrap();
let expected = RemoteTableRef::from((
TableReference::partial("schema", "Table"),
vec![
FunctionArg::Unnamed(Expr::value(Value::Number("1".to_string(), false)).into()),
FunctionArg::Unnamed(Expr::value(Value::Number("2".to_string(), false)).into()),
],
));
assert_eq!(table_ref, expected);
}
#[test]
fn multi_table_reference_with_args_and_whitespace() {
let table_ref = RemoteTableRef::parse_with_default_dialect("schema.table (1, 2)").unwrap();
let expected = RemoteTableRef::from((
TableReference::partial("schema", "table"),
vec![
FunctionArg::Unnamed(Expr::value(Value::Number("1".to_string(), false)).into()),
FunctionArg::Unnamed(Expr::value(Value::Number("2".to_string(), false)).into()),
],
));
assert_eq!(table_ref, expected);
}
#[test]
fn bare_reference_with_named_args() {
let table_ref = RemoteTableRef::parse_with_dialect(
"Table (user_id => 1, age => 2)",
&dialect::PostgreSqlDialect {},
)
.unwrap();
let expected = RemoteTableRef::from((
TableReference::bare("Table"),
vec![
FunctionArg::ExprNamed {
name: ast::Expr::Identifier(Ident::new("user_id")),
arg: Expr::value(Value::Number("1".to_string(), false)).into(),
operator: FunctionArgOperator::RightArrow,
},
FunctionArg::ExprNamed {
name: ast::Expr::Identifier(Ident::new("age")),
arg: Expr::value(Value::Number("2".to_string(), false)).into(),
operator: FunctionArgOperator::RightArrow,
},
],
));
assert_eq!(table_ref, expected);
}
}