-
Notifications
You must be signed in to change notification settings - Fork 1.1k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
e39b5ca
commit 222c458
Showing
14 changed files
with
597 additions
and
33 deletions.
There are no files selected for viewing
103 changes: 103 additions & 0 deletions
103
datafusion/core/src/physical_optimizer/limit_aggregation.rs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,103 @@ | ||
// 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. | ||
|
||
//! An optimizer rule that detects aggregate operations that could use a limited bucket count | ||
|
||
use crate::physical_optimizer::PhysicalOptimizerRule; | ||
use crate::physical_plan::aggregates::AggregateExec; | ||
use crate::physical_plan::sorts::sort::SortExec; | ||
use crate::physical_plan::ExecutionPlan; | ||
use datafusion_common::config::ConfigOptions; | ||
use datafusion_common::{DataFusionError, Result}; | ||
use std::sync::Arc; | ||
|
||
/// An optimizer rule that passes a `limit` hint to aggregations if the whole result is not needed | ||
pub struct LimitAggregation {} | ||
|
||
impl LimitAggregation { | ||
/// Create a new `LimitAggregation` | ||
pub fn new() -> Self { | ||
Self {} | ||
} | ||
|
||
fn recurse(plan: Arc<dyn ExecutionPlan>) -> Result<Arc<dyn ExecutionPlan>> { | ||
// Not a sort | ||
let sort = if let Some(sort) = plan.as_any().downcast_ref::<SortExec>() { | ||
sort | ||
} else { | ||
return Ok(plan); | ||
}; | ||
|
||
// Error if sorting with two inputs | ||
let children = sort.children(); | ||
let child = match children.as_slice() { | ||
[] => Err(DataFusionError::Execution( | ||
"Sorts should have children".to_string(), | ||
))?, | ||
[child] => child, | ||
_ => Err(DataFusionError::Execution( | ||
"Sorts should have 1 child".to_string(), | ||
))?, | ||
}; | ||
|
||
// Sort doesn't have an aggregate before it | ||
let binding = (*child).as_any(); | ||
let aggr = if let Some(aggr) = binding.downcast_ref::<AggregateExec>() { | ||
aggr | ||
} else { | ||
return Ok(plan); | ||
}; | ||
|
||
// We found what we want: clone, copy the limit down, and return modified node | ||
let mut new_aggr = AggregateExec::try_new( | ||
aggr.mode, | ||
aggr.group_by.clone(), | ||
aggr.aggr_expr.clone(), | ||
aggr.filter_expr.clone(), | ||
aggr.order_by_expr.clone(), | ||
aggr.input.clone(), | ||
aggr.input_schema.clone(), | ||
)?; | ||
new_aggr.limit = sort.fetch(); | ||
let plan = Arc::new(SortExec::new(sort.expr().to_vec(), Arc::new(new_aggr))); | ||
Ok(plan) | ||
} | ||
} | ||
|
||
impl Default for LimitAggregation { | ||
fn default() -> Self { | ||
Self::new() | ||
} | ||
} | ||
|
||
impl PhysicalOptimizerRule for LimitAggregation { | ||
fn optimize( | ||
&self, | ||
plan: Arc<dyn ExecutionPlan>, | ||
_config: &ConfigOptions, | ||
) -> Result<Arc<dyn ExecutionPlan>> { | ||
LimitAggregation::recurse(plan.clone()) | ||
} | ||
|
||
fn name(&self) -> &str { | ||
"limit aggregation" | ||
} | ||
|
||
fn schema_check(&self) -> bool { | ||
true | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.