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

feat(read_ file funcs): infer from compressed formats #2639

Merged
merged 26 commits into from
Feb 21, 2024
Merged
Show file tree
Hide file tree
Changes from 19 commits
Commits
Show all changes
26 commits
Select commit Hold shift + click to select a range
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
25 changes: 18 additions & 7 deletions crates/datafusion_ext/src/planner/relation/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,8 @@ use std::collections::HashMap;
use std::path::Path;

use async_recursion::async_recursion;
use datafusion::common::{DataFusionError, OwnedTableReference, Result};
use datafusion::common::{DataFusionError, GetExt, OwnedTableReference, Result};
use datafusion::datasource::file_format::file_compression_type::FileCompressionType;
use datafusion::logical_expr::{LogicalPlan, LogicalPlanBuilder};
use datafusion::scalar::ScalarValue;
use datafusion::sql::planner::PlannerContext;
Expand Down Expand Up @@ -247,24 +248,34 @@ fn infer_func_for_file(path: &str) -> Result<OwnedTableReference> {
Ok(match ext.as_str() {
"parquet" => OwnedTableReference::Partial {
schema: "public".into(),
table: "parquet_scan".into(),
table: "read_parquet".into(),
},
"xlsx" => OwnedTableReference::Partial {
schema: "public".into(),
table: "read_excel".into(),
},
"csv" => OwnedTableReference::Partial {
schema: "public".into(),
table: "csv_scan".into(),
table: "read_csv".into(),
},
"json" | "jsonl" | "ndjson" => OwnedTableReference::Partial {
schema: "public".into(),
table: "ndjson_scan".into(),
table: "read_ndjson".into(),
},
"bson" => OwnedTableReference::Partial {
schema: "public".into(),
table: "read_bson".into(),
},
ext => {
return Err(DataFusionError::Plan(format!(
"unable to infer how to handle file extension: {ext}"
)))
if let Ok(compression_type) = ext.parse::<FileCompressionType>() {
let ext = compression_type.get_ext();
let path = path.trim_end_matches(ext.as_str());
infer_func_for_file(path)?
} else {
return Err(DataFusionError::Plan(format!(
"unable to infer how to handle file extension: {ext}"
)));
}
}
})
}
Binary file added testdata/csv/userdata1.csv.gz
Binary file not shown.
Binary file added testdata/parquet/userdata1.parquet.gz
Binary file not shown.
30 changes: 30 additions & 0 deletions testdata/sqllogictests/infer.slt
Original file line number Diff line number Diff line change
Expand Up @@ -51,3 +51,33 @@ select count(*) from './testdata/parquet/*.parquet'
statement error missing file extension
select count(*) from './testdata/parquet/*'

#Tests for inferring table functions from compressed file formats

query
select count(*) from './testdata/csv/userdata1.csv.gz'
----
1000


query IT
select id, "./testdata/csv/userdata1.csv.gz".first_name
from './testdata/csv/userdata1.csv.gz'
order by id
limit 1
----
1 Amanda

#For read_parquet table function with compressed files

query
select count(*) from './testdata/parquet/userdata1.parquet.gz'
----
1000
Copy link
Contributor

Choose a reason for hiding this comment

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

can we also test other combinations

I think that parquet shouldn't work and we should add a test that ensures we don't change that behavior.


query IT
select id, "./testdata/parquet/userdata1.parquet.gz".first_name
from './testdata/parquet/userdata1.parquet.gz'
order by id
limit 1
----
1 Amanda
Loading