drusso commented on a change in pull request #8836:
URL: https://github.com/apache/arrow/pull/8836#discussion_r545970680



##########
File path: rust/datafusion/src/sql/planner.rs
##########
@@ -407,80 +412,107 @@ impl<'a, S: SchemaProvider> SqlToRel<'a, S> {
         };
         let plan = plan?;
 
-        let projection_expr: Vec<Expr> = select
-            .projection
-            .iter()
-            .map(|e| self.sql_select_to_rex(&e, &plan.schema()))
-            .collect::<Result<Vec<Expr>>>()?;
+        // The SELECT expressions, with wildcards expanded.
+        let select_exprs = self.prepare_select_exprs(&plan, 
&select.projection)?;
 
-        let aggr_expr: Vec<Expr> = projection_expr
-            .iter()
-            .filter(|e| is_aggregate_expr(e))
-            .map(|e| e.clone())
-            .collect();
+        // All of the aggregate expressions (deduplicated).
+        let aggr_exprs = find_aggregate_exprs(&select_exprs);
 
-        // apply projection or aggregate
-        let plan = if (select.group_by.len() > 0) | (aggr_expr.len() > 0) {
-            self.aggregate(&plan, projection_expr, &select.group_by, 
aggr_expr)?
-        } else {
-            self.project(&plan, projection_expr)?
-        };
-        Ok(plan)
+        let (plan, select_exprs_post_aggr) =
+            if select.group_by.len() > 0 || aggr_exprs.len() > 0 {
+                self.aggregate(&plan, &select_exprs, &select.group_by, 
&aggr_exprs)?
+            } else {
+                (plan, select_exprs)
+            };
+
+        self.project(&plan, select_exprs_post_aggr, false)
+    }
+
+    /// Returns the `Expr`'s corresponding to a SQL query's SELECT expressions.
+    ///
+    /// Wildcards are expanded into the concrete list of columns.
+    fn prepare_select_exprs(
+        &self,
+        plan: &LogicalPlan,
+        projection: &Vec<SelectItem>,
+    ) -> Result<Vec<Expr>> {
+        let input_schema = plan.schema();
+
+        Ok(projection
+            .iter()
+            .map(|expr| self.sql_select_to_rex(&expr, &input_schema))
+            .collect::<Result<Vec<Expr>>>()?
+            .iter()
+            .flat_map(|expr| expand_wildcard(&expr, &input_schema))
+            .collect::<Vec<Expr>>())
     }
 
     /// Wrap a plan in a projection
-    fn project(&self, input: &LogicalPlan, expr: Vec<Expr>) -> 
Result<LogicalPlan> {
-        LogicalPlanBuilder::from(input).project(expr)?.build()
+    fn project(

Review comment:
       Updated in fe5a19c20e967cc0b237db0a5ed09851eca97e9b. 




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

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


Reply via email to