Skip to content

Commit 3dd8da1

Browse files
committed
test: record shared statistics in the existing ContextCheckingRule
1 parent 3179bd9 commit 3dd8da1

1 file changed

Lines changed: 11 additions & 46 deletions

File tree

‎datafusion/core/src/physical_planner.rs‎

Lines changed: 11 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -3572,14 +3572,16 @@ mod tests {
35723572
use datafusion_functions_aggregate::expr_fn::sum;
35733573
use datafusion_physical_expr::EquivalenceProperties;
35743574
use datafusion_physical_plan::execution_plan::{Boundedness, EmissionType};
3575+
use datafusion_physical_plan::operator_statistics::StatisticsRegistry;
35753576
use datafusion_physical_plan::statistics::StatisticsArgs;
35763577
use datafusion_physical_plan::{ChildrenPropertiesMode, ReplaceChildrenOptions};
35773578
use datafusion_session::QueryPlanner;
35783579
use parking_lot::Mutex as SyncMutex;
35793580

3581+
/// Records the root statistics computed with the shared statistics context
35803582
#[derive(Debug)]
35813583
struct ContextCheckingRule {
3582-
invoked: Arc<AtomicBool>,
3584+
recorded: Arc<SyncMutex<Vec<Arc<Statistics>>>>,
35833585
}
35843586

35853587
impl PhysicalOptimizerRule for ContextCheckingRule {
@@ -3597,39 +3599,6 @@ mod tests {
35973599
context: &dyn PhysicalOptimizerContext,
35983600
) -> Result<Arc<dyn ExecutionPlan>> {
35993601
assert!(context.statistics_registry().is_some());
3600-
self.invoked.store(true, AtomicOrdering::Relaxed);
3601-
Ok(plan)
3602-
}
3603-
3604-
fn name(&self) -> &str {
3605-
"context_checking_rule"
3606-
}
3607-
3608-
fn schema_check(&self) -> bool {
3609-
true
3610-
}
3611-
}
3612-
3613-
/// Records the root statistics computed with the shared statistics context
3614-
#[derive(Debug)]
3615-
struct StatisticsRecordingRule {
3616-
recorded: Arc<SyncMutex<Vec<Arc<Statistics>>>>,
3617-
}
3618-
3619-
impl PhysicalOptimizerRule for StatisticsRecordingRule {
3620-
fn optimize(
3621-
&self,
3622-
plan: Arc<dyn ExecutionPlan>,
3623-
_config: &ConfigOptions,
3624-
) -> Result<Arc<dyn ExecutionPlan>> {
3625-
Ok(plan)
3626-
}
3627-
3628-
fn optimize_with_context(
3629-
&self,
3630-
plan: Arc<dyn ExecutionPlan>,
3631-
context: &dyn PhysicalOptimizerContext,
3632-
) -> Result<Arc<dyn ExecutionPlan>> {
36333602
let statistics_context = context
36343603
.statistics_context()
36353604
.expect("the planner shares a statistics context");
@@ -3640,7 +3609,7 @@ mod tests {
36403609
}
36413610

36423611
fn name(&self) -> &str {
3643-
"statistics_recording_rule"
3612+
"context_checking_rule"
36443613
}
36453614

36463615
fn schema_check(&self) -> bool {
@@ -3698,10 +3667,7 @@ mod tests {
36983667
self.inner.physical_optimizers()
36993668
}
37003669

3701-
fn statistics_registry(
3702-
&self,
3703-
) -> Option<&datafusion_physical_plan::operator_statistics::StatisticsRegistry>
3704-
{
3670+
fn statistics_registry(&self) -> Option<&StatisticsRegistry> {
37053671
self.inner.statistics_registry()
37063672
}
37073673

@@ -3870,15 +3836,13 @@ mod tests {
38703836

38713837
#[tokio::test]
38723838
async fn plans_with_non_session_state_implementation() -> Result<()> {
3873-
let invoked = Arc::new(AtomicBool::new(false));
3839+
let recorded = Arc::new(SyncMutex::new(vec![]));
38743840
let inner = SessionStateBuilder::new()
38753841
.with_default_features()
38763842
.with_physical_optimizer_rules(vec![Arc::new(ContextCheckingRule {
3877-
invoked: Arc::clone(&invoked),
3843+
recorded: Arc::clone(&recorded),
38783844
})])
3879-
.with_statistics_registry(
3880-
datafusion_physical_plan::operator_statistics::StatisticsRegistry::new(),
3881-
)
3845+
.with_statistics_registry(StatisticsRegistry::new())
38823846
.build();
38833847
let query_planner_invoked = Arc::new(AtomicBool::new(false));
38843848
let session = TestSession {
@@ -3893,21 +3857,22 @@ mod tests {
38933857
let physical_plan = session.create_physical_plan(&logical_plan).await?;
38943858
assert!(physical_plan.is::<EmptyExec>());
38953859
assert!(query_planner_invoked.load(AtomicOrdering::Relaxed));
3896-
assert!(invoked.load(AtomicOrdering::Relaxed));
3860+
assert_eq!(recorded.lock().len(), 1);
38973861
Ok(())
38983862
}
38993863

39003864
#[tokio::test]
39013865
async fn optimizer_rules_share_statistics_context() -> Result<()> {
39023866
let recorded = Arc::new(SyncMutex::new(vec![]));
39033867
let rule = || -> Arc<dyn PhysicalOptimizerRule + Send + Sync> {
3904-
Arc::new(StatisticsRecordingRule {
3868+
Arc::new(ContextCheckingRule {
39053869
recorded: Arc::clone(&recorded),
39063870
})
39073871
};
39083872
let session_state = SessionStateBuilder::new()
39093873
.with_default_features()
39103874
.with_physical_optimizer_rules(vec![rule(), rule()])
3875+
.with_statistics_registry(StatisticsRegistry::new())
39113876
.build();
39123877

39133878
let logical_plan = LogicalPlanBuilder::empty(false).build()?;

0 commit comments

Comments
 (0)