alamb commented on code in PR #8636: URL: https://github.com/apache/arrow-datafusion/pull/8636#discussion_r1439572579
########## datafusion/optimizer/src/analyzer/rewrite_expr.rs: ########## @@ -0,0 +1,232 @@ +// 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. + +//! Optimizer rule for expression rewrite + +use datafusion_common::config::ConfigOptions; +use datafusion_common::tree_node::TreeNode; +use datafusion_common::tree_node::TreeNodeRewriter; +use datafusion_common::Result; +use datafusion_expr::expr::ScalarFunction; +use datafusion_expr::BuiltinScalarFunction; +use datafusion_expr::Operator; +use datafusion_expr::Projection; +use datafusion_expr::ScalarFunctionDefinition; +use datafusion_expr::{BinaryExpr, Expr, LogicalPlan}; + +use super::AnalyzerRule; + +#[derive(Default)] +pub struct OperatorToFunction {} + +impl OperatorToFunction { + pub fn new() -> Self { + Self {} + } +} + +impl AnalyzerRule for OperatorToFunction { + fn name(&self) -> &str { + "operator_to_function" + } + + fn analyze(&self, plan: LogicalPlan, _: &ConfigOptions) -> Result<LogicalPlan> { + analyze_internal(plan) + } +} + +fn analyze_internal(plan: LogicalPlan) -> Result<LogicalPlan> { + // OperatorToFunction is only applied to Projection + match plan { + LogicalPlan::Projection(_) => {} Review Comment: This will not catch uses of operators in all places (like `LogicalPlan::Filter`) I think you can follow the model of https://github.com/apache/arrow-datafusion/blob/f4233a92761e9144b8747e66b95bf0b3f82464b8/datafusion/optimizer/src/analyzer/type_coercion.rs#L74-L122 and call `LogicalPlan::expressions()` to get all the expressions in a `LogicalPlan` node, rewrite them appropriately, and then call` LogicaPlan::new_with_exprs` to get the rewritten node ```rust let new_expr = plan .expressions() .into_iter() .map(|expr| { // ensure aggregate names don't change: // https://github.com/apache/arrow-datafusion/issues/3555 rewrite_preserving_name(expr, &mut expr_rewrite) }) .collect::<Result<Vec<_>>>()?; ``` ########## datafusion/optimizer/src/analyzer/rewrite_expr.rs: ########## @@ -0,0 +1,232 @@ +// 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. + +//! Optimizer rule for expression rewrite Review Comment: ```suggestion //! Analyzer rule for to replace operators with function calls (e.g `||` to array_concat`) ``` -- 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]
