Skip to content

Different unnests on plan_to_sql are merged #15128

Open
@blaginin

Description

@blaginin

Describe the bug

While working on #14781 I noticed that UNNEST is being transformed to sql incorrectly. For example,

SELECT unnest([1, 2, 3, 4]) from unnest([1, 2, 3]);
SELECT unnest([1, 2, 3, 4]) from (select 1);

are transformed into

SELECT * FROM UNNEST([1, 2, 3]);
SELECT 1 FROM UNNEST([1, 2, 3, 4]);

which are both wrong.

To Reproduce

let ctx = SessionContext::new();
let df = ctx
    .sql("SELECT unnest([1, 2, 3, 4]) from unnest([1, 2, 3])")
    .await?;

let dialect = CustomDialectBuilder::default()
    .with_unnest_as_table_factor(true)
    .build();
let unparser = Unparser::new(&dialect);

let roundtrip_statement = unparser.plan_to_sql(&df.logical_plan())?;

assert_eq!(
    format!("{}", roundtrip_statement),
    "SELECT * FROM UNNEST([1, 2, 3])"
);

Expected behavior

Unnests should be preserved

Additional context

I think this is related to #13660

While fixing this, it would be good to not rely on having double alias, e.g.Alias(Alias(Column(... as I plan to remove it in #14781

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't working

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions