alamb commented on code in PR #12467:
URL: https://github.com/apache/datafusion/pull/12467#discussion_r1761723046


##########
datafusion/sql/src/expr/identifier.rs:
##########
@@ -186,7 +186,22 @@ impl<'a, S: ContextProvider> SqlToRel<'a, S> {
                             let s = &ids[0..ids.len()];
                             // safe unwrap as s can never be empty or exceed 
the bounds
                             let (relation, column_name) = 
form_identifier(s).unwrap();
-                            Ok(Expr::Column(Column::new(relation, 
column_name)))
+                            // sanity check on column
+                            match schema

Review Comment:
   I think we are trying to avoid matching on `Err` and converting to `Ok` 
because each `Err` results in at least one `String` allocation (for the error 
message)
   
   Since this code is potentially invoked many times during planning the 
allocations can add up and slow down planning
   
   Is there any way to use a method that doesn't return `Err` here?



##########
datafusion/common/src/dfschema.rs:
##########
@@ -467,6 +464,36 @@ impl DFSchema {
             .collect()
     }
 
+    /// Find all fields that match the given name with qualifier and return 
them with their qualifier
+    pub fn qualified_fields_with_qualified_name(
+        &self,
+        qualifier: &TableReference,
+        name: &str,
+    ) -> Vec<(Option<&TableReference>, &Field)> {
+        self.iter()
+            .filter(|(q, f)| match (qualifier, q) {
+                // field to lookup is qualified.
+                // current field is qualified and not shared between 
relations, compare both
+                // qualifier and name.
+                (q, Some(field_q)) => q.resolved_eq(field_q) && f.name() == 
name,
+                // field to lookup is qualified but current field is 
unqualified.
+                (qq, None) => {
+                    // the original field may now be aliased with a name that 
matches the
+                    // original qualified name
+                    let column = Column::from_qualified_name(f.name());
+                    match column {
+                        Column {
+                            relation: Some(r),
+                            name: column_name,
+                        } => &r == qq && column_name == name,
+                        _ => false,
+                    }

Review Comment:
   Another way to express this logic I would find clearer would be
   
   ```suggestion
                       if let Some(r) = column.relation.as_ref() {
                           r == qq && column.name == name
                       } else {
                           false
                       }
   ```
   
   (though this is fine too)



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]


---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]

Reply via email to