Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 5 additions & 2 deletions datafusion/src/logical_plan/dfschema.rs
Original file line number Diff line number Diff line change
Expand Up @@ -289,7 +289,10 @@ impl DFSchema {
fn get_field_names(&self) -> String {
self.fields
.iter()
.map(|f| format!("'{}'", f.name()))
.map(|f| match f.qualifier() {
Some(qualifier) => format!("'{}.{}'", qualifier, f.name()),
None => format!("'{}'", f.name()),
})
.collect::<Vec<_>>()
.join(", ")
}
Expand Down Expand Up @@ -619,7 +622,7 @@ mod tests {
#[test]
fn helpful_error_messages() -> Result<()> {
let schema = DFSchema::try_from_qualified_schema("t1", &test_schema_1())?;
let expected_help = "Valid fields are \'c0\', \'c1\'.";
let expected_help = "Valid fields are \'t1.c0\', \'t1.c1\'.";
assert!(schema
.field_with_qualified_name("x", "y")
.unwrap_err()
Expand Down
39 changes: 38 additions & 1 deletion datafusion/src/sql/planner.rs
Original file line number Diff line number Diff line change
Expand Up @@ -925,11 +925,40 @@ impl<'a, S: ContextProvider> SqlToRel<'a, S> {

/// Generate a relational expression from a SQL expression
pub fn sql_to_rex(&self, sql: &SQLExpr, schema: &DFSchema) -> Result<Expr> {
let expr = self.sql_expr_to_logical_expr(sql, schema)?;
let mut expr = self.sql_expr_to_logical_expr(sql, schema)?;
expr = self.rewrite_partial_qualifier(expr, schema);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I wonder if we should do the rewrite prior to calling validate_schema_satisfies_exprs?

self.validate_schema_satisfies_exprs(schema, &[expr.clone()])?;
Ok(expr)
}

/// Rewrite aliases which are not-complete (e.g. ones that only include only table qualifier in a schema.table qualified relation)
fn rewrite_partial_qualifier(&self, expr: Expr, schema: &DFSchema) -> Expr {
match expr {
Expr::Column(col) => match &col.relation {
Some(q) => {
match schema
.fields()
.iter()
.find(|field| match field.qualifier() {
Some(field_q) => {
field.name() == &col.name
&& field_q.ends_with(&format!(".{}", q))
}
_ => false,
}) {
Some(df_field) => Expr::Column(Column {
relation: df_field.qualifier().cloned(),
name: df_field.name().clone(),
}),
None => Expr::Column(col),
}
}
None => Expr::Column(col),
},
_ => expr,
}
}

fn sql_fn_arg_to_logical_expr(
&self,
sql: &FunctionArg,
Expand Down Expand Up @@ -3388,4 +3417,12 @@ mod tests {
unimplemented!()
}
}

#[test]
fn select_partially_qualified_column() {
let sql = r#"SELECT person.first_name FROM public.person"#;
let expected = "Projection: #public.person.first_name\
\n TableScan: public.person projection=None";
quick_test(sql, expected);
}
}
15 changes: 15 additions & 0 deletions datafusion/tests/sql.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4058,3 +4058,18 @@ fn normalize_vec_for_explain(v: Vec<Vec<String>>) -> Vec<Vec<String>> {
})
.collect::<Vec<_>>()
}

#[tokio::test]
async fn test_partial_qualified_name() -> Result<()> {
let mut ctx = create_join_context("t1_id", "t2_id")?;
let sql = "SELECT t1.t1_id, t1_name FROM public.t1";
let expected = vec![
vec!["11", "a"],
vec!["22", "b"],
vec!["33", "c"],
vec!["44", "d"],
];
let actual = execute(&mut ctx, sql).await;
assert_eq!(expected, actual);
Ok(())
}