Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Unparse UNION plan with multiple inputs to SQL text #13621

Open
goldmedal opened this issue Dec 2, 2024 · 0 comments
Open

Unparse UNION plan with multiple inputs to SQL text #13621

goldmedal opened this issue Dec 2, 2024 · 0 comments
Labels
enhancement New feature or request

Comments

@goldmedal
Copy link
Contributor

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

1 participant