mingmwang commented on code in PR #3855: URL: https://github.com/apache/arrow-datafusion/pull/3855#discussion_r1000381852
########## datafusion/physical-expr/src/utils.rs: ########## @@ -0,0 +1,117 @@ +// Licensed to the Apache Software Foundation (ASF) under one +// or more contributor license agreements. See the NOTICE file +// distributed with this work for additional information +// regarding copyright ownership. The ASF licenses this file +// to you under the Apache License, Version 2.0 (the +// "License"); you may not use this file except in compliance +// with the License. You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, +// software distributed under the License is distributed on an +// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY +// KIND, either express or implied. See the License for the +// specific language governing permissions and limitations +// under the License. + +//! Collection of utility functions for Physical Expr optimization + +use crate::PhysicalExpr; +use datafusion_common::DataFusionError; +use std::result; +use std::sync::Arc; + +pub type Result<T> = result::Result<T, DataFusionError>; + +/// Apply transform `F` to the PhysicalExpr's children, the transform `F` might have a direction(Preorder or Postorder) +fn map_children<F>( + expr: Arc<dyn PhysicalExpr>, + transform: F, +) -> Result<Arc<dyn PhysicalExpr>> +where + F: Fn(Arc<dyn PhysicalExpr>) -> Result<Arc<dyn PhysicalExpr>>, +{ + if !expr.children().is_empty() { + let new_children: Result<Vec<_>> = + expr.children().into_iter().map(transform).collect(); + with_new_children_if_necessary(expr, new_children?) + } else { + Ok(expr) + } +} + +/// Convenience utils for writing optimizers rule: recursively apply the given `op` to the PhysicalExpr tree. +/// When `op` does not apply to a given expr, it is left unchanged. +/// The default tree traversal direction is transform_down(Preorder Traversal). +pub fn transform<F>(expr: Arc<dyn PhysicalExpr>, op: &F) -> Result<Arc<dyn PhysicalExpr>> +where + F: Fn(Arc<dyn PhysicalExpr>) -> Option<Arc<dyn PhysicalExpr>>, +{ + transform_down(expr, op) +} + +/// Convenience utils for writing optimizers rule: recursively apply the given 'op' to the PhysicalExpr and all of its Review Comment: @alamb I had combined those transform utils and the visitor pattern into a Trait `TreeNodeRewritable`, users can feel free to choose Closure/Lambda expressions or pass in a visitor to do the plan/expr rewriting. TreeNodeRewritable is general enough now and can be applied to both physical plan and physical expr, or even logical plan, please help to take a look. But unfortunately, due to Rust's orphan rule's limitation, the Trait `TreeNodeRewritable` is copied to two crates to make them become Local Trait, still have duplicate code. -- 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]
