datafusion_physical_optimizer/
projection_pushdown.rs

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//! This file implements the `ProjectionPushdown` physical optimization rule.
19//! The function [`remove_unnecessary_projections`] tries to push down all
20//! projections one by one if the operator below is amenable to this. If a
21//! projection reaches a source, it can even disappear from the plan entirely.
22
23use std::sync::Arc;
24
25use crate::PhysicalOptimizerRule;
26
27use datafusion_common::config::ConfigOptions;
28use datafusion_common::tree_node::{TransformedResult, TreeNode};
29use datafusion_common::Result;
30use datafusion_physical_plan::projection::remove_unnecessary_projections;
31use datafusion_physical_plan::ExecutionPlan;
32
33/// This rule inspects `ProjectionExec`'s in the given physical plan and tries to
34/// remove or swap with its child.
35#[derive(Default, Debug)]
36pub struct ProjectionPushdown {}
37
38impl ProjectionPushdown {
39    #[allow(missing_docs)]
40    pub fn new() -> Self {
41        Self {}
42    }
43}
44
45impl PhysicalOptimizerRule for ProjectionPushdown {
46    fn optimize(
47        &self,
48        plan: Arc<dyn ExecutionPlan>,
49        _config: &ConfigOptions,
50    ) -> Result<Arc<dyn ExecutionPlan>> {
51        plan.transform_down(remove_unnecessary_projections).data()
52    }
53
54    fn name(&self) -> &str {
55        "ProjectionPushdown"
56    }
57
58    fn schema_check(&self) -> bool {
59        true
60    }
61}