Skip to content

Commit a35d007

Browse files
Add method SessionStateBuilder::with_object_store (#12578)
This PR adds the the method `SessionStateBuilder::with_object_store`, which registers an object store with a specified URL to the RuntimeEnv.
1 parent 5768bba commit a35d007

File tree

1 file changed

+37
-0
lines changed

1 file changed

+37
-0
lines changed

datafusion/core/src/execution/session_state.rs

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,13 +68,15 @@ use datafusion_sql::parser::{DFParser, Statement};
6868
use datafusion_sql::planner::{ContextProvider, ParserOptions, PlannerContext, SqlToRel};
6969
use itertools::Itertools;
7070
use log::{debug, info};
71+
use object_store::ObjectStore;
7172
use sqlparser::ast::Expr as SQLExpr;
7273
use sqlparser::dialect::dialect_from_str;
7374
use std::any::Any;
7475
use std::collections::hash_map::Entry;
7576
use std::collections::{HashMap, HashSet};
7677
use std::fmt::Debug;
7778
use std::sync::Arc;
79+
use url::Url;
7880
use uuid::Uuid;
7981

8082
/// `SessionState` contains all the necessary state to plan and execute queries,
@@ -1229,6 +1231,41 @@ impl SessionStateBuilder {
12291231
self
12301232
}
12311233

1234+
/// Register an `ObjectStore` to the [`RuntimeEnv`]. See [`RuntimeEnv::register_object_store`]
1235+
/// for more details.
1236+
///
1237+
/// Note that this creates a default [`RuntimeEnv`] if there isn't one passed in already.
1238+
///
1239+
/// ```
1240+
/// # use datafusion::prelude::*;
1241+
/// # use datafusion::execution::session_state::SessionStateBuilder;
1242+
/// # use datafusion_execution::runtime_env::RuntimeEnv;
1243+
/// # use url::Url;
1244+
/// # use std::sync::Arc;
1245+
/// # let http_store = object_store::local::LocalFileSystem::new();
1246+
/// let url = Url::try_from("file://").unwrap();
1247+
/// let object_store = object_store::local::LocalFileSystem::new();
1248+
/// let state = SessionStateBuilder::new()
1249+
/// .with_config(SessionConfig::new())
1250+
/// .with_object_store(&url, Arc::new(object_store))
1251+
/// .with_default_features()
1252+
/// .build();
1253+
/// ```
1254+
pub fn with_object_store(
1255+
mut self,
1256+
url: &Url,
1257+
object_store: Arc<dyn ObjectStore>,
1258+
) -> Self {
1259+
if self.runtime_env.is_none() {
1260+
self.runtime_env = Some(Arc::new(RuntimeEnv::default()));
1261+
}
1262+
self.runtime_env
1263+
.as_ref()
1264+
.unwrap()
1265+
.register_object_store(url, object_store);
1266+
self
1267+
}
1268+
12321269
/// Builds a [`SessionState`] with the current configuration.
12331270
///
12341271
/// Note that there is an explicit option for enabling catalog and schema defaults

0 commit comments

Comments
 (0)