1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
use datafusion::error::Result;
use std::{env, str::FromStr, sync::Arc};
use datafusion::{datasource::object_store::ObjectStoreProvider, error::DataFusionError};
use object_store::{aws::AmazonS3Builder, gcp::GoogleCloudStorageBuilder};
use url::Url;
#[derive(Debug, PartialEq, Eq, clap::ArgEnum, Clone)]
pub enum ObjectStoreScheme {
S3,
GCS,
}
impl FromStr for ObjectStoreScheme {
type Err = DataFusionError;
fn from_str(input: &str) -> Result<Self> {
match input {
"s3" => Ok(ObjectStoreScheme::S3),
"gs" | "gcs" => Ok(ObjectStoreScheme::GCS),
_ => Err(DataFusionError::Execution(format!(
"Unsupported object store scheme {}",
input
))),
}
}
}
#[derive(Debug)]
pub struct DatafusionCliObjectStoreProvider {}
impl ObjectStoreProvider for DatafusionCliObjectStoreProvider {
fn get_by_url(&self, url: &Url) -> Result<Arc<dyn object_store::ObjectStore>> {
ObjectStoreScheme::from_str(url.scheme()).map(|scheme| match scheme {
ObjectStoreScheme::S3 => build_s3_object_store(url),
ObjectStoreScheme::GCS => build_gcs_object_store(url),
})?
}
}
fn build_s3_object_store(url: &Url) -> Result<Arc<dyn object_store::ObjectStore>> {
let host = get_host_name(url)?;
match AmazonS3Builder::from_env().with_bucket_name(host).build() {
Ok(s3) => Ok(Arc::new(s3)),
Err(err) => Err(DataFusionError::External(Box::new(err))),
}
}
fn build_gcs_object_store(url: &Url) -> Result<Arc<dyn object_store::ObjectStore>> {
let host = get_host_name(url)?;
let mut builder = GoogleCloudStorageBuilder::new().with_bucket_name(host);
if let Ok(path) = env::var("GCP_SERVICE_ACCOUNT_PATH") {
builder = builder.with_service_account_path(path);
}
match builder.build() {
Ok(gcs) => Ok(Arc::new(gcs)),
Err(err) => Err(DataFusionError::External(Box::new(err))),
}
}
fn get_host_name(url: &Url) -> Result<&str> {
url.host_str().ok_or_else(|| {
DataFusionError::Execution(format!(
"Not able to parse hostname from url, {}",
url.as_str()
))
})
}
#[cfg(test)]
mod tests {
use std::{env, str::FromStr};
use datafusion::datasource::object_store::ObjectStoreProvider;
use url::Url;
use super::DatafusionCliObjectStoreProvider;
#[test]
fn s3_provider_no_host() {
let no_host_url = "s3:///";
let provider = DatafusionCliObjectStoreProvider {};
let err = provider
.get_by_url(&Url::from_str(no_host_url).unwrap())
.unwrap_err();
assert!(err
.to_string()
.contains("Not able to parse hostname from url"))
}
#[test]
fn gs_provider_no_host() {
let no_host_url = "gs:///";
let provider = DatafusionCliObjectStoreProvider {};
let err = provider
.get_by_url(&Url::from_str(no_host_url).unwrap())
.unwrap_err();
assert!(err
.to_string()
.contains("Not able to parse hostname from url"))
}
#[test]
fn gcs_provider_no_host() {
let no_host_url = "gcs:///";
let provider = DatafusionCliObjectStoreProvider {};
let err = provider
.get_by_url(&Url::from_str(no_host_url).unwrap())
.unwrap_err();
assert!(err
.to_string()
.contains("Not able to parse hostname from url"))
}
#[test]
fn unknown_object_store_type() {
let unknown = "unknown://bucket_name/path";
let provider = DatafusionCliObjectStoreProvider {};
let err = provider
.get_by_url(&Url::from_str(unknown).unwrap())
.unwrap_err();
assert!(err
.to_string()
.contains("Unsupported object store scheme unknown"))
}
#[test]
fn s3_region_validation() {
let s3 = "s3://bucket_name/path";
let provider = DatafusionCliObjectStoreProvider {};
let err = provider
.get_by_url(&Url::from_str(s3).unwrap())
.unwrap_err();
assert!(err.to_string().contains("Generic S3 error: Missing region"));
env::set_var("AWS_REGION", "us-east-1");
let url = Url::from_str(s3).expect("Unable to parse s3 url");
let res = provider.get_by_url(&url);
let msg = match res {
Err(e) => format!("{}", e),
Ok(_) => "".to_string(),
};
assert_eq!("".to_string(), msg); env::remove_var("AWS_REGION");
}
}