Skip to content

Commit e1dc962

Browse files
authored
Move ConfigOptions to core (#4803)
* Move ConfigOptions to core * Fix defaults * Toml format
1 parent ae1465d commit e1dc962

File tree

13 files changed

+65
-109
lines changed

13 files changed

+65
-109
lines changed

datafusion-cli/Cargo.lock

Lines changed: 1 addition & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

datafusion-examples/examples/rewrite_expr.rs

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
// under the License.
1717

1818
use arrow::datatypes::{DataType, Field, Schema, SchemaRef};
19+
use datafusion_common::config::ConfigOptions;
1920
use datafusion_common::{DataFusionError, Result};
2021
use datafusion_expr::expr_rewriter::{ExprRewritable, ExprRewriter};
2122
use datafusion_expr::{
@@ -37,7 +38,7 @@ pub fn main() -> Result<()> {
3738
let statements = Parser::parse_sql(&dialect, sql)?;
3839

3940
// produce a logical plan using the datafusion-sql crate
40-
let context_provider = MyContextProvider {};
41+
let context_provider = MyContextProvider::default();
4142
let sql_to_rel = SqlToRel::new(&context_provider);
4243
let logical_plan = sql_to_rel.sql_statement_to_plan(statements[0].clone())?;
4344
println!(
@@ -120,7 +121,10 @@ impl ExprRewriter for MyExprRewriter {
120121
}
121122
}
122123

123-
struct MyContextProvider {}
124+
#[derive(Default)]
125+
struct MyContextProvider {
126+
options: ConfigOptions,
127+
}
124128

125129
impl ContextProvider for MyContextProvider {
126130
fn get_table_provider(&self, name: TableReference) -> Result<Arc<dyn TableSource>> {
@@ -148,11 +152,8 @@ impl ContextProvider for MyContextProvider {
148152
None
149153
}
150154

151-
fn get_config_option(
152-
&self,
153-
_variable: &str,
154-
) -> Option<datafusion_common::ScalarValue> {
155-
None
155+
fn options(&self) -> &ConfigOptions {
156+
&self.options
156157
}
157158
}
158159

datafusion/common/Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@ apache-avro = { version = "0.14", default-features = false, features = ["snappy"
4343
arrow = { version = "29.0.0", default-features = false }
4444
chrono = { version = "0.4", default-features = false }
4545
cranelift-module = { version = "0.89.0", optional = true }
46+
num_cpus = "1.13.0"
4647
object_store = { version = "0.5.0", default-features = false, optional = true }
4748
parquet = { version = "29.0.0", default-features = false, optional = true }
4849
pyo3 = { version = "0.17.1", optional = true }

datafusion/core/src/config.rs renamed to datafusion/common/src/config.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@
1717

1818
//! DataFusion Configuration Options
1919
20-
use datafusion_common::{DataFusionError, Result};
20+
use crate::{DataFusionError, Result};
2121
use std::any::Any;
2222
use std::collections::BTreeMap;
2323
use std::fmt::Display;

datafusion/common/src/lib.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
pub mod bisect;
1919
pub mod cast;
2020
mod column;
21+
pub mod config;
2122
pub mod delta;
2223
mod dfschema;
2324
mod error;

datafusion/core/src/execution/context.rs

Lines changed: 4 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -1757,18 +1757,8 @@ impl ContextProvider for SessionState {
17571757
.and_then(|provider| provider.get(&provider_type)?.get_type(variable_names))
17581758
}
17591759

1760-
fn get_config_option(&self, variable: &str) -> Option<ScalarValue> {
1761-
// TOOD: Move ConfigOptions into common crate
1762-
match variable {
1763-
"datafusion.execution.time_zone" => self
1764-
.config
1765-
.options
1766-
.execution
1767-
.time_zone
1768-
.as_ref()
1769-
.map(|s| ScalarValue::Utf8(Some(s.clone()))),
1770-
_ => unimplemented!(),
1771-
}
1760+
fn options(&self) -> &ConfigOptions {
1761+
self.config_options()
17721762
}
17731763
}
17741764

@@ -1803,22 +1793,8 @@ impl OptimizerConfig for SessionState {
18031793
self.execution_props.query_execution_start_time
18041794
}
18051795

1806-
fn rule_enabled(&self, name: &str) -> bool {
1807-
use datafusion_optimizer::filter_null_join_keys::FilterNullJoinKeys;
1808-
match name {
1809-
FilterNullJoinKeys::NAME => {
1810-
self.config_options().optimizer.filter_null_join_keys
1811-
}
1812-
_ => true,
1813-
}
1814-
}
1815-
1816-
fn skip_failing_rules(&self) -> bool {
1817-
self.config_options().optimizer.skip_failed_rules
1818-
}
1819-
1820-
fn max_passes(&self) -> u8 {
1821-
self.config_options().optimizer.max_passes as _
1796+
fn options(&self) -> &ConfigOptions {
1797+
self.config_options()
18221798
}
18231799
}
18241800

datafusion/core/src/lib.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -215,7 +215,6 @@ extern crate sqlparser;
215215

216216
pub mod avro_to_arrow;
217217
pub mod catalog;
218-
pub mod config;
219218
pub mod dataframe;
220219
pub mod datasource;
221220
pub mod error;
@@ -234,6 +233,7 @@ pub use parquet;
234233

235234
// re-export DataFusion crates
236235
pub use datafusion_common as common;
236+
pub use datafusion_common::config;
237237
pub use datafusion_expr as logical_expr;
238238
pub use datafusion_optimizer as optimizer;
239239
pub use datafusion_physical_expr as physical_expr;

datafusion/core/tests/sqllogictests/src/insert/util.rs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,8 @@
1616
// under the License.
1717

1818
use arrow::datatypes::DataType;
19-
use datafusion_common::{ScalarValue, TableReference};
19+
use datafusion_common::config::ConfigOptions;
20+
use datafusion_common::TableReference;
2021
use datafusion_expr::{AggregateUDF, ScalarUDF, TableSource};
2122
use datafusion_sql::planner::ContextProvider;
2223
use std::sync::Arc;
@@ -44,7 +45,7 @@ impl ContextProvider for LogicTestContextProvider {
4445
todo!()
4546
}
4647

47-
fn get_config_option(&self, _variable: &str) -> Option<ScalarValue> {
48+
fn options(&self) -> &ConfigOptions {
4849
todo!()
4950
}
5051
}

datafusion/optimizer/src/filter_null_join_keys.rs

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,8 +43,12 @@ impl OptimizerRule for FilterNullJoinKeys {
4343
fn try_optimize(
4444
&self,
4545
plan: &LogicalPlan,
46-
_config: &dyn OptimizerConfig,
46+
config: &dyn OptimizerConfig,
4747
) -> Result<Option<LogicalPlan>> {
48+
if !config.options().optimizer.filter_null_join_keys {
49+
return Ok(None);
50+
}
51+
4852
match plan {
4953
LogicalPlan::Join(join) if join.join_type == JoinType::Inner => {
5054
let mut join = join.clone();

datafusion/optimizer/src/optimizer.rs

Lines changed: 16 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@ use crate::single_distinct_to_groupby::SingleDistinctToGroupBy;
3838
use crate::type_coercion::TypeCoercion;
3939
use crate::unwrap_cast_in_comparison::UnwrapCastInComparison;
4040
use chrono::{DateTime, Utc};
41+
use datafusion_common::config::ConfigOptions;
4142
use datafusion_common::{DataFusionError, Result};
4243
use datafusion_expr::logical_plan::LogicalPlan;
4344
use log::{debug, trace, warn};
@@ -74,14 +75,7 @@ pub trait OptimizerConfig {
7475
/// time is used as the value for now()
7576
fn query_execution_start_time(&self) -> DateTime<Utc>;
7677

77-
/// Returns false if the given rule should be skipped
78-
fn rule_enabled(&self, name: &str) -> bool;
79-
80-
/// The optimizer will skip failing rules if this returns true
81-
fn skip_failing_rules(&self) -> bool;
82-
83-
/// How many times to attempt to optimize the plan
84-
fn max_passes(&self) -> u8;
78+
fn options(&self) -> &ConfigOptions;
8579
}
8680

8781
/// A standalone [`OptimizerConfig`] that can be used independently
@@ -91,28 +85,25 @@ pub struct OptimizerContext {
9185
/// Query execution start time that can be used to rewrite
9286
/// expressions such as `now()` to use a literal value instead
9387
query_execution_start_time: DateTime<Utc>,
94-
/// Option to skip rules that produce errors
95-
skip_failing_rules: bool,
96-
/// Specify whether to enable the filter_null_keys rule
97-
filter_null_keys: bool,
98-
/// Maximum number of times to run optimizer against a plan
99-
max_passes: u8,
88+
89+
options: ConfigOptions,
10090
}
10191

10292
impl OptimizerContext {
10393
/// Create optimizer config
10494
pub fn new() -> Self {
95+
let mut options = ConfigOptions::default();
96+
options.optimizer.filter_null_join_keys = true;
97+
10598
Self {
10699
query_execution_start_time: Utc::now(),
107-
skip_failing_rules: true,
108-
filter_null_keys: true,
109-
max_passes: 3,
100+
options,
110101
}
111102
}
112103

113104
/// Specify whether to enable the filter_null_keys rule
114105
pub fn filter_null_keys(mut self, filter_null_keys: bool) -> Self {
115-
self.filter_null_keys = filter_null_keys;
106+
self.options.optimizer.filter_null_join_keys = filter_null_keys;
116107
self
117108
}
118109

@@ -129,13 +120,13 @@ impl OptimizerContext {
129120
/// Specify whether the optimizer should skip rules that produce
130121
/// errors, or fail the query
131122
pub fn with_skip_failing_rules(mut self, b: bool) -> Self {
132-
self.skip_failing_rules = b;
123+
self.options.optimizer.skip_failed_rules = b;
133124
self
134125
}
135126

136127
/// Specify how many times to attempt to optimize the plan
137128
pub fn with_max_passes(mut self, v: u8) -> Self {
138-
self.max_passes = v;
129+
self.options.optimizer.max_passes = v as usize;
139130
self
140131
}
141132
}
@@ -152,16 +143,8 @@ impl OptimizerConfig for OptimizerContext {
152143
self.query_execution_start_time
153144
}
154145

155-
fn rule_enabled(&self, name: &str) -> bool {
156-
self.filter_null_keys || name != FilterNullJoinKeys::NAME
157-
}
158-
159-
fn skip_failing_rules(&self) -> bool {
160-
self.skip_failing_rules
161-
}
162-
163-
fn max_passes(&self) -> u8 {
164-
self.max_passes
146+
fn options(&self) -> &ConfigOptions {
147+
&self.options
165148
}
166149
}
167150

@@ -271,18 +254,15 @@ impl Optimizer {
271254
where
272255
F: FnMut(&LogicalPlan, &dyn OptimizerRule),
273256
{
257+
let options = config.options();
274258
let start_time = Instant::now();
275259
let mut plan_str = format!("{}", plan.display_indent());
276260
let mut new_plan = plan.clone();
277261
let mut i = 0;
278-
while i < config.max_passes() {
262+
while i < options.optimizer.max_passes {
279263
log_plan(&format!("Optimizer input (pass {i})"), &new_plan);
280264

281265
for rule in &self.rules {
282-
if !config.rule_enabled(rule.name()) {
283-
debug!("Skipping rule {} due to optimizer config", rule.name());
284-
continue;
285-
}
286266
let result = self.optimize_recursively(rule, &new_plan, config);
287267

288268
match result {
@@ -308,7 +288,7 @@ impl Optimizer {
308288
);
309289
}
310290
Err(ref e) => {
311-
if config.skip_failing_rules() {
291+
if options.optimizer.skip_failed_rules {
312292
// Note to future readers: if you see this warning it signals a
313293
// bug in the DataFusion optimizer. Please consider filing a ticket
314294
// https://github.com/apache/arrow-datafusion

0 commit comments

Comments
 (0)