diff --git a/datafusion/optimizer/src/analyzer/count_empty_rule.rs b/datafusion/optimizer/src/analyzer/count_empty_rule.rs deleted file mode 100644 index dc67998f4c6c..000000000000 --- a/datafusion/optimizer/src/analyzer/count_empty_rule.rs +++ /dev/null @@ -1,91 +0,0 @@ -// 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. - -use datafusion_common::tree_node::{Transformed, TreeNode}; -use datafusion_common::Result; -use datafusion_common::{config::ConfigOptions, tree_node::TransformedResult}; -use datafusion_expr::utils::COUNT_STAR_EXPANSION; -use datafusion_expr::{ - expr::{AggregateFunction, AggregateFunctionDefinition, WindowFunction}, - LogicalPlan, WindowFunctionDefinition, -}; -use datafusion_expr::{lit, Expr}; - -use crate::utils::NamePreserver; -use crate::AnalyzerRule; - -/// Rewrite `Count()` to `Count(Expr:Literal(1))`. -#[derive(Default)] -pub struct CountEmptyRule {} - -impl CountEmptyRule { - /// Create a new instance of the rule - pub fn new() -> Self { - Self {} - } -} - -impl AnalyzerRule for CountEmptyRule { - fn analyze(&self, plan: LogicalPlan, _: &ConfigOptions) -> Result { - plan.transform_down_with_subqueries(analyze_internal).data() - } - - fn name(&self) -> &str { - "count_empty_rule" - } -} - -fn is_count_empty_aggregate(aggregate_function: &AggregateFunction) -> bool { - matches!(aggregate_function, - AggregateFunction { - func_def: AggregateFunctionDefinition::UDF(udf), - args, - .. - } if udf.name() == "count" && args.is_empty()) -} - -fn is_count_empty_window_aggregate(window_function: &WindowFunction) -> bool { - let args = &window_function.args; - matches!(window_function.fun, - WindowFunctionDefinition::AggregateUDF(ref udaf) - if udaf.name() == "count" && args.is_empty()) -} - -fn analyze_internal(plan: LogicalPlan) -> Result> { - let name_preserver = NamePreserver::new(&plan); - plan.map_expressions(|expr| { - let original_name = name_preserver.save(&expr)?; - let transformed_expr = expr.transform_up(|expr| match expr { - Expr::WindowFunction(mut window_function) - if is_count_empty_window_aggregate(&window_function) => - { - window_function.args = vec![lit(COUNT_STAR_EXPANSION)]; - Ok(Transformed::yes(Expr::WindowFunction(window_function))) - } - Expr::AggregateFunction(mut aggregate_function) - if is_count_empty_aggregate(&aggregate_function) => - { - aggregate_function.args = vec![lit(COUNT_STAR_EXPANSION)]; - Ok(Transformed::yes(Expr::AggregateFunction( - aggregate_function, - ))) - } - _ => Ok(Transformed::no(expr)), - })?; - transformed_expr.map_data(|data| original_name.restore(data)) - }) -} diff --git a/datafusion/optimizer/src/analyzer/count_wildcard_rule.rs b/datafusion/optimizer/src/analyzer/count_wildcard_rule.rs index 1c482b15319a..fa8aeb86ed31 100644 --- a/datafusion/optimizer/src/analyzer/count_wildcard_rule.rs +++ b/datafusion/optimizer/src/analyzer/count_wildcard_rule.rs @@ -59,14 +59,14 @@ fn is_count_star_aggregate(aggregate_function: &AggregateFunction) -> bool { func_def: AggregateFunctionDefinition::UDF(udf), args, .. - } if udf.name() == "count" && args.len() == 1 && is_wildcard(&args[0])) + } if udf.name() == "count" && (args.len() == 1 && is_wildcard(&args[0]) || args.is_empty())) } fn is_count_star_window_aggregate(window_function: &WindowFunction) -> bool { let args = &window_function.args; matches!(window_function.fun, WindowFunctionDefinition::AggregateUDF(ref udaf) - if udaf.name() == "count" && args.len() == 1 && is_wildcard(&args[0])) + if udaf.name() == "count" && (args.len() == 1 && is_wildcard(&args[0]) || args.is_empty())) } fn analyze_internal(plan: LogicalPlan) -> Result> { diff --git a/datafusion/optimizer/src/analyzer/mod.rs b/datafusion/optimizer/src/analyzer/mod.rs index 622b5fab1eca..32bb2bc70452 100644 --- a/datafusion/optimizer/src/analyzer/mod.rs +++ b/datafusion/optimizer/src/analyzer/mod.rs @@ -29,7 +29,6 @@ use datafusion_expr::expr::InSubquery; use datafusion_expr::expr_rewriter::FunctionRewrite; use datafusion_expr::{Expr, LogicalPlan}; -use crate::analyzer::count_empty_rule::CountEmptyRule; use crate::analyzer::count_wildcard_rule::CountWildcardRule; use crate::analyzer::inline_table_scan::InlineTableScan; use crate::analyzer::subquery::check_subquery_expr; @@ -38,7 +37,6 @@ use crate::utils::log_plan; use self::function_rewrite::ApplyFunctionRewrites; -pub mod count_empty_rule; pub mod count_wildcard_rule; pub mod function_rewrite; pub mod inline_table_scan; @@ -93,7 +91,6 @@ impl Analyzer { Arc::new(InlineTableScan::new()), Arc::new(TypeCoercion::new()), Arc::new(CountWildcardRule::new()), - Arc::new(CountEmptyRule::new()), ]; Self::with_rules(rules) } diff --git a/datafusion/sqllogictest/test_files/count_empty_rule.slt b/datafusion/sqllogictest/test_files/count_star_rule.slt similarity index 100% rename from datafusion/sqllogictest/test_files/count_empty_rule.slt rename to datafusion/sqllogictest/test_files/count_star_rule.slt diff --git a/datafusion/sqllogictest/test_files/explain.slt b/datafusion/sqllogictest/test_files/explain.slt index 04dc27cf618c..3a4e8072bbc7 100644 --- a/datafusion/sqllogictest/test_files/explain.slt +++ b/datafusion/sqllogictest/test_files/explain.slt @@ -183,7 +183,6 @@ logical_plan after apply_function_rewrites SAME TEXT AS ABOVE logical_plan after inline_table_scan SAME TEXT AS ABOVE logical_plan after type_coercion SAME TEXT AS ABOVE logical_plan after count_wildcard_rule SAME TEXT AS ABOVE -logical_plan after count_empty_rule SAME TEXT AS ABOVE analyzed_logical_plan SAME TEXT AS ABOVE logical_plan after eliminate_nested_union SAME TEXT AS ABOVE logical_plan after simplify_expressions SAME TEXT AS ABOVE