Skip to content

[DataFrame] - Add repartition funcation for dataframe #37

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

Merged
merged 2 commits into from
Sep 8, 2022
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: 8 additions & 0 deletions datafusion/tests/test_dataframe.py
Original file line number Diff line number Diff line change
Expand Up @@ -212,3 +212,11 @@ def test_explain(df):
column("a") - column("b"),
)
df.explain()


def test_repartition(df):
df.repartition(2)


def test_repartition_by_hash(df):
df.repartition_by_hash(column("a"), num=2)
17 changes: 16 additions & 1 deletion src/dataframe.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ use datafusion::arrow::pyarrow::PyArrowConvert;
use datafusion::arrow::util::pretty;
use datafusion::dataframe::DataFrame;
use datafusion::logical_plan::JoinType;
use datafusion::prelude::*;
use pyo3::exceptions::PyTypeError;
use pyo3::prelude::*;
use pyo3::types::PyTuple;
Expand Down Expand Up @@ -147,7 +148,7 @@ impl PyDataFrame {
"The join type {} does not exist or is not implemented",
how
))
.into())
.into());
}
};

Expand All @@ -164,4 +165,18 @@ impl PyDataFrame {
let batches = wait_for_future(py, df.collect())?;
Ok(pretty::print_batches(&batches)?)
}

/// Repartition a `DataFrame` based on a logical partitioning scheme.
fn repartition(&self, num: usize) -> PyResult<Self> {
let new_df = self.df.repartition(Partitioning::RoundRobinBatch(num))?;
Ok(Self::new(new_df))
}

/// Repartition a `DataFrame` based on a logical partitioning scheme.
#[args(args = "*", num)]
fn repartition_by_hash(&self, args: Vec<PyExpr>, num: usize) -> PyResult<Self> {
let expr = args.into_iter().map(|py_expr| py_expr.into()).collect();
let new_df = self.df.repartition(Partitioning::Hash(expr, num))?;
Ok(Self::new(new_df))
}
}