Skip to content

Unparse UNNEST projection with the table column alias #15879

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

Merged
merged 3 commits into from
May 1, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 22 additions & 3 deletions datafusion/sql/src/unparser/plan.rs
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ use datafusion_expr::{
UserDefinedLogicalNode,
};
use sqlparser::ast::{self, Ident, OrderByKind, SetExpr, TableAliasColumnDef};
use std::sync::Arc;
use std::{sync::Arc, vec};

/// Convert a DataFusion [`LogicalPlan`] to [`ast::Statement`]
///
Expand Down Expand Up @@ -309,12 +309,13 @@ impl Unparser<'_> {
plan: &LogicalPlan,
relation: &mut RelationBuilder,
lateral: bool,
columns: Vec<Ident>,
) -> Result<()> {
if self.dialect.requires_derived_table_alias() {
if self.dialect.requires_derived_table_alias() || !columns.is_empty() {
self.derive(
plan,
relation,
Some(self.new_table_alias(alias.to_string(), vec![])),
Some(self.new_table_alias(alias.to_string(), columns)),
lateral,
)
} else {
Expand Down Expand Up @@ -392,6 +393,18 @@ impl Unparser<'_> {
}
}

// If it's a unnest projection, we should provide the table column alias
// to provide a column name for the unnest relation.
let columns = if unnest_input_type.is_some() {
p.expr
.iter()
.map(|e| {
self.new_ident_quoted_if_needs(e.schema_name().to_string())
})
.collect()
} else {
vec![]
};
// Projection can be top-level plan for derived table
if select.already_projected() {
return self.derive_with_dialect_alias(
Expand All @@ -401,6 +414,7 @@ impl Unparser<'_> {
unnest_input_type
.filter(|t| matches!(t, UnnestInputType::OuterReference))
.is_some(),
columns,
);
}
self.reconstruct_select_statement(plan, p, select)?;
Expand Down Expand Up @@ -434,6 +448,7 @@ impl Unparser<'_> {
plan,
relation,
false,
vec![],
);
}
if let Some(fetch) = &limit.fetch {
Expand Down Expand Up @@ -472,6 +487,7 @@ impl Unparser<'_> {
plan,
relation,
false,
vec![],
);
}
let Some(query_ref) = query else {
Expand Down Expand Up @@ -543,6 +559,7 @@ impl Unparser<'_> {
plan,
relation,
false,
vec![],
);
}

Expand Down Expand Up @@ -833,6 +850,7 @@ impl Unparser<'_> {
plan,
relation,
false,
vec![],
);
}

Expand Down Expand Up @@ -950,6 +968,7 @@ impl Unparser<'_> {
subquery.subquery.as_ref(),
relation,
true,
vec![],
)
}
}
Expand Down
4 changes: 2 additions & 2 deletions datafusion/sql/tests/cases/plan_to_sql.rs
Original file line number Diff line number Diff line change
Expand Up @@ -698,7 +698,7 @@ fn roundtrip_statement_with_dialect_27() -> Result<(), DataFusionError> {
sql: "SELECT * FROM UNNEST([1,2,3])",
parser_dialect: GenericDialect {},
unparser_dialect: UnparserDefaultDialect {},
expected: @r#"SELECT "UNNEST(make_array(Int64(1),Int64(2),Int64(3)))" FROM (SELECT UNNEST([1, 2, 3]) AS "UNNEST(make_array(Int64(1),Int64(2),Int64(3)))")"#,
expected: @r#"SELECT "UNNEST(make_array(Int64(1),Int64(2),Int64(3)))" FROM (SELECT UNNEST([1, 2, 3]) AS "UNNEST(make_array(Int64(1),Int64(2),Int64(3)))") AS derived_projection ("UNNEST(make_array(Int64(1),Int64(2),Int64(3)))")"#,
);
Ok(())
}
Expand All @@ -720,7 +720,7 @@ fn roundtrip_statement_with_dialect_29() -> Result<(), DataFusionError> {
sql: "SELECT * FROM UNNEST([1,2,3]), j1",
parser_dialect: GenericDialect {},
unparser_dialect: UnparserDefaultDialect {},
expected: @r#"SELECT "UNNEST(make_array(Int64(1),Int64(2),Int64(3)))", j1.j1_id, j1.j1_string FROM (SELECT UNNEST([1, 2, 3]) AS "UNNEST(make_array(Int64(1),Int64(2),Int64(3)))") CROSS JOIN j1"#,
expected: @r#"SELECT "UNNEST(make_array(Int64(1),Int64(2),Int64(3)))", j1.j1_id, j1.j1_string FROM (SELECT UNNEST([1, 2, 3]) AS "UNNEST(make_array(Int64(1),Int64(2),Int64(3)))") AS derived_projection ("UNNEST(make_array(Int64(1),Int64(2),Int64(3)))") CROSS JOIN j1"#,
);
Ok(())
}
Expand Down