Skip to content

Make ObjectStoreProvider fallible (return Result rather than Option) #3584

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

Merged
merged 1 commit into from
Sep 22, 2022
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
42 changes: 16 additions & 26 deletions datafusion/core/src/datasource/object_store.rs
Original file line number Diff line number Diff line change
Expand Up @@ -83,9 +83,8 @@ impl std::fmt::Display for ObjectStoreUrl {

/// Object store provider can detector an object store based on the url
pub trait ObjectStoreProvider: Send + Sync + 'static {
/// Detector a suitable object store based on its url if possible
/// Return the key and object store
fn get_by_url(&self, url: &Url) -> Option<Arc<dyn ObjectStore>>;
/// Return an ObjectStore for the provided url based on its scheme and authority
fn get_by_url(&self, url: &Url) -> Result<Arc<dyn ObjectStore>>;
Copy link
Contributor

@yahoNanJing yahoNanJing Sep 23, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Better to change it to be

Result<Option<Arc<dyn ObjectStore>>>

}

/// Object store registry
Expand Down Expand Up @@ -157,32 +156,23 @@ impl ObjectStoreRegistry {
stores.get(s).cloned()
};

// If not, then try to detector based on its url.
let store = store
.or_else(|| {
if let Some(provider) = &self.provider {
// If detected, register it
if let Some(store) = provider.get_by_url(url) {
let mut stores = self.object_stores.write();
let key =
&url[url::Position::BeforeScheme..url::Position::BeforePath];
stores.insert(key.to_owned(), store.clone());
Some(store)
} else {
None
}
} else {
None
match store {
Some(store) => Ok(store),
None => match &self.provider {
Some(provider) => {
let store = provider.get_by_url(url)?;
let mut stores = self.object_stores.write();
let key =
&url[url::Position::BeforeScheme..url::Position::BeforePath];
stores.insert(key.to_owned(), store.clone());
Ok(store)
}
})
.ok_or_else(|| {
DataFusionError::Internal(format!(
None => Err(DataFusionError::Internal(format!(
"No suitable object store found for {}",
url
))
})?;

Ok(store)
))),
},
}
}
}

Expand Down