Skip to content

Commit 33301d7

Browse files
alambfindepi
authored andcommitted
Add SessionContext::register_object_store (apache#10621)
1 parent 1e6f7d5 commit 33301d7

13 files changed

Lines changed: 44 additions & 28 deletions

File tree

datafusion-cli/src/exec.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -379,7 +379,7 @@ pub(crate) async fn register_object_store_and_config_extensions(
379379
let store = get_object_store(&ctx.state(), scheme, url, &table_options).await?;
380380

381381
// Register the retrieved object store in the session context's runtime environment
382-
ctx.runtime_env().register_object_store(url, store);
382+
ctx.register_object_store(url, store);
383383

384384
Ok(())
385385
}

datafusion-examples/examples/external_dependency/dataframe-to-s3.rs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -49,8 +49,7 @@ async fn main() -> Result<()> {
4949
let path = format!("s3://{bucket_name}");
5050
let s3_url = Url::parse(&path).unwrap();
5151
let arc_s3 = Arc::new(s3);
52-
ctx.runtime_env()
53-
.register_object_store(&s3_url, arc_s3.clone());
52+
ctx.register_object_store(&s3_url, arc_s3.clone());
5453

5554
let path = format!("s3://{bucket_name}/test_data/");
5655
let file_format = ParquetFormat::default().with_enable_pruning(true);

datafusion-examples/examples/external_dependency/query-aws-s3.rs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -48,8 +48,7 @@ async fn main() -> Result<()> {
4848

4949
let path = format!("s3://{bucket_name}");
5050
let s3_url = Url::parse(&path).unwrap();
51-
ctx.runtime_env()
52-
.register_object_store(&s3_url, Arc::new(s3));
51+
ctx.register_object_store(&s3_url, Arc::new(s3));
5352

5453
// cannot query the parquet files from this bucket because the path contains a whitespace
5554
// and we don't support that yet

datafusion-examples/examples/parquet_sql_multiple_files.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -80,7 +80,7 @@ async fn main() -> Result<(), Box<dyn std::error::Error>> {
8080
let local_fs = Arc::new(LocalFileSystem::default());
8181

8282
let u = url::Url::parse("file://./")?;
83-
ctx.runtime_env().register_object_store(&u, local_fs);
83+
ctx.register_object_store(&u, local_fs);
8484

8585
// Register a listing table - this will use all files in the directory as data sources
8686
// for the query

datafusion-examples/examples/query-http-csv.rs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -34,8 +34,7 @@ async fn main() -> Result<()> {
3434
.with_url(base_url.clone())
3535
.build()
3636
.unwrap();
37-
ctx.runtime_env()
38-
.register_object_store(&base_url, Arc::new(http_store));
37+
ctx.register_object_store(&base_url, Arc::new(http_store));
3938

4039
// register csv file with the execution context
4140
ctx.register_csv(

datafusion/core/src/datasource/physical_plan/avro.rs

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -261,9 +261,7 @@ mod tests {
261261
let state = session_ctx.state();
262262

263263
let url = Url::parse("file://").unwrap();
264-
state
265-
.runtime_env()
266-
.register_object_store(&url, store.clone());
264+
session_ctx.register_object_store(&url, store.clone());
267265

268266
let testdata = crate::test_util::arrow_test_data();
269267
let filename = format!("{testdata}/avro/alltypes_plain.avro");

datafusion/core/src/datasource/physical_plan/csv.rs

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -895,7 +895,7 @@ mod tests {
895895
) -> Result<()> {
896896
let ctx = SessionContext::new();
897897
let url = Url::parse("file://").unwrap();
898-
ctx.runtime_env().register_object_store(&url, store.clone());
898+
ctx.register_object_store(&url, store.clone());
899899

900900
let task_ctx = ctx.task_ctx();
901901

@@ -968,9 +968,7 @@ mod tests {
968968
store.put(&path, data).await.unwrap();
969969

970970
let url = Url::parse("memory://").unwrap();
971-
session_ctx
972-
.runtime_env()
973-
.register_object_store(&url, Arc::new(store));
971+
session_ctx.register_object_store(&url, Arc::new(store));
974972

975973
let df = session_ctx
976974
.read_csv("memory:///", CsvReadOptions::new())
@@ -999,7 +997,7 @@ mod tests {
999997
let tmp_dir = TempDir::new()?;
1000998
let local = Arc::new(LocalFileSystem::new_with_prefix(&tmp_dir)?);
1001999
let local_url = Url::parse("file://local").unwrap();
1002-
ctx.runtime_env().register_object_store(&local_url, local);
1000+
ctx.register_object_store(&local_url, local);
10031001
let options = CsvReadOptions::default()
10041002
.schema_infer_max_records(2)
10051003
.has_header(true);
@@ -1039,7 +1037,7 @@ mod tests {
10391037
let local = Arc::new(LocalFileSystem::new_with_prefix(&tmp_dir)?);
10401038
let local_url = Url::parse("file://local").unwrap();
10411039

1042-
ctx.runtime_env().register_object_store(&local_url, local);
1040+
ctx.register_object_store(&local_url, local);
10431041

10441042
// execute a simple query and write the results to CSV
10451043
let out_dir = tmp_dir.as_ref().to_str().unwrap().to_string() + "/out/";

datafusion/core/src/datasource/physical_plan/json.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -446,7 +446,7 @@ mod tests {
446446
) -> Result<()> {
447447
let ctx = SessionContext::new();
448448
let url = Url::parse("file://").unwrap();
449-
ctx.runtime_env().register_object_store(&url, store.clone());
449+
ctx.register_object_store(&url, store.clone());
450450
let filename = "1.json";
451451
let tmp_dir = TempDir::new()?;
452452
let file_groups = partitioned_file_groups(
@@ -752,7 +752,7 @@ mod tests {
752752
let tmp_dir = TempDir::new()?;
753753
let local = Arc::new(LocalFileSystem::new_with_prefix(&tmp_dir)?);
754754
let local_url = Url::parse("file://local").unwrap();
755-
ctx.runtime_env().register_object_store(&local_url, local);
755+
ctx.register_object_store(&local_url, local);
756756

757757
// execute a simple query and write the results to CSV
758758
let out_dir = tmp_dir.as_ref().to_str().unwrap().to_string() + "/out/";
@@ -845,7 +845,7 @@ mod tests {
845845
let tmp_dir = TempDir::new()?;
846846
let local = Arc::new(LocalFileSystem::new_with_prefix(&tmp_dir)?);
847847
let local_url = Url::parse("file://local").unwrap();
848-
ctx.runtime_env().register_object_store(&local_url, local);
848+
ctx.register_object_store(&local_url, local);
849849
let options = CsvReadOptions::default()
850850
.schema_infer_max_records(2)
851851
.has_header(true);

datafusion/core/src/datasource/physical_plan/parquet/mod.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -996,7 +996,7 @@ mod tests {
996996
let tmp_dir = TempDir::new()?;
997997
let local = Arc::new(LocalFileSystem::new_with_prefix(&tmp_dir)?);
998998
let local_url = Url::parse("file://local").unwrap();
999-
ctx.runtime_env().register_object_store(&local_url, local);
999+
ctx.register_object_store(&local_url, local);
10001000

10011001
let options = CsvReadOptions::default()
10021002
.schema_infer_max_records(2)
@@ -2052,7 +2052,7 @@ mod tests {
20522052
// register a local file system object store for /tmp directory
20532053
let local = Arc::new(LocalFileSystem::new_with_prefix(&tmp_dir)?);
20542054
let local_url = Url::parse("file://local").unwrap();
2055-
ctx.runtime_env().register_object_store(&local_url, local);
2055+
ctx.register_object_store(&local_url, local);
20562056

20572057
// Configure listing options
20582058
let file_format = ParquetFormat::default().with_enable_pruning(true);

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

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -91,6 +91,7 @@ use sqlparser::dialect::dialect_from_str;
9191

9292
use async_trait::async_trait;
9393
use chrono::{DateTime, Utc};
94+
use object_store::ObjectStore;
9495
use parking_lot::RwLock;
9596
use url::Url;
9697
use uuid::Uuid;
@@ -354,6 +355,29 @@ impl SessionContext {
354355
self
355356
}
356357

358+
/// Registers an [`ObjectStore`] to be used with a specific URL prefix.
359+
///
360+
/// See [`RuntimeEnv::register_object_store`] for more details.
361+
///
362+
/// # Example: register a local object store for the "file://" URL prefix
363+
/// ```
364+
/// # use std::sync::Arc;
365+
/// # use datafusion::prelude::SessionContext;
366+
/// # use datafusion_execution::object_store::ObjectStoreUrl;
367+
/// let object_store_url = ObjectStoreUrl::parse("file://").unwrap();
368+
/// let object_store = object_store::local::LocalFileSystem::new();
369+
/// let mut ctx = SessionContext::new();
370+
/// // All files with the file:// url prefix will be read from the local file system
371+
/// ctx.register_object_store(object_store_url.as_ref(), Arc::new(object_store));
372+
/// ```
373+
pub fn register_object_store(
374+
&self,
375+
url: &Url,
376+
object_store: Arc<dyn ObjectStore>,
377+
) -> Option<Arc<dyn ObjectStore>> {
378+
self.runtime_env().register_object_store(url, object_store)
379+
}
380+
357381
/// Registers the [`RecordBatch`] as the specified table name
358382
pub fn register_batch(
359383
&self,

0 commit comments

Comments
 (0)