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 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
8 changes: 5 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,12 @@ 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()
{
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
69 changes: 66 additions & 3 deletions datafusion/core/src/execution/context/parquet.rs
Original file line number Diff line number Diff line change
Expand Up @@ -138,10 +138,10 @@ mod tests {
Ok(())
}

#[cfg(not(target_family = "windows"))]
Copy link
Member Author

@Weijun-H Weijun-H Nov 20, 2023

Choose a reason for hiding this comment

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

extend to all platforms

#[tokio::test]
async fn read_from_different_file_extension() -> Result<()> {
let ctx = SessionContext::new();
let sep = std::path::MAIN_SEPARATOR.to_string();

// Make up a new dataframe.
let write_df = ctx.read_batch(RecordBatch::try_new(
Expand Down Expand Up @@ -175,6 +175,25 @@ mod tests {
.unwrap()
.to_string();

let path4 = temp_dir_path
.join("output4.parquet".to_owned() + &sep)
.to_str()
.unwrap()
.to_string();

let path5 = temp_dir_path
.join("bbb..bbb")
.join("filename.parquet")
.to_str()
.unwrap()
.to_string();
let dir = temp_dir_path
.join("bbb..bbb".to_owned() + &sep)
.to_str()
.unwrap()
.to_string();
std::fs::create_dir(dir).expect("create dir failed");

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

// Write the dataframe to a parquet file named 'output3.parquet.snappy.parquet'
write_df
.clone()
.write_parquet(
&path3,
DataFrameWriteOptions::new().with_single_file_output(true),
Expand All @@ -216,6 +236,19 @@ mod tests {
)
.await?;

// Write the dataframe to a parquet file named 'bbb..bbb/filename.parquet'
write_df
.write_parquet(
&path5,
DataFrameWriteOptions::new().with_single_file_output(true),
Some(
WriterProperties::builder()
.set_compression(Compression::SNAPPY)
.build(),
),
)
.await?;

// Read the dataframe from 'output1.parquet' with the default file extension.
let read_df = ctx
.read_parquet(
Expand Down Expand Up @@ -253,10 +286,11 @@ mod tests {
},
)
.await;

let binding = DataFilePaths::to_urls(&path2).unwrap();
let expexted_path = binding[0].as_str();
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 '{}' does not match the expected extension '.parquet'", expexted_path)
);

// Read the dataframe from 'output3.parquet.snappy.parquet' with the correct file extension.
Expand All @@ -269,6 +303,35 @@ mod tests {
)
.await?;

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);

// Read the datafram from doule dot folder;
let read_df = ctx
.read_parquet(
&path5,
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, 5);
Expand Down