jayzhan211 commented on code in PR #11013: URL: https://github.com/apache/datafusion/pull/11013#discussion_r1651124885
########## datafusion/optimizer/src/eliminate_distinct.rs: ########## @@ -0,0 +1,140 @@ + +// 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. + +//! [`EliminateDistinctFromMinMax`] Removes redundant distinct in min and max + +use crate::optimizer::ApplyOrder; +use crate::{OptimizerConfig, OptimizerRule}; +use datafusion_common::tree_node::Transformed; +use datafusion_common::{internal_err, Result}; +use datafusion_expr::expr::AggregateFunction; +use datafusion_expr::logical_plan::LogicalPlan; +use datafusion_expr::{Aggregate, Expr}; +use std::sync::OnceLock; + +/// Optimization rule that eliminate redundant distinct in min and max expr. +#[derive(Default)] +pub struct EliminateDistinct; + +impl EliminateDistinct { + #[allow(missing_docs)] + pub fn new() -> Self { + Self {} + } +} +static WORKSPACE_ROOT_LOCK: OnceLock<Vec<String>> = OnceLock::new(); + +fn rewrite_aggr_expr(expr:Expr) -> (bool, Expr) { + match expr { + Expr::AggregateFunction(ref fun) => { + let fn_name = fun.func_def.name().to_lowercase(); + if fun.distinct && WORKSPACE_ROOT_LOCK.get_or_init(|| vec!["min".to_string(), "max".to_string()]).contains(&fn_name) { Review Comment: Do we need oncelock here? I think `or` is enough 🤔 -- 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] --------------------------------------------------------------------- To unsubscribe, e-mail: [email protected] For additional commands, e-mail: [email protected]
