Apache DataFusion SQL Query Engine
Qualified field resolution too strict (#810)
Mike Seddonb8b046508dba
diff --git a/datafusion/src/logical_plan/dfschema.rs b/datafusion/src/logical_plan/dfschema.rsindex 217e2de6d4..c067b5f963 100644--- a/datafusion/src/logical_plan/dfschema.rs+++ b/datafusion/src/logical_plan/dfschema.rs@@ -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(", ")}@@ -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()diff --git a/datafusion/src/sql/planner.rs b/datafusion/src/sql/planner.rsindex 481f12b968..ef2b634649 100644--- a/datafusion/src/sql/planner.rs+++ b/datafusion/src/sql/planner.rs@@ -992,11 +992,40 @@ impl<'a, S: ContextProvider> SqlToRel<'a, S> {/// Generate a relational expression from a SQL expressionpub 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);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,@@ -3490,4 +3519,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);+ }}diff --git a/datafusion/tests/sql.rs b/datafusion/tests/sql.rsindex 3a83f205aa..379cad6233 100644--- a/datafusion/tests/sql.rs+++ b/datafusion/tests/sql.rs@@ -4210,3 +4210,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(())+}