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: enable list of paths for read_csv #824

Merged
merged 1 commit into from
Aug 22, 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
7 changes: 5 additions & 2 deletions python/datafusion/context.py
Original file line number Diff line number Diff line change
Expand Up @@ -883,7 +883,7 @@ def read_json(

def read_csv(
self,
path: str | pathlib.Path,
path: str | pathlib.Path | list[str] | list[pathlib.Path],
schema: pyarrow.Schema | None = None,
has_header: bool = True,
delimiter: str = ",",
Expand Down Expand Up @@ -914,9 +914,12 @@ def read_csv(
"""
if table_partition_cols is None:
table_partition_cols = []

path = [str(p) for p in path] if isinstance(path, list) else str(path)

return DataFrame(
self.ctx.read_csv(
str(path),
path,
schema,
has_header,
delimiter,
Expand Down
16 changes: 16 additions & 0 deletions python/datafusion/tests/test_context.py
Original file line number Diff line number Diff line change
Expand Up @@ -484,6 +484,22 @@ def test_read_csv(ctx):
csv_df.select(column("c1")).show()


def test_read_csv_list(ctx):
csv_df = ctx.read_csv(path=["testing/data/csv/aggregate_test_100.csv"])
expected = csv_df.count() * 2

double_csv_df = ctx.read_csv(
path=[
"testing/data/csv/aggregate_test_100.csv",
"testing/data/csv/aggregate_test_100.csv",
]
)
actual = double_csv_df.count()

double_csv_df.select(column("c1")).show()
assert actual == expected


def test_read_csv_compressed(ctx, tmp_path):
test_data_path = "testing/data/csv/aggregate_test_100.csv"

Expand Down
15 changes: 7 additions & 8 deletions src/context.rs
Original file line number Diff line number Diff line change
Expand Up @@ -805,7 +805,7 @@ impl PySessionContext {
file_compression_type=None))]
pub fn read_csv(
&self,
path: PathBuf,
path: &Bound<'_, PyAny>,
schema: Option<PyArrowType<Schema>>,
has_header: bool,
delimiter: &str,
Expand All @@ -815,10 +815,6 @@ impl PySessionContext {
file_compression_type: Option<String>,
py: Python,
) -> PyResult<PyDataFrame> {
let path = path
.to_str()
.ok_or_else(|| PyValueError::new_err("Unable to convert path to a string"))?;

let delimiter = delimiter.as_bytes();
if delimiter.len() != 1 {
return Err(PyValueError::new_err(
Expand All @@ -833,13 +829,16 @@ impl PySessionContext {
.file_extension(file_extension)
.table_partition_cols(convert_table_partition_cols(table_partition_cols)?)
.file_compression_type(parse_file_compression_type(file_compression_type)?);
options.schema = schema.as_ref().map(|x| &x.0);
Copy link
Contributor

Choose a reason for hiding this comment

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

What is this line for?

Copy link
Contributor Author

@mesejo mesejo Aug 20, 2024

Choose a reason for hiding this comment

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

This line sets the options.schema; it is equivalent to the lines (that were deleted),

if let Some(py_schema) = schema {
    options.schema = Some(&py_schema.0);

I chose to do it this way to remove a nested conditional since the type must be checked to support list and string.


if let Some(py_schema) = schema {
options.schema = Some(&py_schema.0);
let result = self.ctx.read_csv(path, options);
if path.is_instance_of::<PyList>() {
let paths = path.extract::<Vec<String>>()?;
let paths = paths.iter().map(|p| p as &str).collect::<Vec<&str>>();
let result = self.ctx.read_csv(paths, options);
let df = PyDataFrame::new(wait_for_future(py, result).map_err(DataFusionError::from)?);
Ok(df)
} else {
let path = path.extract::<String>()?;
let result = self.ctx.read_csv(path, options);
let df = PyDataFrame::new(wait_for_future(py, result).map_err(DataFusionError::from)?);
Ok(df)
Expand Down
Loading