Skip to content

Add support for AggregateExpr, WindowExpr rewrite.#10742

Merged
alamb merged 4 commits intoapache:mainfrom
synnada-ai:feature/all_expressions
May 31, 2024
Merged

Add support for AggregateExpr, WindowExpr rewrite.#10742
alamb merged 4 commits intoapache:mainfrom
synnada-ai:feature/all_expressions

Conversation

@mustafasrepo
Copy link
Copy Markdown
Contributor

Which issue does this PR close?

Closes #.

Rationale for this change

While working on another PR, in which I implement a new custom rule. I hit a usecase where I want to re-write AggregateExpr, and WindowExpr according to some kind of projection. (For instance , in terms of the schema of the child). In my use case, I find it useful to define an API fn all_expressions(&self) -> (function args, order bys, ..) to return all expressions referred by the aggregate or window function (By the way current fn expressions(&self) API only returns function arguments).

With this API in place, we can update Arc<dyn PhysicalExpr>s, at the outside (such as inside the rule). Then we can use updated expressions to re-write Arc<dyn AggregateExpr> by using following API:

fn with_new_expressions(
    &self,
    _args: Vec<Arc<dyn PhysicalExpr>>,
    _order_by_exprs: Vec<Arc<dyn PhysicalExpr>>,
) -> Option<Arc<dyn AggregateExpr>>

This behaviour, is very similar to the fn expressions(), , fn with_expressions() methods for LogicalPlans.
Also as far as I am aware @berkaysynnada's PR for projection optimization will also require such an API, to be able to check pushdown an aggregate expression or window expression successfully.

What changes are included in this PR?

Are these changes tested?

Are there any user-facing changes?

@github-actions github-actions bot added the physical-expr Changes to the physical-expr crates label May 31, 2024
Copy link
Copy Markdown
Member

@waynexia waynexia left a comment

Choose a reason for hiding this comment

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

Those new APIs look good to me, thanks @mustafasrepo

Comment thread datafusion/physical-expr-common/src/aggregate/mod.rs Outdated
/// Rewrites [`AggregateExpr`], with new expressions given. The argument should be consistent
/// with the return value of the [`AggregateExpr::all_expressions`] method.
/// Returns `Some(Arc<dyn AggregateExpr>)` if re-write is supported, otherwise returns `None`.
fn with_new_expressions(
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

The only thing that might be worth considering is that this API forces cloning. However, since everything is Arcd that seems relatively minor. Maybe something to keep in mind for the future.

@alamb alamb merged commit 68f8476 into apache:main May 31, 2024
@alamb
Copy link
Copy Markdown
Contributor

alamb commented May 31, 2024

Thanks @mustafasrepo and @waynexia

findepi pushed a commit to findepi/datafusion that referenced this pull request Jul 16, 2024
* Initial commit

* Minor changes

* Minor changes

* Update comments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

physical-expr Changes to the physical-expr crates

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants