You are viewing a plain text version of this content. The canonical link for it is here.
Posted to github@arrow.apache.org by GitBox <gi...@apache.org> on 2022/05/05 18:57:04 UTC

[GitHub] [arrow-datafusion] andygrove opened a new pull request, #2457: [WIP] Fix bugs in SQL planner with GROUP BY scalar function

andygrove opened a new pull request, #2457:
URL: https://github.com/apache/arrow-datafusion/pull/2457

   # Which issue does this PR close?
   
   <!--
   We generally require a GitHub issue to be filed for all bug fixes and enhancements and this helps us generate change logs for our releases. You can link an issue to this PR using the GitHub syntax. For example `Closes #123` indicates that this PR will close issue #123.
   -->
   
   Closes https://github.com/apache/arrow-datafusion/issues/2430
   
   Partially addresses https://github.com/apache/arrow-datafusion/issues/2456
   
    # Rationale for this change
   <!--
    Why are you proposing this change? If this is already explained clearly in the issue then this section is not needed.
    Explaining clearly why changes are proposed helps reviewers understand your changes and offer better suggestions for fixes.  
   -->
   
   Fix bugs that prevent us from running some valid aggregate SQL queries.
   
   # What changes are included in this PR?
   <!--
   There is no need to duplicate the description in the issue here but it is sometimes worth providing a summary of the individual changes in this PR.
   -->
   
   - TBD this is still a work in progress
   
   # Are there any user-facing changes?
   <!--
   If there are user-facing changes then we may require documentation to be updated before approving the PR.
   -->
   
   No
   
   <!--
   If there are any breaking changes to public APIs, please add the `api change` label.
   -->
   


-- 
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-unsubscribe@arrow.apache.org

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


[GitHub] [arrow-datafusion] andygrove commented on a diff in pull request #2457: [WIP] Fix bugs in SQL planner with GROUP BY scalar function

Posted by GitBox <gi...@apache.org>.
andygrove commented on code in PR #2457:
URL: https://github.com/apache/arrow-datafusion/pull/2457#discussion_r866259199


##########
datafusion/core/src/logical_plan/expr.rs:
##########
@@ -138,9 +138,29 @@ pub fn create_udaf(
 /// Create field meta-data from an expression, for use in a result set schema
 pub fn exprlist_to_fields<'a>(
     expr: impl IntoIterator<Item = &'a Expr>,
-    input_schema: &DFSchema,
+    plan: &LogicalPlan,

Review Comment:
   We pass in a plan instead of a schema now, which gives us more control



-- 
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-unsubscribe@arrow.apache.org

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


[GitHub] [arrow-datafusion] andygrove commented on a diff in pull request #2457: Fix bugs in SQL planner with GROUP BY scalar function

Posted by GitBox <gi...@apache.org>.
andygrove commented on code in PR #2457:
URL: https://github.com/apache/arrow-datafusion/pull/2457#discussion_r866259199


##########
datafusion/core/src/logical_plan/expr.rs:
##########
@@ -138,9 +138,29 @@ pub fn create_udaf(
 /// Create field meta-data from an expression, for use in a result set schema
 pub fn exprlist_to_fields<'a>(
     expr: impl IntoIterator<Item = &'a Expr>,
-    input_schema: &DFSchema,
+    plan: &LogicalPlan,

Review Comment:
   We pass in a plan instead of a schema now, which gives us more control



-- 
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-unsubscribe@arrow.apache.org

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


[GitHub] [arrow-datafusion] andygrove merged pull request #2457: Fix bugs in SQL planner with GROUP BY scalar function and alias

Posted by GitBox <gi...@apache.org>.
andygrove merged PR #2457:
URL: https://github.com/apache/arrow-datafusion/pull/2457


-- 
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-unsubscribe@arrow.apache.org

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


[GitHub] [arrow-datafusion] andygrove commented on a diff in pull request #2457: Fix bugs in SQL planner with GROUP BY scalar function

Posted by GitBox <gi...@apache.org>.
andygrove commented on code in PR #2457:
URL: https://github.com/apache/arrow-datafusion/pull/2457#discussion_r866949413


##########
datafusion/core/src/sql/planner.rs:
##########
@@ -1144,30 +1144,45 @@ impl<'a, S: ContextProvider> SqlToRel<'a, S> {
         group_by_exprs: Vec<Expr>,
         aggr_exprs: Vec<Expr>,
     ) -> Result<(LogicalPlan, Vec<Expr>, Option<Expr>)> {
+        // create the aggregate plan
+        let plan = LogicalPlanBuilder::from(input.clone())
+            .aggregate(group_by_exprs.clone(), aggr_exprs.clone())?
+            .build()?;
+
+        // in this next section of code we are re-writing the projection to refer to columns
+        // output by the aggregate plan. For example, if the projection contains the expression
+        // `SUM(a)` then we replace that with a reference to a column `#SUM(a)` produced by
+        // the aggregate plan.
+
+        // combine the original grouping and aggregate expressions into one list (note that
+        // we do not add the "having" expression since that is not part of the projection)
         let aggr_projection_exprs = group_by_exprs
             .iter()
             .chain(aggr_exprs.iter())
             .cloned()
             .collect::<Vec<Expr>>();
 
-        let plan = LogicalPlanBuilder::from(input.clone())
-            .aggregate(group_by_exprs, aggr_exprs)?
-            .build()?;
+        // now attempt to resolve columns and replace with fully-qualified columns
+        let aggr_projection_exprs = aggr_projection_exprs
+            .iter()
+            .map(|expr| resolve_columns(expr, &input))

Review Comment:
   This is the fix .. we were comparing qualified and unqualified names before this, leading to failures in some cases



-- 
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-unsubscribe@arrow.apache.org

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


[GitHub] [arrow-datafusion] andygrove commented on pull request #2457: Fix bugs in SQL planner with GROUP BY scalar function

Posted by GitBox <gi...@apache.org>.
andygrove commented on PR #2457:
URL: https://github.com/apache/arrow-datafusion/pull/2457#issuecomment-1119751821

   @comphead @alamb @tustvold @yjshen @jdye64 This PR is now ready for review


-- 
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-unsubscribe@arrow.apache.org

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


[GitHub] [arrow-datafusion] alamb commented on a diff in pull request #2457: Fix bugs in SQL planner with GROUP BY scalar function

Posted by GitBox <gi...@apache.org>.
alamb commented on code in PR #2457:
URL: https://github.com/apache/arrow-datafusion/pull/2457#discussion_r866994668


##########
datafusion/core/src/sql/planner.rs:
##########
@@ -1144,30 +1144,45 @@ impl<'a, S: ContextProvider> SqlToRel<'a, S> {
         group_by_exprs: Vec<Expr>,
         aggr_exprs: Vec<Expr>,
     ) -> Result<(LogicalPlan, Vec<Expr>, Option<Expr>)> {
+        // create the aggregate plan
+        let plan = LogicalPlanBuilder::from(input.clone())
+            .aggregate(group_by_exprs.clone(), aggr_exprs.clone())?
+            .build()?;
+
+        // in this next section of code we are re-writing the projection to refer to columns

Review Comment:
   I am a huge fan of the comments ❤️  thank you



-- 
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-unsubscribe@arrow.apache.org

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


[GitHub] [arrow-datafusion] andygrove commented on pull request #2457: Fix bugs in SQL planner with GROUP BY scalar function and alias

Posted by GitBox <gi...@apache.org>.
andygrove commented on PR #2457:
URL: https://github.com/apache/arrow-datafusion/pull/2457#issuecomment-1119807886

   Thanks for the review @alamb !


-- 
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-unsubscribe@arrow.apache.org

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