Skip to content

Commit 209a0a2

Browse files
kumarUjjawalalamb
andauthored
fix: unnest struct field with an alias failed with internal error (#19698)
## Which issue does this PR close? <!-- We generally require a GitHub issue to be filed for all bug fixes and enhancements and this helps us generate change logs for our releases. You can link an issue to this PR using the GitHub syntax. For example `Closes #123` indicates that this PR will close issue #123. --> - Closes #19689. ## Rationale for this change Unnesting with an alias resulted in an internal error it should just be ignored. <!-- Why are you proposing this change? If this is already explained clearly in the issue then this section is not needed. Explaining clearly why changes are proposed helps reviewers understand your changes and offer better suggestions for fixes. --> ## What changes are included in this PR? - Added helper method to recognize when an alias wraps an unnest expression - Refined the condition for setting `transformed_root_exprs` to only apply to actual struct types (which return multiple expressions) - Updated slt test <!-- There is no need to duplicate the description in the issue here but it is sometimes worth providing a summary of the individual changes in this PR. --> ## Are these changes tested? Yes <!-- We typically require tests for all PRs in order to: 1. Prevent the code from being accidentally broken by subsequent changes 2. Serve as another way to document the expected behavior of the code If tests are not included in your PR, please explain why (for example, are they covered by existing tests)? --> ## Are there any user-facing changes? <!-- If there are user-facing changes then we may require documentation to be updated before approving the PR. --> <!-- If there are any breaking changes to public APIs, please add the `api change` label. --> --------- Co-authored-by: Andrew Lamb <andrew@nerdnetworks.org>
1 parent 8ba4646 commit 209a0a2

File tree

2 files changed

+51
-4
lines changed

2 files changed

+51
-4
lines changed

datafusion/sql/src/utils.rs

Lines changed: 23 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -406,6 +406,24 @@ impl RecursiveUnnestRewriter<'_> {
406406
.collect()
407407
}
408408

409+
/// Check if the current expression is at the root level for struct unnest purposes.
410+
/// This is true if:
411+
/// 1. The expression IS the root expression, OR
412+
/// 2. The root expression is an Alias wrapping this expression
413+
///
414+
/// This allows `unnest(struct_col) AS alias` to work, where the alias is simply
415+
/// ignored for struct unnest (matching DuckDB behavior).
416+
fn is_at_struct_allowed_root(&self, expr: &Expr) -> bool {
417+
if expr == self.root_expr {
418+
return true;
419+
}
420+
// Allow struct unnest when root is an alias wrapping the unnest
421+
if let Expr::Alias(Alias { expr: inner, .. }) = self.root_expr {
422+
return inner.as_ref() == expr;
423+
}
424+
false
425+
}
426+
409427
fn transform(
410428
&mut self,
411429
level: usize,
@@ -566,15 +584,18 @@ impl TreeNodeRewriter for RecursiveUnnestRewriter<'_> {
566584
// instead of unnest(struct_arr_col, depth = 2)
567585

568586
let unnest_recursion = unnest_stack.len();
569-
let struct_allowed = (&expr == self.root_expr) && unnest_recursion == 1;
587+
let struct_allowed =
588+
self.is_at_struct_allowed_root(&expr) && unnest_recursion == 1;
570589

571590
let mut transformed_exprs = self.transform(
572591
unnest_recursion,
573592
expr.schema_name().to_string(),
574593
inner_expr,
575594
struct_allowed,
576595
)?;
577-
if struct_allowed {
596+
// Only set transformed_root_exprs for struct unnest (which returns multiple expressions).
597+
// For list unnest (single expression), we let the normal rewrite handle the alias.
598+
if struct_allowed && transformed_exprs.len() > 1 {
578599
self.transformed_root_exprs = Some(transformed_exprs.clone());
579600
}
580601
return Ok(Transformed::new(

datafusion/sqllogictest/test_files/unnest.slt

Lines changed: 28 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,20 @@ select unnest(struct(1,2,3));
5858
----
5959
1 2 3
6060

61+
## Basic unnest expression in select struct with alias (alias is ignored for struct unnest)
62+
query III
63+
select unnest(struct(1,2,3)) as ignored_alias;
64+
----
65+
1 2 3
66+
67+
## Verify schema output for struct unnest with alias (alias is ignored)
68+
query TTT
69+
describe select unnest(struct(1,2,3)) as ignored_alias;
70+
----
71+
__unnest_placeholder(struct(Int64(1),Int64(2),Int64(3))).c0 Int64 YES
72+
__unnest_placeholder(struct(Int64(1),Int64(2),Int64(3))).c1 Int64 YES
73+
__unnest_placeholder(struct(Int64(1),Int64(2),Int64(3))).c2 Int64 YES
74+
6175
## Basic unnest list expression in from clause
6276
query I
6377
select * from unnest([1,2,3]);
@@ -798,9 +812,21 @@ NULL 1
798812
query error DataFusion error: Error during planning: Column in SELECT must be in GROUP BY or an aggregate function: While expanding wildcard, column "nested_unnest_table\.column1" must appear in the GROUP BY clause or must be part of an aggregate function, currently only "UNNEST\(nested_unnest_table\.column1\)\[c0\]" appears in the SELECT clause satisfies this requirement
799813
select unnest(column1) c1 from nested_unnest_table group by c1.c0;
800814

801-
# TODO: this query should work. see issue: https://github.com/apache/datafusion/issues/12794
802-
query error DataFusion error: Internal error: Assertion failed: struct_allowed: unnest on struct can only be applied at the root level of select expression
815+
## Unnest struct with alias - alias is ignored (same as DuckDB behavior)
816+
## See: https://github.com/apache/datafusion/issues/12794
817+
query TT?
803818
select unnest(column1) c1 from nested_unnest_table
819+
----
820+
a b {c0: c}
821+
d e {c0: f}
822+
823+
## Verify schema output for struct unnest with alias (alias is ignored)
824+
query TTT
825+
describe select unnest(column1) c1 from nested_unnest_table;
826+
----
827+
__unnest_placeholder(nested_unnest_table.column1).c0 Utf8 YES
828+
__unnest_placeholder(nested_unnest_table.column1).c1 Utf8 YES
829+
__unnest_placeholder(nested_unnest_table.column1).c2 Struct("c0": Utf8) YES
804830

805831
query II??I??
806832
select unnest(column5), * from unnest_table;

0 commit comments

Comments
 (0)