-
Notifications
You must be signed in to change notification settings - Fork 1.3k
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
Recursive unnest
#11062
Merged
Merged
Recursive unnest
#11062
Changes from 10 commits
Commits
Show all changes
13 commits
Select commit
Hold shift + click to select a range
ffde26d
chore: fix map children of unnest
duongcongtoai e5c9e47
adjust test
duongcongtoai d8b8b93
remove debug
duongcongtoai 1e58887
Merge remote-tracking branch 'origin/main' into 10660-recursive-unnest
duongcongtoai 731b71b
Merge remote-tracking branch 'origin/main' into 10660-recursive-unnest
duongcongtoai 2112617
chore: move test to unnest.slt
duongcongtoai c146545
chore: rename
duongcongtoai 82ac9de
add some comment
duongcongtoai 2809297
compile err
duongcongtoai 67a4873
more comment
duongcongtoai 0bcb00f
chore: address comment
duongcongtoai 02eeefe
more coverage
duongcongtoai 93fbd26
one more scenario
duongcongtoai File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -22,7 +22,9 @@ use std::collections::HashMap; | |
use arrow_schema::{ | ||
DataType, DECIMAL128_MAX_PRECISION, DECIMAL256_MAX_PRECISION, DECIMAL_DEFAULT_SCALE, | ||
}; | ||
use datafusion_common::tree_node::{Transformed, TransformedResult, TreeNode}; | ||
use datafusion_common::tree_node::{ | ||
Transformed, TransformedResult, TreeNode, TreeNodeRecursion, | ||
}; | ||
use datafusion_common::{ | ||
exec_err, internal_err, plan_err, Column, DataFusionError, Result, ScalarValue, | ||
}; | ||
|
@@ -267,11 +269,13 @@ pub(crate) fn normalize_ident(id: Ident) -> String { | |
/// - For list column: unnest(col) with type list -> unnest(col) with type list::item | ||
/// - For struct column: unnest(struct(field1, field2)) -> unnest(struct).field1, unnest(struct).field2 | ||
/// The transformed exprs will be used in the outer projection | ||
pub(crate) fn recursive_transform_unnest( | ||
/// If along the path from root to bottom, there are multiple unnest expressions, the transformation | ||
/// is done only for the bottom expression | ||
pub(crate) fn transform_bottom_unnest( | ||
input: &LogicalPlan, | ||
unnest_placeholder_columns: &mut Vec<String>, | ||
inner_projection_exprs: &mut Vec<Expr>, | ||
original_expr: Expr, | ||
original_expr: &Expr, | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. We could avoid clone in L297 too. |
||
) -> Result<Vec<Expr>> { | ||
let mut transform = | ||
|unnest_expr: &Expr, expr_in_unnest: &Expr| -> Result<Vec<Expr>> { | ||
|
@@ -306,21 +310,32 @@ pub(crate) fn recursive_transform_unnest( | |
|
||
// Specifically handle root level unnest expr, this is the only place | ||
// unnest on struct can be handled | ||
if let Expr::Unnest(Unnest { expr: ref arg }) = original_expr { | ||
return transform(&original_expr, arg); | ||
} | ||
// if let Expr::Unnest(Unnest { expr: ref arg }) = original_expr { | ||
// return transform(&original_expr, arg); | ||
// } | ||
let mut root_expr = vec![]; | ||
let Transformed { | ||
data: transformed_expr, | ||
transformed, | ||
tnr: _, | ||
} = original_expr.transform_up(|expr: Expr| { | ||
} = original_expr.clone().transform_up(|expr: Expr| { | ||
let is_root_expr = &expr == original_expr; | ||
|
||
if let Expr::Unnest(Unnest { expr: ref arg }) = expr { | ||
let (data_type, _) = arg.data_type_and_nullable(input.schema())?; | ||
if let DataType::Struct(_) = data_type { | ||
return internal_err!("unnest on struct can ony be applied at the root level of select expression"); | ||
if is_root_expr{ | ||
root_expr.extend(transform(original_expr, arg)?); | ||
return Ok(Transformed::new(expr,true,TreeNodeRecursion::Stop)); | ||
} | ||
if !is_root_expr { | ||
if let DataType::Struct(_) = data_type { | ||
return internal_err!("unnest on struct can ony be applied at the root level of select expression"); | ||
} | ||
} | ||
|
||
let transformed_exprs = transform(&expr, arg)?; | ||
Ok(Transformed::yes(transformed_exprs[0].clone())) | ||
// root_expr.push(transformed_exprs[0].clone()); | ||
Ok(Transformed::new(transformed_exprs[0].clone(),true,TreeNodeRecursion::Stop)) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. use |
||
} else { | ||
Ok(Transformed::no(expr)) | ||
} | ||
|
@@ -338,6 +353,11 @@ pub(crate) fn recursive_transform_unnest( | |
Ok(vec![Expr::Column(Column::from_name(column_name))]) | ||
} | ||
} else { | ||
// A workaround, because at root level | ||
// an unnest on struct column can be transformd into multiple exprs | ||
if !root_expr.is_empty() { | ||
return Ok(root_expr); | ||
} | ||
Ok(vec![transformed_expr]) | ||
} | ||
} | ||
|
@@ -351,12 +371,13 @@ mod tests { | |
use arrow_schema::Fields; | ||
use datafusion_common::{DFSchema, Result}; | ||
use datafusion_expr::{col, lit, unnest, EmptyRelation, LogicalPlan}; | ||
use datafusion_functions::core::expr_ext::FieldAccessor; | ||
use datafusion_functions_aggregate::expr_fn::count; | ||
|
||
use crate::utils::{recursive_transform_unnest, resolve_positions_to_exprs}; | ||
use crate::utils::{resolve_positions_to_exprs, transform_bottom_unnest}; | ||
|
||
#[test] | ||
fn test_recursive_transform_unnest() -> Result<()> { | ||
fn test_transform_bottom_unnest() -> Result<()> { | ||
let schema = Schema::new(vec![ | ||
Field::new( | ||
"struct_col", | ||
|
@@ -390,11 +411,11 @@ mod tests { | |
|
||
// unnest(struct_col) | ||
let original_expr = unnest(col("struct_col")); | ||
let transformed_exprs = recursive_transform_unnest( | ||
let transformed_exprs = transform_bottom_unnest( | ||
&input, | ||
&mut unnest_placeholder_columns, | ||
&mut inner_projection_exprs, | ||
original_expr, | ||
&original_expr, | ||
)?; | ||
assert_eq!( | ||
transformed_exprs, | ||
|
@@ -413,11 +434,11 @@ mod tests { | |
|
||
// unnest(array_col) + 1 | ||
let original_expr = unnest(col("array_col")).add(lit(1i64)); | ||
let transformed_exprs = recursive_transform_unnest( | ||
let transformed_exprs = transform_bottom_unnest( | ||
&input, | ||
&mut unnest_placeholder_columns, | ||
&mut inner_projection_exprs, | ||
original_expr, | ||
&original_expr, | ||
)?; | ||
assert_eq!( | ||
unnest_placeholder_columns, | ||
|
@@ -440,6 +461,62 @@ mod tests { | |
] | ||
); | ||
|
||
// a nested structure struct[[]] | ||
let schema = Schema::new(vec![ | ||
Field::new( | ||
"struct_col", // {array_col: [1,2,3]} | ||
ArrowDataType::Struct(Fields::from(vec![Field::new( | ||
"matrix", | ||
ArrowDataType::List(Arc::new(Field::new( | ||
"matrix_row", | ||
ArrowDataType::List(Arc::new(Field::new( | ||
"item", | ||
ArrowDataType::Int64, | ||
true, | ||
))), | ||
true, | ||
))), | ||
true, | ||
)])), | ||
false, | ||
), | ||
Field::new("int_col", ArrowDataType::Int32, false), | ||
]); | ||
|
||
let dfschema = DFSchema::try_from(schema)?; | ||
|
||
let input = LogicalPlan::EmptyRelation(EmptyRelation { | ||
produce_one_row: false, | ||
schema: Arc::new(dfschema), | ||
}); | ||
|
||
let mut unnest_placeholder_columns = vec![]; | ||
let mut inner_projection_exprs = vec![]; | ||
|
||
// An expr with multiple unnest | ||
let original_expr = unnest(unnest(col("struct_col").field("matrix"))); | ||
let transformed_exprs = transform_bottom_unnest( | ||
&input, | ||
&mut unnest_placeholder_columns, | ||
&mut inner_projection_exprs, | ||
&original_expr, | ||
)?; | ||
// Only the inner most/ bottom most unnest is transformed | ||
assert_eq!( | ||
transformed_exprs, | ||
vec![unnest(col("unnest(struct_col[matrix])"))] | ||
); | ||
assert_eq!( | ||
unnest_placeholder_columns, | ||
vec!["unnest(struct_col[matrix])"] | ||
); | ||
assert_eq!( | ||
inner_projection_exprs, | ||
vec![col("struct_col") | ||
.field("matrix") | ||
.alias("unnest(struct_col[matrix])"),] | ||
); | ||
|
||
Ok(()) | ||
} | ||
|
||
|
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Would transform List / Struct separately help readability here?
something like