alamb commented on code in PR #5092:
URL: https://github.com/apache/arrow-datafusion/pull/5092#discussion_r1089924851
##########
datafusion/expr/src/expr_rewriter.rs:
##########
@@ -519,6 +496,49 @@ pub fn unnormalize_cols(exprs: impl IntoIterator<Item =
Expr>) -> Vec<Expr> {
exprs.into_iter().map(unnormalize_col).collect()
}
+struct RewriterAdapter<F> {
+ f: F,
+}
+
+impl<F> ExprRewriter for RewriterAdapter<F>
+where
+ F: FnMut(Expr) -> Result<Expr>,
+{
+ fn mutate(&mut self, expr: Expr) -> Result<Expr> {
+ (self.f)(expr)
+ }
+}
+
+/// Recursively rewrite an [`Expr`] via a function.
+///
+/// Rewrites the expression bottom up by recursively calling `f(expr)`
+/// on `expr`'s children and then on expr. See [`ExprRewriter`]
+/// for more details and more options to control the walk.
+///
+/// # Example:
+/// ```
+/// # use datafusion_expr::*;
+/// # use datafusion_expr::expr_rewriter::rewrite_expr;
+/// let expr = col("a") + lit(1);
+///
+/// // rewrite all literals to 42
+/// let rewritten = rewrite_expr(expr, |e| {
+/// if let Expr::Literal(_) = e {
+/// Ok(lit(42))
+/// } else {
+/// Ok(e)
+/// }
+/// }).unwrap();
+///
+/// assert_eq!(rewritten, col("a") + lit(42));
+/// ```
+pub fn rewrite_expr<F>(expr: Expr, f: F) -> Result<Expr>
Review Comment:
The real change in this PR is to add this function.
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: [email protected]
For queries about this service, please contact Infrastructure at:
[email protected]