Skip to content

Commit

Permalink
make logic more robust
Browse files Browse the repository at this point in the history
  • Loading branch information
andygrove committed Aug 1, 2022
1 parent c66ae96 commit 2f84746
Showing 1 changed file with 20 additions and 13 deletions.
33 changes: 20 additions & 13 deletions datafusion/sql/src/planner.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1720,24 +1720,31 @@ impl<'a, S: ContextProvider> SqlToRel<'a, S> {
(Some(name), Some(relation)) if var_names.is_empty() => {
match schema.field_with_qualified_name(&relation, &name) {
Ok(_) => {
// table.column identifier
// found an exact match on a qualified name so this is a table.column identifier
Ok(Expr::Column(Column {
relation: Some(relation),
name,
}))
},
Err(_) => if let Some(field) = schema.fields().iter().find(|f| f.name().eq(&relation)) {
// Access to a field of a column which is a structure, example: SELECT my_struct.key
Ok(Expr::GetIndexedField {
expr: Box::new(Expr::Column(field.qualified_column())),
key: ScalarValue::Utf8(Some(name)),
})
} else {
// table.column identifier
Ok(Expr::Column(Column {
relation: Some(relation),
name,
}))
Err(e) => {
let search_term = format!(".{}.{}", relation, name);
if schema.field_names().iter().any(|name| name.as_str().ends_with(&search_term)) {
// this could probably be improved but here we handle the case
// where the qualifier is only a partial qualifier such as when
// referencing "t1.foo" when the available field is "public.t1.foo"
Ok(Expr::Column(Column {
relation: Some(relation),
name,
}))
} else if let Some(field) = schema.fields().iter().find(|f| f.name().eq(&relation)) {
// Access to a field of a column which is a structure, example: SELECT my_struct.key
Ok(Expr::GetIndexedField {
expr: Box::new(Expr::Column(field.qualified_column())),
key: ScalarValue::Utf8(Some(name)),
})
} else {
Err(e)
}
}
}
}
Expand Down

0 comments on commit 2f84746

Please sign in to comment.