|
| 1 | +// Licensed to the Apache Software Foundation (ASF) under one |
| 2 | +// or more contributor license agreements. See the NOTICE file |
| 3 | +// distributed with this work for additional information |
| 4 | +// regarding copyright ownership. The ASF licenses this file |
| 5 | +// to you under the Apache License, Version 2.0 (the |
| 6 | +// "License"); you may not use this file except in compliance |
| 7 | +// with the License. You may obtain a copy of the License at |
| 8 | +// |
| 9 | +// http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | +// |
| 11 | +// Unless required by applicable law or agreed to in writing, |
| 12 | +// software distributed under the License is distributed on an |
| 13 | +// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY |
| 14 | +// KIND, either express or implied. See the License for the |
| 15 | +// specific language governing permissions and limitations |
| 16 | +// under the License. |
| 17 | + |
| 18 | +//! An optimizer rule that detects TopK operations that can generate dynamic filters to be pushed down into file scans |
| 19 | +
|
| 20 | +use std::sync::Arc; |
| 21 | + |
| 22 | +use crate::PhysicalOptimizerRule; |
| 23 | +use datafusion_common::config::ConfigOptions; |
| 24 | +use datafusion_common::tree_node::{TransformedResult, TreeNode}; |
| 25 | +use datafusion_common::Result; |
| 26 | +use datafusion_physical_plan::ExecutionPlan; |
| 27 | + |
| 28 | +/// An optimizer rule that passes a TopK as a DynamicFilterSource to DataSourceExec executors. |
| 29 | +/// |
| 30 | +/// This optimizer looks for TopK operators in the plan and connects them to compatible |
| 31 | +/// data sources by registering the TopK as a source of dynamic filters. |
| 32 | +/// |
| 33 | +/// When a data source is found that can benefit from a TopK's filter, the source is |
| 34 | +/// modified to include the TopK as a dynamic filter source via `with_dynamic_filter_source`. |
| 35 | +/// During execution, the data source will then consult the TopK's current filter state |
| 36 | +/// to determine which files or partitions can be skipped. |
| 37 | +#[derive(Debug)] |
| 38 | +pub struct TopKDynamicFilters {} |
| 39 | + |
| 40 | +impl PhysicalOptimizerRule for TopKDynamicFilters { |
| 41 | + fn optimize( |
| 42 | + &self, |
| 43 | + plan: Arc<dyn ExecutionPlan>, |
| 44 | + config: &ConfigOptions, |
| 45 | + ) -> Result<Arc<dyn ExecutionPlan>> { |
| 46 | + if config.optimizer.enable_dynamic_filter_pushdown { |
| 47 | + plan.transform_down(|_plan| { |
| 48 | + // TODO: recurse only traversing approved nodes (e.g., FilterExec, ProjectionExec, SortExec, CoalesceBatchesExec, RepartitionExec; namely not aggregations, joins, etc.) |
| 49 | + // Collect any DataSourceExecs that can use dynamic filters and register the TopK as a source |
| 50 | + todo!(); |
| 51 | + }) |
| 52 | + .data() |
| 53 | + } else { |
| 54 | + Ok(plan) |
| 55 | + } |
| 56 | + } |
| 57 | + |
| 58 | + fn name(&self) -> &str { |
| 59 | + "TopKDynamicFilters" |
| 60 | + } |
| 61 | + |
| 62 | + fn schema_check(&self) -> bool { |
| 63 | + true |
| 64 | + } |
| 65 | +} |
0 commit comments