Skip to content

Commit d0a1d30

Browse files
authored
refactor: remove unneed mut for session context (#11864)
* doc: remove mut from session context docstring * refactor: remove unnecessary mut for session context * refactor: remove more unused mut
1 parent 60d1d3a commit d0a1d30

File tree

14 files changed

+65
-62
lines changed

14 files changed

+65
-62
lines changed

datafusion-cli/examples/cli-session-context.rs

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -82,7 +82,7 @@ impl CliSessionContext for MyUnionerContext {
8282
#[tokio::main]
8383
/// Runs the example.
8484
pub async fn main() {
85-
let mut my_ctx = MyUnionerContext::default();
85+
let my_ctx = MyUnionerContext::default();
8686

8787
let mut print_options = PrintOptions {
8888
format: datafusion_cli::print_format::PrintFormat::Automatic,
@@ -91,7 +91,5 @@ pub async fn main() {
9191
color: true,
9292
};
9393

94-
exec_from_repl(&mut my_ctx, &mut print_options)
95-
.await
96-
.unwrap();
94+
exec_from_repl(&my_ctx, &mut print_options).await.unwrap();
9795
}

datafusion-cli/src/catalog.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -240,7 +240,7 @@ mod tests {
240240
use datafusion::prelude::SessionContext;
241241

242242
fn setup_context() -> (SessionContext, Arc<dyn SchemaProvider>) {
243-
let mut ctx = SessionContext::new();
243+
let ctx = SessionContext::new();
244244
ctx.register_catalog_list(Arc::new(DynamicFileCatalog::new(
245245
ctx.state().catalog_list().clone(),
246246
ctx.state_weak_ref(),

datafusion-cli/src/command.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@ pub enum OutputFormat {
5555
impl Command {
5656
pub async fn execute(
5757
&self,
58-
ctx: &mut dyn CliSessionContext,
58+
ctx: &dyn CliSessionContext,
5959
print_options: &mut PrintOptions,
6060
) -> Result<()> {
6161
match self {

datafusion-cli/src/exec.rs

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ use tokio::signal;
4949

5050
/// run and execute SQL statements and commands, against a context with the given print options
5151
pub async fn exec_from_commands(
52-
ctx: &mut dyn CliSessionContext,
52+
ctx: &dyn CliSessionContext,
5353
commands: Vec<String>,
5454
print_options: &PrintOptions,
5555
) -> Result<()> {
@@ -62,7 +62,7 @@ pub async fn exec_from_commands(
6262

6363
/// run and execute SQL statements and commands from a file, against a context with the given print options
6464
pub async fn exec_from_lines(
65-
ctx: &mut dyn CliSessionContext,
65+
ctx: &dyn CliSessionContext,
6666
reader: &mut BufReader<File>,
6767
print_options: &PrintOptions,
6868
) -> Result<()> {
@@ -102,7 +102,7 @@ pub async fn exec_from_lines(
102102
}
103103

104104
pub async fn exec_from_files(
105-
ctx: &mut dyn CliSessionContext,
105+
ctx: &dyn CliSessionContext,
106106
files: Vec<String>,
107107
print_options: &PrintOptions,
108108
) -> Result<()> {
@@ -121,7 +121,7 @@ pub async fn exec_from_files(
121121

122122
/// run and execute SQL statements and commands against a context with the given print options
123123
pub async fn exec_from_repl(
124-
ctx: &mut dyn CliSessionContext,
124+
ctx: &dyn CliSessionContext,
125125
print_options: &mut PrintOptions,
126126
) -> rustyline::Result<()> {
127127
let mut rl = Editor::new()?;
@@ -204,7 +204,7 @@ pub async fn exec_from_repl(
204204
}
205205

206206
pub(super) async fn exec_and_print(
207-
ctx: &mut dyn CliSessionContext,
207+
ctx: &dyn CliSessionContext,
208208
print_options: &PrintOptions,
209209
sql: String,
210210
) -> Result<()> {
@@ -300,7 +300,7 @@ fn config_file_type_from_str(ext: &str) -> Option<ConfigFileType> {
300300
}
301301

302302
async fn create_plan(
303-
ctx: &mut dyn CliSessionContext,
303+
ctx: &dyn CliSessionContext,
304304
statement: Statement,
305305
) -> Result<LogicalPlan, DataFusionError> {
306306
let mut plan = ctx.session_state().statement_to_plan(statement).await?;
@@ -473,7 +473,7 @@ mod tests {
473473
"cos://bucket/path/file.parquet",
474474
"gcs://bucket/path/file.parquet",
475475
];
476-
let mut ctx = SessionContext::new();
476+
let ctx = SessionContext::new();
477477
let task_ctx = ctx.task_ctx();
478478
let dialect = &task_ctx.session_config().options().sql_parser.dialect;
479479
let dialect = dialect_from_str(dialect).ok_or_else(|| {
@@ -488,7 +488,7 @@ mod tests {
488488
let statements = DFParser::parse_sql_with_dialect(&sql, dialect.as_ref())?;
489489
for statement in statements {
490490
//Should not fail
491-
let mut plan = create_plan(&mut ctx, statement).await?;
491+
let mut plan = create_plan(&ctx, statement).await?;
492492
if let LogicalPlan::Copy(copy_to) = &mut plan {
493493
assert_eq!(copy_to.output_url, location);
494494
assert_eq!(copy_to.file_type.get_ext(), "parquet".to_string());

datafusion-cli/src/main.rs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -175,7 +175,7 @@ async fn main_inner() -> Result<()> {
175175

176176
let runtime_env = create_runtime_env(rt_config.clone())?;
177177

178-
let mut ctx =
178+
let ctx =
179179
SessionContext::new_with_config_rt(session_config.clone(), Arc::new(runtime_env));
180180
ctx.refresh_catalogs().await?;
181181
// install dynamic catalog provider that knows how to open files
@@ -212,20 +212,20 @@ async fn main_inner() -> Result<()> {
212212

213213
if commands.is_empty() && files.is_empty() {
214214
if !rc.is_empty() {
215-
exec::exec_from_files(&mut ctx, rc, &print_options).await?;
215+
exec::exec_from_files(&ctx, rc, &print_options).await?;
216216
}
217217
// TODO maybe we can have thiserror for cli but for now let's keep it simple
218-
return exec::exec_from_repl(&mut ctx, &mut print_options)
218+
return exec::exec_from_repl(&ctx, &mut print_options)
219219
.await
220220
.map_err(|e| DataFusionError::External(Box::new(e)));
221221
}
222222

223223
if !files.is_empty() {
224-
exec::exec_from_files(&mut ctx, files, &print_options).await?;
224+
exec::exec_from_files(&ctx, files, &print_options).await?;
225225
}
226226

227227
if !commands.is_empty() {
228-
exec::exec_from_commands(&mut ctx, commands, &print_options).await?;
228+
exec::exec_from_commands(&ctx, commands, &print_options).await?;
229229
}
230230

231231
Ok(())

datafusion-examples/examples/catalog.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ async fn main() -> Result<()> {
4444
let dir_a = prepare_example_data()?;
4545
let dir_b = prepare_example_data()?;
4646

47-
let mut ctx = SessionContext::new();
47+
let ctx = SessionContext::new();
4848
let state = ctx.state();
4949
let catlist = Arc::new(CustomCatalogProviderList::new());
5050

datafusion/core/benches/filter_query_sql.rs

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ use futures::executor::block_on;
2727
use std::sync::Arc;
2828
use tokio::runtime::Runtime;
2929

30-
async fn query(ctx: &mut SessionContext, sql: &str) {
30+
async fn query(ctx: &SessionContext, sql: &str) {
3131
let rt = Runtime::new().unwrap();
3232

3333
// execute the query
@@ -70,25 +70,25 @@ fn criterion_benchmark(c: &mut Criterion) {
7070
let batch_size = 4096; // 2^12
7171

7272
c.bench_function("filter_array", |b| {
73-
let mut ctx = create_context(array_len, batch_size).unwrap();
74-
b.iter(|| block_on(query(&mut ctx, "select f32, f64 from t where f32 >= f64")))
73+
let ctx = create_context(array_len, batch_size).unwrap();
74+
b.iter(|| block_on(query(&ctx, "select f32, f64 from t where f32 >= f64")))
7575
});
7676

7777
c.bench_function("filter_scalar", |b| {
78-
let mut ctx = create_context(array_len, batch_size).unwrap();
78+
let ctx = create_context(array_len, batch_size).unwrap();
7979
b.iter(|| {
8080
block_on(query(
81-
&mut ctx,
81+
&ctx,
8282
"select f32, f64 from t where f32 >= 250 and f64 > 250",
8383
))
8484
})
8585
});
8686

8787
c.bench_function("filter_scalar in list", |b| {
88-
let mut ctx = create_context(array_len, batch_size).unwrap();
88+
let ctx = create_context(array_len, batch_size).unwrap();
8989
b.iter(|| {
9090
block_on(query(
91-
&mut ctx,
91+
&ctx,
9292
"select f32, f64 from t where f32 in (10, 20, 30, 40)",
9393
))
9494
})

datafusion/core/src/dataframe/mod.rs

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1550,7 +1550,7 @@ impl DataFrame {
15501550
/// # #[tokio::main]
15511551
/// # async fn main() -> Result<()> {
15521552
/// # use datafusion_common::ScalarValue;
1553-
/// let mut ctx = SessionContext::new();
1553+
/// let ctx = SessionContext::new();
15541554
/// # ctx.register_csv("example", "tests/data/example.csv", CsvReadOptions::new()).await?;
15551555
/// let results = ctx
15561556
/// .sql("SELECT a FROM example WHERE b = $1")
@@ -2649,8 +2649,8 @@ mod tests {
26492649

26502650
#[tokio::test]
26512651
async fn registry() -> Result<()> {
2652-
let mut ctx = SessionContext::new();
2653-
register_aggregate_csv(&mut ctx, "aggregate_test_100").await?;
2652+
let ctx = SessionContext::new();
2653+
register_aggregate_csv(&ctx, "aggregate_test_100").await?;
26542654

26552655
// declare the udf
26562656
let my_fn: ScalarFunctionImplementation =
@@ -2783,8 +2783,8 @@ mod tests {
27832783

27842784
/// Create a logical plan from a SQL query
27852785
async fn create_plan(sql: &str) -> Result<LogicalPlan> {
2786-
let mut ctx = SessionContext::new();
2787-
register_aggregate_csv(&mut ctx, "aggregate_test_100").await?;
2786+
let ctx = SessionContext::new();
2787+
register_aggregate_csv(&ctx, "aggregate_test_100").await?;
27882788
Ok(ctx.sql(sql).await?.into_unoptimized_plan())
27892789
}
27902790

@@ -3147,9 +3147,9 @@ mod tests {
31473147
"datafusion.sql_parser.enable_ident_normalization".to_owned(),
31483148
"false".to_owned(),
31493149
)]))?;
3150-
let mut ctx = SessionContext::new_with_config(config);
3150+
let ctx = SessionContext::new_with_config(config);
31513151
let name = "aggregate_test_100";
3152-
register_aggregate_csv(&mut ctx, name).await?;
3152+
register_aggregate_csv(&ctx, name).await?;
31533153
let df = ctx.table(name);
31543154

31553155
let df = df

datafusion/core/src/dataframe/parquet.rs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -189,14 +189,14 @@ mod tests {
189189
async fn write_parquet_with_small_rg_size() -> Result<()> {
190190
// This test verifies writing a parquet file with small rg size
191191
// relative to datafusion.execution.batch_size does not panic
192-
let mut ctx = SessionContext::new_with_config(
193-
SessionConfig::from_string_hash_map(HashMap::from_iter(
192+
let ctx = SessionContext::new_with_config(SessionConfig::from_string_hash_map(
193+
HashMap::from_iter(
194194
[("datafusion.execution.batch_size", "10")]
195195
.iter()
196196
.map(|(s1, s2)| (s1.to_string(), s2.to_string())),
197-
))?,
198-
);
199-
register_aggregate_csv(&mut ctx, "aggregate_test_100").await?;
197+
),
198+
)?);
199+
register_aggregate_csv(&ctx, "aggregate_test_100").await?;
200200
let test_df = ctx.table("aggregate_test_100").await?;
201201

202202
let output_path = "file://local/test.parquet";

datafusion/core/src/execution/context/mod.rs

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -179,7 +179,7 @@ where
179179
/// # use datafusion::{error::Result, assert_batches_eq};
180180
/// # #[tokio::main]
181181
/// # async fn main() -> Result<()> {
182-
/// let mut ctx = SessionContext::new();
182+
/// let ctx = SessionContext::new();
183183
/// ctx.register_csv("example", "tests/data/example.csv", CsvReadOptions::new()).await?;
184184
/// let results = ctx
185185
/// .sql("SELECT a, min(b) FROM example GROUP BY a LIMIT 100")
@@ -369,7 +369,7 @@ impl SessionContext {
369369
/// # use datafusion_execution::object_store::ObjectStoreUrl;
370370
/// let object_store_url = ObjectStoreUrl::parse("file://").unwrap();
371371
/// let object_store = object_store::local::LocalFileSystem::new();
372-
/// let mut ctx = SessionContext::new();
372+
/// let ctx = SessionContext::new();
373373
/// // All files with the file:// url prefix will be read from the local file system
374374
/// ctx.register_object_store(object_store_url.as_ref(), Arc::new(object_store));
375375
/// ```
@@ -452,7 +452,7 @@ impl SessionContext {
452452
/// # use datafusion::{error::Result, assert_batches_eq};
453453
/// # #[tokio::main]
454454
/// # async fn main() -> Result<()> {
455-
/// let mut ctx = SessionContext::new();
455+
/// let ctx = SessionContext::new();
456456
/// ctx
457457
/// .sql("CREATE TABLE foo (x INTEGER)")
458458
/// .await?
@@ -480,7 +480,7 @@ impl SessionContext {
480480
/// # use datafusion::physical_plan::collect;
481481
/// # #[tokio::main]
482482
/// # async fn main() -> Result<()> {
483-
/// let mut ctx = SessionContext::new();
483+
/// let ctx = SessionContext::new();
484484
/// let options = SQLOptions::new()
485485
/// .with_allow_ddl(false);
486486
/// let err = ctx.sql_with_options("CREATE TABLE foo (x INTEGER)", options)
@@ -1357,7 +1357,7 @@ impl SessionContext {
13571357
}
13581358

13591359
/// Register [`CatalogProviderList`] in [`SessionState`]
1360-
pub fn register_catalog_list(&mut self, catalog_list: Arc<dyn CatalogProviderList>) {
1360+
pub fn register_catalog_list(&self, catalog_list: Arc<dyn CatalogProviderList>) {
13611361
self.state.write().register_catalog_list(catalog_list)
13621362
}
13631363

@@ -1386,15 +1386,18 @@ impl FunctionRegistry for SessionContext {
13861386
fn udwf(&self, name: &str) -> Result<Arc<WindowUDF>> {
13871387
self.state.read().udwf(name)
13881388
}
1389+
13891390
fn register_udf(&mut self, udf: Arc<ScalarUDF>) -> Result<Option<Arc<ScalarUDF>>> {
13901391
self.state.write().register_udf(udf)
13911392
}
1393+
13921394
fn register_udaf(
13931395
&mut self,
13941396
udaf: Arc<AggregateUDF>,
13951397
) -> Result<Option<Arc<AggregateUDF>>> {
13961398
self.state.write().register_udaf(udaf)
13971399
}
1400+
13981401
fn register_udwf(&mut self, udwf: Arc<WindowUDF>) -> Result<Option<Arc<WindowUDF>>> {
13991402
self.state.write().register_udwf(udwf)
14001403
}

datafusion/core/src/test_util/mod.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -112,7 +112,7 @@ pub fn aggr_test_schema() -> SchemaRef {
112112

113113
/// Register session context for the aggregate_test_100.csv file
114114
pub async fn register_aggregate_csv(
115-
ctx: &mut SessionContext,
115+
ctx: &SessionContext,
116116
table_name: &str,
117117
) -> Result<()> {
118118
let schema = aggr_test_schema();
@@ -128,8 +128,8 @@ pub async fn register_aggregate_csv(
128128

129129
/// Create a table from the aggregate_test_100.csv file with the specified name
130130
pub async fn test_table_with_name(name: &str) -> Result<DataFrame> {
131-
let mut ctx = SessionContext::new();
132-
register_aggregate_csv(&mut ctx, name).await?;
131+
let ctx = SessionContext::new();
132+
register_aggregate_csv(&ctx, name).await?;
133133
ctx.table(name).await
134134
}
135135

0 commit comments

Comments
 (0)