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

Allow to unparse UNNEST plan back to a table function SQL text #13601

Open
goldmedal opened this issue Nov 29, 2024 · 1 comment
Open

Allow to unparse UNNEST plan back to a table function SQL text #13601

goldmedal opened this issue Nov 29, 2024 · 1 comment
Assignees
Labels
enhancement New feature or request

Comments

@goldmedal
Copy link
Contributor

Is your feature request related to a problem or challenge?

Given a SQL:

select * from unnest([1,2,3]) as t(c1)

DataFusion plans the unnest to Projection/Unnest/Projection pattern like

Projection: *
  SubqueryAlias: t
    Projection: UNNEST(make_array(Int64(1),Int64(2),Int64(3))) AS c1
      Projection: unnest_placeholder(make_array(Int64(1),Int64(2),Int64(3)),depth=1) AS UNNEST(make_array(Int64(1),Int64(2),Int64(3)))
        Unnest: lists[unnest_placeholder(make_array(Int64(1),Int64(2),Int64(3)))|depth=1] structs[]
          Projection: make_array(Int64(1), Int64(2), Int64(3)) AS unnest_placeholder(make_array(Int64(1),Int64(2),Int64(3)))
            EmptyRelation

If we try to unparse the plan back to the SQL, the result is

SELECT * FROM (SELECT UNNEST([1, 2, 3]) AS "UNNEST(make_array(Int64(1),Int64(2),Int64(3)))") AS t (c1)

However, some databases (e.g. BigQuery), only support using UNNEST as a table function.

Describe the solution you'd like

We can introduce a new option for the unparser Dialect

pub trait Dialect: Send + Sync {
    ...
    /// Allow to unparse the unnest plan as a table function.
    fn unnest_as_table_function(&self) -> bool {
        false
    }
    ...
}

If we found the pattern of plan like Projection/Unnest/Projection, we can try to unparse it to an ast::query::TableFactor::UNNEST.

Describe alternatives you've considered

No response

Additional context

We may need to ignore other instances of UNNEST usage, such as:

SELECT UNNEST(column1), UNNEST(column2) FROM (SELECT column1, column2 FROM xxx);

This issue aims to enable a roundtrip for the UNNEST table function.

@goldmedal goldmedal added the enhancement New feature or request label Nov 29, 2024
@jatin510
Copy link
Contributor

take

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

2 participants