alamb opened a new issue, #7786:
URL: https://github.com/apache/arrow-datafusion/issues/7786

   ### Is your feature request related to a problem or challenge?
   
   The current the plan for a nested `UNION` is still nested resulting in 
unecessary GROUPing
   
   ```
   ❯ explain SELECT 1 UNION (SELECT 1 UNION SELECT 1);
   
+---------------+-----------------------------------------------------------------------------------------+
   | plan_type     | plan                                                       
                             |
   
+---------------+-----------------------------------------------------------------------------------------+
   | logical_plan  | Aggregate: groupBy=[[Int64(1)]], aggr=[[]]                 
                             |
   |               |   Union                                                    
                             |
   |               |     Projection: Int64(1)                                   
                             |
   |               |       EmptyRelation                                        
                             |
   |               |     Aggregate: groupBy=[[Int64(1)]], aggr=[[]]             
                             |
   |               |       Union                                                
                             |
   |               |         Projection: Int64(1)                               
                             |
   |               |           EmptyRelation                                    
                             |
   |               |         Projection: Int64(1)                               
                             |
   |               |           EmptyRelation                                    
                             |
   ```
   
   
   
   
   
   ### Describe the solution you'd like
   
   As suggested by @jackwener: 
https://github.com/apache/arrow-datafusion/pull/7695#discussion_r1349639782, 
ideally the plan should look like the following (the unions should be unnested 
and run through a single group by):
   
   ```
   ❯ explain SELECT 1 UNION (SELECT 1 UNION SELECT 1);
   
+---------------+-----------------------------------------------------------------------------------------+
   | plan_type     | plan                                                       
                             |
   
+---------------+-----------------------------------------------------------------------------------------+
   | logical_plan  | Aggregate: groupBy=[[Int64(1)]], aggr=[[]]                 
                             |
   |               |   Union                                                    
                             |
   |               |     Projection: Int64(1)                                   
                             |
   |               |       EmptyRelation                                        
                             |
   |               |     Projection: Int64(1)                                   
                             |
   |               |       EmptyRelation                                        
                             |
   |               |     Projection: Int64(1)                                   
                             |
   |               |       EmptyRelation                                        
                             |
   ```
   
   ### Describe alternatives you've considered
   
   _No response_
   
   ### Additional context
   
   @maruschin  added a nice optimization (as well as very nice tests) to unnest 
`UNION ALL` in https://github.com/apache/arrow-datafusion/pull/7695
   
   ```sql
   ❯ explain SELECT 1 UNION ALL (SELECT 1 UNION ALL SELECT 1);
   +---------------+----------------------------------------+
   | plan_type     | plan                                   |
   +---------------+----------------------------------------+
   | logical_plan  | Union                                  |
   |               |   Projection: Int64(1) AS Int64(1)     |
   |               |     EmptyRelation                      |
   |               |   Projection: Int64(1) AS Int64(1)     |
   |               |     EmptyRelation                      |
   |               |   Projection: Int64(1) AS Int64(1)     |
   |               |     EmptyRelation                      |
   ```


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