alamb commented on a change in pull request #1067:
URL: https://github.com/apache/arrow-datafusion/pull/1067#discussion_r724335933



##########
File path: datafusion/src/execution/context.rs
##########
@@ -2090,7 +2090,7 @@ mod tests {
         let results = plan_and_collect(
             &mut ctx,
             "SELECT * FROM t as t1  \
-             JOIN (SELECT * FROM t as t2) \
+             JOIN (SELECT * FROM t) as t2 \

Review comment:
       👍 

##########
File path: datafusion/src/logical_plan/builder.rs
##########
@@ -231,34 +231,26 @@ impl LogicalPlanBuilder {
         Ok(Self::from(table_scan))
     }
 
-    /// Apply a projection.
-    ///
-    /// # Errors
-    /// This function errors under any of the following conditions:
-    /// * Two or more expressions have the same name
-    /// * An invalid expression is used (e.g. a `sort` expression)
+    /// Apply a projection without alias.
     pub fn project(&self, expr: impl IntoIterator<Item = Expr>) -> 
Result<Self> {
-        let input_schema = self.plan.schema();
-        let mut projected_expr = vec![];
-        for e in expr {
-            match e {
-                Expr::Wildcard => {
-                    projected_expr.extend(expand_wildcard(input_schema, 
&self.plan)?)
-                }
-                _ => projected_expr
-                    .push(columnize_expr(normalize_col(e, &self.plan)?, 
input_schema)),
-            }
-        }
-
-        validate_unique_names("Projections", projected_expr.iter(), 
input_schema)?;
-
-        let schema = DFSchema::new(exprlist_to_fields(&projected_expr, 
input_schema)?)?;
+        Ok(Self::from(project_with_alias(

Review comment:
       I wonder if you could write this like `self.project_with_alias(expr, 
None)` which might be slightly cleaner

##########
File path: datafusion/src/logical_plan/builder.rs
##########
@@ -610,6 +599,41 @@ pub fn union_with_alias(
     })
 }
 
+/// Project with optional alias
+/// # Errors
+/// This function errors under any of the following conditions:
+/// * Two or more expressions have the same name
+/// * An invalid expression is used (e.g. a `sort` expression)
+pub fn project_with_alias(
+    plan: LogicalPlan,
+    expr: impl IntoIterator<Item = Expr>,
+    alias: Option<String>,
+) -> Result<LogicalPlan> {
+    let input_schema = plan.schema();
+    let mut projected_expr = vec![];
+    for e in expr {
+        match e {
+            Expr::Wildcard => {
+                projected_expr.extend(expand_wildcard(input_schema, &plan)?)
+            }
+            _ => projected_expr
+                .push(columnize_expr(normalize_col(e, &plan)?, input_schema)),
+        }
+    }
+    validate_unique_names("Projections", projected_expr.iter(), input_schema)?;
+    let input_schema = DFSchema::new(exprlist_to_fields(&projected_expr, 
input_schema)?)?;
+    let schema = match alias {
+        Some(ref alias) => input_schema.replace_qualifier(alias.as_str()),
+        None => input_schema,
+    };
+    Ok(LogicalPlan::Projection {

Review comment:
       👍 




-- 
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]


Reply via email to