Skip to main content

datafusion_session/
physical_optimizer.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//! Physical optimizer interfaces.
19
20use std::fmt::Debug;
21use std::sync::Arc;
22
23use datafusion_common::Result;
24use datafusion_common::config::ConfigOptions;
25use datafusion_physical_plan::ExecutionPlan;
26use datafusion_physical_plan::operator_statistics::StatisticsRegistry;
27
28/// Context available to physical optimizer rules.
29///
30/// This trait provides access to configuration options and an optional statistics
31/// registry for enhanced statistics lookup.
32pub trait PhysicalOptimizerContext: Send + Sync {
33    /// Returns the configuration options.
34    fn config_options(&self) -> &ConfigOptions;
35
36    /// Returns the statistics registry for enhanced statistics lookup.
37    ///
38    /// Returns `None` if no registry is configured, in which case rules
39    /// should fall back to using [`ExecutionPlan::partition_statistics`].
40    fn statistics_registry(&self) -> Option<&StatisticsRegistry> {
41        None
42    }
43}
44
45/// `PhysicalOptimizerRule` transforms one [`ExecutionPlan`] into another which
46/// computes the same results, but in a potentially more efficient way.
47///
48/// Use [`SessionState::add_physical_optimizer_rule`] to register additional
49/// `PhysicalOptimizerRule`s.
50///
51/// [`SessionState::add_physical_optimizer_rule`]: https://docs.rs/datafusion/latest/datafusion/execution/session_state/struct.SessionState.html#method.add_physical_optimizer_rule
52pub trait PhysicalOptimizerRule: Debug + std::any::Any {
53    /// Rewrite `plan` to an optimized form.
54    ///
55    /// This is the primary optimization method. For rules that need access to
56    /// the statistics registry, override [`optimize_with_context`](Self::optimize_with_context) instead.
57    fn optimize(
58        &self,
59        plan: Arc<dyn ExecutionPlan>,
60        config: &ConfigOptions,
61    ) -> Result<Arc<dyn ExecutionPlan>>;
62
63    /// Rewrite `plan` with access to extended context (statistics registry, etc.).
64    ///
65    /// Override this method if you need access to the statistics registry for
66    /// enhanced statistics lookup. The default implementation simply calls
67    /// [`optimize`](Self::optimize) with the config options from the context.
68    fn optimize_with_context(
69        &self,
70        plan: Arc<dyn ExecutionPlan>,
71        context: &dyn PhysicalOptimizerContext,
72    ) -> Result<Arc<dyn ExecutionPlan>> {
73        self.optimize(plan, context.config_options())
74    }
75
76    /// A human readable name for this optimizer rule
77    fn name(&self) -> &str;
78
79    /// A flag to indicate whether the physical planner should validate that the rule will not
80    /// change the schema of the plan after the rewriting.
81    /// Some of the optimization rules might change the nullable properties of the schema
82    /// and should disable the schema check.
83    fn schema_check(&self) -> bool;
84}