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

improve file path validation when reading parquet #8267

Merged
merged 4 commits into from
Nov 21, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
10 changes: 7 additions & 3 deletions datafusion/core/src/execution/context/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -858,10 +858,14 @@ impl SessionContext {

// check if the file extension matches the expected extension
for path in &table_paths {
let file_name = path.prefix().filename().unwrap_or_default();
if !path.as_str().ends_with(&option_extension) && file_name.contains('.') {
let file_path = path.as_str();
if (!file_path.ends_with(option_extension.clone().as_str())
&& !path.is_collection())
|| (!file_path.ends_with((option_extension.clone() + "/").as_str())
Copy link
Member

Choose a reason for hiding this comment

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

The path separator here is platform-specific.

Copy link
Member

Choose a reason for hiding this comment

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

You could use std::path::MAIN_SEPARATOR instead here

&& path.is_collection())
{
return exec_err!(
"File '{file_name}' does not match the expected extension '{option_extension}'"
"File path '{file_path}' does not match the expected extension '{option_extension}'"
);
}
}
Expand Down
24 changes: 23 additions & 1 deletion datafusion/core/src/execution/context/parquet.rs
Original file line number Diff line number Diff line change
Expand Up @@ -175,6 +175,12 @@ mod tests {
.unwrap()
.to_string();

let path4 = temp_dir_path
.join("output4.parquet/")
.to_str()
.unwrap()
.to_string();

// Write the dataframe to a parquet file named 'output1.parquet'
write_df
.clone()
Expand Down Expand Up @@ -256,7 +262,7 @@ mod tests {

assert_eq!(
read_df.unwrap_err().strip_backtrace(),
"Execution error: File 'output2.parquet.snappy' does not match the expected extension '.parquet'"
format!("Execution error: File path 'file:///private{}' does not match the expected extension '.parquet'", path2)
);

// Read the dataframe from 'output3.parquet.snappy.parquet' with the correct file extension.
Expand All @@ -272,6 +278,22 @@ mod tests {
let results = read_df.collect().await?;
let total_rows: usize = results.iter().map(|rb| rb.num_rows()).sum();
assert_eq!(total_rows, 5);

// Read the dataframe from 'output4/'
std::fs::create_dir(&path4)?;
let read_df = ctx
.read_parquet(
&path4,
ParquetReadOptions {
..Default::default()
},
)
.await?;

let results = read_df.collect().await?;
let total_rows: usize = results.iter().map(|rb| rb.num_rows()).sum();
assert_eq!(total_rows, 0);

Ok(())
}

Expand Down
Loading