Skip to content

Commit 2c568b4

Browse files
perf: share one StatisticsContext across all physical optimizer rules (apache#26094)
## Which issue does this PR close? - Part of apache#8227. - Related to apache#25141. ## Rationale for this change Each physical optimizer rule that reads statistics creates its own `StatisticsContext`, so the statistics of the same plan nodes are computed again in every rule. `JoinSelection` creates a new context for every `get_stats` call, so it has no caching at all, even within its own pass. apache#25098 showed the gain of sharing one context inside `EnsureDistribution`. apache#25929 made cache entries keep the plan node they were computed for, so a context can now be shared safely across plan rewrites. This PR uses that to share one `StatisticsContext` across all the rules of one `optimize_physical_plan` call. On TPC-DS (98 queries) and TPC-H (21 queries), sf1 Parquet, planned with a local harness (not part of this PR): | | `main` | this PR | |---|---|---| | Statistics cache misses (statistics computed from scratch) | 46,022 | 25,428 (-45%) | | Physical planning time, sum over all queries | 294.8 / 293.5 ms | 274.3 / 279.7 ms (about -6%) | | Physical plans | | identical for all 119 queries | The largest gain is TPC-DS q64: cache misses go from 3,785 to 531 and planning time drops by about 21%. ## What changes are included in this PR? - `StatisticsContext` stores its cache in a `parking_lot::Mutex` instead of `Rc<RefCell>`, so it is `Send + Sync`. This is needed because `PhysicalOptimizerContext: Send + Sync`. The lock is held only for single map lookups and inserts, never across the recursive walk. The `compute_statistics` benchmark shows no difference from `main` (all cases within ±4%, in both directions). - New `PhysicalOptimizerContext::statistics_context()`, which returns `None` by default. `DefaultPhysicalPlanner` creates one context per `optimize_physical_plan` call, built from the session's statistics registry, and returns it to every rule. - `ConfigOnlyContext` also owns a `StatisticsContext`, so a rule called through `optimize()` shares one cache for its whole pass. - `JoinSelection`, `EnsureRequirements` (including `PlanSize::from_plan`), `AggregateStatistics` and `LimitPushdown` use the shared context. When a context does not share one (for example, a context received through FFI), the rules create a new context from the statistics registry, as they did before. - New default method `PhysicalOptimizerContext::compute_statistics`, which custom rules can call on the context they already receive. Without it, a custom rule would have to repeat the fallback itself (use the shared context, otherwise build one from `statistics_registry()`), and the obvious shortcut, `StatisticsContext::new()`, does not consult the registered statistics providers. With it, custom rules behave exactly like the built-in ones. - The fallback itself is the public function `with_statistics_context`. Rules that walk the plan (`EnsureRequirements`, `LimitPushdown`) use it to get one `StatisticsContext` for the whole traversal, so a fallback context still caches within the pass. - `pushdown_limit_helper` is deprecated in favour of the new `pushdown_limit_helper_with_stats`, which takes a `&StatisticsContext`. This is the same pattern as `ensure_distribution_with_stats`. - The shared cache keeps every plan node whose statistics were computed alive until `optimize_physical_plan` returns, including nodes that a later rule replaces. Only the rules that read statistics add entries, and the memory is released at the end of physical planning. ## What is the testing strategy for this PR? - New `optimizer_rules_share_statistics_context` test in `physical_planner.rs`: two rules compute the root statistics through the shared context, and the second one gets the `Arc` cached by the first. - Existing tests cover the rewired rules. All sqllogictests pass with no plan changes. ## Are there any user-facing changes? No breaking changes. - New default methods `PhysicalOptimizerContext::statistics_context()` and `PhysicalOptimizerContext::compute_statistics()`. - New public functions `datafusion_session::with_statistics_context` and `pushdown_limit_helper_with_stats`; `pushdown_limit_helper` is deprecated. All are described in the 56.0.0 upgrade guide. - `StatisticsContext` is now `Send + Sync`. - `AggregateStatistics`, `LimitPushdown` and `PlanSize::from_plan` now consult the session's statistics providers, as `JoinSelection` and `EnsureRequirements` already do. Without registered providers (the default), their results are unchanged. ---- Disclaimer: I used AI to assist in the code generation, I have manually reviewed the output and it matches my intention and understanding. --------- Co-authored-by: Qi Zhu <[email protected]>
1 parent 45de64c commit 2c568b4

14 files changed

Lines changed: 463 additions & 140 deletions

File tree

‎datafusion/core/src/physical_planner.rs‎

Lines changed: 65 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,7 @@ use arrow_schema::Field;
6464
use datafusion_catalog::ScanArgs;
6565
use datafusion_common::Column;
6666
use datafusion_common::HashMap as DFHashMap;
67+
use datafusion_common::config::ConfigOptions;
6768
use datafusion_common::display::ToStringifiedPlan;
6869
use datafusion_common::format::ExplainAnalyzeCategories;
6970
use datafusion_common::tree_node::{Transformed, TreeNode, TreeNodeRecursion};
@@ -103,9 +104,11 @@ use datafusion_physical_expr::{
103104
use datafusion_physical_plan::empty::EmptyExec;
104105
use datafusion_physical_plan::execution_plan::InvariantLevel;
105106
use datafusion_physical_plan::joins::PiecewiseMergeJoinExec;
107+
use datafusion_physical_plan::operator_statistics::StatisticsRegistry;
106108
use datafusion_physical_plan::placeholder_row::PlaceholderRowExec;
107109
use datafusion_physical_plan::recursive_query::RecursiveQueryExec;
108110
use datafusion_physical_plan::scalar_subquery::{ScalarSubqueryExec, ScalarSubqueryLink};
111+
use datafusion_physical_plan::statistics::StatisticsContext;
109112
use datafusion_physical_plan::unnest::ListUnnest;
110113
use datafusion_session::{PhysicalOptimizerContext, PhysicalOptimizerRule, Session};
111114

@@ -123,18 +126,34 @@ pub use datafusion_session::{ExtensionPlanner, PhysicalPlanner};
123126

124127
struct SessionOptimizerContext<'a> {
125128
session: &'a dyn Session,
129+
statistics_context: StatisticsContext,
130+
}
131+
132+
impl<'a> SessionOptimizerContext<'a> {
133+
fn new(session: &'a dyn Session) -> Self {
134+
let statistics_context = match session.statistics_registry() {
135+
Some(registry) => StatisticsContext::new_with_registry(registry.clone()),
136+
None => StatisticsContext::new(),
137+
};
138+
Self {
139+
session,
140+
statistics_context,
141+
}
142+
}
126143
}
127144

128145
impl PhysicalOptimizerContext for SessionOptimizerContext<'_> {
129-
fn config_options(&self) -> &datafusion_common::config::ConfigOptions {
146+
fn config_options(&self) -> &ConfigOptions {
130147
self.session.config_options()
131148
}
132149

133-
fn statistics_registry(
134-
&self,
135-
) -> Option<&datafusion_physical_plan::operator_statistics::StatisticsRegistry> {
150+
fn statistics_registry(&self) -> Option<&StatisticsRegistry> {
136151
self.session.statistics_registry()
137152
}
153+
154+
fn statistics_context(&self) -> Option<&StatisticsContext> {
155+
Some(&self.statistics_context)
156+
}
138157
}
139158

140159
/// Default single node physical query planner that converts a
@@ -3125,9 +3144,7 @@ impl DefaultPhysicalPlanner {
31253144
InvariantChecker(InvariantLevel::Always).check(&plan)?;
31263145

31273146
let mut new_plan = Arc::clone(&plan);
3128-
let optimizer_context = SessionOptimizerContext {
3129-
session: session_state,
3130-
};
3147+
let optimizer_context = SessionOptimizerContext::new(session_state);
31313148
for optimizer in optimizers {
31323149
let before_schema = new_plan.schema();
31333150
new_plan = optimizer
@@ -3538,6 +3555,7 @@ mod tests {
35383555
use arrow::datatypes::{DataType, Field, Int32Type};
35393556
use arrow_schema::{FieldRef, SchemaRef};
35403557
use datafusion_catalog::CatalogProviderList;
3558+
use datafusion_common::Statistics;
35413559
use datafusion_common::config::{ConfigOptions, TableOptions};
35423560
use datafusion_common::{
35433561
DFSchemaRef, ScalarValue, SplitPoint, TableReference, ToDFSchema as _,
@@ -3560,12 +3578,16 @@ mod tests {
35603578
use datafusion_functions_aggregate::expr_fn::sum;
35613579
use datafusion_physical_expr::EquivalenceProperties;
35623580
use datafusion_physical_plan::execution_plan::{Boundedness, EmissionType};
3581+
use datafusion_physical_plan::operator_statistics::StatisticsRegistry;
3582+
use datafusion_physical_plan::statistics::StatisticsArgs;
35633583
use datafusion_physical_plan::{ChildrenPropertiesMode, ReplaceChildrenOptions};
35643584
use datafusion_session::QueryPlanner;
3585+
use parking_lot::Mutex as SyncMutex;
35653586

3587+
/// Records the root statistics computed with the shared statistics context
35663588
#[derive(Debug)]
35673589
struct ContextCheckingRule {
3568-
invoked: Arc<AtomicBool>,
3590+
recorded: Arc<SyncMutex<Vec<Arc<Statistics>>>>,
35693591
}
35703592

35713593
impl PhysicalOptimizerRule for ContextCheckingRule {
@@ -3583,7 +3605,12 @@ mod tests {
35833605
context: &dyn PhysicalOptimizerContext,
35843606
) -> Result<Arc<dyn ExecutionPlan>> {
35853607
assert!(context.statistics_registry().is_some());
3586-
self.invoked.store(true, AtomicOrdering::Relaxed);
3608+
let statistics_context = context
3609+
.statistics_context()
3610+
.expect("the planner shares a statistics context");
3611+
let statistics =
3612+
statistics_context.compute_arc(&plan, &StatisticsArgs::new())?;
3613+
self.recorded.lock().push(statistics);
35873614
Ok(plan)
35883615
}
35893616

@@ -3646,10 +3673,7 @@ mod tests {
36463673
self.inner.physical_optimizers()
36473674
}
36483675

3649-
fn statistics_registry(
3650-
&self,
3651-
) -> Option<&datafusion_physical_plan::operator_statistics::StatisticsRegistry>
3652-
{
3676+
fn statistics_registry(&self) -> Option<&StatisticsRegistry> {
36533677
self.inner.statistics_registry()
36543678
}
36553679

@@ -3818,15 +3842,13 @@ mod tests {
38183842

38193843
#[tokio::test]
38203844
async fn plans_with_non_session_state_implementation() -> Result<()> {
3821-
let invoked = Arc::new(AtomicBool::new(false));
3845+
let recorded = Arc::new(SyncMutex::new(vec![]));
38223846
let inner = SessionStateBuilder::new()
38233847
.with_default_features()
38243848
.with_physical_optimizer_rules(vec![Arc::new(ContextCheckingRule {
3825-
invoked: Arc::clone(&invoked),
3849+
recorded: Arc::clone(&recorded),
38263850
})])
3827-
.with_statistics_registry(
3828-
datafusion_physical_plan::operator_statistics::StatisticsRegistry::new(),
3829-
)
3851+
.with_statistics_registry(StatisticsRegistry::new())
38303852
.build();
38313853
let query_planner_invoked = Arc::new(AtomicBool::new(false));
38323854
let session = TestSession {
@@ -3841,7 +3863,31 @@ mod tests {
38413863
let physical_plan = session.create_physical_plan(&logical_plan).await?;
38423864
assert!(physical_plan.is::<EmptyExec>());
38433865
assert!(query_planner_invoked.load(AtomicOrdering::Relaxed));
3844-
assert!(invoked.load(AtomicOrdering::Relaxed));
3866+
assert_eq!(recorded.lock().len(), 1);
3867+
Ok(())
3868+
}
3869+
3870+
#[tokio::test]
3871+
async fn optimizer_rules_share_statistics_context() -> Result<()> {
3872+
let recorded = Arc::new(SyncMutex::new(vec![]));
3873+
let rule = || -> Arc<dyn PhysicalOptimizerRule + Send + Sync> {
3874+
Arc::new(ContextCheckingRule {
3875+
recorded: Arc::clone(&recorded),
3876+
})
3877+
};
3878+
let session_state = SessionStateBuilder::new()
3879+
.with_default_features()
3880+
.with_physical_optimizer_rules(vec![rule(), rule()])
3881+
.with_statistics_registry(StatisticsRegistry::new())
3882+
.build();
3883+
3884+
let logical_plan = LogicalPlanBuilder::empty(false).build()?;
3885+
session_state.create_physical_plan(&logical_plan).await?;
3886+
3887+
// The second rule reads the statistics the first rule cached
3888+
let recorded = recorded.lock();
3889+
assert_eq!(recorded.len(), 2);
3890+
assert!(Arc::ptr_eq(&recorded[0], &recorded[1]));
38453891
Ok(())
38463892
}
38473893

‎datafusion/core/tests/physical_optimizer/aggregate_statistics.rs‎

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ use datafusion::datasource::memory::MemTable;
2828
use datafusion::datasource::memory::MemorySourceConfig;
2929
use datafusion::datasource::physical_plan::ParquetSource;
3030
use datafusion::datasource::source::DataSourceExec;
31+
use datafusion::execution::session_state::SessionStateBuilder;
3132
use datafusion::prelude::{SessionConfig, SessionContext};
3233
use datafusion_common::cast::as_int64_array;
3334
use datafusion_common::config::ConfigOptions;
@@ -52,7 +53,11 @@ use datafusion_physical_plan::coalesce_partitions::CoalescePartitionsExec;
5253
use datafusion_physical_plan::common;
5354
use datafusion_physical_plan::displayable;
5455
use datafusion_physical_plan::filter::FilterExec;
56+
use datafusion_physical_plan::operator_statistics::{
57+
ClosureStatisticsProvider, StatisticsRegistry, StatisticsResult,
58+
};
5559
use datafusion_physical_plan::projection::ProjectionExec;
60+
use datafusion_physical_plan::statistics::StatisticsArgs;
5661

5762
/// Mock data using a MemorySourceConfig which has an exact count statistic
5863
fn mock_data() -> Result<Arc<DataSourceExec>> {
@@ -856,3 +861,47 @@ async fn test_sum_from_statistics() -> Result<()> {
856861

857862
Ok(())
858863
}
864+
865+
/// `AggregateStatistics` trusts an `Exact` row count from a registered
866+
/// provider: it answers `COUNT(*)` without scanning
867+
#[tokio::test]
868+
async fn aggregate_statistics_consults_statistics_providers() -> Result<()> {
869+
let provider = ClosureStatisticsProvider::with_matches(
870+
|plan| plan.is::<DataSourceExec>(),
871+
|plan, child_stats| {
872+
let child_stats = child_stats
873+
.iter()
874+
.map(|c| Arc::clone(c.base_arc()))
875+
.collect::<Vec<_>>();
876+
let mut stats = Arc::unwrap_or_clone(
877+
plan.statistics_from_inputs(&child_stats, &StatisticsArgs::new())?,
878+
);
879+
stats.num_rows = Precision::Exact(42);
880+
Ok(StatisticsResult::Computed(stats.into()))
881+
},
882+
);
883+
let state = SessionStateBuilder::new()
884+
.with_default_features()
885+
.with_statistics_registry(StatisticsRegistry::with_providers(vec![Arc::new(
886+
provider,
887+
)]))
888+
.build();
889+
let ctx = SessionContext::new_with_state(state);
890+
ctx.sql("CREATE TABLE t AS VALUES (1), (2), (3)")
891+
.await?
892+
.collect()
893+
.await?;
894+
895+
let batches = ctx.sql("SELECT COUNT(*) FROM t").await?.collect().await?;
896+
assert_batches_eq!(
897+
&[
898+
"+----------+",
899+
"| count(*) |",
900+
"+----------+",
901+
"| 42 |",
902+
"+----------+"
903+
],
904+
&batches
905+
);
906+
Ok(())
907+
}

‎datafusion/core/tests/physical_optimizer/limit_pushdown.rs‎

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,20 +24,28 @@ use crate::physical_optimizer::test_utils::{
2424

2525
use arrow::compute::SortOptions;
2626
use arrow::datatypes::{DataType, Field, Schema, SchemaRef};
27+
use datafusion::execution::session_state::SessionStateBuilder;
28+
use datafusion::prelude::{SessionConfig, SessionContext};
2729
use datafusion_common::config::ConfigOptions;
2830
use datafusion_common::error::Result;
31+
use datafusion_common::stats::Precision;
2932
use datafusion_expr::{JoinType, Operator};
3033
use datafusion_physical_expr::Partitioning;
3134
use datafusion_physical_expr::expressions::{BinaryExpr, col, lit};
3235
use datafusion_physical_expr_common::physical_expr::PhysicalExprRef;
3336
use datafusion_physical_expr_common::sort_expr::{LexOrdering, PhysicalSortExpr};
3437
use datafusion_physical_optimizer::PhysicalOptimizerRule;
3538
use datafusion_physical_optimizer::limit_pushdown::LimitPushdown;
39+
use datafusion_physical_plan::aggregates::AggregateExec;
3640
use datafusion_physical_plan::empty::EmptyExec;
3741
use datafusion_physical_plan::filter::FilterExec;
3842
use datafusion_physical_plan::joins::NestedLoopJoinExec;
43+
use datafusion_physical_plan::operator_statistics::{
44+
ClosureStatisticsProvider, StatisticsRegistry, StatisticsResult,
45+
};
3946
use datafusion_physical_plan::projection::ProjectionExec;
4047
use datafusion_physical_plan::repartition::RepartitionExec;
48+
use datafusion_physical_plan::statistics::StatisticsArgs;
4149
use datafusion_physical_plan::{ExecutionPlan, get_plan_string};
4250

4351
fn create_schema() -> SchemaRef {
@@ -834,3 +842,43 @@ fn outer_offset_with_same_sort_key_still_pushes_limit() -> Result<()> {
834842

835843
Ok(())
836844
}
845+
846+
/// `LimitPushdown` trusts an `Exact(0)` row count from a registered
847+
/// provider: it removes a limit over that input
848+
#[tokio::test]
849+
async fn limit_pushdown_consults_statistics_providers() -> Result<()> {
850+
let provider = ClosureStatisticsProvider::with_matches(
851+
|plan| plan.is::<AggregateExec>(),
852+
|plan, child_stats| {
853+
let child_stats = child_stats
854+
.iter()
855+
.map(|c| Arc::clone(c.base_arc()))
856+
.collect::<Vec<_>>();
857+
let mut stats = Arc::unwrap_or_clone(
858+
plan.statistics_from_inputs(&child_stats, &StatisticsArgs::new())?,
859+
);
860+
stats.num_rows = Precision::Exact(0);
861+
Ok(StatisticsResult::Computed(stats.into()))
862+
},
863+
);
864+
let state = SessionStateBuilder::new()
865+
.with_default_features()
866+
.with_config(SessionConfig::new().with_target_partitions(1))
867+
.with_statistics_registry(StatisticsRegistry::with_providers(vec![Arc::new(
868+
provider,
869+
)]))
870+
.build();
871+
let ctx = SessionContext::new_with_state(state);
872+
ctx.sql("CREATE TABLE t AS VALUES (1), (2), (3)")
873+
.await?
874+
.collect()
875+
.await?;
876+
877+
let batches = ctx
878+
.sql("SELECT column1, count(*) FROM t GROUP BY column1 LIMIT 1")
879+
.await?
880+
.collect()
881+
.await?;
882+
assert_eq!(batches.iter().map(|b| b.num_rows()).sum::<usize>(), 3);
883+
Ok(())
884+
}

‎datafusion/physical-optimizer/src/aggregate_statistics.rs‎

Lines changed: 20 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -25,14 +25,15 @@ use datafusion_physical_plan::aggregates::{
2525
};
2626
use datafusion_physical_plan::placeholder_row::PlaceholderRowExec;
2727
use datafusion_physical_plan::projection::{ProjectionExec, ProjectionExpr};
28-
use datafusion_physical_plan::statistics::{StatisticsArgs, StatisticsContext};
28+
use datafusion_physical_plan::statistics::StatisticsArgs;
2929
use datafusion_physical_plan::udaf::{
3030
AggregateFunctionExpr, StatisticsArgs as PlanStatisticsArgs,
3131
};
3232
use datafusion_physical_plan::{ExecutionPlan, expressions};
3333
use std::sync::Arc;
3434

3535
use crate::PhysicalOptimizerRule;
36+
use crate::optimizer::{ConfigOnlyContext, PhysicalOptimizerContext};
3637

3738
/// Optimizer that uses available statistics for aggregate functions
3839
#[derive(Default, Debug)]
@@ -46,20 +47,26 @@ impl AggregateStatistics {
4647
}
4748

4849
impl PhysicalOptimizerRule for AggregateStatistics {
49-
#[cfg_attr(feature = "recursive_protection", recursive::recursive)]
50-
#[expect(clippy::allow_attributes)] // See https://github.com/apache/datafusion/issues/18881#issuecomment-3621545670
51-
#[allow(clippy::only_used_in_recursion)] // See https://github.com/rust-lang/rust-clippy/issues/14566
5250
fn optimize(
5351
&self,
5452
plan: Arc<dyn ExecutionPlan>,
5553
config: &ConfigOptions,
54+
) -> Result<Arc<dyn ExecutionPlan>> {
55+
self.optimize_with_context(plan, &ConfigOnlyContext::new(config))
56+
}
57+
58+
#[cfg_attr(feature = "recursive_protection", recursive::recursive)]
59+
fn optimize_with_context(
60+
&self,
61+
plan: Arc<dyn ExecutionPlan>,
62+
context: &dyn PhysicalOptimizerContext,
5663
) -> Result<Arc<dyn ExecutionPlan>> {
5764
if let Some(partial_agg_exec) = take_optimizable(&plan) {
5865
let partial_agg_exec = partial_agg_exec
5966
.downcast_ref::<AggregateExec>()
6067
.expect("take_optimizable() ensures that this is a AggregateExec");
61-
let stats = StatisticsContext::new()
62-
.compute(partial_agg_exec.input().as_ref(), &StatisticsArgs::new())?;
68+
let stats = context
69+
.compute_statistics(partial_agg_exec.input(), &StatisticsArgs::new())?;
6370
let mut projections = vec![];
6471
for expr in partial_agg_exec.aggr_expr() {
6572
let field = expr.field();
@@ -92,13 +99,17 @@ impl PhysicalOptimizerRule for AggregateStatistics {
9299
)?))
93100
} else {
94101
plan.map_children(|child| {
95-
self.optimize(child, config).map(Transformed::yes)
102+
self.optimize_with_context(child, context)
103+
.map(Transformed::yes)
96104
})
97105
.data()
98106
}
99107
} else {
100-
plan.map_children(|child| self.optimize(child, config).map(Transformed::yes))
101-
.data()
108+
plan.map_children(|child| {
109+
self.optimize_with_context(child, context)
110+
.map(Transformed::yes)
111+
})
112+
.data()
102113
}
103114
}
104115

0 commit comments

Comments
 (0)