Skip to content

Unparse UNION plan with multiple inputs to SQL text #13621

@goldmedal

Description

@goldmedal

Is your feature request related to a problem or challenge?

Given a SQL like

WITH vals AS
  (
    SELECT 1 x, 'a' y UNION ALL
    SELECT 1 x, 'b' y UNION ALL
    SELECT 2 x, 'a' y UNION ALL
    SELECT 2 x, 'c' y
  )
SELECT x, y
FROM vals

The unoptimized plan is

 Projection: vals.x, vals.y
  SubqueryAlias: vals
    Union
      Union
        Union
          Projection: Int64(1) AS x, Utf8("a") AS y
            EmptyRelation
          Projection: Int64(1) AS x, Utf8("b") AS y
            EmptyRelation
        Projection: Int64(2) AS x, Utf8("a") AS y
          EmptyRelation
      Projection: Int64(2) AS x, Utf8("c") AS y
        EmptyRelation

It's fine for the unparser. However, after EliminateNestedUnion optimization rule, the nested union will be flatten to one union like

Projection: vals.x, vals.y
  SubqueryAlias: vals
    Union
      Projection: Int64(1) AS x, Utf8("a") AS y
        EmptyRelation
      Projection: Int64(1) AS x, Utf8("b") AS y
        EmptyRelation
      Projection: Int64(2) AS x, Utf8("a") AS y
        EmptyRelation
      Projection: Int64(2) AS x, Utf8("c") AS y
        EmptyRelation

If we tried to unparse this plan, we will get the error message:

Error: This feature is not implemented: UNION ALL expected 2 inputs, but found 4

Describe the solution you'd like

Currently, the unpaser only supports unparsing an union with 2 input

if union.inputs.len() != 2 {
return not_impl_err!(
"UNION ALL expected 2 inputs, but found {}",
union.inputs.len()
);
}

We should support unparsing the union with more 2 inputs for the Unparser.

Describe alternatives you've considered

No response

Additional context

No response

Metadata

Metadata

Assignees

No one assigned

    Labels

    enhancementNew feature or request

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions