jonahgao commented on code in PR #11562:
URL: https://github.com/apache/datafusion/pull/11562#discussion_r1687702567


##########
datafusion/sql/src/expr/identifier.rs:
##########
@@ -47,40 +47,58 @@ impl<'a, S: ContextProvider> SqlToRel<'a, S> {
             // compound identifiers, but this is not a compound
             // identifier. (e.g. it is "foo.bar" not foo.bar)
             let normalize_ident = self.normalizer.normalize(id);
-            match schema.field_with_unqualified_name(normalize_ident.as_str()) 
{
-                Ok(_) => {
-                    // found a match without a qualified name, this is a inner 
table column
-                    Ok(Expr::Column(Column {
-                        relation: None,
+
+            // Check for qualified field with unqualified name
+            if let Ok((Some(qualifier), _)) =
+                
schema.qualified_field_with_unqualified_name(normalize_ident.as_str())
+            {
+                let is_unnamed_table = match &qualifier {
+                    TableReference::Bare { table } => table.as_ref() == 
UNNAMED_TABLE,
+                    TableReference::Partial { table, .. } => {
+                        table.as_ref() == UNNAMED_TABLE
+                    }
+                    TableReference::Full { table, .. } => table.as_ref() == 
UNNAMED_TABLE,
+                };
+
+                if !is_unnamed_table {
+                    // Found a match with a qualified name, return it with the 
qualifier
+                    return Ok(Expr::Column(Column {
+                        relation: Some(qualifier.clone()),
                         name: normalize_ident,
-                    }))
+                    }));
                 }
-                Err(_) => {
-                    // check the outer_query_schema and try to find a match
-                    if let Some(outer) = planner_context.outer_query_schema() {
-                        match outer.qualified_field_with_unqualified_name(
-                            normalize_ident.as_str(),
-                        ) {
-                            Ok((qualifier, field)) => {
-                                // found an exact match on a qualified name in 
the outer plan schema, so this is an outer reference column
-                                Ok(Expr::OuterReferenceColumn(
-                                    field.data_type().clone(),
-                                    Column::from((qualifier, field)),
-                                ))
-                            }
-                            Err(_) => Ok(Expr::Column(Column {
-                                relation: None,
-                                name: normalize_ident,
-                            })),
-                        }
-                    } else {
-                        Ok(Expr::Column(Column {
-                            relation: None,
-                            name: normalize_ident,
-                        }))
-                    }
+            }
+
+            // Check for unqualified field
+            if schema
+                .field_with_unqualified_name(normalize_ident.as_str())

Review Comment:
   As mentioned above, this should be unnecessary



##########
datafusion/sql/src/expr/identifier.rs:
##########
@@ -47,40 +47,58 @@ impl<'a, S: ContextProvider> SqlToRel<'a, S> {
             // compound identifiers, but this is not a compound
             // identifier. (e.g. it is "foo.bar" not foo.bar)
             let normalize_ident = self.normalizer.normalize(id);
-            match schema.field_with_unqualified_name(normalize_ident.as_str()) 
{
-                Ok(_) => {
-                    // found a match without a qualified name, this is a inner 
table column
-                    Ok(Expr::Column(Column {
-                        relation: None,
+
+            // Check for qualified field with unqualified name
+            if let Ok((Some(qualifier), _)) =
+                
schema.qualified_field_with_unqualified_name(normalize_ident.as_str())

Review Comment:
   I think it can be simplified to
   ```rust
               if let Ok((qualifier, _)) =
                   
schema.qualified_field_with_unqualified_name(normalize_ident.as_str())
               {
                   return Ok(Expr::Column(Column {
                       relation: qualifier.filter(|q| q.table() != 
UNNAMED_TABLE).cloned(),
                       name: normalize_ident,
                   }));
               }
   ```
   And we don't need to check again for unqualified field.  
   `qualified_field_with_unqualified_name()` also returns unqualified fields.



-- 
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: github-unsubscr...@datafusion.apache.org

For queries about this service, please contact Infrastructure at:
us...@infra.apache.org


---------------------------------------------------------------------
To unsubscribe, e-mail: github-unsubscr...@datafusion.apache.org
For additional commands, e-mail: github-h...@datafusion.apache.org

Reply via email to