Jeadie opened a new issue, #24446:
URL: https://github.com/apache/datafusion/issues/24446

   ### Describe the bug
   
   Consider `a UNION ALL (b UNION a)`, that becomes a plan
   
   ```
   Union
       a
       Distinct::All
         Union
           b
           a
   ```
   
   This will get incorrectly unparsed as `SELECT a UNION SELECT b UNION SELECT 
a`.
   
   All union operations are now distinct, instead of correctly preserving the 
true per node nature. Root cause: traversing the inner `Distinct::All(Union)` 
sets `QueryBuilder::distinct_union = true` globally. All subsequent Unions are 
considered distinct during unpausing the query. A single, query-level boolean 
is insufficient to track `ast::SetQuantifier` for each UNION operation.
   
   
   ### To Reproduce
   
   Add this example case to `datafusion/sql/tests/cases/plan_to_sql.rs` and 
rerun.
   ```
   r#"SELECT j1_string FROM j1 UNION ALL (SELECT j2_string FROM j2 UNION SELECT 
j1_string FROM j1)"#,
   ```
   
   ### Expected behavior
   
   **Actual**: the outer ALL is dropped.
   ```
   SELECT a.x FROM a UNION SELECT b.x FROM b UNION SELECT a.x FROM a
   ```
   
   **Expected**
   ```
   SELECT a.x FROM a UNION ALL (SELECT b.x FROM b UNION SELECT a.x FROM a)
   ```
   
   ### Additional context
   
   _No response_


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


---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]

Reply via email to