Skip to content

Commit a9edd8a

Browse files
authored
Fix errors in the sql feature (#124)
1 parent 0f9ffbb commit a9edd8a

File tree

5 files changed

+54
-32
lines changed

5 files changed

+54
-32
lines changed

datafusion-federation/src/sql/analyzer.rs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
use std::{collections::HashMap, sync::Arc};
22

33
use datafusion::{
4-
common::Column,
4+
common::{Column, Spans},
55
logical_expr::{
66
expr::{
77
AggregateFunction, AggregateFunctionParams, Alias, Exists, InList, InSubquery,
@@ -192,6 +192,7 @@ fn rewrite_table_scans_in_expr(
192192
Ok(Expr::ScalarSubquery(Subquery {
193193
subquery: Arc::new(new_subquery),
194194
outer_ref_columns,
195+
spans: Spans::new(),
195196
}))
196197
}
197198
Expr::BinaryExpr(binary_expr) => {
@@ -465,6 +466,7 @@ fn rewrite_table_scans_in_expr(
465466
let subquery = Subquery {
466467
subquery: Arc::new(subquery_plan),
467468
outer_ref_columns,
469+
spans: Spans::new(),
468470
};
469471
Ok(Expr::Exists(Exists::new(subquery, exists.negated)))
470472
}
@@ -480,6 +482,7 @@ fn rewrite_table_scans_in_expr(
480482
let subquery = Subquery {
481483
subquery: Arc::new(subquery_plan),
482484
outer_ref_columns,
485+
spans: Spans::new(),
483486
};
484487
Ok(Expr::InSubquery(InSubquery::new(
485488
Box::new(expr),

datafusion-federation/src/sql/ast_analyzer.rs

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ pub fn replace_table_args_analyzer(mut visitor: TableArgReplace) -> AstAnalyzer
2828
/// let mut analyzer = TableArgReplace::default().with(
2929
/// TableReference::parse_str("table1"),
3030
/// vec![FunctionArg::Unnamed(
31-
/// Expr::Value(
31+
/// Expr::value(
3232
/// Value::Number("1".to_string(), false),
3333
/// )
3434
/// .into(),
@@ -108,9 +108,18 @@ impl VisitorMut for TableArgReplace {
108108
}
109109

110110
fn name_to_table_reference(name: &ObjectName) -> TableReference {
111-
let first = name.0.first().map(|n| n.value.to_string());
112-
let second = name.0.get(1).map(|n| n.value.to_string());
113-
let third = name.0.get(2).map(|n| n.value.to_string());
111+
let first = name
112+
.0
113+
.first()
114+
.map(|n| n.as_ident().expect("expected Ident").value.to_string());
115+
let second = name
116+
.0
117+
.get(1)
118+
.map(|n| n.as_ident().expect("expected Ident").value.to_string());
119+
let third = name
120+
.0
121+
.get(2)
122+
.map(|n| n.as_ident().expect("expected Ident").value.to_string());
114123

115124
match (first, second, third) {
116125
(Some(first), Some(second), Some(third)) => TableReference::full(first, second, third),

datafusion-federation/src/sql/mod.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -505,7 +505,7 @@ mod tests {
505505
let expected = vec![
506506
"SELECT table_a1.a, table_a1.b, table_a1.c FROM table_a1",
507507
"SELECT table_a2.a, table_a2.b, table_a2.c FROM table_a2",
508-
"SELECT table_b1.a, table_b1.b, table_b1.c FROM table_b1(1)",
508+
"SELECT table_b1.a, table_b1.b, table_b1.c FROM table_b1(1) AS table_b1",
509509
];
510510

511511
assert_eq!(
@@ -593,7 +593,7 @@ mod tests {
593593
});
594594

595595
let expected = vec![
596-
r#"SELECT "table".a, "table".b, "table".c FROM "default"."table" UNION ALL SELECT "Table".a, "Table".b, "Table".c FROM "default"."Table"(1)"#,
596+
r#"SELECT "table".a, "table".b, "table".c FROM "default"."table" UNION ALL SELECT "Table".a, "Table".b, "Table".c FROM "default"."Table"(1) AS Table"#,
597597
];
598598

599599
assert_eq!(

datafusion-federation/src/sql/table.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ impl RemoteTable {
5050
/// Creates a new `RemoteTable` instance.
5151
///
5252
/// Examples:
53-
/// ```rust
53+
/// ```ignore
5454
/// use datafusion::sql::TableReference;
5555
///
5656
/// RemoteTable::new("myschema.table".try_into()?, schema);

datafusion-federation/src/sql/table_reference.rs

Lines changed: 34 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ use datafusion::{
55
sql::{
66
sqlparser::{
77
self,
8-
ast::FunctionArg,
8+
ast::{FunctionArg, ObjectNamePart},
99
dialect::{Dialect, GenericDialect},
1010
tokenizer::Token,
1111
},
@@ -15,14 +15,16 @@ use datafusion::{
1515

1616
/// A multipart identifier to a remote table, view or parameterized view.
1717
///
18-
/// RemoteTableRef can be created by parsing from a string represeting a table obbject with optional
18+
/// RemoteTableRef can be created by parsing from a string representing a table object with optional
1919
/// ```rust
20+
/// use datafusion_federation::sql::RemoteTableRef;
21+
/// use datafusion::sql::sqlparser::dialect::PostgreSqlDialect;
2022
///
2123
/// RemoteTableRef::try_from("myschema.table");
2224
/// RemoteTableRef::try_from(r#"myschema."Table""#);
2325
/// RemoteTableRef::try_from("myschema.view('obj')");
2426
///
25-
/// RemoteTableRef::parse_with_dialect("myschema.view(name = 'obj')", &PostgresSqlDialect {});
27+
/// RemoteTableRef::parse_with_dialect("myschema.view(name = 'obj')", &PostgreSqlDialect {});
2628
/// ```
2729
#[derive(Debug, Clone, PartialEq, Eq, Hash)]
2830
pub struct RemoteTableRef {
@@ -41,7 +43,7 @@ impl RemoteTableRef {
4143
Self::parse_with_dialect(s, &GenericDialect {})
4244
}
4345

44-
/// Create new using a specfic instance of dialect.
46+
/// Create new using a specific instance of dialect.
4547
pub fn parse_with_dialect(s: &str, dialect: &dyn Dialect) -> Result<Self, DataFusionError> {
4648
let mut parser = sqlparser::parser::Parser::new(dialect).try_with_sql(s)?;
4749
let name = parser.parse_object_name(true)?;
@@ -52,15 +54,23 @@ impl RemoteTableRef {
5254
};
5355

5456
let table_ref = match (name.0.first(), name.0.get(1), name.0.get(2)) {
55-
(Some(catalog), Some(schema), Some(table)) => TableReference::full(
57+
(
58+
Some(ObjectNamePart::Identifier(catalog)),
59+
Some(ObjectNamePart::Identifier(schema)),
60+
Some(ObjectNamePart::Identifier(table)),
61+
) => TableReference::full(
5662
catalog.value.clone(),
5763
schema.value.clone(),
5864
table.value.clone(),
5965
),
60-
(Some(schema), Some(table), None) => {
61-
TableReference::partial(schema.value.clone(), table.value.clone())
66+
(
67+
Some(ObjectNamePart::Identifier(schema)),
68+
Some(ObjectNamePart::Identifier(table)),
69+
None,
70+
) => TableReference::partial(schema.value.clone(), table.value.clone()),
71+
(Some(ObjectNamePart::Identifier(table)), None, None) => {
72+
TableReference::bare(table.value.clone())
6273
}
63-
(Some(table), None, None) => TableReference::bare(table.value.clone()),
6474
_ => {
6575
return Err(DataFusionError::NotImplemented(
6676
"Unable to parse string into TableReference".to_string(),
@@ -166,8 +176,8 @@ mod tests {
166176
let expected = RemoteTableRef::from((
167177
TableReference::bare("table"),
168178
vec![
169-
FunctionArg::Unnamed(Expr::Value(Value::Number("1".to_string(), false)).into()),
170-
FunctionArg::Unnamed(Expr::Value(Value::Number("2".to_string(), false)).into()),
179+
FunctionArg::Unnamed(Expr::value(Value::Number("1".to_string(), false)).into()),
180+
FunctionArg::Unnamed(Expr::value(Value::Number("2".to_string(), false)).into()),
171181
],
172182
));
173183
assert_eq!(table_ref, expected);
@@ -176,8 +186,8 @@ mod tests {
176186
let expected = RemoteTableRef::from((
177187
TableReference::bare("Table"),
178188
vec![
179-
FunctionArg::Unnamed(Expr::Value(Value::Number("1".to_string(), false)).into()),
180-
FunctionArg::Unnamed(Expr::Value(Value::Number("2".to_string(), false)).into()),
189+
FunctionArg::Unnamed(Expr::value(Value::Number("1".to_string(), false)).into()),
190+
FunctionArg::Unnamed(Expr::value(Value::Number("2".to_string(), false)).into()),
181191
],
182192
));
183193
assert_eq!(table_ref, expected);
@@ -189,8 +199,8 @@ mod tests {
189199
let expected = RemoteTableRef::from((
190200
TableReference::bare("table"),
191201
vec![
192-
FunctionArg::Unnamed(Expr::Value(Value::Number("1".to_string(), false)).into()),
193-
FunctionArg::Unnamed(Expr::Value(Value::Number("2".to_string(), false)).into()),
202+
FunctionArg::Unnamed(Expr::value(Value::Number("1".to_string(), false)).into()),
203+
FunctionArg::Unnamed(Expr::value(Value::Number("2".to_string(), false)).into()),
194204
],
195205
));
196206
assert_eq!(table_ref, expected);
@@ -199,8 +209,8 @@ mod tests {
199209
let expected = RemoteTableRef::from((
200210
TableReference::bare("Table"),
201211
vec![
202-
FunctionArg::Unnamed(Expr::Value(Value::Number("1".to_string(), false)).into()),
203-
FunctionArg::Unnamed(Expr::Value(Value::Number("2".to_string(), false)).into()),
212+
FunctionArg::Unnamed(Expr::value(Value::Number("1".to_string(), false)).into()),
213+
FunctionArg::Unnamed(Expr::value(Value::Number("2".to_string(), false)).into()),
204214
],
205215
));
206216
assert_eq!(table_ref, expected);
@@ -223,8 +233,8 @@ mod tests {
223233
let expected = RemoteTableRef::from((
224234
TableReference::partial("schema", "table"),
225235
vec![
226-
FunctionArg::Unnamed(Expr::Value(Value::Number("1".to_string(), false)).into()),
227-
FunctionArg::Unnamed(Expr::Value(Value::Number("2".to_string(), false)).into()),
236+
FunctionArg::Unnamed(Expr::value(Value::Number("1".to_string(), false)).into()),
237+
FunctionArg::Unnamed(Expr::value(Value::Number("2".to_string(), false)).into()),
228238
],
229239
));
230240
assert_eq!(table_ref, expected);
@@ -233,8 +243,8 @@ mod tests {
233243
let expected = RemoteTableRef::from((
234244
TableReference::partial("schema", "Table"),
235245
vec![
236-
FunctionArg::Unnamed(Expr::Value(Value::Number("1".to_string(), false)).into()),
237-
FunctionArg::Unnamed(Expr::Value(Value::Number("2".to_string(), false)).into()),
246+
FunctionArg::Unnamed(Expr::value(Value::Number("1".to_string(), false)).into()),
247+
FunctionArg::Unnamed(Expr::value(Value::Number("2".to_string(), false)).into()),
238248
],
239249
));
240250
assert_eq!(table_ref, expected);
@@ -246,8 +256,8 @@ mod tests {
246256
let expected = RemoteTableRef::from((
247257
TableReference::partial("schema", "table"),
248258
vec![
249-
FunctionArg::Unnamed(Expr::Value(Value::Number("1".to_string(), false)).into()),
250-
FunctionArg::Unnamed(Expr::Value(Value::Number("2".to_string(), false)).into()),
259+
FunctionArg::Unnamed(Expr::value(Value::Number("1".to_string(), false)).into()),
260+
FunctionArg::Unnamed(Expr::value(Value::Number("2".to_string(), false)).into()),
251261
],
252262
));
253263
assert_eq!(table_ref, expected);
@@ -265,12 +275,12 @@ mod tests {
265275
vec![
266276
FunctionArg::ExprNamed {
267277
name: ast::Expr::Identifier(Ident::new("user_id")),
268-
arg: Expr::Value(Value::Number("1".to_string(), false)).into(),
278+
arg: Expr::value(Value::Number("1".to_string(), false)).into(),
269279
operator: FunctionArgOperator::RightArrow,
270280
},
271281
FunctionArg::ExprNamed {
272282
name: ast::Expr::Identifier(Ident::new("age")),
273-
arg: Expr::Value(Value::Number("2".to_string(), false)).into(),
283+
arg: Expr::value(Value::Number("2".to_string(), false)).into(),
274284
operator: FunctionArgOperator::RightArrow,
275285
},
276286
],

0 commit comments

Comments
 (0)