Skip to content
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

Reduce repetition in try_process_group_by_unnest and try_process_unnest #11714

Merged
merged 2 commits into from
Jul 31, 2024
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
42 changes: 13 additions & 29 deletions datafusion/sql/src/select.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ use crate::planner::{
};
use crate::utils::{
check_columns_satisfy_exprs, extract_aliases, rebase_expr, resolve_aliases_to_exprs,
resolve_columns, resolve_positions_to_exprs, transform_bottom_unnest,
resolve_columns, resolve_positions_to_exprs, transform_bottom_unnests,
};

use datafusion_common::tree_node::{TreeNode, TreeNodeRecursion};
Expand Down Expand Up @@ -318,20 +318,12 @@ impl<'a, S: ContextProvider> SqlToRel<'a, S> {
// - unnest(struct_col) will be transformed into unnest(struct_col).field1, unnest(struct_col).field2
// - unnest(array_col) will be transformed into unnest(array_col).element
// - unnest(array_col) + 1 will be transformed into unnest(array_col).element +1
let outer_projection_exprs: Vec<Expr> = intermediate_select_exprs
.iter()
.map(|expr| {
transform_bottom_unnest(
&intermediate_plan,
&mut unnest_columns,
&mut inner_projection_exprs,
expr,
)
})
.collect::<Result<Vec<_>>>()?
.into_iter()
.flatten()
.collect();
let outer_projection_exprs = transform_bottom_unnests(
&intermediate_plan,
&mut unnest_columns,
&mut inner_projection_exprs,
&intermediate_select_exprs,
)?;

// No more unnest is possible
if unnest_columns.is_empty() {
Expand Down Expand Up @@ -417,20 +409,12 @@ impl<'a, S: ContextProvider> SqlToRel<'a, S> {
let mut unnest_columns = vec![];
let mut inner_projection_exprs = vec![];

let outer_projection_exprs: Vec<Expr> = intermediate_select_exprs
.iter()
.map(|expr| {
transform_bottom_unnest(
&intermediate_plan,
&mut unnest_columns,
&mut inner_projection_exprs,
expr,
)
})
.collect::<Result<Vec<_>>>()?
.into_iter()
.flatten()
.collect();
let outer_projection_exprs = transform_bottom_unnests(
&intermediate_plan,
&mut unnest_columns,
&mut inner_projection_exprs,
&intermediate_select_exprs,
)?;

if unnest_columns.is_empty() {
break;
Expand Down
22 changes: 22 additions & 0 deletions datafusion/sql/src/utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -287,6 +287,28 @@ pub(crate) fn value_to_string(value: &Value) -> Option<String> {
}
}

pub(crate) fn transform_bottom_unnests(
input: &LogicalPlan,
unnest_placeholder_columns: &mut Vec<String>,
inner_projection_exprs: &mut Vec<Expr>,
original_exprs: &[Expr],
) -> Result<Vec<Expr>> {
Ok(original_exprs
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This does the recursion manually -- I wonder if it would be possible / useful to do the recursion using transform_up from TreeNode: https://docs.rs/datafusion/latest/datafusion/common/tree_node/trait.TreeNode.html#method.transform_up

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you @alamb for your review. I'm not quite sure what you mean, maybe you can add more description, thanks again. 🙏

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think I was confused -- sorry

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think we can, in fact transform_up_down is needed, i'll introduce why in this PR when it's ready: #11577

.iter()
.map(|expr| {
transform_bottom_unnest(
input,
unnest_placeholder_columns,
inner_projection_exprs,
expr,
)
})
.collect::<Result<Vec<_>>>()?
.into_iter()
.flatten()
.collect::<Vec<_>>())
}

/// The context is we want to rewrite unnest() into InnerProjection->Unnest->OuterProjection
/// Given an expression which contains unnest expr as one of its children,
/// Try transform depends on unnest type
Expand Down