Skip to content

Correctness issue with unparsing UNION & UNION ALL #24446

Description

@Jeadie

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

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

bugSomething isn't working

Type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions