1use std::borrow::Cow;
2
3use futures::TryStreamExt;
4use object_store::path::Path;
5use polars_error::{PolarsResult, polars_bail, polars_err};
6use polars_utils::pl_path::{CloudScheme, PlPath, PlRefPath};
7use polars_utils::pl_str::PlSmallStr;
8use regex::Regex;
9
10use super::CloudOptions;
11
12pub(crate) fn extract_prefix_expansion(path: &str) -> PolarsResult<(Cow<'_, str>, Option<String>)> {
18 let mut replacements: Vec<(usize, usize, &[u8])> = vec![];
20
21 let mut pos: usize = if let Some(after_last_slash) =
25 memchr::memchr2(b'*', b'[', path.as_bytes()).map(|i| {
26 path.as_bytes()[..i]
27 .iter()
28 .rposition(|x| *x == b'/')
29 .map_or(0, |x| 1 + x)
30 }) {
31 replacements.push((after_last_slash, 0, &[]));
33 after_last_slash
34 } else {
35 usize::MAX
36 };
37
38 while pos < path.len() {
39 match memchr::memchr2(b'*', b'.', &path.as_bytes()[pos..]) {
40 None => break,
41 Some(i) => pos += i,
42 }
43
44 let (len, replace): (usize, &[u8]) = match &path[pos..] {
45 v if v.starts_with("**") && (v.len() == 2 || v.as_bytes()[2] == b'/') => {
49 (3, b"(.*/)?" as _)
51 },
52 v if v.starts_with("**") => {
53 polars_bail!(ComputeError: "invalid ** glob pattern")
54 },
55 v if v.starts_with('*') => (1, b"[^/]*" as _),
56 v if v.starts_with('.') => (1, b"\\." as _),
58 _ => {
59 pos += 1;
60 continue;
61 },
62 };
63
64 replacements.push((pos, len, replace));
65 pos += len;
66 }
67
68 if replacements.is_empty() {
69 return Ok((Cow::Borrowed(path), None));
70 }
71
72 let prefix = Cow::Borrowed(&path[..replacements[0].0]);
73
74 let mut pos = replacements[0].0;
75 let mut expansion = Vec::with_capacity(path.len() - pos);
76 expansion.push(b'^');
77
78 for (offset, len, replace) in replacements {
79 expansion.extend_from_slice(&path.as_bytes()[pos..offset]);
80 expansion.extend_from_slice(replace);
81 pos = offset + len;
82 }
83
84 if pos < path.len() {
85 expansion.extend_from_slice(&path.as_bytes()[pos..]);
86 }
87
88 expansion.push(b'$');
89
90 Ok((prefix, Some(String::from_utf8(expansion).unwrap())))
91}
92
93#[derive(PartialEq, Debug, Default)]
95pub struct CloudLocation {
96 pub scheme: &'static str,
98 pub bucket: PlSmallStr,
100 pub prefix: String,
102 pub expansion: Option<PlSmallStr>,
104}
105
106pub(crate) fn split_http_url(path: &PlPath) -> Option<(&str, &str)> {
109 let (base_url, rest) = path.as_str().split_at(path.authority_end_position());
110 if base_url.contains(['?', '#', '\\']) {
112 return None;
113 }
114 let object_path = rest.strip_prefix('/').unwrap_or(rest);
115 Path::parse(object_path)
116 .is_ok_and(|p| p.as_ref() == object_path)
117 .then_some((base_url, object_path))
118}
119
120impl CloudLocation {
121 pub fn new(path: PlRefPath, glob: bool) -> PolarsResult<Self> {
122 if let Some(scheme @ CloudScheme::Http | scheme @ CloudScheme::Https) = path.scheme() {
123 return Ok(CloudLocation {
125 scheme: scheme.as_str(),
126 prefix: split_http_url(&path).map_or_else(String::new, |(_, p)| p.to_string()),
127 ..Default::default()
128 });
129 }
130
131 let path_is_local = matches!(
132 path.scheme(),
133 None | Some(CloudScheme::File | CloudScheme::FileNoHostname)
134 );
135
136 let (bucket, key) = path
137 .strip_scheme_split_authority()
138 .ok_or(Cow::Borrowed(
139 "could not extract bucket/key (path did not contain '/')",
140 ))
141 .and_then(|x @ (bucket, _)| {
142 let bucket_is_empty = bucket.is_empty();
143
144 if path_is_local && !bucket_is_empty {
145 Err(Cow::Owned(format!(
146 "unsupported: non-empty hostname for 'file:' URI: '{bucket}'",
147 )))
148 } else if bucket_is_empty && !path_is_local {
149 Err(Cow::Borrowed("empty bucket name"))
150 } else {
151 Ok(x)
152 }
153 })
154 .map_err(|failed_reason| {
155 polars_err!(
156 ComputeError:
157 "failed to create CloudLocation: {} (path: '{}')",
158 failed_reason,
159 path,
160 )
161 })?;
162
163 let key = if path_is_local {
164 key
165 } else {
166 key.strip_prefix('/').unwrap_or(key)
167 };
168
169 let (prefix, expansion) = if glob {
170 let (prefix, expansion) = extract_prefix_expansion(key)?;
171
172 assert_eq!(prefix.starts_with('/'), key.starts_with('/'));
173
174 (prefix, expansion.map(|x| x.into()))
175 } else {
176 (key.into(), None)
177 };
178
179 Ok(CloudLocation {
180 scheme: path.scheme().unwrap_or(CloudScheme::File).as_str(),
181 bucket: PlSmallStr::from_str(bucket),
182 prefix: prefix.into_owned(),
183 expansion,
184 })
185 }
186}
187
188fn full_url(scheme: &str, bucket: &str, key: Path) -> String {
190 format!("{scheme}://{bucket}/{key}")
191}
192
193pub(crate) struct Matcher {
196 prefix: String,
197 re: Option<Regex>,
198}
199
200impl Matcher {
201 pub(crate) fn new(prefix: String, expansion: Option<&str>) -> PolarsResult<Matcher> {
203 let re = expansion
205 .map(polars_utils::regex_cache::compile_regex)
206 .transpose()?;
207 Ok(Matcher { prefix, re })
208 }
209
210 pub(crate) fn is_matching(&self, key: &str) -> bool {
211 if !key.starts_with(self.prefix.as_str()) {
212 return false;
214 }
215 if self.re.is_none() {
216 return true;
217 }
218 let last = &key[self.prefix.len()..];
219 self.re.as_ref().unwrap().is_match(last.as_ref())
220 }
221}
222
223pub async fn glob(
225 url: PlRefPath,
226 cloud_options: Option<&CloudOptions>,
227) -> PolarsResult<Vec<(String, u64)>> {
228 let (
231 CloudLocation {
232 scheme,
233 bucket,
234 prefix,
235 expansion,
236 },
237 store,
238 ) = super::build_object_store(url.clone(), cloud_options, true).await?;
239 let matcher = &Matcher::new(
240 if scheme == "file" {
241 prefix[1..].into()
243 } else {
244 prefix.clone()
245 },
246 expansion.as_deref(),
247 )?;
248
249 let path = &Path::from(prefix.as_str());
250
251 let mut locations = store
252 .exec_with_rebuild_retry_on_err(path, |store| async move {
253 store
254 .list(Some(path))
255 .try_filter_map(|x| async move {
256 let out = (x.size > 0 && matcher.is_matching(x.location.as_ref()))
258 .then_some((x.location, x.size));
259 Ok(out)
260 })
261 .try_collect::<Vec<_>>()
262 .await
263 })
264 .await
265 .map_err(|e| e.wrap_msg(|e| format!("listing glob {url} failed: {e}")))?;
267
268 locations.sort_unstable_by(|a, b| a.0.cmp(&b.0));
269 Ok(locations
270 .into_iter()
271 .map(|(l, size)| (full_url(scheme, &bucket, l), size))
272 .collect::<Vec<_>>())
273}
274
275#[cfg(test)]
276mod test {
277 use super::*;
278
279 #[test]
280 fn test_cloud_location() {
281 assert_eq!(
282 CloudLocation::new(PlRefPath::new("s3://a/b"), true).unwrap(),
283 CloudLocation {
284 scheme: "s3",
285 bucket: "a".into(),
286 prefix: "b".into(),
287 expansion: None,
288 }
289 );
290 assert_eq!(
291 CloudLocation::new(PlRefPath::new("s3://a/b/*.c"), true).unwrap(),
292 CloudLocation {
293 scheme: "s3",
294 bucket: "a".into(),
295 prefix: "b/".into(),
296 expansion: Some("^[^/]*\\.c$".into()),
297 }
298 );
299 assert_eq!(
300 CloudLocation::new(PlRefPath::new("file:///a/b"), true).unwrap(),
301 CloudLocation {
302 scheme: "file",
303 bucket: "".into(),
304 prefix: "/a/b".into(),
305 expansion: None,
306 }
307 );
308 assert_eq!(
309 CloudLocation::new(PlRefPath::new("file:/a/b"), true).unwrap(),
310 CloudLocation {
311 scheme: "file",
312 bucket: "".into(),
313 prefix: "/a/b".into(),
314 expansion: None,
315 }
316 );
317 }
318
319 #[test]
320 fn test_extract_prefix_expansion() {
321 assert!(extract_prefix_expansion("**url").is_err());
322 assert_eq!(
323 extract_prefix_expansion("a/b.c").unwrap(),
324 ("a/b.c".into(), None)
325 );
326 assert_eq!(
327 extract_prefix_expansion("a/**").unwrap(),
328 ("a/".into(), Some("^(.*/)?$".into()))
329 );
330 assert_eq!(
331 extract_prefix_expansion("a/**/b").unwrap(),
332 ("a/".into(), Some("^(.*/)?b$".into()))
333 );
334 assert_eq!(
335 extract_prefix_expansion("a/**/*b").unwrap(),
336 ("a/".into(), Some("^(.*/)?[^/]*b$".into()))
337 );
338 assert_eq!(
339 extract_prefix_expansion("a/**/data/*b").unwrap(),
340 ("a/".into(), Some("^(.*/)?data/[^/]*b$".into()))
341 );
342 assert_eq!(
343 extract_prefix_expansion("a/*b").unwrap(),
344 ("a/".into(), Some("^[^/]*b$".into()))
345 );
346 }
347
348 #[test]
349 fn test_matcher_file_name() {
350 let cloud_location =
351 CloudLocation::new(PlRefPath::new("s3://bucket/folder/*.parquet"), true).unwrap();
352 let a = Matcher::new(cloud_location.prefix, cloud_location.expansion.as_deref()).unwrap();
353 assert!(a.is_matching(Path::from("folder/1.parquet").as_ref()));
355 assert!(!a.is_matching(Path::from("folder/1parquet").as_ref()));
357 assert!(!a.is_matching(Path::from("folder/other/1.parquet").as_ref()));
359 }
360
361 #[test]
362 fn test_matcher_folders() {
363 let cloud_location =
364 CloudLocation::new(PlRefPath::new("s3://bucket/folder/**/*.parquet"), true).unwrap();
365
366 let a = Matcher::new(cloud_location.prefix, cloud_location.expansion.as_deref()).unwrap();
367 assert!(a.is_matching(Path::from("folder/1.parquet").as_ref()));
369 assert!(a.is_matching(Path::from("folder/other/1.parquet").as_ref()));
371
372 let cloud_location =
373 CloudLocation::new(PlRefPath::new("s3://bucket/folder/**/data/*.parquet"), true)
374 .unwrap();
375 let a = Matcher::new(cloud_location.prefix, cloud_location.expansion.as_deref()).unwrap();
376
377 assert!(!a.is_matching(Path::from("folder/1.parquet").as_ref()));
379 assert!(a.is_matching(Path::from("folder/data/1.parquet").as_ref()));
381 assert!(a.is_matching(Path::from("folder/other/data/1.parquet").as_ref()));
383 }
384
385 #[test]
386 fn test_cloud_location_no_glob() {
387 let cloud_location = CloudLocation::new(PlRefPath::new("s3://bucket/[*"), false).unwrap();
388 assert_eq!(
389 cloud_location,
390 CloudLocation {
391 scheme: "s3",
392 bucket: "bucket".into(),
393 prefix: "[*".into(),
394 expansion: None,
395 },
396 )
397 }
398
399 #[test]
400 fn test_cloud_location_percentages() {
401 use super::CloudLocation;
402
403 let path = "s3://bucket/%25";
404 let cloud_location = CloudLocation::new(PlRefPath::new(path), true).unwrap();
405
406 assert_eq!(
407 cloud_location,
408 CloudLocation {
409 scheme: "s3",
410 bucket: "bucket".into(),
411 prefix: "%25".into(),
412 expansion: None,
413 }
414 );
415
416 let path = "https://pola.rs/%25";
417 let cloud_location = CloudLocation::new(PlRefPath::new(path), true).unwrap();
418
419 assert_eq!(
420 cloud_location,
421 CloudLocation {
422 scheme: "https",
423 bucket: "".into(),
424 prefix: "%25".into(),
425 expansion: None,
426 }
427 );
428 }
429
430 #[test]
431 fn test_split_http_url() {
432 use super::split_http_url;
433
434 let check = |url: &str, expected: Option<(&str, &str)>| {
435 assert_eq!(split_http_url(&PlRefPath::new(url)), expected, "{url}")
436 };
437
438 check(
439 "https://pola.rs/a/b.parquet",
440 Some(("https://pola.rs", "a/b.parquet")),
441 );
442 check(
443 "http://u:[email protected]:9000/b/k.ipc?X-Amz-Signature=a%2Fb#f",
444 Some((
445 "http://u:[email protected]:9000",
446 "b/k.ipc?X-Amz-Signature=a%2Fb#f",
447 )),
448 );
449 check("https://pola.rs/%25", Some(("https://pola.rs", "%25")));
450 check("https://pola.rs/", Some(("https://pola.rs", "")));
451 check("https://pola.rs", Some(("https://pola.rs", "")));
452
453 check("https://pola.rs/dir/", None);
455 check("https://pola.rs//a", None);
456 check("https://pola.rs/a//b", None);
457 check("https://pola.rs/a/../b", None);
458 check("https://pola.rs?a=/b", None);
459 check("https://pola.rs\\a/b", None);
460 }
461
462 #[test]
463 fn test_glob_wildcard_21736() {
464 let path = "s3://bucket/folder/**/data.parquet";
465 let cloud_location = CloudLocation::new(PlRefPath::new(path), true).unwrap();
466
467 let a = Matcher::new(cloud_location.prefix, cloud_location.expansion.as_deref()).unwrap();
468
469 assert!(!a.is_matching("folder/_data.parquet"));
470
471 assert!(a.is_matching("folder/data.parquet"));
472 assert!(a.is_matching("folder/abc/data.parquet"));
473 assert!(a.is_matching("folder/abc/def/data.parquet"));
474
475 let path = "s3://bucket/folder/data_*.parquet";
476 let cloud_location = CloudLocation::new(PlRefPath::new(path), true).unwrap();
477
478 let a = Matcher::new(cloud_location.prefix, cloud_location.expansion.as_deref()).unwrap();
479
480 assert!(!a.is_matching("folder/data_1.ipc"))
481 }
482}