Dandandan commented on a change in pull request #620: URL: https://github.com/apache/arrow-datafusion/pull/620#discussion_r659339611
########## File path: datafusion/src/optimizer/aggregate_statistics.rs ########## @@ -0,0 +1,217 @@ +// 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. + +//! Utilizing exact statistics from sources to avoid scanning data +use std::sync::Arc; + +use crate::{ + execution::context::ExecutionProps, + logical_plan::{DFSchema, Expr, LogicalPlan}, + physical_plan::aggregates::AggregateFunction, + scalar::ScalarValue, +}; + +use super::{optimizer::OptimizerRule, utils}; +use crate::error::Result; + +/// Optimizer that uses available statistics for aggregate functions +pub struct AggregateStatistics {} + +impl AggregateStatistics { + #[allow(missing_docs)] + pub fn new() -> Self { + Self {} + } +} + +impl OptimizerRule for AggregateStatistics { + fn optimize( + &self, + plan: &LogicalPlan, + execution_props: &ExecutionProps, + ) -> crate::error::Result<LogicalPlan> { + match plan { + // match only select count(*) from table_scan + LogicalPlan::Aggregate { + input, + group_expr, + aggr_expr, + schema, + } if group_expr.is_empty() && aggr_expr.len() == 1 => { + if let Some(num_rows) = match input.as_ref() { + LogicalPlan::TableScan { source, .. } + if source.has_exact_statistics() => + { + source.statistics().num_rows + } + _ => None, + } { + return match &aggr_expr[0] { + Expr::AggregateFunction { + fun: AggregateFunction::Count, + args, + distinct: false, + } if args == &[Expr::Literal(ScalarValue::UInt8(Some(1)))] => { Review comment: We have `group_expr.is_empty()`, but we should add a test for this as well 👍 -- 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]
