Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Minor: Add getter for logical optimizer rules #12379

Merged
merged 3 commits into from
Sep 8, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
34 changes: 34 additions & 0 deletions datafusion/core/src/execution/session_state.rs
Original file line number Diff line number Diff line change
Expand Up @@ -801,6 +801,11 @@ impl SessionState {
&mut self.config
}

/// Return the logical optimizers
pub fn optimizers(&self) -> &[Arc<dyn OptimizerRule + Send + Sync>] {
&self.optimizer.rules
}

/// Return the physical optimizers
pub fn physical_optimizers(&self) -> &[Arc<dyn PhysicalOptimizerRule + Send + Sync>] {
&self.physical_optimizers.rules
Expand Down Expand Up @@ -1826,6 +1831,8 @@ mod tests {
use datafusion_common::Result;
use datafusion_execution::config::SessionConfig;
use datafusion_expr::Expr;
use datafusion_optimizer::optimizer::OptimizerRule;
use datafusion_optimizer::Optimizer;
use datafusion_sql::planner::{PlannerContext, SqlToRel};
use std::collections::HashMap;
use std::sync::Arc;
Expand Down Expand Up @@ -1922,4 +1929,31 @@ mod tests {
assert!(new_state.catalog_list().catalog(&default_catalog).is_none());
Ok(())
}

#[test]
fn test_session_state_with_optimizer_rules() {
struct DummyRule {}

impl OptimizerRule for DummyRule {
fn name(&self) -> &str {
"dummy_rule"
}
}
// test building sessions with fresh set of rules
let state = SessionStateBuilder::new()
.with_optimizer_rules(vec![Arc::new(DummyRule {})])
.build();

assert_eq!(state.optimizers().len(), 1);

// test adding rules to default recommendations
let state = SessionStateBuilder::new()
.with_optimizer_rule(Arc::new(DummyRule {}))
.build();

assert_eq!(
state.optimizers().len(),
Optimizer::default().rules.len() + 1
);
}
}