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

Stop copying LogicalPlan and Exprs in ReplaceDistinctWithAggregate #10460

Merged
merged 5 commits into from
May 13, 2024

Conversation

ClSlaid
Copy link
Contributor

@ClSlaid ClSlaid commented May 11, 2024

Which issue does this PR close?

Closes #10293.

Rationale for this change

What changes are included in this PR?

Are these changes tested?

Are there any user-facing changes?

ClSlaid added 2 commits May 11, 2024 21:55
Signed-off-by: cailue <cailue@bupt.edu.cn>
Signed-off-by: 蔡略 <cailue@bupt.edu.cn>
@github-actions github-actions bot added the optimizer Optimizer rules label May 11, 2024
Comment on lines +109 to +110
let aggr_expr = normalize_cols(aggr_expr, input.as_ref())?;
let group_expr = normalize_cols(on_expr, input.as_ref())?;
Copy link
Contributor Author

Choose a reason for hiding this comment

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

maybe unnecessary?

Comment on lines 92 to 107
let aggr_expr = select_expr
.iter()
.map(|e| {
Expr::AggregateFunction(AggregateFunction::new(
AggregateFunctionFunc::FirstValue,
vec![e.clone()],
false,
None,
sort_expr.clone(),
None,
))
})
.collect::<Vec<Expr>>();
let aggr_expr = vec![Expr::AggregateFunction(AggregateFunction::new(
AggregateFunctionFunc::FirstValue,
select_expr,
false,
None,
sort_expr.clone(),
None,
))];
Copy link
Contributor Author

Choose a reason for hiding this comment

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

I don't know if they are semantically identical.
Instructions welcomed.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

CI failed, i guess the answer is no. :(

Copy link
Contributor

Choose a reason for hiding this comment

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

The difference is that the select_expr.iter()... makes a vec with the same number of elements as select_expr

To avoid a clone, perhaps you can use into_iter instead of iter, like

                let aggr_expr = select_expr
                    .into_iter() // <---- Use into_iter() here
                    .map(|e| {
                        Expr::AggregateFunction(AggregateFunction::new(
                            AggregateFunctionFunc::FirstValue,
                            vec![e],    // <--- to avoid a clone here
                            false,
                            None,
                            sort_expr.clone(),
                            None,
                        ))
                    })
                    .collect::<Vec<Expr>>();

Copy link
Contributor Author

Choose a reason for hiding this comment

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

The difference is that the select_expr.iter()... makes a vec with the same number of elements as select_expr

To avoid a clone, perhaps you can use into_iter instead of iter, like

                let aggr_expr = select_expr
                    .into_iter() // <---- Use into_iter() here
                    .map(|e| {
                        Expr::AggregateFunction(AggregateFunction::new(
                            AggregateFunctionFunc::FirstValue,
                            vec![e],    // <--- to avoid a clone here
                            false,
                            None,
                            sort_expr.clone(),
                            None,
                        ))
                    })
                    .collect::<Vec<Expr>>();

What bothers me most is that I cannot avoid cloning sort_expr. However, it looks like the only way to make it correct.

Copy link
Contributor

@alamb alamb left a comment

Choose a reason for hiding this comment

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

This looks pretty cool @ClSlaid -- thank you 🙏

Signed-off-by: 蔡略 <cailue@bupt.edu.cn>
@alamb alamb changed the title refactor: replace distinct with aggr Stop copying LogicalPlan and Exprs in ReplaceDistinctWithAggregate May 12, 2024
Copy link
Contributor

@alamb alamb left a comment

Choose a reason for hiding this comment

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

Thank you @ClSlaid -- this is looking close.

Copy link
Contributor

@alamb alamb left a comment

Choose a reason for hiding this comment

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

I reviewed this PR and I think it looks very nice to me @ClSlaid . Thank you

Also thank you for the suggestion about LogicalPlanBuilder -- perhaps you would be willing to do a follow on (to implement #10485 and then switch this code to use it)?

https://github.com/apache/datafusion/pull/10460/files#r1598480059

Copy link
Contributor

@alamb alamb left a comment

Choose a reason for hiding this comment

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

I took the liberty of adding a comment to this pr and merging up from main. I plan to merge when it passes CI

THanks again @ClSlaid

@alamb alamb merged commit a2eca29 into apache:main May 13, 2024
23 checks passed
findepi pushed a commit to findepi/datafusion that referenced this pull request Jul 16, 2024
…pache#10460)

* patch: implement rewrite for RDWA

Signed-off-by: cailue <cailue@bupt.edu.cn>

* refactor: rewrite replace_distinct_aggregate

Signed-off-by: 蔡略 <cailue@bupt.edu.cn>

* patch: recorrect aggr_expr

Signed-off-by: 蔡略 <cailue@bupt.edu.cn>

* Update datafusion/optimizer/src/replace_distinct_aggregate.rs

---------

Signed-off-by: cailue <cailue@bupt.edu.cn>
Signed-off-by: 蔡略 <cailue@bupt.edu.cn>
Co-authored-by: Andrew Lamb <andrew@nerdnetworks.org>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
optimizer Optimizer rules
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Stop copying LogicalPlan and Exprs in ReplaceDistinctWithAggregate
2 participants