use futures::TryStreamExt;
use object_store::path::Path;
use polars_core::error::to_compute_err;
use polars_core::prelude::polars_ensure;
use polars_error::{polars_bail, PolarsResult};
use polars_utils::format_pl_smallstr;
use polars_utils::pl_str::PlSmallStr;
use regex::Regex;
use url::Url;
use super::{parse_url, CloudOptions};
const DELIMITER: char = '/';
pub(crate) fn extract_prefix_expansion(url: &str) -> PolarsResult<(String, Option<String>)> {
let splits = url.split(DELIMITER);
let mut prefix = String::new();
let mut expansion = String::new();
let mut last_split_was_wildcard = false;
for split in splits {
if expansion.is_empty() && memchr::memchr2(b'*', b'[', split.as_bytes()).is_none() {
if !prefix.is_empty() {
prefix.push(DELIMITER);
}
prefix.push_str(split);
continue;
}
if split == "**" {
last_split_was_wildcard = true;
expansion.push_str(".*");
continue;
}
polars_ensure!(
!split.contains("**"),
ComputeError: "expected '**' by itself in path component, got {}", url
);
if !last_split_was_wildcard && !expansion.is_empty() {
expansion.push(DELIMITER);
}
if memchr::memchr2(b'.', b'*', split.as_bytes()).is_some() {
let processed = split.replace('.', "\\.");
expansion.push_str(&processed.replace('*', "([^/]*)"));
continue;
}
last_split_was_wildcard = false;
expansion.push_str(split);
}
if !prefix.is_empty() && !expansion.is_empty() {
prefix.push(DELIMITER);
}
if !expansion.is_empty() {
expansion.insert(0, '^');
expansion.push('$');
}
Ok((
prefix,
if !expansion.is_empty() {
Some(expansion)
} else {
None
},
))
}
#[derive(PartialEq, Debug, Default)]
pub struct CloudLocation {
pub scheme: PlSmallStr,
pub bucket: PlSmallStr,
pub prefix: String,
pub expansion: Option<PlSmallStr>,
}
impl CloudLocation {
pub fn from_url(parsed: &Url, glob: bool) -> PolarsResult<CloudLocation> {
let is_local = parsed.scheme() == "file";
let (bucket, key) = if is_local {
("".into(), parsed.path())
} else {
if parsed.scheme().starts_with("http") {
return Ok(CloudLocation {
scheme: parsed.scheme().into(),
..Default::default()
});
}
let key = parsed.path();
let bucket = format_pl_smallstr!(
"{}",
&parsed[url::Position::BeforeUsername..url::Position::AfterPort]
);
if bucket.is_empty() {
polars_bail!(ComputeError: "CloudLocation::from_url(): empty bucket: {}", parsed);
}
(bucket, key)
};
let key = percent_encoding::percent_decode_str(key)
.decode_utf8()
.map_err(to_compute_err)?;
let (prefix, expansion) = if glob {
let (mut prefix, expansion) = extract_prefix_expansion(&key)?;
if is_local && key.starts_with(DELIMITER) {
prefix.insert(0, DELIMITER);
}
(prefix, expansion.map(|x| x.into()))
} else {
(key.as_ref().into(), None)
};
Ok(CloudLocation {
scheme: parsed.scheme().into(),
bucket,
prefix,
expansion,
})
}
pub fn new(url: &str, glob: bool) -> PolarsResult<CloudLocation> {
let parsed = parse_url(url).map_err(to_compute_err)?;
Self::from_url(&parsed, glob)
}
}
fn full_url(scheme: &str, bucket: &str, key: Path) -> String {
format!("{scheme}://{bucket}/{key}")
}
pub(crate) struct Matcher {
prefix: String,
re: Option<Regex>,
}
impl Matcher {
pub(crate) fn new(prefix: String, expansion: Option<&str>) -> PolarsResult<Matcher> {
let re = expansion.map(Regex::new).transpose()?;
Ok(Matcher { prefix, re })
}
pub(crate) fn is_matching(&self, key: &str) -> bool {
if !key.starts_with(self.prefix.as_str()) {
return false;
}
if self.re.is_none() {
return true;
}
let last = &key[self.prefix.len()..];
self.re.as_ref().unwrap().is_match(last.as_ref())
}
}
pub async fn glob(url: &str, cloud_options: Option<&CloudOptions>) -> PolarsResult<Vec<String>> {
let (
CloudLocation {
scheme,
bucket,
prefix,
expansion,
},
store,
) = super::build_object_store(url, cloud_options, true).await?;
let matcher = &Matcher::new(
if scheme == "file" {
prefix[1..].into()
} else {
prefix.clone()
},
expansion.as_deref(),
)?;
let path = Path::from(prefix.as_str());
let path = Some(&path);
let mut locations = store
.try_exec_rebuild_on_err(|store| {
let st = store.clone();
async {
let store = st;
store
.list(path)
.try_filter_map(|x| async move {
let out = (x.size > 0 && matcher.is_matching(x.location.as_ref()))
.then_some(x.location);
Ok(out)
})
.try_collect::<Vec<_>>()
.await
.map_err(to_compute_err)
}
})
.await?;
locations.sort_unstable();
Ok(locations
.into_iter()
.map(|l| full_url(&scheme, &bucket, l))
.collect::<Vec<_>>())
}
#[cfg(test)]
mod test {
use super::*;
#[test]
fn test_cloud_location() {
assert_eq!(
CloudLocation::new("s3://a/b", true).unwrap(),
CloudLocation {
scheme: "s3".into(),
bucket: "a".into(),
prefix: "b".into(),
expansion: None,
}
);
assert_eq!(
CloudLocation::new("s3://a/b/*.c", true).unwrap(),
CloudLocation {
scheme: "s3".into(),
bucket: "a".into(),
prefix: "b/".into(),
expansion: Some("^([^/]*)\\.c$".into()),
}
);
assert_eq!(
CloudLocation::new("file:///a/b", true).unwrap(),
CloudLocation {
scheme: "file".into(),
bucket: "".into(),
prefix: "/a/b".into(),
expansion: None,
}
);
}
#[test]
fn test_extract_prefix_expansion() {
assert!(extract_prefix_expansion("**url").is_err());
assert_eq!(
extract_prefix_expansion("a/b.c").unwrap(),
("a/b.c".into(), None)
);
assert_eq!(
extract_prefix_expansion("a/**").unwrap(),
("a/".into(), Some("^.*$".into()))
);
assert_eq!(
extract_prefix_expansion("a/**/b").unwrap(),
("a/".into(), Some("^.*b$".into()))
);
assert_eq!(
extract_prefix_expansion("a/**/*b").unwrap(),
("a/".into(), Some("^.*([^/]*)b$".into()))
);
assert_eq!(
extract_prefix_expansion("a/**/data/*b").unwrap(),
("a/".into(), Some("^.*data/([^/]*)b$".into()))
);
assert_eq!(
extract_prefix_expansion("a/*b").unwrap(),
("a/".into(), Some("^([^/]*)b$".into()))
);
}
#[test]
fn test_matcher_file_name() {
let cloud_location = CloudLocation::new("s3://bucket/folder/*.parquet", true).unwrap();
let a = Matcher::new(cloud_location.prefix, cloud_location.expansion.as_deref()).unwrap();
assert!(a.is_matching(Path::from("folder/1.parquet").as_ref()));
assert!(!a.is_matching(Path::from("folder/1parquet").as_ref()));
assert!(!a.is_matching(Path::from("folder/other/1.parquet").as_ref()));
}
#[test]
fn test_matcher_folders() {
let cloud_location = CloudLocation::new("s3://bucket/folder/**/*.parquet", true).unwrap();
let a = Matcher::new(cloud_location.prefix, cloud_location.expansion.as_deref()).unwrap();
assert!(a.is_matching(Path::from("folder/1.parquet").as_ref()));
assert!(a.is_matching(Path::from("folder/other/1.parquet").as_ref()));
let cloud_location =
CloudLocation::new("s3://bucket/folder/**/data/*.parquet", true).unwrap();
let a = Matcher::new(cloud_location.prefix, cloud_location.expansion.as_deref()).unwrap();
assert!(!a.is_matching(Path::from("folder/1.parquet").as_ref()));
assert!(a.is_matching(Path::from("folder/data/1.parquet").as_ref()));
assert!(a.is_matching(Path::from("folder/other/data/1.parquet").as_ref()));
}
#[test]
fn test_cloud_location_no_glob() {
let cloud_location = CloudLocation::new("s3://bucket/[*", false).unwrap();
assert_eq!(
cloud_location,
CloudLocation {
scheme: "s3".into(),
bucket: "bucket".into(),
prefix: "/[*".into(),
expansion: None,
},
)
}
#[test]
fn test_cloud_location_percentages() {
use super::CloudLocation;
let path = "s3://bucket/%25";
let cloud_location = CloudLocation::new(path, true).unwrap();
assert_eq!(
cloud_location,
CloudLocation {
scheme: "s3".into(),
bucket: "bucket".into(),
prefix: "%25".into(),
expansion: None,
}
);
let path = "https://pola.rs/%25";
let cloud_location = CloudLocation::new(path, true).unwrap();
assert_eq!(
cloud_location,
CloudLocation {
scheme: "https".into(),
bucket: "".into(),
prefix: "".into(),
expansion: None,
}
);
}
}