Skip to content

Fix: unnest with alias reports error #16854

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

Draft
wants to merge 1 commit into
base: main
Choose a base branch
from
Draft
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
17 changes: 13 additions & 4 deletions datafusion/expr/src/logical_plan/builder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2090,10 +2090,19 @@ pub fn unnest(input: LogicalPlan, columns: Vec<Column>) -> Result<LogicalPlan> {
pub fn get_struct_unnested_columns(
col_name: &String,
inner_fields: &Fields,
) -> Vec<Column> {
alias: Option<&String>,
) -> Vec<Expr> {
inner_fields
.iter()
.map(|f| Column::from_name(format!("{}.{}", col_name, f.name())))
.map(|f| {
let col =
Expr::Column(Column::from_name(format!("{}.{}", col_name, f.name())));
if let Some(alias_name) = alias {
col.alias(format!("{alias_name}.{}", f.name()))
} else {
col
}
})
.collect()
}

Expand Down Expand Up @@ -2660,12 +2669,12 @@ mod tests {

assert_snapshot!(plan, @r"
Union
Cross Join:
Cross Join:
SubqueryAlias: left
Values: (Int32(1))
SubqueryAlias: right
Values: (Int32(1))
Cross Join:
Cross Join:
SubqueryAlias: left
Values: (Int32(1))
SubqueryAlias: right
Expand Down
21 changes: 14 additions & 7 deletions datafusion/sql/src/utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -415,6 +415,12 @@ impl RecursiveUnnestRewriter<'_> {

match data_type {
DataType::Struct(inner_fields) => {
let mut alias_name = None;
if let Expr::Alias(Alias { name, .. }) = self.root_expr {
alias_name = Some(name);
}

dbg!(alias_name.clone());
if !struct_allowed {
return internal_err!("unnest on struct can only be applied at the root level of select expression");
}
Expand All @@ -424,12 +430,11 @@ impl RecursiveUnnestRewriter<'_> {
);
self.columns_unnestings
.insert(Column::from_name(placeholder_name.clone()), None);
Ok(
get_struct_unnested_columns(&placeholder_name, &inner_fields)
.into_iter()
.map(Expr::Column)
.collect(),
)
Ok(get_struct_unnested_columns(
&placeholder_name,
&inner_fields,
alias_name,
))
}
DataType::List(_)
| DataType::FixedSizeList(_, _)
Expand Down Expand Up @@ -554,7 +559,9 @@ impl TreeNodeRewriter for RecursiveUnnestRewriter<'_> {
// instead of unnest(struct_arr_col, depth = 2)

let unnest_recursion = unnest_stack.len();
let struct_allowed = (&expr == self.root_expr) && unnest_recursion == 1;
let struct_allowed = ((&expr == self.root_expr)
|| (matches!(self.root_expr, Expr::Alias(Alias { expr: inner, .. }) if inner.as_ref() == &expr)))
&& unnest_recursion == 1;

let mut transformed_exprs = self.transform(
unnest_recursion,
Expand Down
Loading