Skip to content
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

fix: Percent encode for Hugging Face paths #17718

Merged
merged 1 commit into from
Jul 19, 2024
Merged
Show file tree
Hide file tree
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
9 changes: 2 additions & 7 deletions crates/polars-io/src/partition.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ use rayon::prelude::*;
use crate::parquet::write::ParquetWriteOptions;
#[cfg(feature = "ipc")]
use crate::prelude::IpcWriterOptions;
use crate::prelude::URL_ENCODE_CHAR_SET;
use crate::{SerWriter, WriteDataFrameToFile};

impl WriteDataFrameToFile for ParquetWriteOptions {
Expand Down Expand Up @@ -58,12 +59,6 @@ where
})
.collect::<PolarsResult<Vec<_>>>()?;

const CHAR_SET: &percent_encoding::AsciiSet = &percent_encoding::CONTROLS
.add(b'/')
.add(b'=')
.add(b':')
.add(b' ');

move |df: &DataFrame| {
let cols = df.get_columns();

Expand All @@ -81,7 +76,7 @@ where
.get(0)
.unwrap_or("__HIVE_DEFAULT_PARTITION__")
.as_bytes(),
CHAR_SET
URL_ENCODE_CHAR_SET
)
)
})
Expand Down
20 changes: 18 additions & 2 deletions crates/polars-io/src/path_utils/hugging_face.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ use polars_error::{polars_bail, to_compute_err, PolarsResult};
use crate::cloud::{extract_prefix_expansion, Matcher};
use crate::path_utils::HiveIdxTracker;
use crate::pl_async::with_concurrency_budget;
use crate::prelude::URL_ENCODE_CHAR_SET;

#[derive(Debug, PartialEq)]
struct HFPathParts {
Expand All @@ -25,6 +26,9 @@ struct HFRepoLocation {

impl HFRepoLocation {
fn new(bucket: &str, repository: &str, revision: &str) -> Self {
let bucket = percent_encode(bucket.as_bytes());
let repository = percent_encode(repository.as_bytes());

// "https://huggingface.co/api/ [datasets | spaces] / {username} / {reponame} / tree / {revision} / {path from root}"
let api_base_path = format!(
"{}{}{}{}{}{}{}",
Expand All @@ -42,11 +46,19 @@ impl HFRepoLocation {
}

fn get_file_uri(&self, rel_path: &str) -> String {
format!("{}{}", self.download_base_path, rel_path)
format!(
"{}{}",
self.download_base_path,
percent_encode(rel_path.as_bytes())
)
}

fn get_api_uri(&self, rel_path: &str) -> String {
format!("{}{}", self.api_base_path, rel_path)
format!(
"{}{}",
self.api_base_path,
percent_encode(rel_path.as_bytes())
)
}
}

Expand Down Expand Up @@ -304,6 +316,10 @@ pub(super) async fn expand_paths_hf(
Ok((hive_idx_tracker.idx, out_paths))
}

fn percent_encode(bytes: &[u8]) -> percent_encoding::PercentEncode {
percent_encoding::percent_encode(bytes, URL_ENCODE_CHAR_SET)
}

mod tests {

#[test]
Expand Down
7 changes: 7 additions & 0 deletions crates/polars-io/src/utils/mod.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,10 @@
mod other;

pub use other::*;

pub const URL_ENCODE_CHAR_SET: &percent_encoding::AsciiSet = &percent_encoding::CONTROLS
.add(b'/')
.add(b'=')
.add(b':')
.add(b' ')
.add(b'%');